Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add api for user to create org #5268

Merged
merged 9 commits into from
Nov 20, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
add api for user to create org
  • Loading branch information
lunny committed Nov 11, 2018
commit e230039d6818a0b0385837c47ac7740a818812ae
1 change: 1 addition & 0 deletions routers/api/v1/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,7 @@ func RegisterRoutes(m *macaron.Macaron) {
// Organizations
m.Get("/user/orgs", reqToken(), org.ListMyOrgs)
m.Get("/users/:username/orgs", org.ListUserOrgs)
m.Post("/orgs", reqToken(), org.Create)
m.Group("/orgs/:orgname", func() {
m.Get("/repos", user.ListOrgRepos)
m.Combo("").Get(org.Get).
Expand Down
59 changes: 59 additions & 0 deletions routers/api/v1/org/org.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,65 @@ func ListUserOrgs(ctx *context.APIContext) {
listUserOrgs(ctx, u, false)
}

// Create api for create organization
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add Gitea copyright to header of this file?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ugh sorry @techknowlogick forgot to look for that!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@zeripath it's ok. It's something that is missed a lot, and we thankfully have git so if we do miss in one PR it we can just look at the history of the repo and add the appropriate dates when fixing.

func Create(ctx *context.APIContext, form api.CreateOrgOption) {
// swagger:operation POST /orgs organization orgCreate
// ---
// summary: Create an organization
// consumes:
// - application/json
// produces:
// - application/json
// parameters:
// - name: username
// in: path
// description: username of the user that will own the created organization
// type: string
// required: true
// - name: organization
// in: body
// required: true
// schema: { "$ref": "#/definitions/CreateOrgOption" }
// responses:
// "201":
// "$ref": "#/responses/Organization"
// "403":
// "$ref": "#/responses/forbidden"
// "422":
// "$ref": "#/responses/validationError"
u := user.GetUserByParams(ctx)
if ctx.Written() {
return
}

if !u.AllowCreateOrganization {
ctx.Error(403, "Create organization not allowed", nil)
return
}

org := &models.User{
Name: form.UserName,
FullName: form.FullName,
Description: form.Description,
Website: form.Website,
Location: form.Location,
IsActive: true,
Type: models.UserTypeOrganization,
}
if err := models.CreateOrganization(org, u); err != nil {
if models.IsErrUserAlreadyExist(err) ||
models.IsErrNameReserved(err) ||
models.IsErrNamePatternNotAllowed(err) {
ctx.Error(422, "", err)
} else {
ctx.Error(500, "CreateOrganization", err)
}
return
}

ctx.JSON(201, convert.ToOrganization(org))
}

// Get get an organization
func Get(ctx *context.APIContext) {
// swagger:operation GET /orgs/{org} organization orgGet
Expand Down
45 changes: 44 additions & 1 deletion templates/swagger/v1_json.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,49 @@
}
}
},
"/orgs": {
"post": {
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"organization"
],
"summary": "Create an organization",
"operationId": "orgCreate",
"parameters": [
{
"type": "string",
"description": "username of the user that will own the created organization",
"name": "username",
"in": "path",
"required": true
},
{
"name": "organization",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/CreateOrgOption"
}
}
],
"responses": {
"201": {
"$ref": "#/responses/Organization"
},
"403": {
"$ref": "#/responses/forbidden"
},
"422": {
"$ref": "#/responses/validationError"
}
}
}
},
"/orgs/{org}": {
"get": {
"produces": [
Expand Down Expand Up @@ -8359,4 +8402,4 @@
"SudoHeader": []
}
]
}
}