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

Update branch API endpoint to show effective branch protection. #9031

Merged
5 changes: 5 additions & 0 deletions models/branches.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,11 @@ func (repo *Repository) GetProtectedBranches() ([]*ProtectedBranch, error) {
return protectedBranches, x.Find(&protectedBranches, &ProtectedBranch{RepoID: repo.ID})
}

// GetBranchProtection get the branch protection of a branch
func (repo *Repository) GetBranchProtection(branchName string) (*ProtectedBranch, error) {
return GetProtectedBranchBy(repo.ID, branchName)
}

// IsProtectedBranch checks if branch is protected
func (repo *Repository) IsProtectedBranch(branchName string, doer *User) (bool, error) {
if doer == nil {
Expand Down
20 changes: 17 additions & 3 deletions modules/convert/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,24 @@ func ToEmail(email *models.EmailAddress) *api.Email {
}

// ToBranch convert a git.Commit and git.Branch to an api.Branch
func ToBranch(repo *models.Repository, b *git.Branch, c *git.Commit) *api.Branch {
func ToBranch(repo *models.Repository, b *git.Branch, c *git.Commit, bp *models.ProtectedBranch, user *models.User) *api.Branch {
if bp == nil {
return &api.Branch{
Name: b.Name,
Commit: ToCommit(repo, c),
Protected: false,
RequiredApprovals: 0,
UserCanPush: true,
UserCanMerge: true,
}
}
return &api.Branch{
Name: b.Name,
Commit: ToCommit(repo, c),
Name: b.Name,
Commit: ToCommit(repo, c),
Protected: true,
RequiredApprovals: bp.RequiredApprovals,
UserCanPush: bp.CanUserPush(user.ID),
UserCanMerge: bp.CanUserMerge(user.ID),
}
}

Expand Down
8 changes: 6 additions & 2 deletions modules/structs/repo_branch.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ package structs

// Branch represents a repository branch
type Branch struct {
Name string `json:"name"`
Commit *PayloadCommit `json:"commit"`
Name string `json:"name"`
Commit *PayloadCommit `json:"commit"`
Protected bool `json:"protected"`
RequiredApprovals int64 `json:"required_approvals"`
UserCanPush bool `json:"user_can_push"`
UserCanMerge bool `json:"user_can_merge"`
}
17 changes: 14 additions & 3 deletions routers/api/v1/repo/branch.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
func GetBranch(ctx *context.APIContext) {
// swagger:operation GET /repos/{owner}/{repo}/branches/{branch} repository repoGetBranch
// ---
// summary: Retrieve a specific branch from a repository
// summary: Retrieve a specific branch from a repository, including its effective branch protection
// produces:
// - application/json
// parameters:
Expand Down Expand Up @@ -61,7 +61,13 @@ func GetBranch(ctx *context.APIContext) {
return
}

ctx.JSON(200, convert.ToBranch(ctx.Repo.Repository, branch, c))
branchProtection, err := ctx.Repo.Repository.GetBranchProtection(ctx.Repo.BranchName)
if err != nil {
ctx.Error(500, "GetBranchProtection", err)
return
}

ctx.JSON(200, convert.ToBranch(ctx.Repo.Repository, branch, c, branchProtection, ctx.User))
}

// ListBranches list all the branches of a repository
Expand Down Expand Up @@ -98,7 +104,12 @@ func ListBranches(ctx *context.APIContext) {
ctx.Error(500, "GetCommit", err)
return
}
apiBranches[i] = convert.ToBranch(ctx.Repo.Repository, branches[i], c)
branchProtection, err := ctx.Repo.Repository.GetBranchProtection(branches[i].Name)
if err != nil {
ctx.Error(500, "GetBranchProtection", err)
return
}
apiBranches[i] = convert.ToBranch(ctx.Repo.Repository, branches[i], c, branchProtection, ctx.User)
}

ctx.JSON(200, &apiBranches)
Expand Down
19 changes: 18 additions & 1 deletion templates/swagger/v1_json.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -1492,7 +1492,7 @@
"tags": [
"repository"
],
"summary": "Retrieve a specific branch from a repository",
"summary": "Retrieve a specific branch from a repository, including its effective branch protection",
"operationId": "repoGetBranch",
"parameters": [
{
Expand Down Expand Up @@ -7586,6 +7586,23 @@
"name": {
"type": "string",
"x-go-name": "Name"
},
"protected": {
"type": "boolean",
"x-go-name": "Protected"
},
"required_approvals": {
"type": "integer",
"format": "int64",
"x-go-name": "RequiredApprovals"
},
"user_can_merge": {
"type": "boolean",
"x-go-name": "UserCanMerge"
},
"user_can_push": {
"type": "boolean",
"x-go-name": "UserCanPush"
}
},
"x-go-package": "code.gitea.io/gitea/modules/structs"
Expand Down