reworked to have two distinct REST handlers
This commit is contained in:
parent
05b6dad81e
commit
ae236a3d4d
4 changed files with 44 additions and 37 deletions
|
@ -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)
|
||||
}
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
|
||||
|
|
|
@ -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 {
|
||||
|
|
Loading…
Reference in a new issue