From f57640a402ebf9d7fd1e245b504d2e0b518cb090 Mon Sep 17 00:00:00 2001 From: Ulrich Schreiner Date: Fri, 6 Feb 2015 11:36:31 +0100 Subject: [PATCH] remove hook/key from a bitbucket repo --- plugin/remote/bitbucket/bitbucket.go | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/plugin/remote/bitbucket/bitbucket.go b/plugin/remote/bitbucket/bitbucket.go index 2e0b4c8e..c4bd1fcc 100644 --- a/plugin/remote/bitbucket/bitbucket.go +++ b/plugin/remote/bitbucket/bitbucket.go @@ -5,6 +5,7 @@ import ( "net/http" "net/url" "regexp" + "strings" "time" "github.com/drone/drone/shared/httputil" @@ -244,7 +245,26 @@ func (r *Bitbucket) Activate(user *model.User, repo *model.Repo, link string) er // Deactivate removes a repository by removing all the post-commit hooks // which are equal to link and removing the SSH deploy key. func (r *Bitbucket) Deactivate(user *model.User, repo *model.Repo, link string) error { - return fmt.Errorf("Remove %#v in bitbucket not implemented", *repo) + var client = bitbucket.New( + r.Client, + r.Secret, + user.Access, + user.Secret, + ) + + if keys, err := client.RepoKeys.List(repo.Owner, repo.Name); err == nil { + repokey := strings.TrimSpace(repo.PublicKey) + for _, k := range keys { + if k.Key == repokey { + if err := client.RepoKeys.Delete(repo.Owner, repo.Name, k.Id); err != nil { + return err + } else { + break + } + } + } + } + return client.Brokers.DeleteUrl(repo.Owner, repo.Name, link, bitbucket.BrokerTypePost) } // ParseHook parses the post-commit hook from the Request body