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

Inclusion of rename organization api #33303

Merged
merged 7 commits into from
Feb 1, 2025
Merged
Show file tree
Hide file tree
Changes from 3 commits
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
9 changes: 9 additions & 0 deletions modules/structs/org.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,12 @@ type EditOrgOption struct {
Visibility string `json:"visibility" binding:"In(,public,limited,private)"`
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"`
}
1 change: 1 addition & 0 deletions routers/api/v1/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -1530,6 +1530,7 @@ func Routes() *web.Router {
m.Combo("").Get(org.Get).
Patch(reqToken(), reqOrgOwnership(), bind(api.EditOrgOption{}), org.Edit).
Delete(reqToken(), reqOrgOwnership(), org.Delete)
m.Post("/rename", reqToken(), reqOrgOwnership(), bind(api.RenameOrgOption{}), org.Rename)
m.Combo("/repos").Get(user.ListOrgRepos).
Post(reqToken(), bind(api.CreateRepoOption{}), repo.CreateOrgRepo)
m.Group("/members", func() {
Expand Down
47 changes: 47 additions & 0 deletions routers/api/v1/org/org.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"code.gitea.io/gitea/models/organization"
"code.gitea.io/gitea/models/perm"
user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/optional"
api "code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/modules/web"
Expand Down Expand Up @@ -315,6 +316,52 @@ func Get(ctx *context.APIContext) {
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"

org := ctx.Org.Organization
form := web.GetForm(ctx).(*api.RenameOrgOption)

oldName := org.AsUser().Name

if err := user_service.RenameUser(ctx, org.AsUser(), form.NewName); err != nil {
if user_model.IsErrUserAlreadyExist(err) {
ctx.Error(http.StatusUnprocessableEntity, "RenameOrg", ctx.Tr("form.username_been_taken"))
wxiaoguang marked this conversation as resolved.
Show resolved Hide resolved
} else if db.IsErrNameReserved(err) {
ctx.Error(http.StatusUnprocessableEntity, "RenameOrg", ctx.Tr("repo.form.name_reserved", err.(db.ErrNameReserved).Name))
} else if db.IsErrNamePatternNotAllowed(err) {
ctx.Error(http.StatusUnprocessableEntity, "RenameOrg", ctx.Tr("repo.form.name_pattern_not_allowed", err.(db.ErrNamePatternNotAllowed).Pattern))
} else {
ctx.ServerError("RenameOrg", err)
}
} else {
log.Info("Org name changed: %s -> %s", oldName, form.NewName)
ctx.Status(http.StatusNoContent)
}
}
lunny marked this conversation as resolved.
Show resolved Hide resolved

// Edit change an organization's information
func Edit(ctx *context.APIContext) {
// swagger:operation PATCH /orgs/{org} organization orgEdit
Expand Down
3 changes: 3 additions & 0 deletions routers/api/v1/swagger/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,9 @@ type swaggerParameterBodies struct {
// in:body
CreateVariableOption api.CreateVariableOption

// in:body
RenameOrgOption api.RenameOrgOption

// in:body
UpdateVariableOption api.UpdateVariableOption
}
56 changes: 56 additions & 0 deletions templates/swagger/v1_json.tmpl

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions tests/integration/api_org_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,3 +226,26 @@ func TestAPIOrgSearchEmptyTeam(t *testing.T) {
}
})
}

func TestAPIOrgRename(t *testing.T) {
onGiteaRun(t, func(*testing.T, *url.URL) {
wxiaoguang marked this conversation as resolved.
Show resolved Hide resolved
token := getUserToken(t, "user1", auth_model.AccessTokenScopeWriteOrganization)
orgName := "org_to_rename"
newOrgName := "renamed_org"

// create org
req := NewRequestWithJSON(t, "POST", "/api/v1/orgs", &api.CreateOrgOption{
UserName: orgName,
}).AddTokenAuth(token)
MakeRequest(t, req, http.StatusCreated)

req = NewRequestWithJSON(t, "POST", "/api/v1/orgs/org_to_rename/rename", &api.RenameOrgOption{
NewName: newOrgName,
}).AddTokenAuth(token)
MakeRequest(t, req, http.StatusNoContent)

unittest.AssertExistsAndLoadBean(t, &org_model.Organization{
Name: newOrgName,
})
})
}