impelement a delete of a repository

This commit is contained in:
Ulrich Schreiner
2015-02-04 14:42:24 +01:00
parent 22fa37c3f1
commit 0ed7ae7e3f
10 changed files with 171 additions and 3 deletions

View File

@@ -193,6 +193,23 @@ func (r *GitHub) GetScript(user *model.User, repo *model.Repo, hook *model.Hook)
return GetFile(client, repo.Owner, repo.Name, ".drone.yml", hook.Sha)
}
// Deactivate removes a repository by removing all the post-commit hooks
// which are equal to link and removing the SSH deploy key.
func (r *GitHub) Deactivate(user *model.User, repo *model.Repo, link string) error {
var client = NewClient(r.API, user.Access, r.SkipVerify)
var title, err = GetKeyTitle(link)
if err != nil {
return err
}
// remove the deploy-key if it is installed remote.
if err := DeleteKey(client, repo.Owner, repo.Name, title, repo.PublicKey); err != nil {
return err
}
return DeleteHook(client, repo.Owner, repo.Name, link)
}
// Activate activates a repository by adding a Post-commit hook and
// a Public Deploy key, if applicable.
func (r *GitHub) Activate(user *model.User, repo *model.Repo, link string) error {

View File

@@ -173,6 +173,16 @@ func GetHook(client *github.Client, owner, name, url string) (*github.Hook, erro
return nil, nil
}
func DeleteHook(client *github.Client, owner, name, url string) error {
hook, err := GetHook(client, owner, name, url)
if err != nil {
return err
}
_, err = client.Repositories.DeleteHook(owner, name, *hook.ID)
return err
}
// CreateHook is a heper function that creates a post-commit hook
// for the specified repository.
func CreateHook(client *github.Client, owner, name, url string) (*github.Hook, error) {
@@ -230,7 +240,26 @@ func GetKeyTitle(rawurl string) (string, error) {
return fmt.Sprintf("drone@%s", uri.Host), nil
}
// CreateKey is a heper function that creates a deploy key
// DeleteKey is a helper function that deletes a deploy key
// for the specified repository.
func DeleteKey(client *github.Client, owner, name, title, key string) error {
var k = new(github.Key)
k.Title = github.String(title)
k.Key = github.String(key)
keys, _, err := client.Repositories.ListKeys(owner, name, nil)
if err != nil {
return err
}
for _, rk := range keys {
if rk.Key != nil && rk.Key == k.Key {
_, err = client.Repositories.DeleteKey(owner, name, *rk.ID)
return err
}
}
return fmt.Errorf("%s not found in the list of keys", title)
}
// CreateKey is a helper function that creates a deploy key
// for the specified repository.
func CreateKey(client *github.Client, owner, name, title, key string) (*github.Key, error) {
var k = new(github.Key)
@@ -240,7 +269,7 @@ func CreateKey(client *github.Client, owner, name, title, key string) (*github.K
return created, err
}
// CreateUpdateKey is a heper function that creates a deployment key
// CreateUpdateKey is a helper function that creates a deployment key
// for the specified repository if it does not already exist, otherwise
// it updates the existing key
func CreateUpdateKey(client *github.Client, owner, name, title, key string) (*github.Key, error) {