Browse Source

api: add endpoint /api/v1/user/orgs (#4835)

* Add API endpoint /api/v1/user/orgs

The difference between this endpoint and
/api/v1/admin/users/<username>/orgs,
is that here you're creating a repo with the `user`
that corresponds to the API token you're using.

* Extract duplicate API org creation
pull/4762/merge
Anton Antonov 7 years ago committed by 无闻
parent
commit
77c77fbd42
  1. 30
      routes/api/v1/admin/org.go
  2. 3
      routes/api/v1/api.go
  3. 33
      routes/api/v1/org/org.go

30
routes/api/v1/admin/org.go

@ -7,38 +7,12 @@ package admin
import (
api "github.com/gogits/go-gogs-client"
"github.com/gogits/gogs/models"
"github.com/gogits/gogs/pkg/context"
"github.com/gogits/gogs/routes/api/v1/convert"
"github.com/gogits/gogs/routes/api/v1/user"
"github.com/gogits/gogs/routes/api/v1/org"
)
// https://github.com/gogits/go-gogs-client/wiki/Administration-Organizations#create-a-new-organization
func CreateOrg(c *context.APIContext, form api.CreateOrgOption) {
u := user.GetUserByParams(c)
if c.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) {
c.Error(422, "", err)
} else {
c.Error(500, "CreateOrganization", err)
}
return
}
c.JSON(201, convert.ToOrganization(org))
org.CreateOrgForUser(c, form, user.GetUserByParams(c))
}

3
routes/api/v1/api.go

@ -316,7 +316,8 @@ func RegisterRoutes(m *macaron.Macaron) {
m.Get("/issues", reqToken(), repo.ListUserIssues)
// Organizations
m.Get("/user/orgs", reqToken(), org.ListMyOrgs)
m.Combo("/user/orgs", reqToken()).Get(org.ListMyOrgs).Post(bind(api.CreateOrgOption{}), org.CreateMyOrg)
m.Get("/users/:username/orgs", org.ListUserOrgs)
m.Group("/orgs/:orgname", func() {
m.Combo("").Get(org.Get).Patch(bind(api.EditOrgOption{}), org.Edit)

33
routes/api/v1/org/org.go

@ -13,6 +13,34 @@ import (
"github.com/gogits/gogs/routes/api/v1/user"
)
func CreateOrgForUser(c *context.APIContext, apiForm api.CreateOrgOption, user *models.User) {
if c.Written() {
return
}
org := &models.User{
Name: apiForm.UserName,
FullName: apiForm.FullName,
Description: apiForm.Description,
Website: apiForm.Website,
Location: apiForm.Location,
IsActive: true,
Type: models.USER_TYPE_ORGANIZATION,
}
if err := models.CreateOrganization(org, user); err != nil {
if models.IsErrUserAlreadyExist(err) ||
models.IsErrNameReserved(err) ||
models.IsErrNamePatternNotAllowed(err) {
c.Error(422, "", err)
} else {
c.Error(500, "CreateOrganization", err)
}
return
}
c.JSON(201, convert.ToOrganization(org))
}
func listUserOrgs(c *context.APIContext, u *models.User, all bool) {
if err := u.GetOrganizations(all); err != nil {
c.Error(500, "GetOrganizations", err)
@ -31,6 +59,11 @@ func ListMyOrgs(c *context.APIContext) {
listUserOrgs(c, c.User, true)
}
// https://github.com/gogits/go-gogs-client/wiki/Organizations#create-your-organization
func CreateMyOrg(c *context.APIContext, apiForm api.CreateOrgOption) {
CreateOrgForUser(c, apiForm, c.User)
}
// https://github.com/gogits/go-gogs-client/wiki/Organizations#list-user-organizations
func ListUserOrgs(c *context.APIContext) {
u := user.GetUserByParams(c)

Loading…
Cancel
Save