Skip to content

Commit 2908573

Browse files
authored
allow to set mutliple check names (#8)
1 parent 224b4cf commit 2908573

File tree

6 files changed

+43
-81
lines changed

6 files changed

+43
-81
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,18 @@ Flags:
1515
-t, --github-token string GitHub token (can be passed also as GITHUB_TOKEN env variable
1616
-h, --help help for github-flow-manager
1717
-v, --verbose Print table with commits evaluation status
18+
-s, --separator Set string separator of status checks (default ,)
1819
```
1920

2021
## Example
2122
- Evaluating commit status success based on the cumulative commit checks result
2223
```
23-
GITHUB_TOKEN=xxx github-flow-manager octocat Hello-World test master "StatusSuccess == false" "pipeline-name-to-be-checked" --verbose --dry-run
24+
GITHUB_TOKEN=xxx github-flow-manager octocat Hello-World test master "StatusSuccess == false" --verbose --dry-run
2425
```
2526
- Passing specific commit check name for the evaluation of the status success of the commit
2627
```
2728
GITHUB_TOKEN=xxx github-flow-manager octocat Hello-World test master "StatusSuccess == false" "pipeline-name-to-be-checked" --verbose --dry-run
29+
GITHUB_TOKEN=xxx github-flow-manager octocat Hello-World test master "StatusSuccess == false" "pipeline-1-name-to-be-checked,pipeline-2-name-to-be-checked" --verbose --dry-run
2830
```
2931

3032
# Expressions

cmd/root.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ var githubToken *string
1616
var force *bool
1717
var verbose *bool
1818
var dryRun *bool
19+
var separator *string
1920

2021
const SYMBOL_SUCCESS = "✔"
2122
const SYMBOL_FAIL = "✖"
@@ -34,9 +35,9 @@ If a SPECIFIC_COMMIT_CHECK_NAME is specified, the StatusSuccess will be calculat
3435
sourceBranch := args[2]
3536
destinationBranch := args[3]
3637
expression := strings.Join(args[4:], " ")
37-
specificCheckName := ""
38+
specificChecksNames := ""
3839
if len(args) > 5 {
39-
specificCheckName = args[5]
40+
specificChecksNames = args[5]
4041
}
4142

4243
for _, a := range args {
@@ -55,7 +56,7 @@ If a SPECIFIC_COMMIT_CHECK_NAME is specified, the StatusSuccess will be calculat
5556
}
5657
}
5758

58-
results, err := flow_manager.Manage(*githubToken, owner, repo, sourceBranch, destinationBranch, expression, specificCheckName, *commitsNumber, *force, *dryRun)
59+
results, err := flow_manager.Manage(*githubToken, owner, repo, sourceBranch, destinationBranch, expression, specificChecksNames, *separator, *commitsNumber, *force, *dryRun)
5960
if nil != err {
6061
fmt.Println(err.Error())
6162
os.Exit(1)
@@ -115,4 +116,5 @@ func init() {
115116
force = rootCmd.Flags().BoolP("force", "f", false, "Use the force Luke... - Changes branch HEAD with force")
116117
verbose = rootCmd.Flags().BoolP("verbose", "v", false, "Print table with commits evaluation status")
117118
dryRun = rootCmd.Flags().BoolP("dry-run", "d", false, "Don't modify repository")
119+
separator = rootCmd.Flags().StringP("separator", "s", ",", "Set string separator of status checks")
118120
}

flow-manager/manager.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,16 @@ import (
1010
"github.com/araddon/qlbridge/vm"
1111
)
1212

13-
func Manage(githubToken, owner, repo, sourceBranch, destinationBranch, expression string, specificCheckName string, lastCommitsNumber int, force, dryRun bool) ([]evaluationResult, error) {
13+
func Manage(githubToken, owner, repo, sourceBranch, destinationBranch, expression string, specificChecksNames string, sep string, lastCommitsNumber int, force, dryRun bool) ([]evaluationResult, error) {
1414
parsedExpression := expr.MustParse(expression)
1515
gm := github.New(githubToken)
16-
commits, err := gm.GetCommits(owner, repo, sourceBranch, lastCommitsNumber, specificCheckName)
16+
commits, err := gm.GetCommits(owner, repo, sourceBranch, lastCommitsNumber, specificChecksNames, sep)
1717
if nil != err {
1818
return nil, err
1919
}
2020
firstParentCommits := github.PickFirstParentCommits(commits)
2121

22-
destinationCommits, err := gm.GetCommits(owner, repo, destinationBranch, 1, specificCheckName)
22+
destinationCommits, err := gm.GetCommits(owner, repo, destinationBranch, 1, specificChecksNames, sep)
2323
if nil != err {
2424
return nil, err
2525
}

github/manager.go

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package github
22

33
import (
44
"net/http"
5+
"strings"
56

67
"github.com/google/go-github/github"
78
"github.com/shurcooL/githubv4"
@@ -26,7 +27,7 @@ func New(githubAccessToken string) *githubManager {
2627
return &githubManager{Context: ctx, Client: client, HttpClient: httpClient}
2728
}
2829

29-
func (gm *githubManager) GetCommits(owner, repo, branch string, lastCommitsNumber int, specificCheckName string) ([]Commit, error) {
30+
func (gm *githubManager) GetCommits(owner, repo, branch string, lastCommitsNumber int, specificChecksNames string, sep string) ([]Commit, error) {
3031
if lastCommitsNumber > 100 || lastCommitsNumber < 1 {
3132
return nil, &Error{Message: "lastCommitsNumber must be a number between 1 and 100"} // TODO maybe in future implement pagination
3233
}
@@ -45,7 +46,7 @@ func (gm *githubManager) GetCommits(owner, repo, branch string, lastCommitsNumbe
4546
return nil, err
4647
}
4748

48-
return hydrateCommits(q, specificCheckName), nil
49+
return hydrateCommits(q, specificChecksNames, sep), nil
4950
}
5051

5152
func PickFirstParentCommits(fullCommitsList []Commit) []Commit {
@@ -96,7 +97,7 @@ func (gm *githubManager) ChangeBranchHead(owner, repo, branch, sha string, force
9697
return nil
9798
}
9899

99-
func hydrateCommits(q *githubQuery, specificCheckName string) []Commit {
100+
func hydrateCommits(q *githubQuery, specificChecksNames string, sep string) []Commit {
100101
var fullCommitsList []Commit
101102
for _, edge := range q.Repository.Ref.Target.Commit.History.Edges {
102103
var parents []Commit
@@ -108,25 +109,40 @@ func hydrateCommits(q *githubQuery, specificCheckName string) []Commit {
108109
}
109110

110111
statusSuccess := false
111-
// In case a commit check name is specified, it override and get priority over the commit cumulative status
112-
if specificCheckName != "" {
112+
checkNames := strings.Split(specificChecksNames, sep)
113+
numChecks := len(checkNames)
114+
sc := 0
115+
cc := 0
116+
117+
for _, cn := range checkNames {
118+
113119
// first check if commit has commit status set
114120
for _, context := range edge.Node.Status.Contexts {
115-
if githubv4.String(specificCheckName) == context.Context {
116-
statusSuccess = context.State == githubv4.String(githubv4.StatusStateSuccess)
121+
if githubv4.String(cn) == context.Context {
122+
if context.State == githubv4.String(githubv4.StatusStateSuccess) {
123+
sc++
124+
}
117125
}
118126
}
119127

120128
// then check if commit has check-run set
121129
for _, checkSuite := range edge.Node.CheckSuites.Nodes {
122130
for _, checkRuns := range checkSuite.CheckRuns.Nodes {
123-
if githubv4.String(specificCheckName) == checkRuns.Name {
124-
statusSuccess = checkRuns.Conclusion == githubv4.String(githubv4.StatusStateSuccess)
131+
if githubv4.String(cn) == checkRuns.Name {
132+
if checkRuns.Conclusion == githubv4.String(githubv4.StatusStateSuccess) {
133+
cc++
134+
}
125135
}
126136
}
127137
}
128-
} else {
129-
statusSuccess = bool(edge.Node.StatusCheckRollup.State == githubv4.String(githubv4.StatusStateSuccess))
138+
}
139+
140+
if numChecks == sc || numChecks == cc {
141+
statusSuccess = true
142+
}
143+
144+
if numChecks == 0 {
145+
statusSuccess = edge.Node.StatusCheckRollup.State == githubv4.String(githubv4.StatusStateSuccess)
130146
}
131147

132148
fullCommitsList = append(fullCommitsList, Commit{

go.mod

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,39 +4,24 @@ go 1.16
44

55
require (
66
github.com/araddon/dateparse v0.0.0-20210429162001-6b43995a97de // indirect
7-
github.com/araddon/gou v0.0.0-20190110011759-c797efecbb61 // indirect
87
github.com/araddon/qlbridge v0.0.2
98
github.com/dchest/siphash v1.2.2 // indirect
109
github.com/gogo/protobuf v1.3.2 // indirect
1110
github.com/golang/protobuf v1.5.2 // indirect
12-
github.com/google/btree v1.0.0 // indirect
1311
github.com/google/go-github v17.0.0+incompatible
1412
github.com/google/go-querystring v1.1.0 // indirect
1513
github.com/google/uuid v1.2.0 // indirect
1614
github.com/hashicorp/go-memdb v1.3.1 // indirect
17-
github.com/inconshreveable/mousetrap v1.0.0 // indirect
1815
github.com/jmespath/go-jmespath v0.4.0 // indirect
1916
github.com/jmoiron/sqlx v1.3.1 // indirect
20-
github.com/leekchan/timeutil v0.0.0-20150802142658-28917288c48d // indirect
21-
github.com/lytics/datemath v0.0.0-20180727225141-3ada1c10b5de // indirect
22-
github.com/lytics/logrus v0.0.0-20170528191427-4389a17ed024 // indirect
2317
github.com/mattn/go-runewidth v0.0.13 // indirect
24-
github.com/mb0/glob v0.0.0-20160210091149-1eb79d2de6c4 // indirect
2518
github.com/mssola/user_agent v0.5.3 // indirect
2619
github.com/olekukonko/tablewriter v0.0.5
2720
github.com/pborman/uuid v1.2.1 // indirect
28-
github.com/rivo/uniseg v0.2.0 // indirect
2921
github.com/shurcooL/githubv4 v0.0.0-20201206200315-234843c633fa
3022
github.com/shurcooL/graphql v0.0.0-20200928012149-18c5c3165e3a // indirect
31-
github.com/simplereach/timeutils v1.2.0 // indirect
3223
github.com/spf13/cobra v1.1.3
33-
github.com/spf13/pflag v1.0.5 // indirect
34-
github.com/stretchr/testify v1.7.0 // indirect
3524
golang.org/x/net v0.0.0-20210525063256-abc453219eb5
3625
golang.org/x/oauth2 v0.0.0-20210514164344-f6687ab2804c
37-
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c // indirect
38-
golang.org/x/sys v0.0.0-20210603125802-9665404d3644 // indirect
3926
google.golang.org/appengine v1.6.7 // indirect
40-
google.golang.org/protobuf v1.26.0 // indirect
41-
gopkg.in/mgo.v2 v2.0.0-20190816093944-a6b53ec6cb22 // indirect
4227
)

0 commit comments

Comments
 (0)