Skip to content

Commit 9e4a1fd

Browse files
authored
Add fields to update repository API and create branch protection API to match GitHub APIs (#2709)
1 parent 9bfbc00 commit 9e4a1fd

File tree

6 files changed

+176
-2
lines changed

6 files changed

+176
-2
lines changed

github/github-accessors.go

Lines changed: 48 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

github/github-accessors_test.go

Lines changed: 57 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

github/github-stringify_test.go

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

github/repos.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ type Repository struct {
6868
AllowMergeCommit *bool `json:"allow_merge_commit,omitempty"`
6969
AllowAutoMerge *bool `json:"allow_auto_merge,omitempty"`
7070
AllowForking *bool `json:"allow_forking,omitempty"`
71+
WebCommitSignoffRequired *bool `json:"web_commit_signoff_required,omitempty"`
7172
DeleteBranchOnMerge *bool `json:"delete_branch_on_merge,omitempty"`
7273
UseSquashPRTitleAsDefault *bool `json:"use_squash_pr_title_as_default,omitempty"`
7374
SquashMergeCommitTitle *string `json:"squash_merge_commit_title,omitempty"` // Can be one of: "PR_TITLE", "COMMIT_OR_PR_TITLE"
@@ -843,10 +844,18 @@ type Protection struct {
843844
AllowForcePushes *AllowForcePushes `json:"allow_force_pushes"`
844845
AllowDeletions *AllowDeletions `json:"allow_deletions"`
845846
RequiredConversationResolution *RequiredConversationResolution `json:"required_conversation_resolution"`
847+
BlockCreations *BlockCreations `json:"block_creations,omitempty"`
846848
LockBranch *LockBranch `json:"lock_branch,omitempty"`
847849
AllowForkSyncing *AllowForkSyncing `json:"allow_fork_syncing,omitempty"`
848850
}
849851

852+
// BlockCreations represents whether users can push changes that create branches. If this is true, this
853+
// setting blocks pushes that create new branches, unless the push is initiated by a user, team, or app
854+
// which has the ability to push.
855+
type BlockCreations struct {
856+
Enabled *bool `json:"enabled,omitempty"`
857+
}
858+
850859
// LockBranch represents if the branch is marked as read-only. If this is true, users will not be able to push to the branch.
851860
type LockBranch struct {
852861
Enabled *bool `json:"enabled,omitempty"`
@@ -994,6 +1003,14 @@ type ProtectionRequest struct {
9941003
// RequiredConversationResolution, if set to true, requires all comments
9951004
// on the pull request to be resolved before it can be merged to a protected branch.
9961005
RequiredConversationResolution *bool `json:"required_conversation_resolution,omitempty"`
1006+
// BlockCreations, if set to true, will cause the restrictions setting to also block pushes
1007+
// which create new branches, unless initiated by a user, team, app with the ability to push.
1008+
BlockCreations *bool `json:"block_creations,omitempty"`
1009+
// LockBranch, if set to true, will prevent users from pushing to the branch.
1010+
LockBranch *bool `json:"lock_branch,omitempty"`
1011+
// AllowForkSyncing, if set to true, will allow users to pull changes from upstream
1012+
// when the branch is locked.
1013+
AllowForkSyncing *bool `json:"allow_fork_syncing,omitempty"`
9971014
}
9981015

9991016
// RequiredStatusChecks represents the protection status of a individual branch.

github/repos_test.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1096,6 +1096,15 @@ func TestRepositoriesService_GetBranchProtection(t *testing.T) {
10961096
},
10971097
"required_conversation_resolution": {
10981098
"enabled": true
1099+
},
1100+
"block_creations": {
1101+
"enabled": false
1102+
},
1103+
"lock_branch": {
1104+
"enabled": false
1105+
},
1106+
"allow_fork_syncing": {
1107+
"enabled": false
10991108
}
11001109
}`)
11011110
})
@@ -1151,6 +1160,15 @@ func TestRepositoriesService_GetBranchProtection(t *testing.T) {
11511160
RequiredConversationResolution: &RequiredConversationResolution{
11521161
Enabled: true,
11531162
},
1163+
BlockCreations: &BlockCreations{
1164+
Enabled: Bool(false),
1165+
},
1166+
LockBranch: &LockBranch{
1167+
Enabled: Bool(false),
1168+
},
1169+
AllowForkSyncing: &AllowForkSyncing{
1170+
Enabled: Bool(false),
1171+
},
11541172
}
11551173
if !cmp.Equal(protection, want) {
11561174
t.Errorf("Repositories.GetBranchProtection returned %+v, want %+v", protection, want)
@@ -1299,6 +1317,9 @@ func TestRepositoriesService_UpdateBranchProtection_Contexts(t *testing.T) {
12991317
Teams: []string{"t"},
13001318
Apps: []string{"a"},
13011319
},
1320+
BlockCreations: Bool(true),
1321+
LockBranch: Bool(true),
1322+
AllowForkSyncing: Bool(true),
13021323
}
13031324

13041325
mux.HandleFunc("/repos/o/r/branches/b/protection", func(w http.ResponseWriter, r *http.Request) {
@@ -1350,6 +1371,15 @@ func TestRepositoriesService_UpdateBranchProtection_Contexts(t *testing.T) {
13501371
"users":[{"id":1,"login":"u"}],
13511372
"teams":[{"id":2,"slug":"t"}],
13521373
"apps":[{"id":3,"slug":"a"}]
1374+
},
1375+
"block_creations": {
1376+
"enabled": true
1377+
},
1378+
"lock_branch": {
1379+
"enabled": true
1380+
},
1381+
"allow_fork_syncing": {
1382+
"enabled": true
13531383
}
13541384
}`)
13551385
})
@@ -1407,6 +1437,15 @@ func TestRepositoriesService_UpdateBranchProtection_Contexts(t *testing.T) {
14071437
{Slug: String("a"), ID: Int64(3)},
14081438
},
14091439
},
1440+
BlockCreations: &BlockCreations{
1441+
Enabled: Bool(true),
1442+
},
1443+
LockBranch: &LockBranch{
1444+
Enabled: Bool(true),
1445+
},
1446+
AllowForkSyncing: &AllowForkSyncing{
1447+
Enabled: Bool(true),
1448+
},
14101449
}
14111450
if !cmp.Equal(protection, want) {
14121451
t.Errorf("Repositories.UpdateBranchProtection returned %+v, want %+v", protection, want)

test/integration/repos_test.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,10 @@ func TestRepositories_EditBranches(t *testing.T) {
112112
// TODO: Only organization repositories can have users and team restrictions.
113113
// In order to be able to test these Restrictions, need to add support
114114
// for creating temporary organization repositories.
115-
Restrictions: nil,
115+
Restrictions: nil,
116+
BlockCreations: github.Bool(false),
117+
LockBranch: github.Bool(false),
118+
AllowForkSyncing: github.Bool(false),
116119
}
117120

118121
protection, _, err := client.Repositories.UpdateBranchProtection(context.Background(), *repo.Owner.Login, *repo.Name, "master", protectionRequest)
@@ -134,6 +137,15 @@ func TestRepositories_EditBranches(t *testing.T) {
134137
Enabled: true,
135138
},
136139
Restrictions: nil,
140+
BlockCreations: &github.BlockCreations{
141+
Enabled: github.Bool(false),
142+
},
143+
LockBranch: &github.LockBranch{
144+
Enabled: github.Bool(false),
145+
},
146+
AllowForkSyncing: &github.AllowForkSyncing{
147+
Enabled: github.Bool(false),
148+
},
137149
}
138150
if !cmp.Equal(protection, want) {
139151
t.Errorf("Repositories.UpdateBranchProtection() returned %+v, want %+v", protection, want)

0 commit comments

Comments
 (0)