Skip to content

Commit b14c5e7

Browse files
committed
Inclusion of rename organization api
Signed-off-by: Bruno Sofiato <[email protected]>
1 parent ab347fd commit b14c5e7

File tree

4 files changed

+107
-0
lines changed

4 files changed

+107
-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
@@ -1530,6 +1530,7 @@ func Routes() *web.Router {
15301530
m.Combo("").Get(org.Get).
15311531
Patch(reqToken(), reqOrgOwnership(), bind(api.EditOrgOption{}), org.Edit).
15321532
Delete(reqToken(), reqOrgOwnership(), org.Delete)
1533+
m.Post("/rename", reqToken(), reqOrgOwnership(), bind(api.RenameOrgOption{}), org.Rename)
15331534
m.Combo("/repos").Get(user.ListOrgRepos).
15341535
Post(reqToken(), bind(api.CreateRepoOption{}), repo.CreateOrgRepo)
15351536
m.Group("/members", func() {

routers/api/v1/org/org.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,47 @@ func Get(ctx *context.APIContext) {
315315
ctx.JSON(http.StatusOK, org)
316316
}
317317

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

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.

0 commit comments

Comments
 (0)