Skip to content

Commit

Permalink
[FEATURE] [API] Add Endpoint for Branch Creation
Browse files Browse the repository at this point in the history
Issue: go-gitea#11376

This commit introduces an API endpoint for branch creation.

The added route is POST /repos/{owner}/{repo}/branches.
A JSON with the name of the new branch and the name of the old branch is
required as parameters.

Signed-off-by: Terence Le Huu Phuong <[email protected]>
  • Loading branch information
tle-huu committed May 29, 2020
1 parent ab73b56 commit 2036f4b
Show file tree
Hide file tree
Showing 5 changed files with 172 additions and 0 deletions.
16 changes: 16 additions & 0 deletions modules/structs/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,22 @@ type EditRepoOption struct {
Archived *bool `json:"archived,omitempty"`
}

// CreateBranchRepoOption options when creating a branch in a repository
// swagger:model
type CreateBranchRepoOption struct {

// Name of the branch to create
//
// required: true
// unique: true
BranchName string `json:"new_branch_name" binding:"Required;GitRefName;MaxSize(100)"`

// Name of the old branch to create from
//
// unique: true
OldBranchName string `json:"old_branch_name" binding:"GitRefName;MaxSize(100)"`
}

// TransferRepoOption options when transfer a repository's ownership
// swagger:model
type TransferRepoOption struct {
Expand Down
1 change: 1 addition & 0 deletions routers/api/v1/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -665,6 +665,7 @@ func RegisterRoutes(m *macaron.Macaron) {
m.Get("", repo.ListBranches)
m.Get("/*", context.RepoRefByType(context.RepoRefBranch), repo.GetBranch)
m.Delete("/*", reqRepoWriter(models.UnitTypeCode), context.RepoRefByType(context.RepoRefBranch), repo.DeleteBranch)
m.Post("", reqRepoWriter(models.UnitTypeCode), bind(api.CreateBranchRepoOption{}), repo.CreateBranch)
}, reqRepoReader(models.UnitTypeCode))
m.Group("/branch_protections", func() {
m.Get("", repo.ListBranchProtections)
Expand Down
86 changes: 86 additions & 0 deletions routers/api/v1/repo/branch.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,92 @@ func DeleteBranch(ctx *context.APIContext) {
ctx.Status(http.StatusNoContent)
}

// CreateRepoBranch creates a branch for a user's repository
func CreateRepoBranch(ctx *context.APIContext, opt api.CreateBranchRepoOption) {

if len(opt.OldBranchName) == 0 {
opt.OldBranchName = ctx.Repo.Repository.DefaultBranch
}

err := repo_module.CreateNewBranch(ctx.User, ctx.Repo.Repository, opt.OldBranchName, opt.BranchName)

if err != nil {
if models.IsErrTagAlreadyExists(err) {
ctx.Error(http.StatusConflict, "", "The branch with the same tag already exists.")

} else if models.IsErrBranchAlreadyExists(err) || git.IsErrPushOutOfDate(err) {
ctx.Error(http.StatusConflict, "", "The branch already exists.")

} else if models.IsErrBranchNameConflict(err) {
ctx.Error(http.StatusConflict, "", "The branch with the same name already exists.")

} else {
ctx.Error(http.StatusInternalServerError, "CreateRepoBranch", err)

}
return
}

branch, err := repo_module.GetBranch(ctx.Repo.Repository, opt.BranchName)
if err != nil {
ctx.Error(http.StatusInternalServerError, "GetBranch", err)
return
}

commit, err := branch.GetCommit()
if err != nil {
ctx.Error(http.StatusInternalServerError, "GetCommit", err)
return
}

branchProtection, err := ctx.Repo.Repository.GetBranchProtection(branch.Name)
if err != nil {
ctx.Error(http.StatusInternalServerError, "GetBranchProtection", err)
return
}

br, err := convert.ToBranch(ctx.Repo.Repository, branch, commit, branchProtection, ctx.User, ctx.Repo.IsAdmin())
if err != nil {
ctx.Error(http.StatusInternalServerError, "convert.ToBranch", err)
return
}

ctx.JSON(http.StatusCreated, br)
}

// CreateBranch create a branch of a repository
func CreateBranch(ctx *context.APIContext, opt api.CreateBranchRepoOption) {
// swagger:operation POST /repos/{owner}/{repo}/branches repository repoCreateBranch
// ---
// summary: Create a branch
// consumes:
// - application/json
// produces:
// - application/json
// parameters:
// - name: owner
// in: path
// description: owner of the repo
// type: string
// required: true
// - name: repo
// in: path
// description: name of the repo
// type: string
// required: true
// - name: body
// in: body
// schema:
// "$ref": "#/definitions/CreateBranchRepoOption"
// responses:
// "201":
// "$ref": "#/responses/Branch"
// "409":
// description: The branch with the same name already exists.

CreateRepoBranch(ctx, opt)
}

// ListBranches list all the branches of a repository
func ListBranches(ctx *context.APIContext) {
// swagger:operation GET /repos/{owner}/{repo}/branches repository repoListBranches
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 @@ -129,6 +129,9 @@ type swaggerParameterBodies struct {
// in:body
EditReactionOption api.EditReactionOption

// in:body
CreateBranchRepoOption api.CreateBranchRepoOption

// in:body
CreateBranchProtectionOption api.CreateBranchProtectionOption

Expand Down
66 changes: 66 additions & 0 deletions templates/swagger/v1_json.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -2241,6 +2241,50 @@
"$ref": "#/responses/BranchList"
}
}
},
"post": {
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"repository"
],
"summary": "Create a branch",
"operationId": "repoCreateBranch",
"parameters": [
{
"type": "string",
"description": "owner of the repo",
"name": "owner",
"in": "path",
"required": true
},
{
"type": "string",
"description": "name of the repo",
"name": "repo",
"in": "path",
"required": true
},
{
"name": "body",
"in": "body",
"schema": {
"$ref": "#/definitions/CreateBranchRepoOption"
}
}
],
"responses": {
"201": {
"$ref": "#/responses/Branch"
},
"409": {
"description": "The branch with the same name already exists."
}
}
}
},
"/repos/{owner}/{repo}/branches/{branch}": {
Expand Down Expand Up @@ -10886,6 +10930,28 @@
},
"x-go-package": "code.gitea.io/gitea/modules/structs"
},
"CreateBranchRepoOption": {
"description": "CreateBranchRepoOption options when creating a branch in a repository",
"type": "object",
"required": [
"new_branch_name"
],
"properties": {
"new_branch_name": {
"description": "Name of the branch to create",
"type": "string",
"uniqueItems": true,
"x-go-name": "BranchName"
},
"old_branch_name": {
"description": "Name of the old branch to create from",
"type": "string",
"uniqueItems": true,
"x-go-name": "OldBranchName"
}
},
"x-go-package": "code.gitea.io/gitea/modules/structs"
},
"CreateEmailOption": {
"description": "CreateEmailOption options when creating email addresses",
"type": "object",
Expand Down

0 comments on commit 2036f4b

Please sign in to comment.