From bd8fb51c2a9936e011dd387ae7b415c8538558a8 Mon Sep 17 00:00:00 2001 From: Ulrich Schreiner Date: Thu, 5 Feb 2015 13:13:17 +0100 Subject: [PATCH] rework to delete the repository from the datastore if there are previous errors from the remote --- plugin/remote/github/helper.go | 2 +- plugin/remote/gogs/gogs.go | 2 +- server/handler/repo.go | 48 ++++++++++++++++++---------------- 3 files changed, 27 insertions(+), 25 deletions(-) diff --git a/plugin/remote/github/helper.go b/plugin/remote/github/helper.go index 257bdfb8e..0fdbadc9f 100644 --- a/plugin/remote/github/helper.go +++ b/plugin/remote/github/helper.go @@ -256,7 +256,7 @@ func DeleteKey(client *github.Client, owner, name, title, key string) error { return err } } - return fmt.Errorf("%s not found in the list of keys", title) + return nil } // CreateKey is a helper function that creates a deploy key diff --git a/plugin/remote/gogs/gogs.go b/plugin/remote/gogs/gogs.go index af81015fd..73038a4dc 100644 --- a/plugin/remote/gogs/gogs.go +++ b/plugin/remote/gogs/gogs.go @@ -156,7 +156,7 @@ func (r *Gogs) Activate(user *model.User, repo *model.Repo, link string) error { // Deactivate removes a repository by removing all the post-commit hooks // which are equal to link and removing the SSH deploy key. func (r *Gogs) Deactivate(user *model.User, repo *model.Repo, link string) error { - return fmt.Errorf("Remove %#v in gots not implemented", *repo) + return fmt.Errorf("Remove %#v in gogs not implemented", *repo) } // ParseHook parses the post-commit hook from the Request body diff --git a/server/handler/repo.go b/server/handler/repo.go index 97bd842e9..3fdf0d8c2 100644 --- a/server/handler/repo.go +++ b/server/handler/repo.go @@ -7,6 +7,7 @@ import ( "github.com/drone/drone/plugin/remote" "github.com/drone/drone/server/datastore" + "github.com/drone/drone/shared/build/log" "github.com/drone/drone/shared/httputil" "github.com/drone/drone/shared/model" "github.com/drone/drone/shared/sshutil" @@ -59,33 +60,34 @@ func DelRepo(c web.C, w http.ResponseWriter, r *http.Request) { var user = ToUser(c) var remote = remote.Lookup(repo.Host) if remote == nil { - w.WriteHeader(http.StatusNotFound) - return + log.Errf("no remote for host '%s' found", repo.Host) + } else { + // Request a new token and update + user_token, err := remote.GetToken(user) + if err != nil { + log.Errf("no token for user '%s' on remote '%s' ", repo.Host) + } else { + if user_token != nil { + user.Access = user_token.AccessToken + user.Secret = user_token.RefreshToken + datastore.PutUser(ctx, user) + } + // setup the post-commit hook with the remote system and + // and deactiveate this hook/user on the remote system + var hook = fmt.Sprintf("%s/api/hook/%s/%s", httputil.GetURL(r), repo.Remote, repo.Token) + if err := remote.Deactivate(user, repo, hook); err != nil { + log.Errf("deactivate on remote '%s' failed: %s", repo.Host, err) + } + } } - - // Request a new token and update - user_token, err := remote.GetToken(user) - if user_token != nil { - user.Access = user_token.AccessToken - user.Secret = user_token.RefreshToken - datastore.PutUser(ctx, user) - } else if err != nil { - w.WriteHeader(http.StatusBadRequest) - return - } - - // setup the post-commit hook with the remote system and - // if necessary, register the public key - var hook = fmt.Sprintf("%s/api/hook/%s/%s", httputil.GetURL(r), repo.Remote, repo.Token) - if err := remote.Deactivate(user, repo, hook); err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } - + // fail through: if any of the actions on the remote failed + // we try to delete the repo in our datastore anyway if err := datastore.DelRepo(ctx, repo); err != nil { w.WriteHeader(http.StatusInternalServerError) - return + } else { + w.WriteHeader(http.StatusNoContent) } + return } // disable everything