Skip to content

Commit c7151c2

Browse files
realaravinthwxiaoguangzeripath
authored
- name: new parameter in CreateForkOption to give the forked repository (#18066)
a custom name, intended to be used when there's a name conflict - When a fork request results in a name conflict, HTTP 409: Conflict is returned instead of 500 - API documentation for the above mentioned changes Signed-off-by: realaravinth <[email protected]> Co-authored-by: wxiaoguang <[email protected]> Co-authored-by: zeripath <[email protected]>
1 parent 532383d commit c7151c2

File tree

3 files changed

+25
-2
lines changed

3 files changed

+25
-2
lines changed

modules/structs/fork.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,6 @@ package structs
88
type CreateForkOption struct {
99
// organization name, if forking into an organization
1010
Organization *string `json:"organization"`
11+
// name of the forked repository
12+
Name *string `json:"name"`
1113
}

routers/api/v1/repo/fork.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@ func CreateFork(ctx *context.APIContext) {
9797
// "$ref": "#/responses/Repository"
9898
// "403":
9999
// "$ref": "#/responses/forbidden"
100+
// "409":
101+
// description: The repository with the same name already exists.
100102
// "422":
101103
// "$ref": "#/responses/validationError"
102104

@@ -126,13 +128,24 @@ func CreateFork(ctx *context.APIContext) {
126128
forker = org.AsUser()
127129
}
128130

131+
var name string
132+
if form.Name == nil {
133+
name = repo.Name
134+
} else {
135+
name = *form.Name
136+
}
137+
129138
fork, err := repo_service.ForkRepository(ctx.User, forker, repo_service.ForkRepoOptions{
130139
BaseRepo: repo,
131-
Name: repo.Name,
140+
Name: name,
132141
Description: repo.Description,
133142
})
134143
if err != nil {
135-
ctx.Error(http.StatusInternalServerError, "ForkRepository", err)
144+
if repo_model.IsErrRepoAlreadyExist(err) {
145+
ctx.Error(http.StatusConflict, "ForkRepository", err)
146+
} else {
147+
ctx.Error(http.StatusInternalServerError, "ForkRepository", err)
148+
}
136149
return
137150
}
138151

templates/swagger/v1_json.tmpl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3509,6 +3509,9 @@
35093509
"403": {
35103510
"$ref": "#/responses/forbidden"
35113511
},
3512+
"409": {
3513+
"description": "The repository with the same name already exists."
3514+
},
35123515
"422": {
35133516
"$ref": "#/responses/validationError"
35143517
}
@@ -13376,6 +13379,11 @@
1337613379
"description": "CreateForkOption options for creating a fork",
1337713380
"type": "object",
1337813381
"properties": {
13382+
"name": {
13383+
"description": "name of the forked repository",
13384+
"type": "string",
13385+
"x-go-name": "Name"
13386+
},
1337913387
"organization": {
1338013388
"description": "organization name, if forking into an organization",
1338113389
"type": "string",

0 commit comments

Comments
 (0)