diff options
Diffstat (limited to 'routers')
-rw-r--r-- | routers/api/v1/admin/orgs.go | 44 | ||||
-rw-r--r-- | routers/api/v1/admin/users.go | 6 | ||||
-rw-r--r-- | routers/api/v1/api.go | 7 | ||||
-rw-r--r-- | routers/api/v1/convert/convert.go (renamed from routers/api/v1/utils/convert.go) | 38 | ||||
-rw-r--r-- | routers/api/v1/org/org.go | 74 | ||||
-rw-r--r-- | routers/api/v1/repo/hooks.go | 10 | ||||
-rw-r--r-- | routers/api/v1/repo/keys.go | 8 | ||||
-rw-r--r-- | routers/api/v1/repo/repo.go | 12 | ||||
-rw-r--r-- | routers/api/v1/user/email.go | 9 | ||||
-rw-r--r-- | routers/api/v1/user/keys.go | 18 | ||||
-rw-r--r-- | routers/repo/pull.go | 2 | ||||
-rw-r--r-- | routers/user/home.go | 2 |
12 files changed, 187 insertions, 43 deletions
diff --git a/routers/api/v1/admin/orgs.go b/routers/api/v1/admin/orgs.go new file mode 100644 index 0000000000..5b5302ca15 --- /dev/null +++ b/routers/api/v1/admin/orgs.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/middleware" + "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 *middleware.Context, 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.ORGANIZATION, + } + if err := models.CreateOrganization(org, u); err != nil { + if models.IsErrUserAlreadyExist(err) || + models.IsErrNameReserved(err) || + models.IsErrNamePatternNotAllowed(err) { + ctx.APIError(422, "CreateOrganization", err) + } else { + ctx.APIError(500, "CreateOrganization", err) + } + return + } + + ctx.JSON(201, convert.ToApiOrganization(org)) +} diff --git a/routers/api/v1/admin/users.go b/routers/api/v1/admin/users.go index 1bd253681b..9a8c906f29 100644 --- a/routers/api/v1/admin/users.go +++ b/routers/api/v1/admin/users.go @@ -12,8 +12,8 @@ import ( "github.com/gogits/gogs/modules/mailer" "github.com/gogits/gogs/modules/middleware" "github.com/gogits/gogs/modules/setting" + "github.com/gogits/gogs/routers/api/v1/convert" "github.com/gogits/gogs/routers/api/v1/user" - to "github.com/gogits/gogs/routers/api/v1/utils" ) func parseLoginSource(ctx *middleware.Context, u *models.User, sourceID int64, loginName string) { @@ -69,7 +69,7 @@ func CreateUser(ctx *middleware.Context, form api.CreateUserOption) { mailer.SendRegisterNotifyMail(ctx.Context, u) } - ctx.JSON(201, to.ApiUser(u)) + ctx.JSON(201, convert.ToApiUser(u)) } // https://github.com/gogits/go-gogs-client/wiki/Administration-Users#edit-an-existing-user @@ -118,7 +118,7 @@ func EditUser(ctx *middleware.Context, form api.EditUserOption) { } log.Trace("Account profile updated by admin (%s): %s", ctx.User.Name, u.Name) - ctx.JSON(200, to.ApiUser(u)) + ctx.JSON(200, convert.ToApiUser(u)) } // https://github.com/gogits/go-gogs-client/wiki/Administration-Users#delete-a-user diff --git a/routers/api/v1/api.go b/routers/api/v1/api.go index eeba713945..bc7c14006e 100644 --- a/routers/api/v1/api.go +++ b/routers/api/v1/api.go @@ -17,6 +17,7 @@ import ( "github.com/gogits/gogs/modules/middleware" "github.com/gogits/gogs/routers/api/v1/admin" "github.com/gogits/gogs/routers/api/v1/misc" + "github.com/gogits/gogs/routers/api/v1/org" "github.com/gogits/gogs/routers/api/v1/repo" "github.com/gogits/gogs/routers/api/v1/user" ) @@ -179,6 +180,11 @@ func RegisterRoutes(m *macaron.Macaron) { }, RepoAssignment()) }, ReqToken()) + // Organizations + m.Get("/user/orgs", org.ListMyOrgs) + m.Get("/users/:username/orgs", org.ListUserOrgs) + m.Combo("/orgs/:orgname").Get(org.Get).Patch(bind(api.EditOrgOption{}), org.Edit) + m.Any("/*", func(ctx *middleware.Context) { ctx.Error(404) }) @@ -191,6 +197,7 @@ func RegisterRoutes(m *macaron.Macaron) { m.Combo("").Patch(bind(api.EditUserOption{}), admin.EditUser). Delete(admin.DeleteUser) m.Post("/keys", admin.CreatePublicKey) + m.Post("/orgs", bind(api.CreateOrgOption{}), admin.CreateOrg) }) }) }, ReqAdmin()) diff --git a/routers/api/v1/utils/convert.go b/routers/api/v1/convert/convert.go index 3871c0da11..b91abbdd39 100644 --- a/routers/api/v1/utils/convert.go +++ b/routers/api/v1/convert/convert.go @@ -2,7 +2,7 @@ // Use of this source code is governed by a MIT-style // license that can be found in the LICENSE file. -package utils +package convert import ( "fmt" @@ -15,8 +15,8 @@ import ( "github.com/gogits/gogs/modules/setting" ) -// ApiUser converts user to its API format. -func ApiUser(u *models.User) *api.User { +// ToApiUser converts user to its API format. +func ToApiUser(u *models.User) *api.User { return &api.User{ ID: u.Id, UserName: u.Name, @@ -26,7 +26,7 @@ func ApiUser(u *models.User) *api.User { } } -func ApiEmail(email *models.EmailAddress) *api.Email { +func ToApiEmail(email *models.EmailAddress) *api.Email { return &api.Email{ Email: email.Email, Verified: email.IsActivated, @@ -34,12 +34,12 @@ func ApiEmail(email *models.EmailAddress) *api.Email { } } -// ApiRepository converts repository to API format. -func ApiRepository(owner *models.User, repo *models.Repository, permission api.Permission) *api.Repository { +// ToApiRepository converts repository to API format. +func ToApiRepository(owner *models.User, repo *models.Repository, permission api.Permission) *api.Repository { cl := repo.CloneLink() return &api.Repository{ Id: repo.ID, - Owner: *ApiUser(owner), + Owner: *ToApiUser(owner), FullName: owner.Name + "/" + repo.Name, Private: repo.IsPrivate, Fork: repo.IsFork, @@ -50,8 +50,8 @@ func ApiRepository(owner *models.User, repo *models.Repository, permission api.P } } -// ApiPublicKey converts public key to its API format. -func ApiPublicKey(apiLink string, key *models.PublicKey) *api.PublicKey { +// ToApiPublicKey converts public key to its API format. +func ToApiPublicKey(apiLink string, key *models.PublicKey) *api.PublicKey { return &api.PublicKey{ ID: key.ID, Key: key.Content, @@ -61,8 +61,8 @@ func ApiPublicKey(apiLink string, key *models.PublicKey) *api.PublicKey { } } -// ApiHook converts webhook to its API format. -func ApiHook(repoLink string, w *models.Webhook) *api.Hook { +// ToApiHook converts webhook to its API format. +func ToApiHook(repoLink string, w *models.Webhook) *api.Hook { config := map[string]string{ "url": w.URL, "content_type": w.ContentType.Name(), @@ -87,8 +87,8 @@ func ApiHook(repoLink string, w *models.Webhook) *api.Hook { } } -// ApiDeployKey converts deploy key to its API format. -func ApiDeployKey(apiLink string, key *models.DeployKey) *api.DeployKey { +// ToApiDeployKey converts deploy key to its API format. +func ToApiDeployKey(apiLink string, key *models.DeployKey) *api.DeployKey { return &api.DeployKey{ ID: key.ID, Key: key.Content, @@ -98,3 +98,15 @@ func ApiDeployKey(apiLink string, key *models.DeployKey) *api.DeployKey { ReadOnly: true, // All deploy keys are read-only. } } + +func ToApiOrganization(org *models.User) *api.Organization { + return &api.Organization{ + ID: org.Id, + AvatarUrl: org.AvatarLink(), + UserName: org.Name, + FullName: org.FullName, + Description: org.Description, + Website: org.Website, + Location: org.Location, + } +} diff --git a/routers/api/v1/org/org.go b/routers/api/v1/org/org.go new file mode 100644 index 0000000000..2c6d6fd3f8 --- /dev/null +++ b/routers/api/v1/org/org.go @@ -0,0 +1,74 @@ +// 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 org + +import ( + api "github.com/gogits/go-gogs-client" + + "github.com/gogits/gogs/models" + "github.com/gogits/gogs/modules/middleware" + "github.com/gogits/gogs/routers/api/v1/convert" + "github.com/gogits/gogs/routers/api/v1/user" +) + +func listUserOrgs(ctx *middleware.Context, u *models.User, all bool) { + if err := u.GetOrganizations(all); err != nil { + ctx.APIError(500, "GetOrganizations", err) + return + } + + apiOrgs := make([]*api.Organization, len(u.Orgs)) + for i := range u.Orgs { + apiOrgs[i] = convert.ToApiOrganization(u.Orgs[i]) + } + ctx.JSON(200, &apiOrgs) +} + +// https://github.com/gogits/go-gogs-client/wiki/Organizations#list-your-organizations +func ListMyOrgs(ctx *middleware.Context) { + listUserOrgs(ctx, ctx.User, true) +} + +// https://github.com/gogits/go-gogs-client/wiki/Organizations#list-user-organizations +func ListUserOrgs(ctx *middleware.Context) { + u := user.GetUserByParams(ctx) + if ctx.Written() { + return + } + listUserOrgs(ctx, u, false) +} + +// https://github.com/gogits/go-gogs-client/wiki/Organizations#get-an-organization +func Get(ctx *middleware.Context) { + org := user.GetUserByParamsName(ctx, ":orgname") + if ctx.Written() { + return + } + ctx.JSON(200, convert.ToApiOrganization(org)) +} + +// https://github.com/gogits/go-gogs-client/wiki/Organizations#edit-an-organization +func Edit(ctx *middleware.Context, form api.EditOrgOption) { + org := user.GetUserByParamsName(ctx, ":orgname") + if ctx.Written() { + return + } + + if !org.IsOwnedBy(ctx.User.Id) { + ctx.Error(403) + return + } + + org.FullName = form.FullName + org.Description = form.Description + org.Website = form.Website + org.Location = form.Location + if err := models.UpdateUser(org); err != nil { + ctx.APIError(500, "UpdateUser", err) + return + } + + ctx.JSON(200, convert.ToApiOrganization(org)) +} diff --git a/routers/api/v1/repo/hooks.go b/routers/api/v1/repo/hooks.go index 1ec7088670..e0893a014c 100644 --- a/routers/api/v1/repo/hooks.go +++ b/routers/api/v1/repo/hooks.go @@ -13,7 +13,7 @@ import ( "github.com/gogits/gogs/models" "github.com/gogits/gogs/modules/middleware" - to "github.com/gogits/gogs/routers/api/v1/utils" + "github.com/gogits/gogs/routers/api/v1/convert" ) // https://github.com/gogits/go-gogs-client/wiki/Repositories#list-hooks @@ -26,7 +26,7 @@ func ListHooks(ctx *middleware.Context) { apiHooks := make([]*api.Hook, len(hooks)) for i := range hooks { - apiHooks[i] = to.ApiHook(ctx.Repo.RepoLink, hooks[i]) + apiHooks[i] = convert.ToApiHook(ctx.Repo.RepoLink, hooks[i]) } ctx.JSON(200, &apiHooks) @@ -94,7 +94,7 @@ func CreateHook(ctx *middleware.Context, form api.CreateHookOption) { return } - ctx.JSON(201, to.ApiHook(ctx.Repo.RepoLink, w)) + ctx.JSON(201, convert.ToApiHook(ctx.Repo.RepoLink, w)) } // https://github.com/gogits/go-gogs-client/wiki/Repositories#edit-a-hook @@ -104,7 +104,7 @@ func EditHook(ctx *middleware.Context, form api.EditHookOption) { if models.IsErrWebhookNotExist(err) { ctx.Error(404) } else { - ctx.APIError(500, "GetWebhookById", err) + ctx.APIError(500, "GetWebhookByID", err) } return } @@ -161,5 +161,5 @@ func EditHook(ctx *middleware.Context, form api.EditHookOption) { return } - ctx.JSON(200, to.ApiHook(ctx.Repo.RepoLink, w)) + ctx.JSON(200, convert.ToApiHook(ctx.Repo.RepoLink, w)) } diff --git a/routers/api/v1/repo/keys.go b/routers/api/v1/repo/keys.go index b0d8f9146d..2ec0b754f2 100644 --- a/routers/api/v1/repo/keys.go +++ b/routers/api/v1/repo/keys.go @@ -12,7 +12,7 @@ import ( "github.com/gogits/gogs/models" "github.com/gogits/gogs/modules/middleware" "github.com/gogits/gogs/modules/setting" - to "github.com/gogits/gogs/routers/api/v1/utils" + "github.com/gogits/gogs/routers/api/v1/convert" ) func composeDeployKeysAPILink(repoPath string) string { @@ -34,7 +34,7 @@ func ListDeployKeys(ctx *middleware.Context) { ctx.APIError(500, "GetContent", err) return } - apiKeys[i] = to.ApiDeployKey(apiLink, keys[i]) + apiKeys[i] = convert.ToApiDeployKey(apiLink, keys[i]) } ctx.JSON(200, &apiKeys) @@ -58,7 +58,7 @@ func GetDeployKey(ctx *middleware.Context) { } apiLink := composeDeployKeysAPILink(ctx.Repo.Owner.Name + "/" + ctx.Repo.Repository.Name) - ctx.JSON(200, to.ApiDeployKey(apiLink, key)) + ctx.JSON(200, convert.ToApiDeployKey(apiLink, key)) } func HandleCheckKeyStringError(ctx *middleware.Context, err error) { @@ -96,7 +96,7 @@ func CreateDeployKey(ctx *middleware.Context, form api.CreateKeyOption) { key.Content = content apiLink := composeDeployKeysAPILink(ctx.Repo.Owner.Name + "/" + ctx.Repo.Repository.Name) - ctx.JSON(201, to.ApiDeployKey(apiLink, key)) + ctx.JSON(201, convert.ToApiDeployKey(apiLink, key)) } // https://github.com/gogits/go-gogs-client/wiki/Repositories-Deploy-Keys#remove-a-deploy-key diff --git a/routers/api/v1/repo/repo.go b/routers/api/v1/repo/repo.go index c0d897b5d4..7790377910 100644 --- a/routers/api/v1/repo/repo.go +++ b/routers/api/v1/repo/repo.go @@ -16,7 +16,7 @@ import ( "github.com/gogits/gogs/modules/log" "github.com/gogits/gogs/modules/middleware" "github.com/gogits/gogs/modules/setting" - to "github.com/gogits/gogs/routers/api/v1/utils" + "github.com/gogits/gogs/routers/api/v1/convert" ) // https://github.com/gogits/go-gogs-client/wiki/Repositories#search-repositories @@ -97,12 +97,12 @@ func ListMyRepos(ctx *middleware.Context) { repos := make([]*api.Repository, numOwnRepos+len(accessibleRepos)) for i := range ownRepos { - repos[i] = to.ApiRepository(ctx.User, ownRepos[i], api.Permission{true, true, true}) + repos[i] = convert.ToApiRepository(ctx.User, ownRepos[i], api.Permission{true, true, true}) } i := numOwnRepos for repo, access := range accessibleRepos { - repos[i] = to.ApiRepository(repo.Owner, repo, api.Permission{ + repos[i] = convert.ToApiRepository(repo.Owner, repo, api.Permission{ Admin: access >= models.ACCESS_MODE_ADMIN, Push: access >= models.ACCESS_MODE_WRITE, Pull: true, @@ -139,7 +139,7 @@ func createRepo(ctx *middleware.Context, owner *models.User, opt api.CreateRepoO return } - ctx.JSON(201, to.ApiRepository(owner, repo, api.Permission{true, true, true})) + ctx.JSON(201, convert.ToApiRepository(owner, repo, api.Permission{true, true, true})) } // https://github.com/gogits/go-gogs-client/wiki/Repositories#create @@ -239,7 +239,7 @@ func Migrate(ctx *middleware.Context, form auth.MigrateRepoForm) { } log.Trace("Repository migrated: %s/%s", ctxUser.Name, form.RepoName) - ctx.JSON(201, to.ApiRepository(ctxUser, repo, api.Permission{true, true, true})) + ctx.JSON(201, convert.ToApiRepository(ctxUser, repo, api.Permission{true, true, true})) } func parseOwnerAndRepo(ctx *middleware.Context) (*models.User, *models.Repository) { @@ -273,7 +273,7 @@ func Get(ctx *middleware.Context) { return } - ctx.JSON(200, to.ApiRepository(owner, repo, api.Permission{true, true, true})) + ctx.JSON(200, convert.ToApiRepository(owner, repo, api.Permission{true, true, true})) } // https://github.com/gogits/go-gogs-client/wiki/Repositories#delete diff --git a/routers/api/v1/user/email.go b/routers/api/v1/user/email.go index 449560e782..fd9193bd74 100644 --- a/routers/api/v1/user/email.go +++ b/routers/api/v1/user/email.go @@ -10,9 +10,10 @@ import ( "github.com/gogits/gogs/models" "github.com/gogits/gogs/modules/middleware" "github.com/gogits/gogs/modules/setting" - to "github.com/gogits/gogs/routers/api/v1/utils" + "github.com/gogits/gogs/routers/api/v1/convert" ) +// https://github.com/gogits/go-gogs-client/wiki/Users-Emails#list-email-addresses-for-a-user func ListEmails(ctx *middleware.Context) { emails, err := models.GetEmailAddresses(ctx.User.Id) if err != nil { @@ -21,11 +22,12 @@ func ListEmails(ctx *middleware.Context) { } apiEmails := make([]*api.Email, len(emails)) for i := range emails { - apiEmails[i] = to.ApiEmail(emails[i]) + apiEmails[i] = convert.ToApiEmail(emails[i]) } ctx.JSON(200, &apiEmails) } +// https://github.com/gogits/go-gogs-client/wiki/Users-Emails#add-email-addresses func AddEmail(ctx *middleware.Context, form api.CreateEmailOption) { if len(form.Emails) == 0 { ctx.Status(422) @@ -52,11 +54,12 @@ func AddEmail(ctx *middleware.Context, form api.CreateEmailOption) { apiEmails := make([]*api.Email, len(emails)) for i := range emails { - apiEmails[i] = to.ApiEmail(emails[i]) + apiEmails[i] = convert.ToApiEmail(emails[i]) } ctx.JSON(201, &apiEmails) } +// https://github.com/gogits/go-gogs-client/wiki/Users-Emails#delete-email-addresses func DeleteEmail(ctx *middleware.Context, form api.CreateEmailOption) { if len(form.Emails) == 0 { ctx.Status(204) diff --git a/routers/api/v1/user/keys.go b/routers/api/v1/user/keys.go index 213631e146..8ba73d9934 100644 --- a/routers/api/v1/user/keys.go +++ b/routers/api/v1/user/keys.go @@ -10,13 +10,12 @@ import ( "github.com/gogits/gogs/models" "github.com/gogits/gogs/modules/middleware" "github.com/gogits/gogs/modules/setting" + "github.com/gogits/gogs/routers/api/v1/convert" "github.com/gogits/gogs/routers/api/v1/repo" - to "github.com/gogits/gogs/routers/api/v1/utils" ) -// GetUserByParams returns user whose name is presented in URL paramenter. -func GetUserByParams(ctx *middleware.Context) *models.User { - user, err := models.GetUserByName(ctx.Params(":username")) +func GetUserByParamsName(ctx *middleware.Context, name string) *models.User { + user, err := models.GetUserByName(ctx.Params(name)) if err != nil { if models.IsErrUserNotExist(err) { ctx.Error(404) @@ -28,6 +27,11 @@ func GetUserByParams(ctx *middleware.Context) *models.User { return user } +// GetUserByParams returns user whose name is presented in URL paramenter. +func GetUserByParams(ctx *middleware.Context) *models.User { + return GetUserByParamsName(ctx, ":username") +} + func composePublicKeysAPILink() string { return setting.AppUrl + "api/v1/user/keys/" } @@ -42,7 +46,7 @@ func listPublicKeys(ctx *middleware.Context, uid int64) { apiLink := composePublicKeysAPILink() apiKeys := make([]*api.PublicKey, len(keys)) for i := range keys { - apiKeys[i] = to.ApiPublicKey(apiLink, keys[i]) + apiKeys[i] = convert.ToApiPublicKey(apiLink, keys[i]) } ctx.JSON(200, &apiKeys) @@ -75,7 +79,7 @@ func GetPublicKey(ctx *middleware.Context) { } apiLink := composePublicKeysAPILink() - ctx.JSON(200, to.ApiPublicKey(apiLink, key)) + ctx.JSON(200, convert.ToApiPublicKey(apiLink, key)) } // CreateUserPublicKey creates new public key to given user by ID. @@ -92,7 +96,7 @@ func CreateUserPublicKey(ctx *middleware.Context, form api.CreateKeyOption, uid return } apiLink := composePublicKeysAPILink() - ctx.JSON(201, to.ApiPublicKey(apiLink, key)) + ctx.JSON(201, convert.ToApiPublicKey(apiLink, key)) } // https://github.com/gogits/go-gogs-client/wiki/Users-Public-Keys#create-a-public-key diff --git a/routers/repo/pull.go b/routers/repo/pull.go index f543fc31c6..ee018ebda7 100644 --- a/routers/repo/pull.go +++ b/routers/repo/pull.go @@ -54,7 +54,7 @@ func getForkRepository(ctx *middleware.Context) *models.Repository { } ctx.Data["ForkFrom"] = forkRepo.Owner.Name + "/" + forkRepo.Name - if err := ctx.User.GetOrganizations(); err != nil { + if err := ctx.User.GetOrganizations(true); err != nil { ctx.Handle(500, "GetOrganizations", err) return nil } diff --git a/routers/user/home.go b/routers/user/home.go index 8b25d799f1..cd1d0fac5c 100644 --- a/routers/user/home.go +++ b/routers/user/home.go @@ -44,7 +44,7 @@ func getDashboardContextUser(ctx *middleware.Context) *models.User { } ctx.Data["ContextUser"] = ctxUser - if err := ctx.User.GetOrganizations(); err != nil { + if err := ctx.User.GetOrganizations(true); err != nil { ctx.Handle(500, "GetOrganizations", err) return nil } |