rework to delete the repository from the datastore if there are previous errors from the remote

This commit is contained in:
Ulrich Schreiner 2015-02-05 13:13:17 +01:00
parent 347aa24785
commit bd8fb51c2a
3 changed files with 27 additions and 25 deletions

View file

@ -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

View file

@ -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

View file

@ -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)
} 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
// 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 {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
log.Errf("deactivate on remote '%s' failed: %s", repo.Host, err)
}
}
}
// 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