From e6f927f61af927156798390e64f17dd6755697e7 Mon Sep 17 00:00:00 2001 From: Unknwon Date: Mon, 21 Mar 2016 12:47:54 -0400 Subject: #1692 api: admin list and create team under organization --- README.md | 2 +- gogs.go | 2 +- models/access.go | 27 +++++++ routers/api/v1/admin/org.go | 44 ++++++++++ routers/api/v1/admin/org_team.go | 56 +++++++++++++ routers/api/v1/admin/orgs.go | 44 ---------- routers/api/v1/admin/repo.go | 23 ++++++ routers/api/v1/admin/repos.go | 23 ------ routers/api/v1/admin/user.go | 152 +++++++++++++++++++++++++++++++++++ routers/api/v1/admin/users.go | 152 ----------------------------------- routers/api/v1/api.go | 4 + routers/api/v1/convert/convert.go | 9 +++ routers/api/v1/repo/hook.go | 164 ++++++++++++++++++++++++++++++++++++++ routers/api/v1/repo/hooks.go | 164 -------------------------------------- routers/api/v1/repo/key.go | 114 ++++++++++++++++++++++++++ routers/api/v1/repo/keys.go | 114 -------------------------- routers/api/v1/user/follower.go | 121 ++++++++++++++++++++++++++++ routers/api/v1/user/followers.go | 121 ---------------------------- routers/api/v1/user/key.go | 119 +++++++++++++++++++++++++++ routers/api/v1/user/keys.go | 119 --------------------------- routers/org/teams.go | 16 +--- templates/.VERSION | 2 +- 22 files changed, 837 insertions(+), 755 deletions(-) create mode 100644 routers/api/v1/admin/org.go create mode 100644 routers/api/v1/admin/org_team.go delete mode 100644 routers/api/v1/admin/orgs.go create mode 100644 routers/api/v1/admin/repo.go delete mode 100644 routers/api/v1/admin/repos.go create mode 100644 routers/api/v1/admin/user.go delete mode 100644 routers/api/v1/admin/users.go create mode 100644 routers/api/v1/repo/hook.go delete mode 100644 routers/api/v1/repo/hooks.go create mode 100644 routers/api/v1/repo/key.go delete mode 100644 routers/api/v1/repo/keys.go create mode 100644 routers/api/v1/user/follower.go delete mode 100644 routers/api/v1/user/followers.go create mode 100644 routers/api/v1/user/key.go delete mode 100644 routers/api/v1/user/keys.go diff --git a/README.md b/README.md index 0886903b..935be180 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ Gogs - Go Git Service [![Build Status](https://travis-ci.org/gogits/gogs.svg?bra ![](https://github.com/gogits/gogs/blob/master/public/img/gogs-large-resize.png?raw=true) -##### Current version: 0.9.13 +##### Current version: 0.9.14 | Web | UI | Preview | |:-------------:|:-------:|:-------:| diff --git a/gogs.go b/gogs.go index cc3c436f..1e2573b2 100644 --- a/gogs.go +++ b/gogs.go @@ -17,7 +17,7 @@ import ( "github.com/gogits/gogs/modules/setting" ) -const APP_VER = "0.9.13.0321" +const APP_VER = "0.9.14.0321" func init() { runtime.GOMAXPROCS(runtime.NumCPU()) diff --git a/models/access.go b/models/access.go index 447777ad..b4c1349b 100644 --- a/models/access.go +++ b/models/access.go @@ -20,6 +20,33 @@ const ( ACCESS_MODE_OWNER // 4 ) +func (mode AccessMode) String() string { + switch mode { + case ACCESS_MODE_READ: + return "read" + case ACCESS_MODE_WRITE: + return "write" + case ACCESS_MODE_ADMIN: + return "admin" + case ACCESS_MODE_OWNER: + return "owner" + default: + return "none" + } +} + +// ParseAccessMode returns corresponding access mode to given permission string. +func ParseAccessMode(permission string) AccessMode { + switch permission { + case "write": + return ACCESS_MODE_WRITE + case "admin": + return ACCESS_MODE_ADMIN + default: + return ACCESS_MODE_READ + } +} + // Access represents the highest access level of a user to the repository. The only access type // that is not in this table is the real owner of a repository. In case of an organization // repository, the members of the owners team are in this table. diff --git a/routers/api/v1/admin/org.go b/routers/api/v1/admin/org.go new file mode 100644 index 00000000..fcce3b93 --- /dev/null +++ b/routers/api/v1/admin/org.go @@ -0,0 +1,44 @@ +// Copyright 2015 The Gogs Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package admin + +import ( + api "github.com/gogits/go-gogs-client" + + "github.com/gogits/gogs/models" + "github.com/gogits/gogs/modules/context" + "github.com/gogits/gogs/routers/api/v1/convert" + "github.com/gogits/gogs/routers/api/v1/user" +) + +// https://github.com/gogits/go-gogs-client/wiki/Administration-Organizations#create-a-new-organization +func CreateOrg(ctx *context.APIContext, form api.CreateOrgOption) { + u := user.GetUserByParams(ctx) + if ctx.Written() { + return + } + + org := &models.User{ + Name: form.UserName, + FullName: form.FullName, + Description: form.Description, + Website: form.Website, + Location: form.Location, + IsActive: true, + Type: models.USER_TYPE_ORGANIZATION, + } + if err := models.CreateOrganization(org, u); err != nil { + if models.IsErrUserAlreadyExist(err) || + models.IsErrNameReserved(err) || + models.IsErrNamePatternNotAllowed(err) { + ctx.Error(422, "CreateOrganization", err) + } else { + ctx.Error(500, "CreateOrganization", err) + } + return + } + + ctx.JSON(201, convert.ToOrganization(org)) +} diff --git a/routers/api/v1/admin/org_team.go b/routers/api/v1/admin/org_team.go new file mode 100644 index 00000000..618dd9a9 --- /dev/null +++ b/routers/api/v1/admin/org_team.go @@ -0,0 +1,56 @@ +// Copyright 2016 The Gogs Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package admin + +import ( + api "github.com/gogits/go-gogs-client" + + "github.com/gogits/gogs/models" + "github.com/gogits/gogs/modules/context" + "github.com/gogits/gogs/routers/api/v1/convert" + "github.com/gogits/gogs/routers/api/v1/user" +) + +func ListTeams(ctx *context.APIContext) { + org := user.GetUserByParamsName(ctx, ":orgname") + if ctx.Written() { + return + } + + if err := org.GetTeams(); err != nil { + ctx.Error(500, "GetTeams", err) + return + } + + apiTeams := make([]*api.Team, len(org.Teams)) + for i := range org.Teams { + apiTeams[i] = convert.ToTeam(org.Teams[i]) + } + ctx.JSON(200, apiTeams) +} + +func CreateTeam(ctx *context.APIContext, form api.CreateTeamOption) { + org := user.GetUserByParamsName(ctx, ":orgname") + if ctx.Written() { + return + } + + team := &models.Team{ + OrgID: org.Id, + Name: form.Name, + Description: form.Description, + Authorize: models.ParseAccessMode(form.Permission), + } + if err := models.NewTeam(team); err != nil { + if models.IsErrTeamAlreadyExist(err) { + ctx.Error(422, "NewTeam", err) + } else { + ctx.Error(500, "NewTeam", err) + } + return + } + + ctx.JSON(200, convert.ToTeam(team)) +} diff --git a/routers/api/v1/admin/orgs.go b/routers/api/v1/admin/orgs.go deleted file mode 100644 index fcce3b93..00000000 --- a/routers/api/v1/admin/orgs.go +++ /dev/null @@ -1,44 +0,0 @@ -// Copyright 2015 The Gogs Authors. All rights reserved. -// Use of this source code is governed by a MIT-style -// license that can be found in the LICENSE file. - -package admin - -import ( - api "github.com/gogits/go-gogs-client" - - "github.com/gogits/gogs/models" - "github.com/gogits/gogs/modules/context" - "github.com/gogits/gogs/routers/api/v1/convert" - "github.com/gogits/gogs/routers/api/v1/user" -) - -// https://github.com/gogits/go-gogs-client/wiki/Administration-Organizations#create-a-new-organization -func CreateOrg(ctx *context.APIContext, form api.CreateOrgOption) { - u := user.GetUserByParams(ctx) - if ctx.Written() { - return - } - - org := &models.User{ - Name: form.UserName, - FullName: form.FullName, - Description: form.Description, - Website: form.Website, - Location: form.Location, - IsActive: true, - Type: models.USER_TYPE_ORGANIZATION, - } - if err := models.CreateOrganization(org, u); err != nil { - if models.IsErrUserAlreadyExist(err) || - models.IsErrNameReserved(err) || - models.IsErrNamePatternNotAllowed(err) { - ctx.Error(422, "CreateOrganization", err) - } else { - ctx.Error(500, "CreateOrganization", err) - } - return - } - - ctx.JSON(201, convert.ToOrganization(org)) -} diff --git a/routers/api/v1/admin/repo.go b/routers/api/v1/admin/repo.go new file mode 100644 index 00000000..0f0c3862 --- /dev/null +++ b/routers/api/v1/admin/repo.go @@ -0,0 +1,23 @@ +// Copyright 2015 The Gogs Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package admin + +import ( + api "github.com/gogits/go-gogs-client" + + "github.com/gogits/gogs/modules/context" + "github.com/gogits/gogs/routers/api/v1/repo" + "github.com/gogits/gogs/routers/api/v1/user" +) + +// https://github.com/gogits/go-gogs-client/wiki/Administration-Repositories#create-a-new-repository +func CreateRepo(ctx *context.APIContext, form api.CreateRepoOption) { + owner := user.GetUserByParams(ctx) + if ctx.Written() { + return + } + + repo.CreateUserRepo(ctx, owner, form) +} diff --git a/routers/api/v1/admin/repos.go b/routers/api/v1/admin/repos.go deleted file mode 100644 index 0f0c3862..00000000 --- a/routers/api/v1/admin/repos.go +++ /dev/null @@ -1,23 +0,0 @@ -// Copyright 2015 The Gogs Authors. All rights reserved. -// Use of this source code is governed by a MIT-style -// license that can be found in the LICENSE file. - -package admin - -import ( - api "github.com/gogits/go-gogs-client" - - "github.com/gogits/gogs/modules/context" - "github.com/gogits/gogs/routers/api/v1/repo" - "github.com/gogits/gogs/routers/api/v1/user" -) - -// https://github.com/gogits/go-gogs-client/wiki/Administration-Repositories#create-a-new-repository -func CreateRepo(ctx *context.APIContext, form api.CreateRepoOption) { - owner := user.GetUserByParams(ctx) - if ctx.Written() { - return - } - - repo.CreateUserRepo(ctx, owner, form) -} diff --git a/routers/api/v1/admin/user.go b/routers/api/v1/admin/user.go new file mode 100644 index 00000000..ce13804f --- /dev/null +++ b/routers/api/v1/admin/user.go @@ -0,0 +1,152 @@ +// Copyright 2015 The Gogs Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package admin + +import ( + api "github.com/gogits/go-gogs-client" + + "github.com/gogits/gogs/models" + "github.com/gogits/gogs/modules/context" + "github.com/gogits/gogs/modules/log" + "github.com/gogits/gogs/modules/mailer" + "github.com/gogits/gogs/modules/setting" + "github.com/gogits/gogs/routers/api/v1/convert" + "github.com/gogits/gogs/routers/api/v1/user" +) + +func parseLoginSource(ctx *context.APIContext, u *models.User, sourceID int64, loginName string) { + if sourceID == 0 { + return + } + + source, err := models.GetLoginSourceByID(sourceID) + if err != nil { + if models.IsErrAuthenticationNotExist(err) { + ctx.Error(422, "", err) + } else { + ctx.Error(500, "GetLoginSourceByID", err) + } + return + } + + u.LoginType = source.Type + u.LoginSource = source.ID + u.LoginName = loginName +} + +// https://github.com/gogits/go-gogs-client/wiki/Administration-Users#create-a-new-user +func CreateUser(ctx *context.APIContext, form api.CreateUserOption) { + u := &models.User{ + Name: form.Username, + Email: form.Email, + Passwd: form.Password, + IsActive: true, + LoginType: models.LOGIN_PLAIN, + } + + parseLoginSource(ctx, u, form.SourceID, form.LoginName) + if ctx.Written() { + return + } + + if err := models.CreateUser(u); err != nil { + if models.IsErrUserAlreadyExist(err) || + models.IsErrEmailAlreadyUsed(err) || + models.IsErrNameReserved(err) || + models.IsErrNamePatternNotAllowed(err) { + ctx.Error(422, "", err) + } else { + ctx.Error(500, "CreateUser", err) + } + return + } + log.Trace("Account created by admin (%s): %s", ctx.User.Name, u.Name) + + // Send e-mail notification. + if form.SendNotify && setting.MailService != nil { + mailer.SendRegisterNotifyMail(ctx.Context.Context, u) + } + + ctx.JSON(201, convert.ToUser(u)) +} + +// https://github.com/gogits/go-gogs-client/wiki/Administration-Users#edit-an-existing-user +func EditUser(ctx *context.APIContext, form api.EditUserOption) { + u := user.GetUserByParams(ctx) + if ctx.Written() { + return + } + + parseLoginSource(ctx, u, form.SourceID, form.LoginName) + if ctx.Written() { + return + } + + if len(form.Password) > 0 { + u.Passwd = form.Password + u.Salt = models.GetUserSalt() + u.EncodePasswd() + } + + u.LoginName = form.LoginName + u.FullName = form.FullName + u.Email = form.Email + u.Website = form.Website + u.Location = form.Location + if form.Active != nil { + u.IsActive = *form.Active + } + if form.Admin != nil { + u.IsAdmin = *form.Admin + } + if form.AllowGitHook != nil { + u.AllowGitHook = *form.AllowGitHook + } + if form.AllowImportLocal != nil { + u.AllowImportLocal = *form.AllowImportLocal + } + + if err := models.UpdateUser(u); err != nil { + if models.IsErrEmailAlreadyUsed(err) { + ctx.Error(422, "", err) + } else { + ctx.Error(500, "UpdateUser", err) + } + return + } + log.Trace("Account profile updated by admin (%s): %s", ctx.User.Name, u.Name) + + ctx.JSON(200, convert.ToUser(u)) +} + +// https://github.com/gogits/go-gogs-client/wiki/Administration-Users#delete-a-user +func DeleteUser(ctx *context.APIContext) { + u := user.GetUserByParams(ctx) + if ctx.Written() { + return + } + + if err := models.DeleteUser(u); err != nil { + if models.IsErrUserOwnRepos(err) || + models.IsErrUserHasOrgs(err) { + ctx.Error(422, "", err) + } else { + ctx.Error(500, "DeleteUser", err) + } + return + } + log.Trace("Account deleted by admin(%s): %s", ctx.User.Name, u.Name) + + ctx.Status(204) +} + +// https://github.com/gogits/go-gogs-client/wiki/Administration-Users#create-a-public-key-for-user +func CreatePublicKey(ctx *context.APIContext, form api.CreateKeyOption) { + u := user.GetUserByParams(ctx) + if ctx.Written() { + return + } + user.CreateUserPublicKey(ctx, form, u.Id) +} diff --git a/routers/api/v1/admin/users.go b/routers/api/v1/admin/users.go deleted file mode 100644 index ce13804f..00000000 --- a/routers/api/v1/admin/users.go +++ /dev/null @@ -1,152 +0,0 @@ -// Copyright 2015 The Gogs Authors. All rights reserved. -// Use of this source code is governed by a MIT-style -// license that can be found in the LICENSE file. - -package admin - -import ( - api "github.com/gogits/go-gogs-client" - - "github.com/gogits/gogs/models" - "github.com/gogits/gogs/modules/context" - "github.com/gogits/gogs/modules/log" - "github.com/gogits/gogs/modules/mailer" - "github.com/gogits/gogs/modules/setting" - "github.com/gogits/gogs/routers/api/v1/convert" - "github.com/gogits/gogs/routers/api/v1/user" -) - -func parseLoginSource(ctx *context.APIContext, u *models.User, sourceID int64, loginName string) { - if sourceID == 0 { - return - } - - source, err := models.GetLoginSourceByID(sourceID) - if err != nil { - if models.IsErrAuthenticationNotExist(err) { - ctx.Error(422, "", err) - } else { - ctx.Error(500, "GetLoginSourceByID", err) - } - return - } - - u.LoginType = source.Type - u.LoginSource = source.ID - u.LoginName = loginName -} - -// https://github.com/gogits/go-gogs-client/wiki/Administration-Users#create-a-new-user -func CreateUser(ctx *context.APIContext, form api.CreateUserOption) { - u := &models.User{ - Name: form.Username, - Email: form.Email, - Passwd: form.Password, - IsActive: true, - LoginType: models.LOGIN_PLAIN, - } - - parseLoginSource(ctx, u, form.SourceID, form.LoginName) - if ctx.Written() { - return - } - - if err := models.CreateUser(u); err != nil { - if models.IsErrUserAlreadyExist(err) || - models.IsErrEmailAlreadyUsed(err) || - models.IsErrNameReserved(err) || - models.IsErrNamePatternNotAllowed(err) { - ctx.Error(422, "", err) - } else { - ctx.Error(500, "CreateUser", err) - } - return - } - log.Trace("Account created by admin (%s): %s", ctx.User.Name, u.Name) - - // Send e-mail notification. - if form.SendNotify && setting.MailService != nil { - mailer.SendRegisterNotifyMail(ctx.Context.Context, u) - } - - ctx.JSON(201, convert.ToUser(u)) -} - -// https://github.com/gogits/go-gogs-client/wiki/Administration-Users#edit-an-existing-user -func EditUser(ctx *context.APIContext, form api.EditUserOption) { - u := user.GetUserByParams(ctx) - if ctx.Written() { - return - } - - parseLoginSource(ctx, u, form.SourceID, form.LoginName) - if ctx.Written() { - return - } - - if len(form.Password) > 0 { - u.Passwd = form.Password - u.Salt = models.GetUserSalt() - u.EncodePasswd() - } - - u.LoginName = form.LoginName - u.FullName = form.FullName - u.Email = form.Email - u.Website = form.Website - u.Location = form.Location - if form.Active != nil { - u.IsActive = *form.Active - } - if form.Admin != nil { - u.IsAdmin = *form.Admin - } - if form.AllowGitHook != nil { - u.AllowGitHook = *form.AllowGitHook - } - if form.AllowImportLocal != nil { - u.AllowImportLocal = *form.AllowImportLocal - } - - if err := models.UpdateUser(u); err != nil { - if models.IsErrEmailAlreadyUsed(err) { - ctx.Error(422, "", err) - } else { - ctx.Error(500, "UpdateUser", err) - } - return - } - log.Trace("Account profile updated by admin (%s): %s", ctx.User.Name, u.Name) - - ctx.JSON(200, convert.ToUser(u)) -} - -// https://github.com/gogits/go-gogs-client/wiki/Administration-Users#delete-a-user -func DeleteUser(ctx *context.APIContext) { - u := user.GetUserByParams(ctx) - if ctx.Written() { - return - } - - if err := models.DeleteUser(u); err != nil { - if models.IsErrUserOwnRepos(err) || - models.IsErrUserHasOrgs(err) { - ctx.Error(422, "", err) - } else { - ctx.Error(500, "DeleteUser", err) - } - return - } - log.Trace("Account deleted by admin(%s): %s", ctx.User.Name, u.Name) - - ctx.Status(204) -} - -// https://github.com/gogits/go-gogs-client/wiki/Administration-Users#create-a-public-key-for-user -func CreatePublicKey(ctx *context.APIContext, form api.CreateKeyOption) { - u := user.GetUserByParams(ctx) - if ctx.Written() { - return - } - user.CreateUserPublicKey(ctx, form, u.Id) -} diff --git a/routers/api/v1/api.go b/routers/api/v1/api.go index 59625efd..744289ac 100644 --- a/routers/api/v1/api.go +++ b/routers/api/v1/api.go @@ -223,6 +223,10 @@ func RegisterRoutes(m *macaron.Macaron) { m.Post("/repos", bind(api.CreateRepoOption{}), admin.CreateRepo) }) }) + + m.Group("/orgs/:orgname", func() { + m.Combo("/teams").Get(admin.ListTeams).Post(bind(api.CreateTeamOption{}), admin.CreateTeam) + }) }, ReqAdmin()) }, context.APIContexter()) } diff --git a/routers/api/v1/convert/convert.go b/routers/api/v1/convert/convert.go index 7e3e380b..8eca5f4e 100644 --- a/routers/api/v1/convert/convert.go +++ b/routers/api/v1/convert/convert.go @@ -196,3 +196,12 @@ func ToOrganization(org *models.User) *api.Organization { Location: org.Location, } } + +func ToTeam(team *models.Team) *api.Team { + return &api.Team{ + ID: team.ID, + Name: team.Name, + Description: team.Description, + Permission: team.Authorize.String(), + } +} diff --git a/routers/api/v1/repo/hook.go b/routers/api/v1/repo/hook.go new file mode 100644 index 00000000..0cbe6762 --- /dev/null +++ b/routers/api/v1/repo/hook.go @@ -0,0 +1,164 @@ +// Copyright 2014 The Gogs Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package repo + +import ( + "encoding/json" + + "github.com/Unknwon/com" + + api "github.com/gogits/go-gogs-client" + + "github.com/gogits/gogs/models" + "github.com/gogits/gogs/modules/context" + "github.com/gogits/gogs/routers/api/v1/convert" +) + +// https://github.com/gogits/go-gogs-client/wiki/Repositories#list-hooks +func ListHooks(ctx *context.APIContext) { + hooks, err := models.GetWebhooksByRepoID(ctx.Repo.Repository.ID) + if err != nil { + ctx.Error(500, "GetWebhooksByRepoID", err) + return + } + + apiHooks := make([]*api.Hook, len(hooks)) + for i := range hooks { + apiHooks[i] = convert.ToHook(ctx.Repo.RepoLink, hooks[i]) + } + ctx.JSON(200, &apiHooks) +} + +// https://github.com/gogits/go-gogs-client/wiki/Repositories#create-a-hook +func CreateHook(ctx *context.APIContext, form api.CreateHookOption) { + if !models.IsValidHookTaskType(form.Type) { + ctx.Error(422, "", "Invalid hook type") + return + } + for _, name := range []string{"url", "content_type"} { + if _, ok := form.Config[name]; !ok { + ctx.Error(422, "", "Missing config option: "+name) + return + } + } + if !models.IsValidHookContentType(form.Config["content_type"]) { + ctx.Error(422, "", "Invalid content type") + return + } + + if len(form.Events) == 0 { + form.Events = []string{"push"} + } + w := &models.Webhook{ + RepoID: ctx.Repo.Repository.ID, + URL: form.Config["url"], + ContentType: models.ToHookContentType(form.Config["content_type"]), + Secret: form.Config["secret"], + HookEvent: &models.HookEvent{ + ChooseEvents: true, + HookEvents: models.HookEvents{ + Create: com.IsSliceContainsStr(form.Events, string(models.HOOK_EVENT_CREATE)), + Push: com.IsSliceContainsStr(form.Events, string(models.HOOK_EVENT_PUSH)), + }, + }, + IsActive: form.Active, + HookTaskType: models.ToHookTaskType(form.Type), + } + if w.HookTaskType == models.SLACK { + channel, ok := form.Config["channel"] + if !ok { + ctx.Error(422, "", "Missing config option: channel") + return + } + meta, err := json.Marshal(&models.SlackMeta{ + Channel: channel, + Username: form.Config["username"], + IconURL: form.Config["icon_url"], + Color: form.Config["color"], + }) + if err != nil { + ctx.Error(500, "slack: JSON marshal failed", err) + return + } + w.Meta = string(meta) + } + + if err := w.UpdateEvent(); err != nil { + ctx.Error(500, "UpdateEvent", err) + return + } else if err := models.CreateWebhook(w); err != nil { + ctx.Error(500, "CreateWebhook", err) + return + } + + ctx.JSON(201, convert.ToHook(ctx.Repo.RepoLink, w)) +} + +// https://github.com/gogits/go-gogs-client/wiki/Repositories#edit-a-hook +func EditHook(ctx *context.APIContext, form api.EditHookOption) { + w, err := models.GetWebhookByID(ctx.ParamsInt64(":id")) + if err != nil { + if models.IsErrWebhookNotExist(err) { + ctx.Status(404) + } else { + ctx.Error(500, "GetWebhookByID", err) + } + return + } + + if form.Config != nil { + if url, ok := form.Config["url"]; ok { + w.URL = url + } + if ct, ok := form.Config["content_type"]; ok { + if !models.IsValidHookContentType(ct) { + ctx.Error(422, "", "Invalid content type") + return + } + w.ContentType = models.ToHookContentType(ct) + } + + if w.HookTaskType == models.SLACK { + if channel, ok := form.Config["channel"]; ok { + meta, err := json.Marshal(&models.SlackMeta{ + Channel: channel, + Username: form.Config["username"], + IconURL: form.Config["icon_url"], + Color: form.Config["color"], + }) + if err != nil { + ctx.Error(500, "slack: JSON marshal failed", err) + return + } + w.Meta = string(meta) + } + } + } + + // Update events + if len(form.Events) == 0 { + form.Events = []string{"push"} + } + w.PushOnly = false + w.SendEverything = false + w.ChooseEvents = true + w.Create = com.IsSliceContainsStr(form.Events, string(models.HOOK_EVENT_CREATE)) + w.Push = com.IsSliceContainsStr(form.Events, string(models.HOOK_EVENT_PUSH)) + if err = w.UpdateEvent(); err != nil { + ctx.Error(500, "UpdateEvent", err) + return + } + + if form.Active != nil { + w.IsActive = *form.Active + } + + if err := models.UpdateWebhook(w); err != nil { + ctx.Error(500, "UpdateWebhook", err) + return + } + + ctx.JSON(200, convert.ToHook(ctx.Repo.RepoLink, w)) +} diff --git a/routers/api/v1/repo/hooks.go b/routers/api/v1/repo/hooks.go deleted file mode 100644 index 0cbe6762..00000000 --- a/routers/api/v1/repo/hooks.go +++ /dev/null @@ -1,164 +0,0 @@ -// Copyright 2014 The Gogs Authors. All rights reserved. -// Use of this source code is governed by a MIT-style -// license that can be found in the LICENSE file. - -package repo - -import ( - "encoding/json" - - "github.com/Unknwon/com" - - api "github.com/gogits/go-gogs-client" - - "github.com/gogits/gogs/models" - "github.com/gogits/gogs/modules/context" - "github.com/gogits/gogs/routers/api/v1/convert" -) - -// https://github.com/gogits/go-gogs-client/wiki/Repositories#list-hooks -func ListHooks(ctx *context.APIContext) { - hooks, err := models.GetWebhooksByRepoID(ctx.Repo.Repository.ID) - if err != nil { - ctx.Error(500, "GetWebhooksByRepoID", err) - return - } - - apiHooks := make([]*api.Hook, len(hooks)) - for i := range hooks { - apiHooks[i] = convert.ToHook(ctx.Repo.RepoLink, hooks[i]) - } - ctx.JSON(200, &apiHooks) -} - -// https://github.com/gogits/go-gogs-client/wiki/Repositories#create-a-hook -func CreateHook(ctx *context.APIContext, form api.CreateHookOption) { - if !models.IsValidHookTaskType(form.Type) { - ctx.Error(422, "", "Invalid hook type") - return - } - for _, name := range []string{"url", "content_type"} { - if _, ok := form.Config[name]; !ok { - ctx.Error(422, "", "Missing config option: "+name) - return - } - } - if !models.IsValidHookContentType(form.Config["content_type"]) { - ctx.Error(422, "", "Invalid content type") - return - } - - if len(form.Events) == 0 { - form.Events = []string{"push"} - } - w := &models.Webhook{ - RepoID: ctx.Repo.Repository.ID, - URL: form.Config["url"], - ContentType: models.ToHookContentType(form.Config["content_type"]), - Secret: form.Config["secret"], - HookEvent: &models.HookEvent{ - ChooseEvents: true, - HookEvents: models.HookEvents{ - Create: com.IsSliceContainsStr(form.Events, string(models.HOOK_EVENT_CREATE)), - Push: com.IsSliceContainsStr(form.Events, string(models.HOOK_EVENT_PUSH)), - }, - }, - IsActive: form.Active, - HookTaskType: models.ToHookTaskType(form.Type), - } - if w.HookTaskType == models.SLACK { - channel, ok := form.Config["channel"] - if !ok { - ctx.Error(422, "", "Missing config option: channel") - return - } - meta, err := json.Marshal(&models.SlackMeta{ - Channel: channel, - Username: form.Config["username"], - IconURL: form.Config["icon_url"], - Color: form.Config["color"], - }) - if err != nil { - ctx.Error(500, "slack: JSON marshal failed", err) - return - } - w.Meta = string(meta) - } - - if err := w.UpdateEvent(); err != nil { - ctx.Error(500, "UpdateEvent", err) - return - } else if err := models.CreateWebhook(w); err != nil { - ctx.Error(500, "CreateWebhook", err) - return - } - - ctx.JSON(201, convert.ToHook(ctx.Repo.RepoLink, w)) -} - -// https://github.com/gogits/go-gogs-client/wiki/Repositories#edit-a-hook -func EditHook(ctx *context.APIContext, form api.EditHookOption) { - w, err := models.GetWebhookByID(ctx.ParamsInt64(":id")) - if err != nil { - if models.IsErrWebhookNotExist(err) { - ctx.Status(404) - } else { - ctx.Error(500, "GetWebhookByID", err) - } - return - } - - if form.Config != nil { - if url, ok := form.Config["url"]; ok { - w.URL = url - } - if ct, ok := form.Config["content_type"]; ok { - if !models.IsValidHookContentType(ct) { - ctx.Error(422, "", "Invalid content type") - return - } - w.ContentType = models.ToHookContentType(ct) - } - - if w.HookTaskType == models.SLACK { - if channel, ok := form.Config["channel"]; ok { - meta, err := json.Marshal(&models.SlackMeta{ - Channel: channel, - Username: form.Config["username"], - IconURL: form.Config["icon_url"], - Color: form.Config["color"], - }) - if err != nil { - ctx.Error(500, "slack: JSON marshal failed", err) - return - } - w.Meta = string(meta) - } - } - } - - // Update events - if len(form.Events) == 0 { - form.Events = []string{"push"} - } - w.PushOnly = false - w.SendEverything = false - w.ChooseEvents = true - w.Create = com.IsSliceContainsStr(form.Events, string(models.HOOK_EVENT_CREATE)) - w.Push = com.IsSliceContainsStr(form.Events, string(models.HOOK_EVENT_PUSH)) - if err = w.UpdateEvent(); err != nil { - ctx.Error(500, "UpdateEvent", err) - return - } - - if form.Active != nil { - w.IsActive = *form.Active - } - - if err := models.UpdateWebhook(w); err != nil { - ctx.Error(500, "UpdateWebhook", err) - return - } - - ctx.JSON(200, convert.ToHook(ctx.Repo.RepoLink, w)) -} diff --git a/routers/api/v1/repo/key.go b/routers/api/v1/repo/key.go new file mode 100644 index 00000000..563dac26 --- /dev/null +++ b/routers/api/v1/repo/key.go @@ -0,0 +1,114 @@ +// Copyright 2015 The Gogs Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package repo + +import ( + "fmt" + + api "github.com/gogits/go-gogs-client" + + "github.com/gogits/gogs/models" + "github.com/gogits/gogs/modules/context" + "github.com/gogits/gogs/modules/setting" + "github.com/gogits/gogs/routers/api/v1/convert" +) + +func composeDeployKeysAPILink(repoPath string) string { + return setting.AppUrl + "api/v1/repos/" + repoPath + "/keys/" +} + +// https://github.com/gogits/go-gogs-client/wiki/Repositories-Deploy-Keys#list-deploy-keys +func ListDeployKeys(ctx *context.APIContext) { + keys, err := models.ListDeployKeys(ctx.Repo.Repository.ID) + if err != nil { + ctx.Error(500, "ListDeployKeys", err) + return + } + + apiLink := composeDeployKeysAPILink(ctx.Repo.Owner.Name + "/" + ctx.Repo.Repository.Name) + apiKeys := make([]*api.DeployKey, len(keys)) + for i := range keys { + if err = keys[i].GetContent(); err != nil { + ctx.Error(500, "GetContent", err) + return + } + apiKeys[i] = convert.ToDeployKey(apiLink, keys[i]) + } + + ctx.JSON(200, &apiKeys) +} + +// https://github.com/gogits/go-gogs-client/wiki/Repositories-Deploy-Keys#get-a-deploy-key +func GetDeployKey(ctx *context.APIContext) { + key, err := models.GetDeployKeyByID(ctx.ParamsInt64(":id")) + if err != nil { + if models.IsErrDeployKeyNotExist(err) { + ctx.Status(404) + } else { + ctx.Error(500, "GetDeployKeyByID", err) + } + return + } + + if err = key.GetContent(); err != nil { + ctx.Error(500, "GetContent", err) + return + } + + apiLink := composeDeployKeysAPILink(ctx.Repo.Owner.Name + "/" + ctx.Repo.Repository.Name) + ctx.JSON(200, convert.ToDeployKey(apiLink, key)) +} + +func HandleCheckKeyStringError(ctx *context.APIContext, err error) { + if models.IsErrKeyUnableVerify(err) { + ctx.Error(422, "", "Unable to verify key content") + } else { + ctx.Error(422, "", fmt.Errorf("Invalid key content: %v", err)) + } +} + +func HandleAddKeyError(ctx *context.APIContext, err error) { + switch { + case models.IsErrKeyAlreadyExist(err): + ctx.Error(422, "", "Key content has been used as non-deploy key") + case models.IsErrKeyNameAlreadyUsed(err): + ctx.Error(422, "", "Key title has been used") + default: + ctx.Error(500, "AddKey", err) + } +} + +// https://github.com/gogits/go-gogs-client/wiki/Repositories-Deploy-Keys#add-a-new-deploy-key +func CreateDeployKey(ctx *context.APIContext, form api.CreateKeyOption) { + content, err := models.CheckPublicKeyString(form.Key) + if err != nil { + HandleCheckKeyStringError(ctx, err) + return + } + + key, err := models.AddDeployKey(ctx.Repo.Repository.ID, form.Title, content) + if err != nil { + HandleAddKeyError(ctx, err) + return + } + + key.Content = content + apiLink := composeDeployKeysAPILink(ctx.Repo.Owner.Name + "/" + ctx.Repo.Repository.Name) + ctx.JSON(201, convert.ToDeployKey(apiLink, key)) +} + +// https://github.com/gogits/go-gogs-client/wiki/Repositories-Deploy-Keys#remove-a-deploy-key +func DeleteDeploykey(ctx *context.APIContext) { + if err := models.DeleteDeployKey(ctx.User, ctx.ParamsInt64(":id")); err != nil { + if models.IsErrKeyAccessDenied(err) { + ctx.Error(403, "", "You do not have access to this key") + } else { + ctx.Error(500, "DeleteDeployKey", err) + } + return + } + + ctx.Status(204) +} diff --git a/routers/api/v1/repo/keys.go b/routers/api/v1/repo/keys.go deleted file mode 100644 index 563dac26..00000000 --- a/routers/api/v1/repo/keys.go +++ /dev/null @@ -1,114 +0,0 @@ -// Copyright 2015 The Gogs Authors. All rights reserved. -// Use of this source code is governed by a MIT-style -// license that can be found in the LICENSE file. - -package repo - -import ( - "fmt" - - api "github.com/gogits/go-gogs-client" - - "github.com/gogits/gogs/models" - "github.com/gogits/gogs/modules/context" - "github.com/gogits/gogs/modules/setting" - "github.com/gogits/gogs/routers/api/v1/convert" -) - -func composeDeployKeysAPILink(repoPath string) string { - return setting.AppUrl + "api/v1/repos/" + repoPath + "/keys/" -} - -// https://github.com/gogits/go-gogs-client/wiki/Repositories-Deploy-Keys#list-deploy-keys -func ListDeployKeys(ctx *context.APIContext) { - keys, err := models.ListDeployKeys(ctx.Repo.Repository.ID) - if err != nil { - ctx.Error(500, "ListDeployKeys", err) - return - } - - apiLink := composeDeployKeysAPILink(ctx.Repo.Owner.Name + "/" + ctx.Repo.Repository.Name) - apiKeys := make([]*api.DeployKey, len(keys)) - for i := range keys { - if err = keys[i].GetContent(); err != nil { - ctx.Error(500, "GetContent", err) - return - } - apiKeys[i] = convert.ToDeployKey(apiLink, keys[i]) - } - - ctx.JSON(200, &apiKeys) -} - -// https://github.com/gogits/go-gogs-client/wiki/Repositories-Deploy-Keys#get-a-deploy-key -func GetDeployKey(ctx *context.APIContext) { - key, err := models.GetDeployKeyByID(ctx.ParamsInt64(":id")) - if err != nil { - if models.IsErrDeployKeyNotExist(err) { - ctx.Status(404) - } else { - ctx.Error(500, "GetDeployKeyByID", err) - } - return - } - - if err = key.GetContent(); err != nil { - ctx.Error(500, "GetContent", err) - return - } - - apiLink := composeDeployKeysAPILink(ctx.Repo.Owner.Name + "/" + ctx.Repo.Repository.Name) - ctx.JSON(200, convert.ToDeployKey(apiLink, key)) -} - -func HandleCheckKeyStringError(ctx *context.APIContext, err error) { - if models.IsErrKeyUnableVerify(err) { - ctx.Error(422, "", "Unable to verify key content") - } else { - ctx.Error(422, "", fmt.Errorf("Invalid key content: %v", err)) - } -} - -func HandleAddKeyError(ctx *context.APIContext, err error) { - switch { - case models.IsErrKeyAlreadyExist(err): - ctx.Error(422, "", "Key content has been used as non-deploy key") - case models.IsErrKeyNameAlreadyUsed(err): - ctx.Error(422, "", "Key title has been used") - default: - ctx.Error(500, "AddKey", err) - } -} - -// https://github.com/gogits/go-gogs-client/wiki/Repositories-Deploy-Keys#add-a-new-deploy-key -func CreateDeployKey(ctx *context.APIContext, form api.CreateKeyOption) { - content, err := models.CheckPublicKeyString(form.Key) - if err != nil { - HandleCheckKeyStringError(ctx, err) - return - } - - key, err := models.AddDeployKey(ctx.Repo.Repository.ID, form.Title, content) - if err != nil { - HandleAddKeyError(ctx, err) - return - } - - key.Content = content - apiLink := composeDeployKeysAPILink(ctx.Repo.Owner.Name + "/" + ctx.Repo.Repository.Name) - ctx.JSON(201, convert.ToDeployKey(apiLink, key)) -} - -// https://github.com/gogits/go-gogs-client/wiki/Repositories-Deploy-Keys#remove-a-deploy-key -func DeleteDeploykey(ctx *context.APIContext) { - if err := models.DeleteDeployKey(ctx.User, ctx.ParamsInt64(":id")); err != nil { - if models.IsErrKeyAccessDenied(err) { - ctx.Error(403, "", "You do not have access to this key") - } else { - ctx.Error(500, "DeleteDeployKey", err) - } - return - } - - ctx.Status(204) -} diff --git a/routers/api/v1/user/follower.go b/routers/api/v1/user/follower.go new file mode 100644 index 00000000..00d1952d --- /dev/null +++ b/routers/api/v1/user/follower.go @@ -0,0 +1,121 @@ +// Copyright 2015 The Gogs Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package user + +import ( + api "github.com/gogits/go-gogs-client" + + "github.com/gogits/gogs/models" + "github.com/gogits/gogs/modules/context" + "github.com/gogits/gogs/routers/api/v1/convert" +) + +func responseApiUsers(ctx *context.APIContext, users []*models.User) { + apiUsers := make([]*api.User, len(users)) + for i := range users { + apiUsers[i] = convert.ToUser(users[i]) + } + ctx.JSON(200, &apiUsers) +} + +func listUserFollowers(ctx *context.APIContext, u *models.User) { + users, err := u.GetFollowers(ctx.QueryInt("page")) + if err != nil { + ctx.Error(500, "GetUserFollowers", err) + return + } + responseApiUsers(ctx, users) +} + +func ListMyFollowers(ctx *context.APIContext) { + listUserFollowers(ctx, ctx.User) +} + +// https://github.com/gogits/go-gogs-client/wiki/Users-Followers#list-followers-of-a-user +func ListFollowers(ctx *context.APIContext) { + u := GetUserByParams(ctx) + if ctx.Written() { + return + } + listUserFollowers(ctx, u) +} + +func listUserFollowing(ctx *context.APIContext, u *models.User) { + users, err := u.GetFollowing(ctx.QueryInt("page")) + if err != nil { + ctx.Error(500, "GetFollowing", err) + return + } + responseApiUsers(ctx, users) +} + +func ListMyFollowing(ctx *context.APIContext) { + listUserFollowing(ctx, ctx.User) +} + +// https://github.com/gogits/go-gogs-client/wiki/Users-Followers#list-users-followed-by-another-user +func ListFollowing(ctx *context.APIContext) { + u := GetUserByParams(ctx) + if ctx.Written() { + return + } + listUserFollowing(ctx, u) +} + +func checkUserFollowing(ctx *context.APIContext, u *models.User, followID int64) { + if u.IsFollowing(followID) { + ctx.Status(204) + } else { + ctx.Status(404) + } +} + +// https://github.com/gogits/go-gogs-client/wiki/Users-Followers#check-if-you-are-following-a-user +func CheckMyFollowing(ctx *context.APIContext) { + target := GetUserByParams(ctx) + if ctx.Written() { + return + } + checkUserFollowing(ctx, ctx.User, target.Id) +} + +// https://github.com/gogits/go-gogs-client/wiki/Users-Followers#check-if-one-user-follows-another +func CheckFollowing(ctx *context.APIContext) { + u := GetUserByParams(ctx) + if ctx.Written() { + return + } + target := GetUserByParamsName(ctx, ":target") + if ctx.Written() { + return + } + checkUserFollowing(ctx, u, target.Id) +} + +// https://github.com/gogits/go-gogs-client/wiki/Users-Followers#follow-a-user +func Follow(ctx *context.APIContext) { + target := GetUserByParams(ctx) + if ctx.Written() { + return + } + if err := models.FollowUser(ctx.User.Id, target.Id); err != nil { + ctx.Error(500, "FollowUser", err) + return + } + ctx.Status(204) +} + +// https://github.com/gogits/go-gogs-client/wiki/Users-Followers#unfollow-a-user +func Unfollow(ctx *context.APIContext) { + target := GetUserByParams(ctx) + if ctx.Written() { + return + } + if err := models.UnfollowUser(ctx.User.Id, target.Id); err != nil { + ctx.Error(500, "UnfollowUser", err) + return + } + ctx.Status(204) +} diff --git a/routers/api/v1/user/followers.go b/routers/api/v1/user/followers.go deleted file mode 100644 index 00d1952d..00000000 --- a/routers/api/v1/user/followers.go +++ /dev/null @@ -1,121 +0,0 @@ -// Copyright 2015 The Gogs Authors. All rights reserved. -// Use of this source code is governed by a MIT-style -// license that can be found in the LICENSE file. - -package user - -import ( - api "github.com/gogits/go-gogs-client" - - "github.com/gogits/gogs/models" - "github.com/gogits/gogs/modules/context" - "github.com/gogits/gogs/routers/api/v1/convert" -) - -func responseApiUsers(ctx *context.APIContext, users []*models.User) { - apiUsers := make([]*api.User, len(users)) - for i := range users { - apiUsers[i] = convert.ToUser(users[i]) - } - ctx.JSON(200, &apiUsers) -} - -func listUserFollowers(ctx *context.APIContext, u *models.User) { - users, err := u.GetFollowers(ctx.QueryInt("page")) - if err != nil { - ctx.Error(500, "GetUserFollowers", err) - return - } - responseApiUsers(ctx, users) -} - -func ListMyFollowers(ctx *context.APIContext) { - listUserFollowers(ctx, ctx.User) -} - -// https://github.com/gogits/go-gogs-client/wiki/Users-Followers#list-followers-of-a-user -func ListFollowers(ctx *context.APIContext) { - u := GetUserByParams(ctx) - if ctx.Written() { - return - } - listUserFollowers(ctx, u) -} - -func listUserFollowing(ctx *context.APIContext, u *models.User) { - users, err := u.GetFollowing(ctx.QueryInt("page")) - if err != nil { - ctx.Error(500, "GetFollowing", err) - return - } - responseApiUsers(ctx, users) -} - -func ListMyFollowing(ctx *context.APIContext) { - listUserFollowing(ctx, ctx.User) -} - -// https://github.com/gogits/go-gogs-client/wiki/Users-Followers#list-users-followed-by-another-user -func ListFollowing(ctx *context.APIContext) { - u := GetUserByParams(ctx) - if ctx.Written() { - return - } - listUserFollowing(ctx, u) -} - -func checkUserFollowing(ctx *context.APIContext, u *models.User, followID int64) { - if u.IsFollowing(followID) { - ctx.Status(204) - } else { - ctx.Status(404) - } -} - -// https://github.com/gogits/go-gogs-client/wiki/Users-Followers#check-if-you-are-following-a-user -func CheckMyFollowing(ctx *context.APIContext) { - target := GetUserByParams(ctx) - if ctx.Written() { - return - } - checkUserFollowing(ctx, ctx.User, target.Id) -} - -// https://github.com/gogits/go-gogs-client/wiki/Users-Followers#check-if-one-user-follows-another -func CheckFollowing(ctx *context.APIContext) { - u := GetUserByParams(ctx) - if ctx.Written() { - return - } - target := GetUserByParamsName(ctx, ":target") - if ctx.Written() { - return - } - checkUserFollowing(ctx, u, target.Id) -} - -// https://github.com/gogits/go-gogs-client/wiki/Users-Followers#follow-a-user -func Follow(ctx *context.APIContext) { - target := GetUserByParams(ctx) - if ctx.Written() { - return - } - if err := models.FollowUser(ctx.User.Id, target.Id); err != nil { - ctx.Error(500, "FollowUser", err) - return - } - ctx.Status(204) -} - -// https://github.com/gogits/go-gogs-client/wiki/Users-Followers#unfollow-a-user -func Unfollow(ctx *context.APIContext) { - target := GetUserByParams(ctx) - if ctx.Written() { - return - } - if err := models.UnfollowUser(ctx.User.Id, target.Id); err != nil { - ctx.Error(500, "UnfollowUser", err) - return - } - ctx.Status(204) -} diff --git a/routers/api/v1/user/key.go b/routers/api/v1/user/key.go new file mode 100644 index 00000000..7337112e --- /dev/null +++ b/routers/api/v1/user/key.go @@ -0,0 +1,119 @@ +// Copyright 2015 The Gogs Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package user + +import ( + api "github.com/gogits/go-gogs-client" + + "github.com/gogits/gogs/models" + "github.com/gogits/gogs/modules/context" + "github.com/gogits/gogs/modules/setting" + "github.com/gogits/gogs/routers/api/v1/convert" + "github.com/gogits/gogs/routers/api/v1/repo" +) + +func GetUserByParamsName(ctx *context.APIContext, name string) *models.User { + user, err := models.GetUserByName(ctx.Params(name)) + if err != nil { + if models.IsErrUserNotExist(err) { + ctx.Status(404) + } else { + ctx.Error(500, "GetUserByName", err) + } + return nil + } + return user +} + +// GetUserByParams returns user whose name is presented in URL paramenter. +func GetUserByParams(ctx *context.APIContext) *models.User { + return GetUserByParamsName(ctx, ":username") +} + +func composePublicKeysAPILink() string { + return setting.AppUrl + "api/v1/user/keys/" +} + +func listPublicKeys(ctx *context.APIContext, uid int64) { + keys, err := models.ListPublicKeys(uid) + if err != nil { + ctx.Error(500, "ListPublicKeys", err) + return + } + + apiLink := composePublicKeysAPILink() + apiKeys := make([]*api.PublicKey, len(keys)) + for i := range keys { + apiKeys[i] = convert.ToPublicKey(apiLink, keys[i]) + } + + ctx.JSON(200, &apiKeys) +} + +// https://github.com/gogits/go-gogs-client/wiki/Users-Public-Keys#list-your-public-keys +func ListMyPublicKeys(ctx *context.APIContext) { + listPublicKeys(ctx, ctx.User.Id) +} + +// https://github.com/gogits/go-gogs-client/wiki/Users-Public-Keys#list-public-keys-for-a-user +func ListPublicKeys(ctx *context.APIContext) { + user := GetUserByParams(ctx) + if ctx.Written() { + return + } + listPublicKeys(ctx, user.Id) +} + +// https://github.com/gogits/go-gogs-client/wiki/Users-Public-Keys#get-a-single-public-key +func GetPublicKey(ctx *context.APIContext) { + key, err := models.GetPublicKeyByID(ctx.ParamsInt64(":id")) + if err != nil { + if models.IsErrKeyNotExist(err) { + ctx.Status(404) + } else { + ctx.Error(500, "GetPublicKeyByID", err) + } + return + } + + apiLink := composePublicKeysAPILink() + ctx.JSON(200, convert.ToPublicKey(apiLink, key)) +} + +// CreateUserPublicKey creates new public key to given user by ID. +func CreateUserPublicKey(ctx *context.APIContext, form api.CreateKeyOption, uid int64) { + content, err := models.CheckPublicKeyString(form.Key) + if err != nil { + repo.HandleCheckKeyStringError(ctx, err) + return + } + + key, err := models.AddPublicKey(uid, form.Title, content) + if err != nil { + repo.HandleAddKeyError(ctx, err) + return + } + apiLink := composePublicKeysAPILink() + ctx.JSON(201, convert.ToPublicKey(apiLink, key)) +} + +// https://github.com/gogits/go-gogs-client/wiki/Users-Public-Keys#create-a-public-key +func CreatePublicKey(ctx *context.APIContext, form api.CreateKeyOption) { + CreateUserPublicKey(ctx, form, ctx.User.Id) +} + +// https://github.com/gogits/go-gogs-client/wiki/Users-Public-Keys#delete-a-public-key +func DeletePublicKey(ctx *context.APIContext) { + if err := models.DeletePublicKey(ctx.User, ctx.ParamsInt64(":id")); err != nil { + if models.IsErrKeyAccessDenied(err) { + ctx.Error(403, "", "You do not have access to this key") + } else { + ctx.Error(500, "DeletePublicKey", err) + } + return + } + + ctx.Status(204) +} diff --git a/routers/api/v1/user/keys.go b/routers/api/v1/user/keys.go deleted file mode 100644 index 7337112e..00000000 --- a/routers/api/v1/user/keys.go +++ /dev/null @@ -1,119 +0,0 @@ -// Copyright 2015 The Gogs Authors. All rights reserved. -// Use of this source code is governed by a MIT-style -// license that can be found in the LICENSE file. - -package user - -import ( - api "github.com/gogits/go-gogs-client" - - "github.com/gogits/gogs/models" - "github.com/gogits/gogs/modules/context" - "github.com/gogits/gogs/modules/setting" - "github.com/gogits/gogs/routers/api/v1/convert" - "github.com/gogits/gogs/routers/api/v1/repo" -) - -func GetUserByParamsName(ctx *context.APIContext, name string) *models.User { - user, err := models.GetUserByName(ctx.Params(name)) - if err != nil { - if models.IsErrUserNotExist(err) { - ctx.Status(404) - } else { - ctx.Error(500, "GetUserByName", err) - } - return nil - } - return user -} - -// GetUserByParams returns user whose name is presented in URL paramenter. -func GetUserByParams(ctx *context.APIContext) *models.User { - return GetUserByParamsName(ctx, ":username") -} - -func composePublicKeysAPILink() string { - return setting.AppUrl + "api/v1/user/keys/" -} - -func listPublicKeys(ctx *context.APIContext, uid int64) { - keys, err := models.ListPublicKeys(uid) - if err != nil { - ctx.Error(500, "ListPublicKeys", err) - return - } - - apiLink := composePublicKeysAPILink() - apiKeys := make([]*api.PublicKey, len(keys)) - for i := range keys { - apiKeys[i] = convert.ToPublicKey(apiLink, keys[i]) - } - - ctx.JSON(200, &apiKeys) -} - -// https://github.com/gogits/go-gogs-client/wiki/Users-Public-Keys#list-your-public-keys -func ListMyPublicKeys(ctx *context.APIContext) { - listPublicKeys(ctx, ctx.User.Id) -} - -// https://github.com/gogits/go-gogs-client/wiki/Users-Public-Keys#list-public-keys-for-a-user -func ListPublicKeys(ctx *context.APIContext) { - user := GetUserByParams(ctx) - if ctx.Written() { - return - } - listPublicKeys(ctx, user.Id) -} - -// https://github.com/gogits/go-gogs-client/wiki/Users-Public-Keys#get-a-single-public-key -func GetPublicKey(ctx *context.APIContext) { - key, err := models.GetPublicKeyByID(ctx.ParamsInt64(":id")) - if err != nil { - if models.IsErrKeyNotExist(err) { - ctx.Status(404) - } else { - ctx.Error(500, "GetPublicKeyByID", err) - } - return - } - - apiLink := composePublicKeysAPILink() - ctx.JSON(200, convert.ToPublicKey(apiLink, key)) -} - -// CreateUserPublicKey creates new public key to given user by ID. -func CreateUserPublicKey(ctx *context.APIContext, form api.CreateKeyOption, uid int64) { - content, err := models.CheckPublicKeyString(form.Key) - if err != nil { - repo.HandleCheckKeyStringError(ctx, err) - return - } - - key, err := models.AddPublicKey(uid, form.Title, content) - if err != nil { - repo.HandleAddKeyError(ctx, err) - return - } - apiLink := composePublicKeysAPILink() - ctx.JSON(201, convert.ToPublicKey(apiLink, key)) -} - -// https://github.com/gogits/go-gogs-client/wiki/Users-Public-Keys#create-a-public-key -func CreatePublicKey(ctx *context.APIContext, form api.CreateKeyOption) { - CreateUserPublicKey(ctx, form, ctx.User.Id) -} - -// https://github.com/gogits/go-gogs-client/wiki/Users-Public-Keys#delete-a-public-key -func DeletePublicKey(ctx *context.APIContext) { - if err := models.DeletePublicKey(ctx.User, ctx.ParamsInt64(":id")); err != nil { - if models.IsErrKeyAccessDenied(err) { - ctx.Error(403, "", "You do not have access to this key") - } else { - ctx.Error(500, "DeletePublicKey", err) - } - return - } - - ctx.Status(204) -} diff --git a/routers/org/teams.go b/routers/org/teams.go index e8ae8291..430f08ec 100644 --- a/routers/org/teams.go +++ b/routers/org/teams.go @@ -154,25 +154,11 @@ func NewTeamPost(ctx *context.Context, form auth.CreateTeamForm) { ctx.Data["PageIsOrgTeams"] = true ctx.Data["PageIsOrgTeamsNew"] = true - // Validate permission level. - var auth models.AccessMode - switch form.Permission { - case "read": - auth = models.ACCESS_MODE_READ - case "write": - auth = models.ACCESS_MODE_WRITE - case "admin": - auth = models.ACCESS_MODE_ADMIN - default: - ctx.Error(401) - return - } - t := &models.Team{ OrgID: ctx.Org.Organization.Id, Name: form.TeamName, Description: form.Description, - Authorize: auth, + Authorize: models.ParseAccessMode(form.Permission), } ctx.Data["Team"] = t diff --git a/templates/.VERSION b/templates/.VERSION index f983a7d6..10cd7f56 100644 --- a/templates/.VERSION +++ b/templates/.VERSION @@ -1 +1 @@ -0.9.13.0321 \ No newline at end of file +0.9.14.0321 \ No newline at end of file -- cgit v1.2.3