Skip to content

Commit ba5964c

Browse files
feat: add flag --pr-merge-branch-protection
This flag lets you pick how to handle branch protection rules. `statusChecks` waits until all **required** statues & check rules (from Statues & Checks API, respectively) are in a "success" state. For Checks this also means any rule that has a "neutral" or "skipped" conclusion. It looks at both required statues configured under branch protection rules **and** rulesets. This is the flag default to maintain backwards compatibility pre-`v1.6.1`. `all` waits until **all** branch protection rules are met. This also includes any matching Rules defined via the Rulesets API. `bypass` lets you bypass all branch protection rules once the authenticated user has the ability. That is, the user/app is configured to bypass rules. Also bumped Github Client to v57 to be able to use the Ruleset API. This should fix issue dailymotion-oss#293.
1 parent c4e9bde commit ba5964c

File tree

203 files changed

+35234
-13252
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

203 files changed

+35234
-13252
lines changed

docs/current-version/content/repos/pull-request.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,3 +96,7 @@ All the following flags only apply if `--pr-merge` is enabled.
9696
- `--pr-merge-poll-timeout` (string/duration): maximum duration to wait for a Pull Request to be mergeable/merged, using the [Golang syntax](https://golang.org/pkg/time/#ParseDuration). Default to `10m` (10 minutes).
9797
- `--pr-merge-poll-interval` (string/duration): duration to wait for between each GitHub API call to check if a PR is mergeable/merged, using the [Golang syntax](https://golang.org/pkg/time/#ParseDuration). Default to `30s` (30 seconds).
9898
- `--pr-merge-retry-count` (int): number of times to retry the merge operation in case of merge failure. Default to `3`.
99+
- `--pr-merge-branch-protection` (string): One of `statusChecks`, `all`, `bypass`. Wait for the specified kind of branch protection rules to be satisfied before attempting to merge.
100+
- `statusChecks` waits only for status checks to be passing. This is the default.
101+
- `all` waits for every rule (approvals, commit signature, etc).
102+
- `bypass` will bypass branch protection rules when possible (i.e. the authenticated user/app have permissions to do so).

go.mod

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ require (
88
github.com/bradleyfalzon/ghinstallation v1.1.1
99
github.com/cosiner/argv v0.1.1-0.20200416041250-86e3c689263e
1010
github.com/go-git/go-git/v5 v5.10.1
11-
github.com/google/go-github/v36 v36.0.0
11+
github.com/google/go-github/v57 v57.0.0
1212
github.com/imdario/mergo v0.3.16
1313
github.com/mattn/go-zglob v0.0.4
1414
github.com/mholt/archiver v3.1.1+incompatible
@@ -22,6 +22,7 @@ require (
2222
github.com/stretchr/testify v1.8.4
2323
github.com/writeas/go-strip-markdown v2.0.1+incompatible
2424
github.com/ybbus/httpretry v1.0.2
25+
github.com/zoumo/goset v0.2.0
2526
go.mozilla.org/sops/v3 v3.7.3
2627
golang.org/x/oauth2 v0.15.0
2728
gopkg.in/op/go-logging.v1 v1.0.0-20160211212156-b2cb9fa56473

go.sum

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -212,14 +212,13 @@ github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
212212
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
213213
github.com/google/go-cmp v0.5.3/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
214214
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
215-
github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
216215
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
217216
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
218217
github.com/google/go-github/v29 v29.0.2/go.mod h1:CHKiKKPHJ0REzfwc14QMklvtHwCveD0PxlMjLlzAM5E=
219218
github.com/google/go-github/v29 v29.0.3 h1:IktKCTwU//aFHnpA+2SLIi7Oo9uhAzgsdZNbcAqhgdc=
220219
github.com/google/go-github/v29 v29.0.3/go.mod h1:CHKiKKPHJ0REzfwc14QMklvtHwCveD0PxlMjLlzAM5E=
221-
github.com/google/go-github/v36 v36.0.0 h1:ndCzM616/oijwufI7nBRa+5eZHLldT+4yIB68ib5ogs=
222-
github.com/google/go-github/v36 v36.0.0/go.mod h1:LFlKC047IOqiglRGNqNb9s/iAPTnnjtlshm+bxp+kwk=
220+
github.com/google/go-github/v57 v57.0.0 h1:L+Y3UPTY8ALM8x+TV0lg+IEBI+upibemtBD8Q9u7zHs=
221+
github.com/google/go-github/v57 v57.0.0/go.mod h1:s0omdnye0hvK/ecLvpsGfJMiRt85PimQh4oygmLIxHw=
223222
github.com/google/go-querystring v1.0.0/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO6wN/zVPAxq5ck=
224223
github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8=
225224
github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU=
@@ -487,6 +486,8 @@ github.com/xi2/xz v0.0.0-20171230120015-48954b6210f8/go.mod h1:HUYIGzjTL3rfEspMx
487486
github.com/ybbus/httpretry v1.0.2 h1:QIU8dfSF+kZx5xO1bUcLKyxYNEUsLX/hsN6gN6Up1So=
488487
github.com/ybbus/httpretry v1.0.2/go.mod h1:fwOEa1URVFYikEqgQLCBtLyExFt5danZrxF5xF2qZh8=
489488
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
489+
github.com/zoumo/goset v0.2.0 h1:mflvfwZfKiayxR8/7fFBhvWHzhXU+aVP2nOzTEKVTfU=
490+
github.com/zoumo/goset v0.2.0/go.mod h1:9N4gYaiVAbD/SytgmvFUlacbp1lSItmws1xNdzVslH4=
490491
go.mozilla.org/gopgagent v0.0.0-20170926210634-4d7ea76ff71a h1:N7VD+PwpJME2ZfQT8+ejxwA4Ow10IkGbU0MGf94ll8k=
491492
go.mozilla.org/gopgagent v0.0.0-20170926210634-4d7ea76ff71a/go.mod h1:YDKUvO0b//78PaaEro6CAPH6NqohCmL2Cwju5XI2HoE=
492493
go.mozilla.org/sops/v3 v3.7.3 h1:CYx02LnWTATWv6NqWJIt4JCKVKSnGV+MsRiDpvwWQhg=

main.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ var options struct {
3737
}
3838

3939
func init() {
40+
// defaults
41+
options.GitHub.PullRequest.Merge.BranchProtection = repository.BranchProtectionKindStatusChecks
42+
4043
// required flags
4144
pflag.StringArrayVarP(&options.updates, "update", "u", nil, `An update operation, such as "yaml(file=config.yaml,path='version')=file(path=VERSION)" - see the online documentation for all available updaters.`)
4245
assert(pflag.CommandLine.SetAnnotation("update", "mandatory", []string{"true"}))
@@ -73,6 +76,7 @@ func init() {
7376
pflag.DurationVar(&options.GitHub.PullRequest.Merge.PollTimeout, "pr-merge-poll-timeout", 10*time.Minute, "If pr-merge is enabled, this is the maximum duration to wait for a Pull Request to be mergeable/merged.")
7477
pflag.DurationVar(&options.GitHub.PullRequest.Merge.PollInterval, "pr-merge-poll-interval", 30*time.Second, "If pr-merge is enabled, this is the duration to wait for between each GitHub API call to check if a PR is mergeable/merged.")
7578
pflag.IntVar(&options.GitHub.PullRequest.Merge.RetryCount, "pr-merge-retry-count", 3, "If pr-merge is enabled, this is the number of times to retry the merge operation in case of merge failure.")
79+
pflag.Var(&options.GitHub.PullRequest.Merge.BranchProtection, "pr-merge-branch-protection", `If pr-merge is enabled, then wait for the specified kind of branch protection rules to be satisfied before attempting to merge. "statusChecks" waits only for status checks to be passing. "all" waits for every rule (approvals, commit signature, etc). "bypass" will bypass branch protection rules when possible.`)
7680

7781
// git-related flags
7882
pflag.StringVar(&options.UpdateOptions.Git.CloneDir, "git-clone-dir", temporaryDirectory(), "Directory used to clone the repositories.")

repository/discover_from_query.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"context"
55
"fmt"
66

7-
"github.com/google/go-github/v36/github"
7+
"github.com/google/go-github/v57/github"
88
)
99

1010
func discoverRepositoriesFromQuery(ctx context.Context, searchType SearchType, query string, params map[string]string, githubOpts GitHubOptions) ([]Repository, error) {

repository/github_client.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
"net/url"
88

99
"github.com/bradleyfalzon/ghinstallation"
10-
"github.com/google/go-github/v36/github"
10+
"github.com/google/go-github/v57/github"
1111
"github.com/shurcooL/githubv4"
1212
"github.com/sirupsen/logrus"
1313
"github.com/ybbus/httpretry"
@@ -57,9 +57,22 @@ func githubClient(ctx context.Context, ghOptions GitHubOptions) (*github.Client,
5757
return ghc, token, nil
5858
}
5959

60+
type graphqlTransport struct {
61+
base http.RoundTripper
62+
}
63+
64+
func (t *graphqlTransport) RoundTrip(req *http.Request) (*http.Response, error) {
65+
// Enable PullRequest.mergeStateStatus
66+
req.Header.Add("accept", "application/vnd.github.merge-info-preview+json")
67+
68+
return t.base.RoundTrip(req)
69+
}
70+
6071
func githubGraphqlClient(ctx context.Context, ghOptions GitHubOptions) (*githubv4.Client, error) {
6172
httpClient, _, err := githubAuthenticatedHTTPClient(ctx, ghOptions)
6273

74+
httpClient.Transport = &graphqlTransport{base: httpClient.Transport}
75+
6376
if err != nil {
6477
return nil, err
6578
}

repository/options.go

Lines changed: 51 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package repository
22

33
import (
4+
"errors"
45
"fmt"
56
"strings"
67
"time"
@@ -74,18 +75,58 @@ type PullRequestOptions struct {
7475
Merge PullRequestMergeOptions
7576
}
7677

78+
// BranchProtectionKind enumerates possible branch protections to wait for before attempting a PR merge.
79+
type BranchProtectionKind string
80+
81+
const (
82+
// BranchProtectionKindStatusChecks waits for all required statues & checksuite runs to be passing ("success", "neutral", "skipped")
83+
BranchProtectionKindStatusChecks BranchProtectionKind = "statusChecks"
84+
85+
// BranchProtectionKindAll wait for all protection rules
86+
BranchProtectionKindAll BranchProtectionKind = "all"
87+
88+
// BranchProtectionKindBypass waits until the user can bypass branch protection rules
89+
BranchProtectionKindBypass BranchProtectionKind = "bypass"
90+
)
91+
92+
func (b *BranchProtectionKind) String() string {
93+
return string(*b)
94+
}
95+
96+
func (b *BranchProtectionKind) Set(s string) error {
97+
switch s {
98+
case string(BranchProtectionKindStatusChecks), string(BranchProtectionKindAll), string(BranchProtectionKindBypass):
99+
*b = BranchProtectionKind(s)
100+
return nil
101+
default:
102+
return errors.New("invalid value")
103+
}
104+
}
105+
106+
func (b *BranchProtectionKind) Type() string {
107+
return strings.Join(
108+
[]string{
109+
string(BranchProtectionKindStatusChecks),
110+
string(BranchProtectionKindAll),
111+
string(BranchProtectionKindBypass),
112+
},
113+
"|",
114+
)
115+
}
116+
77117
// PullRequestMergeOptions holds all the options required to merge github PRs
78118
type PullRequestMergeOptions struct {
79-
Enabled bool
80-
Auto bool
81-
AutoWait bool
82-
Method string
83-
CommitTitle string
84-
CommitMessage string
85-
SHA string
86-
PollInterval time.Duration
87-
PollTimeout time.Duration
88-
RetryCount int
119+
Enabled bool
120+
Auto bool
121+
AutoWait bool
122+
Method string
123+
CommitTitle string
124+
CommitMessage string
125+
SHA string
126+
PollInterval time.Duration
127+
PollTimeout time.Duration
128+
RetryCount int
129+
BranchProtection BranchProtectionKind
89130
}
90131

91132
func (o *GitOptions) setDefaultValues(updaters []update.Updater, tplExecutorFunc templateExecutor) error {

0 commit comments

Comments
 (0)