From ae236a3d4d28b8d7b54d14aa66b59f5aa054db47 Mon Sep 17 00:00:00 2001 From: Ulrich Schreiner Date: Thu, 5 Feb 2015 15:27:26 +0100 Subject: [PATCH] reworked to have two distinct REST handlers --- client/repos.go | 4 +-- server/handler/repo.go | 74 ++++++++++++++++++++++------------------- server/router/router.go | 1 + shared/model/user.go | 2 +- 4 files changed, 44 insertions(+), 37 deletions(-) diff --git a/client/repos.go b/client/repos.go index 0b77c15eb..5afa1452b 100644 --- a/client/repos.go +++ b/client/repos.go @@ -35,12 +35,12 @@ func (s *RepoService) Enable(host, owner, name string) error { // DELETE /api/repos/{host}/{owner}/{name} func (s *RepoService) Disable(host, owner, name string) error { 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 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) } diff --git a/server/handler/repo.go b/server/handler/repo.go index 137384112..a299bc2a6 100644 --- a/server/handler/repo.go +++ b/server/handler/repo.go @@ -43,9 +43,8 @@ func GetRepo(c web.C, w http.ResponseWriter, r *http.Request) { }{repo, repo.PublicKey, repo.Params, role}) } -// DelRepo accepts a request to inactivate the named -// repository. This will disable all builds in the system -// for this repository. +// DelRepo accepts a request to delete the named +// repository. // // 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 repo = ToRepo(c) - var rm = r.FormValue("remove") // using ?remove=true - // completely remove the repository from the database - if len(rm) != 0 { - var user = ToUser(c) - var remote = remote.Lookup(repo.Host) - if remote == nil { - log.Errf("no remote for host '%s' found", repo.Host) + var user = ToUser(c) + var remote = remote.Lookup(repo.Host) + if remote == nil { + 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 { - // 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) - } + if user_token != nil { + user.Access = user_token.AccessToken + user.Secret = user_token.RefreshToken + user.TokenExpiry = user_token.Expiry + 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) } } - // 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 repo.Active = false diff --git a/server/router/router.go b/server/router/router.go index 0980a8482..51bc2c6d0 100644 --- a/server/router/router.go +++ b/server/router/router.go @@ -46,6 +46,7 @@ func New() *web.Mux { repos.Put("/api/repos/:host/:owner/:name", handler.PutRepo) repos.Post("/api/repos/:host/:owner/:name", handler.PostRepo) 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) diff --git a/shared/model/user.go b/shared/model/user.go index 2b7b5ad15..7f7f9a653 100644 --- a/shared/model/user.go +++ b/shared/model/user.go @@ -20,7 +20,7 @@ type User struct { Created int64 `meddler:"user_created" json:"created_at"` Updated int64 `meddler:"user_updated" json:"updated_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 {