Skip to content

Commit 689fb82

Browse files
bsofiatoearl-warren
authored andcommitted
Inclusion of rename organization api (go-gitea#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 go-gitea#32995 --------- Signed-off-by: Bruno Sofiato <[email protected]> Co-authored-by: delvh <[email protected]> Co-authored-by: wxiaoguang <[email protected]> (cherry picked from commit 040c830) 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
1 parent f44f3cb commit 689fb82

File tree

6 files changed

+130
-0
lines changed

6 files changed

+130
-0
lines changed

modules/structs/org.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,12 @@ type EditOrgOption struct {
5757
Visibility string `json:"visibility" binding:"In(,public,limited,private)"`
5858
RepoAdminChangeTeamAccess *bool `json:"repo_admin_change_team_access"`
5959
}
60+
61+
// RenameOrgOption options when renaming an organization
62+
type RenameOrgOption struct {
63+
// New username for this org. This name cannot be in use yet by any other user.
64+
//
65+
// required: true
66+
// unique: true
67+
NewName string `json:"new_name" binding:"Required"`
68+
}

routers/api/v1/api.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1505,6 +1505,7 @@ func Routes() *web.Route {
15051505
m.Combo("").Get(org.Get).
15061506
Patch(reqToken(), reqOrgOwnership(), bind(api.EditOrgOption{}), org.Edit).
15071507
Delete(reqToken(), reqOrgOwnership(), org.Delete)
1508+
m.Post("/rename", reqToken(), reqOrgOwnership(), bind(api.RenameOrgOption{}), org.Rename)
15081509
m.Combo("/repos").Get(user.ListOrgRepos).
15091510
Post(reqToken(), bind(api.CreateRepoOption{}), context.EnforceQuotaAPI(quota_model.LimitSubjectSizeReposAll, context.QuotaTargetOrg), repo.CreateOrgRepo)
15101511
m.Group("/members", func() {

routers/api/v1/org/org.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,44 @@ func Get(ctx *context.APIContext) {
320320
ctx.JSON(http.StatusOK, org)
321321
}
322322

323+
func Rename(ctx *context.APIContext) {
324+
// swagger:operation POST /orgs/{org}/rename organization renameOrg
325+
// ---
326+
// summary: Rename an organization
327+
// produces:
328+
// - application/json
329+
// parameters:
330+
// - name: org
331+
// in: path
332+
// description: existing org name
333+
// type: string
334+
// required: true
335+
// - name: body
336+
// in: body
337+
// required: true
338+
// schema:
339+
// "$ref": "#/definitions/RenameOrgOption"
340+
// responses:
341+
// "204":
342+
// "$ref": "#/responses/empty"
343+
// "403":
344+
// "$ref": "#/responses/forbidden"
345+
// "422":
346+
// "$ref": "#/responses/validationError"
347+
348+
form := web.GetForm(ctx).(*api.RenameOrgOption)
349+
orgUser := ctx.Org.Organization.AsUser()
350+
if err := user_service.RenameUser(ctx, orgUser, form.NewName); err != nil {
351+
if user_model.IsErrUserAlreadyExist(err) || db.IsErrNameReserved(err) || db.IsErrNamePatternNotAllowed(err) || db.IsErrNameCharsNotAllowed(err) {
352+
ctx.Error(http.StatusUnprocessableEntity, "RenameOrg", err)
353+
} else {
354+
ctx.ServerError("RenameOrg", err)
355+
}
356+
return
357+
}
358+
ctx.Status(http.StatusNoContent)
359+
}
360+
323361
// Edit change an organization's information
324362
func Edit(ctx *context.APIContext) {
325363
// swagger:operation PATCH /orgs/{org} organization orgEdit

routers/api/v1/swagger/options.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,9 @@ type swaggerParameterBodies struct {
216216
// in:body
217217
CreateVariableOption api.CreateVariableOption
218218

219+
// in:body
220+
RenameOrgOption api.RenameOrgOption
221+
219222
// in:body
220223
UpdateVariableOption api.UpdateVariableOption
221224

templates/swagger/v1_json.tmpl

Lines changed: 56 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/integration/api_org_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,29 @@ func TestAPIOrgCreate(t *testing.T) {
9999
assert.EqualValues(t, "user1", users[0].UserName)
100100
}
101101

102+
func TestAPIOrgRename(t *testing.T) {
103+
defer tests.PrepareTestEnv(t)()
104+
token := getUserToken(t, "user1", auth_model.AccessTokenScopeWriteOrganization)
105+
106+
org := api.CreateOrgOption{
107+
UserName: "user1_org",
108+
FullName: "User1's organization",
109+
Description: "This organization created by user1",
110+
Website: "https://try.gitea.io",
111+
Location: "Shanghai",
112+
Visibility: "limited",
113+
}
114+
req := NewRequestWithJSON(t, "POST", "/api/v1/orgs", &org).
115+
AddTokenAuth(token)
116+
MakeRequest(t, req, http.StatusCreated)
117+
118+
req = NewRequestWithJSON(t, "POST", "/api/v1/orgs/user1_org/rename", &api.RenameOrgOption{
119+
NewName: "renamed_org",
120+
}).AddTokenAuth(token)
121+
MakeRequest(t, req, http.StatusNoContent)
122+
unittest.AssertExistsAndLoadBean(t, &org_model.Organization{Name: "renamed_org"})
123+
}
124+
102125
func TestAPIOrgEdit(t *testing.T) {
103126
defer tests.PrepareTestEnv(t)()
104127
session := loginUser(t, "user1")

0 commit comments

Comments
 (0)