Skip to content

Commit 6a7d9d4

Browse files
Add API endpoint for displaying effective branch protection.
1 parent c58fba9 commit 6a7d9d4

File tree

5 files changed

+60
-9
lines changed

5 files changed

+60
-9
lines changed

models/branches.go

+5
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,11 @@ func (repo *Repository) GetProtectedBranches() ([]*ProtectedBranch, error) {
242242
return protectedBranches, x.Find(&protectedBranches, &ProtectedBranch{RepoID: repo.ID})
243243
}
244244

245+
// GetBranchProtection get the branch protection of a branch
246+
func (repo *Repository) GetBranchProtection(branchName string) (*ProtectedBranch, error) {
247+
return GetProtectedBranchBy(repo.ID, branchName)
248+
}
249+
245250
// IsProtectedBranch checks if branch is protected
246251
func (repo *Repository) IsProtectedBranch(branchName string, doer *User) (bool, error) {
247252
if doer == nil {

modules/convert/convert.go

+17-3
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,24 @@ func ToEmail(email *models.EmailAddress) *api.Email {
3030
}
3131

3232
// ToBranch convert a git.Commit and git.Branch to an api.Branch
33-
func ToBranch(repo *models.Repository, b *git.Branch, c *git.Commit) *api.Branch {
33+
func ToBranch(repo *models.Repository, b *git.Branch, c *git.Commit, bp *models.ProtectedBranch, user *models.User) *api.Branch {
34+
if bp == nil {
35+
return &api.Branch{
36+
Name: b.Name,
37+
Commit: ToCommit(repo, c),
38+
Protected: false,
39+
RequiredApprovals: 0,
40+
UserCanPush: true,
41+
UserCanMerge: true,
42+
}
43+
}
3444
return &api.Branch{
35-
Name: b.Name,
36-
Commit: ToCommit(repo, c),
45+
Name: b.Name,
46+
Commit: ToCommit(repo, c),
47+
Protected: true,
48+
RequiredApprovals: bp.RequiredApprovals,
49+
UserCanPush: bp.CanUserPush(user.ID),
50+
UserCanMerge: bp.CanUserMerge(user.ID),
3751
}
3852
}
3953

modules/structs/repo_branch.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ package structs
66

77
// Branch represents a repository branch
88
type Branch struct {
9-
Name string `json:"name"`
10-
Commit *PayloadCommit `json:"commit"`
9+
Name string `json:"name"`
10+
Commit *PayloadCommit `json:"commit"`
11+
Protected bool `json:"protected"`
12+
RequiredApprovals int64 `json:"required_approvals"`
13+
UserCanPush bool `json:"user_can_push"`
14+
UserCanMerge bool `json:"user_can_merge"`
1115
}

routers/api/v1/repo/branch.go

+14-3
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import (
1616
func GetBranch(ctx *context.APIContext) {
1717
// swagger:operation GET /repos/{owner}/{repo}/branches/{branch} repository repoGetBranch
1818
// ---
19-
// summary: Retrieve a specific branch from a repository
19+
// summary: Retrieve a specific branch from a repository, including its effective branch protection
2020
// produces:
2121
// - application/json
2222
// parameters:
@@ -61,7 +61,13 @@ func GetBranch(ctx *context.APIContext) {
6161
return
6262
}
6363

64-
ctx.JSON(200, convert.ToBranch(ctx.Repo.Repository, branch, c))
64+
branchProtection, err := ctx.Repo.Repository.GetBranchProtection(ctx.Repo.BranchName)
65+
if err != nil {
66+
ctx.Error(500, "GetBranchProtection", err)
67+
return
68+
}
69+
70+
ctx.JSON(200, convert.ToBranch(ctx.Repo.Repository, branch, c, branchProtection, ctx.User))
6571
}
6672

6773
// ListBranches list all the branches of a repository
@@ -98,7 +104,12 @@ func ListBranches(ctx *context.APIContext) {
98104
ctx.Error(500, "GetCommit", err)
99105
return
100106
}
101-
apiBranches[i] = convert.ToBranch(ctx.Repo.Repository, branches[i], c)
107+
branchProtection, err := ctx.Repo.Repository.GetBranchProtection(branches[i].Name)
108+
if err != nil {
109+
ctx.Error(500, "GetBranchProtection", err)
110+
return
111+
}
112+
apiBranches[i] = convert.ToBranch(ctx.Repo.Repository, branches[i], c, branchProtection, ctx.User)
102113
}
103114

104115
ctx.JSON(200, &apiBranches)

templates/swagger/v1_json.tmpl

+18-1
Original file line numberDiff line numberDiff line change
@@ -1492,7 +1492,7 @@
14921492
"tags": [
14931493
"repository"
14941494
],
1495-
"summary": "Retrieve a specific branch from a repository",
1495+
"summary": "Retrieve a specific branch from a repository, including its effective branch protection",
14961496
"operationId": "repoGetBranch",
14971497
"parameters": [
14981498
{
@@ -7586,6 +7586,23 @@
75867586
"name": {
75877587
"type": "string",
75887588
"x-go-name": "Name"
7589+
},
7590+
"protected": {
7591+
"type": "boolean",
7592+
"x-go-name": "Protected"
7593+
},
7594+
"required_approvals": {
7595+
"type": "integer",
7596+
"format": "int64",
7597+
"x-go-name": "RequiredApprovals"
7598+
},
7599+
"user_can_merge": {
7600+
"type": "boolean",
7601+
"x-go-name": "UserCanMerge"
7602+
},
7603+
"user_can_push": {
7604+
"type": "boolean",
7605+
"x-go-name": "UserCanPush"
75897606
}
75907607
},
75917608
"x-go-package": "code.gitea.io/gitea/modules/structs"

0 commit comments

Comments
 (0)