reworked to have two distinct REST handlers

This commit is contained in:
Ulrich Schreiner 2015-02-05 15:27:26 +01:00
parent 05b6dad81e
commit ae236a3d4d
4 changed files with 44 additions and 37 deletions

View file

@ -35,12 +35,12 @@ func (s *RepoService) Enable(host, owner, name string) error {
// DELETE /api/repos/{host}/{owner}/{name} // DELETE /api/repos/{host}/{owner}/{name}
func (s *RepoService) Disable(host, owner, name string) error { func (s *RepoService) Disable(host, owner, name string) error {
var path = fmt.Sprintf("/api/repos/%s/%s/%s", host, owner, name) var path = fmt.Sprintf("/api/repos/%s/%s/%s", host, owner, name)
return s.run("DELETE", path, nil, nil) return s.run("PATCH", path, nil, nil)
} }
// DELETE /api/repos/{host}/{owner}/{name}?remove=true // DELETE /api/repos/{host}/{owner}/{name}?remove=true
func (s *RepoService) Delete(host, owner, name string) error { func (s *RepoService) Delete(host, owner, name string) error {
var path = fmt.Sprintf("/api/repos/%s/%s/%s?remove=true", host, owner, name) var path = fmt.Sprintf("/api/repos/%s/%s/%s", host, owner, name)
return s.run("DELETE", path, nil, nil) return s.run("DELETE", path, nil, nil)
} }

View file

@ -43,9 +43,8 @@ func GetRepo(c web.C, w http.ResponseWriter, r *http.Request) {
}{repo, repo.PublicKey, repo.Params, role}) }{repo, repo.PublicKey, repo.Params, role})
} }
// DelRepo accepts a request to inactivate the named // DelRepo accepts a request to delete the named
// repository. This will disable all builds in the system // repository.
// for this repository.
// //
// DEL /api/repos/:host/:owner/:name // DEL /api/repos/:host/:owner/:name
// //
@ -53,42 +52,49 @@ func DelRepo(c web.C, w http.ResponseWriter, r *http.Request) {
var ctx = context.FromC(c) var ctx = context.FromC(c)
var repo = ToRepo(c) var repo = ToRepo(c)
var rm = r.FormValue("remove") // using ?remove=true
// completely remove the repository from the database // completely remove the repository from the database
if len(rm) != 0 { var user = ToUser(c)
var user = ToUser(c) var remote = remote.Lookup(repo.Host)
var remote = remote.Lookup(repo.Host) if remote == nil {
if remote == nil { log.Errf("no remote for host '%s' found", repo.Host)
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 { } else {
// Request a new token and update if user_token != nil {
user_token, err := remote.GetToken(user) user.Access = user_token.AccessToken
if err != nil { user.Secret = user_token.RefreshToken
log.Errf("no token for user '%s' on remote '%s' ", repo.Host) user.TokenExpiry = user_token.Expiry
} else { datastore.PutUser(ctx, user)
if user_token != nil { }
user.Access = user_token.AccessToken // setup the post-commit hook with the remote system and
user.Secret = user_token.RefreshToken // and deactiveate this hook/user on the remote system
datastore.PutUser(ctx, user) 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 {
// setup the post-commit hook with the remote system and log.Errf("deactivate on remote '%s' failed: %s", repo.Host, err)
// 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)
}
} }
} }
// 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)
} else {
w.WriteHeader(http.StatusNoContent)
}
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)
} else {
w.WriteHeader(http.StatusNoContent)
}
}
// PatchRepo accepts a request to inactivate the named
// repository. This will disable all builds in the system
// for this repository.
//
// PATCH /api/repos/:host/:owner/:name
//
func PatchRepo(c web.C, w http.ResponseWriter, r *http.Request) {
var ctx = context.FromC(c)
var repo = ToRepo(c)
// disable everything // disable everything
repo.Active = false repo.Active = false

View file

@ -46,6 +46,7 @@ func New() *web.Mux {
repos.Put("/api/repos/:host/:owner/:name", handler.PutRepo) repos.Put("/api/repos/:host/:owner/:name", handler.PutRepo)
repos.Post("/api/repos/:host/:owner/:name", handler.PostRepo) repos.Post("/api/repos/:host/:owner/:name", handler.PostRepo)
repos.Delete("/api/repos/:host/:owner/:name", handler.DelRepo) repos.Delete("/api/repos/:host/:owner/:name", handler.DelRepo)
repos.Patch("/api/repos/:host/:owner/:name", handler.PatchRepo)
mux.Handle("/api/repos/:host/:owner/:name", repos) mux.Handle("/api/repos/:host/:owner/:name", repos)
mux.Handle("/api/repos/:host/:owner/:name/*", repos) mux.Handle("/api/repos/:host/:owner/:name/*", repos)

View file

@ -20,7 +20,7 @@ type User struct {
Created int64 `meddler:"user_created" json:"created_at"` Created int64 `meddler:"user_created" json:"created_at"`
Updated int64 `meddler:"user_updated" json:"updated_at"` Updated int64 `meddler:"user_updated" json:"updated_at"`
Synced int64 `meddler:"user_synced" json:"synced_at"` Synced int64 `meddler:"user_synced" json:"synced_at"`
TokenExpiry int64 `meddler:"user_access_expires" json:"-"` TokenExpiry int64 `meddler:"user_access_expires,zeroisnull" json:"-"`
} }
func NewUser(remote, login, email string) *User { func NewUser(remote, login, email string) *User {