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

allow to set mutliple check names #8

Merged
merged 1 commit into from
Jun 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,18 @@ Flags:
-t, --github-token string GitHub token (can be passed also as GITHUB_TOKEN env variable
-h, --help help for github-flow-manager
-v, --verbose Print table with commits evaluation status
-s, --separator Set string separator of status checks (default ,)
```

## Example
- Evaluating commit status success based on the cumulative commit checks result
```
GITHUB_TOKEN=xxx github-flow-manager octocat Hello-World test master "StatusSuccess == false" "pipeline-name-to-be-checked" --verbose --dry-run
GITHUB_TOKEN=xxx github-flow-manager octocat Hello-World test master "StatusSuccess == false" --verbose --dry-run
```
- Passing specific commit check name for the evaluation of the status success of the commit
```
GITHUB_TOKEN=xxx github-flow-manager octocat Hello-World test master "StatusSuccess == false" "pipeline-name-to-be-checked" --verbose --dry-run
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
```

# Expressions
Expand Down
8 changes: 5 additions & 3 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ var githubToken *string
var force *bool
var verbose *bool
var dryRun *bool
var separator *string

const SYMBOL_SUCCESS = "✔"
const SYMBOL_FAIL = "✖"
Expand All @@ -34,9 +35,9 @@ If a SPECIFIC_COMMIT_CHECK_NAME is specified, the StatusSuccess will be calculat
sourceBranch := args[2]
destinationBranch := args[3]
expression := strings.Join(args[4:], " ")
specificCheckName := ""
specificChecksNames := ""
if len(args) > 5 {
specificCheckName = args[5]
specificChecksNames = args[5]
}

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

results, err := flow_manager.Manage(*githubToken, owner, repo, sourceBranch, destinationBranch, expression, specificCheckName, *commitsNumber, *force, *dryRun)
results, err := flow_manager.Manage(*githubToken, owner, repo, sourceBranch, destinationBranch, expression, specificChecksNames, *separator, *commitsNumber, *force, *dryRun)
if nil != err {
fmt.Println(err.Error())
os.Exit(1)
Expand Down Expand Up @@ -115,4 +116,5 @@ func init() {
force = rootCmd.Flags().BoolP("force", "f", false, "Use the force Luke... - Changes branch HEAD with force")
verbose = rootCmd.Flags().BoolP("verbose", "v", false, "Print table with commits evaluation status")
dryRun = rootCmd.Flags().BoolP("dry-run", "d", false, "Don't modify repository")
separator = rootCmd.Flags().StringP("separator", "s", ",", "Set string separator of status checks")
}
6 changes: 3 additions & 3 deletions flow-manager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@ import (
"github.com/araddon/qlbridge/vm"
)

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

destinationCommits, err := gm.GetCommits(owner, repo, destinationBranch, 1, specificCheckName)
destinationCommits, err := gm.GetCommits(owner, repo, destinationBranch, 1, specificChecksNames, sep)
if nil != err {
return nil, err
}
Expand Down
38 changes: 27 additions & 11 deletions github/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package github

import (
"net/http"
"strings"

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

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

return hydrateCommits(q, specificCheckName), nil
return hydrateCommits(q, specificChecksNames, sep), nil
}

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

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

statusSuccess := false
// In case a commit check name is specified, it override and get priority over the commit cumulative status
if specificCheckName != "" {
checkNames := strings.Split(specificChecksNames, sep)
numChecks := len(checkNames)
sc := 0
cc := 0

for _, cn := range checkNames {

// first check if commit has commit status set
for _, context := range edge.Node.Status.Contexts {
if githubv4.String(specificCheckName) == context.Context {
statusSuccess = context.State == githubv4.String(githubv4.StatusStateSuccess)
if githubv4.String(cn) == context.Context {
if context.State == githubv4.String(githubv4.StatusStateSuccess) {
sc++
}
}
}

// then check if commit has check-run set
for _, checkSuite := range edge.Node.CheckSuites.Nodes {
for _, checkRuns := range checkSuite.CheckRuns.Nodes {
if githubv4.String(specificCheckName) == checkRuns.Name {
statusSuccess = checkRuns.Conclusion == githubv4.String(githubv4.StatusStateSuccess)
if githubv4.String(cn) == checkRuns.Name {
if checkRuns.Conclusion == githubv4.String(githubv4.StatusStateSuccess) {
cc++
}
}
}
}
} else {
statusSuccess = bool(edge.Node.StatusCheckRollup.State == githubv4.String(githubv4.StatusStateSuccess))
}

if numChecks == sc || numChecks == cc {
statusSuccess = true
}

if numChecks == 0 {
statusSuccess = edge.Node.StatusCheckRollup.State == githubv4.String(githubv4.StatusStateSuccess)
}

fullCommitsList = append(fullCommitsList, Commit{
Expand Down
15 changes: 0 additions & 15 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,39 +4,24 @@ go 1.16

require (
github.com/araddon/dateparse v0.0.0-20210429162001-6b43995a97de // indirect
github.com/araddon/gou v0.0.0-20190110011759-c797efecbb61 // indirect
github.com/araddon/qlbridge v0.0.2
github.com/dchest/siphash v1.2.2 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/btree v1.0.0 // indirect
github.com/google/go-github v17.0.0+incompatible
github.com/google/go-querystring v1.1.0 // indirect
github.com/google/uuid v1.2.0 // indirect
github.com/hashicorp/go-memdb v1.3.1 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/jmoiron/sqlx v1.3.1 // indirect
github.com/leekchan/timeutil v0.0.0-20150802142658-28917288c48d // indirect
github.com/lytics/datemath v0.0.0-20180727225141-3ada1c10b5de // indirect
github.com/lytics/logrus v0.0.0-20170528191427-4389a17ed024 // indirect
github.com/mattn/go-runewidth v0.0.13 // indirect
github.com/mb0/glob v0.0.0-20160210091149-1eb79d2de6c4 // indirect
github.com/mssola/user_agent v0.5.3 // indirect
github.com/olekukonko/tablewriter v0.0.5
github.com/pborman/uuid v1.2.1 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
github.com/shurcooL/githubv4 v0.0.0-20201206200315-234843c633fa
github.com/shurcooL/graphql v0.0.0-20200928012149-18c5c3165e3a // indirect
github.com/simplereach/timeutils v1.2.0 // indirect
github.com/spf13/cobra v1.1.3
github.com/spf13/pflag v1.0.5 // indirect
github.com/stretchr/testify v1.7.0 // indirect
golang.org/x/net v0.0.0-20210525063256-abc453219eb5
golang.org/x/oauth2 v0.0.0-20210514164344-f6687ab2804c
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c // indirect
golang.org/x/sys v0.0.0-20210603125802-9665404d3644 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/protobuf v1.26.0 // indirect
gopkg.in/mgo.v2 v2.0.0-20190816093944-a6b53ec6cb22 // indirect
)
Loading