mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2025-02-22 19:35:47 -05:00
Inclusion of rename organization api (#33303)
This adds an endpoint (`/orgs/{org}/rename`) to rename organizations. I've modeled the endpoint using the rename user endpoint -- `/admin/users/{username}/rename` -- as base. It is the 1st time I wrote a new API endpoint (I've tried to follow the rename users endpoint code while writing it). So feel free to ping me if there is something wrong or missing. Resolves #32995 --------- Signed-off-by: Bruno Sofiato <bruno.sofiato@gmail.com> Co-authored-by: delvh <dev.lh@web.de> Co-authored-by: wxiaoguang <wxiaoguang@gmail.com> (cherry picked from commit 040c830dec5c727a56d16df62b1673bce6fca645) Conflicts: routers/api/v1/admin/user.go ignore this unrelated change templates/swagger/v1_json.tmpl regenerate tests/integration/api_org_test.go port as a standalone test instead of refactoring the tests
This commit is contained in:
parent
f44f3cbc2a
commit
689fb82a70
6 changed files with 130 additions and 0 deletions
|
@ -57,3 +57,12 @@ type EditOrgOption struct {
|
||||||
Visibility string `json:"visibility" binding:"In(,public,limited,private)"`
|
Visibility string `json:"visibility" binding:"In(,public,limited,private)"`
|
||||||
RepoAdminChangeTeamAccess *bool `json:"repo_admin_change_team_access"`
|
RepoAdminChangeTeamAccess *bool `json:"repo_admin_change_team_access"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// RenameOrgOption options when renaming an organization
|
||||||
|
type RenameOrgOption struct {
|
||||||
|
// New username for this org. This name cannot be in use yet by any other user.
|
||||||
|
//
|
||||||
|
// required: true
|
||||||
|
// unique: true
|
||||||
|
NewName string `json:"new_name" binding:"Required"`
|
||||||
|
}
|
||||||
|
|
|
@ -1505,6 +1505,7 @@ func Routes() *web.Route {
|
||||||
m.Combo("").Get(org.Get).
|
m.Combo("").Get(org.Get).
|
||||||
Patch(reqToken(), reqOrgOwnership(), bind(api.EditOrgOption{}), org.Edit).
|
Patch(reqToken(), reqOrgOwnership(), bind(api.EditOrgOption{}), org.Edit).
|
||||||
Delete(reqToken(), reqOrgOwnership(), org.Delete)
|
Delete(reqToken(), reqOrgOwnership(), org.Delete)
|
||||||
|
m.Post("/rename", reqToken(), reqOrgOwnership(), bind(api.RenameOrgOption{}), org.Rename)
|
||||||
m.Combo("/repos").Get(user.ListOrgRepos).
|
m.Combo("/repos").Get(user.ListOrgRepos).
|
||||||
Post(reqToken(), bind(api.CreateRepoOption{}), context.EnforceQuotaAPI(quota_model.LimitSubjectSizeReposAll, context.QuotaTargetOrg), repo.CreateOrgRepo)
|
Post(reqToken(), bind(api.CreateRepoOption{}), context.EnforceQuotaAPI(quota_model.LimitSubjectSizeReposAll, context.QuotaTargetOrg), repo.CreateOrgRepo)
|
||||||
m.Group("/members", func() {
|
m.Group("/members", func() {
|
||||||
|
|
|
@ -320,6 +320,44 @@ func Get(ctx *context.APIContext) {
|
||||||
ctx.JSON(http.StatusOK, org)
|
ctx.JSON(http.StatusOK, org)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func Rename(ctx *context.APIContext) {
|
||||||
|
// swagger:operation POST /orgs/{org}/rename organization renameOrg
|
||||||
|
// ---
|
||||||
|
// summary: Rename an organization
|
||||||
|
// produces:
|
||||||
|
// - application/json
|
||||||
|
// parameters:
|
||||||
|
// - name: org
|
||||||
|
// in: path
|
||||||
|
// description: existing org name
|
||||||
|
// type: string
|
||||||
|
// required: true
|
||||||
|
// - name: body
|
||||||
|
// in: body
|
||||||
|
// required: true
|
||||||
|
// schema:
|
||||||
|
// "$ref": "#/definitions/RenameOrgOption"
|
||||||
|
// responses:
|
||||||
|
// "204":
|
||||||
|
// "$ref": "#/responses/empty"
|
||||||
|
// "403":
|
||||||
|
// "$ref": "#/responses/forbidden"
|
||||||
|
// "422":
|
||||||
|
// "$ref": "#/responses/validationError"
|
||||||
|
|
||||||
|
form := web.GetForm(ctx).(*api.RenameOrgOption)
|
||||||
|
orgUser := ctx.Org.Organization.AsUser()
|
||||||
|
if err := user_service.RenameUser(ctx, orgUser, form.NewName); err != nil {
|
||||||
|
if user_model.IsErrUserAlreadyExist(err) || db.IsErrNameReserved(err) || db.IsErrNamePatternNotAllowed(err) || db.IsErrNameCharsNotAllowed(err) {
|
||||||
|
ctx.Error(http.StatusUnprocessableEntity, "RenameOrg", err)
|
||||||
|
} else {
|
||||||
|
ctx.ServerError("RenameOrg", err)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
ctx.Status(http.StatusNoContent)
|
||||||
|
}
|
||||||
|
|
||||||
// Edit change an organization's information
|
// Edit change an organization's information
|
||||||
func Edit(ctx *context.APIContext) {
|
func Edit(ctx *context.APIContext) {
|
||||||
// swagger:operation PATCH /orgs/{org} organization orgEdit
|
// swagger:operation PATCH /orgs/{org} organization orgEdit
|
||||||
|
|
|
@ -216,6 +216,9 @@ type swaggerParameterBodies struct {
|
||||||
// in:body
|
// in:body
|
||||||
CreateVariableOption api.CreateVariableOption
|
CreateVariableOption api.CreateVariableOption
|
||||||
|
|
||||||
|
// in:body
|
||||||
|
RenameOrgOption api.RenameOrgOption
|
||||||
|
|
||||||
// in:body
|
// in:body
|
||||||
UpdateVariableOption api.UpdateVariableOption
|
UpdateVariableOption api.UpdateVariableOption
|
||||||
|
|
||||||
|
|
56
templates/swagger/v1_json.tmpl
generated
56
templates/swagger/v1_json.tmpl
generated
|
@ -3772,6 +3772,46 @@
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"/orgs/{org}/rename": {
|
||||||
|
"post": {
|
||||||
|
"produces": [
|
||||||
|
"application/json"
|
||||||
|
],
|
||||||
|
"tags": [
|
||||||
|
"organization"
|
||||||
|
],
|
||||||
|
"summary": "Rename an organization",
|
||||||
|
"operationId": "renameOrg",
|
||||||
|
"parameters": [
|
||||||
|
{
|
||||||
|
"type": "string",
|
||||||
|
"description": "existing org name",
|
||||||
|
"name": "org",
|
||||||
|
"in": "path",
|
||||||
|
"required": true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "body",
|
||||||
|
"in": "body",
|
||||||
|
"required": true,
|
||||||
|
"schema": {
|
||||||
|
"$ref": "#/definitions/RenameOrgOption"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"responses": {
|
||||||
|
"204": {
|
||||||
|
"$ref": "#/responses/empty"
|
||||||
|
},
|
||||||
|
"403": {
|
||||||
|
"$ref": "#/responses/forbidden"
|
||||||
|
},
|
||||||
|
"422": {
|
||||||
|
"$ref": "#/responses/validationError"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
"/orgs/{org}/repos": {
|
"/orgs/{org}/repos": {
|
||||||
"get": {
|
"get": {
|
||||||
"produces": [
|
"produces": [
|
||||||
|
@ -26634,6 +26674,22 @@
|
||||||
},
|
},
|
||||||
"x-go-package": "code.gitea.io/gitea/modules/structs"
|
"x-go-package": "code.gitea.io/gitea/modules/structs"
|
||||||
},
|
},
|
||||||
|
"RenameOrgOption": {
|
||||||
|
"description": "RenameOrgOption options when renaming an organization",
|
||||||
|
"type": "object",
|
||||||
|
"required": [
|
||||||
|
"new_name"
|
||||||
|
],
|
||||||
|
"properties": {
|
||||||
|
"new_name": {
|
||||||
|
"description": "New username for this org. This name cannot be in use yet by any other user.",
|
||||||
|
"type": "string",
|
||||||
|
"uniqueItems": true,
|
||||||
|
"x-go-name": "NewName"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"x-go-package": "code.gitea.io/gitea/modules/structs"
|
||||||
|
},
|
||||||
"RenameUserOption": {
|
"RenameUserOption": {
|
||||||
"description": "RenameUserOption options when renaming a user",
|
"description": "RenameUserOption options when renaming a user",
|
||||||
"type": "object",
|
"type": "object",
|
||||||
|
|
|
@ -99,6 +99,29 @@ func TestAPIOrgCreate(t *testing.T) {
|
||||||
assert.EqualValues(t, "user1", users[0].UserName)
|
assert.EqualValues(t, "user1", users[0].UserName)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestAPIOrgRename(t *testing.T) {
|
||||||
|
defer tests.PrepareTestEnv(t)()
|
||||||
|
token := getUserToken(t, "user1", auth_model.AccessTokenScopeWriteOrganization)
|
||||||
|
|
||||||
|
org := api.CreateOrgOption{
|
||||||
|
UserName: "user1_org",
|
||||||
|
FullName: "User1's organization",
|
||||||
|
Description: "This organization created by user1",
|
||||||
|
Website: "https://try.gitea.io",
|
||||||
|
Location: "Shanghai",
|
||||||
|
Visibility: "limited",
|
||||||
|
}
|
||||||
|
req := NewRequestWithJSON(t, "POST", "/api/v1/orgs", &org).
|
||||||
|
AddTokenAuth(token)
|
||||||
|
MakeRequest(t, req, http.StatusCreated)
|
||||||
|
|
||||||
|
req = NewRequestWithJSON(t, "POST", "/api/v1/orgs/user1_org/rename", &api.RenameOrgOption{
|
||||||
|
NewName: "renamed_org",
|
||||||
|
}).AddTokenAuth(token)
|
||||||
|
MakeRequest(t, req, http.StatusNoContent)
|
||||||
|
unittest.AssertExistsAndLoadBean(t, &org_model.Organization{Name: "renamed_org"})
|
||||||
|
}
|
||||||
|
|
||||||
func TestAPIOrgEdit(t *testing.T) {
|
func TestAPIOrgEdit(t *testing.T) {
|
||||||
defer tests.PrepareTestEnv(t)()
|
defer tests.PrepareTestEnv(t)()
|
||||||
session := loginUser(t, "user1")
|
session := loginUser(t, "user1")
|
||||||
|
|
Loading…
Add table
Reference in a new issue