Skip to content

Commit a6d1ddf

Browse files
authored
Merge pull request #2 from DocPlanner/feature/DQSAAS-862_check-_specific_pipeline_status
DQSAAS-862 add a new input parameter to check the commit status based on a specific commit check
2 parents 7a0644c + 374c889 commit a6d1ddf

File tree

5 files changed

+48
-16
lines changed

5 files changed

+48
-16
lines changed

cmd/root.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,22 @@ const SYMBOL_FAIL = "✖"
2222

2323
var rootCmd = &cobra.Command{
2424
Args: cobra.MinimumNArgs(5),
25-
Use: "github-flow-manager [OWNER] [REPOSITORY] [SOURCE_BRANCH] [DESTINATION_BRANCH] [EXPRESSION]",
25+
Use: "github-flow-manager [OWNER] [REPOSITORY] [SOURCE_BRANCH] [DESTINATION_BRANCH] [EXPRESSION] [SPECIFIC_COMMIT_CHECK_NAME]",
2626
Short: "GitHub Flow Manager",
2727
Long: `Main goal for that app is to push commits between branches
2828
but just those which pass evaluation checks.
29-
Example use case "push all commits pushed to branch develop more than 30 minutes ago to branch master"`,
29+
Example use case "push all commits pushed to branch develop more than 30 minutes ago to branch master"
30+
If a SPECIFIC_COMMIT_CHECK_NAME is specified, the StatusSuccess will be calculated based ONLY on the result of that specific commit check`,
3031
Run: func(cmd *cobra.Command, args []string) {
3132
owner := args[0]
3233
repo := args[1]
3334
sourceBranch := args[2]
34-
destinationBrnach := args[3]
35+
destinationBranch := args[3]
3536
expression := strings.Join(args[4:], " ")
37+
specificCheckName := ""
38+
if len(args) > 5 {
39+
specificCheckName = args[5]
40+
}
3641

3742
for _, a := range args {
3843
if len(a) < 1 {
@@ -50,7 +55,7 @@ Example use case "push all commits pushed to branch develop more than 30 minutes
5055
}
5156
}
5257

53-
results, err := flow_manager.Manage(*githubToken, owner, repo, sourceBranch, destinationBrnach, expression, *commitsNumber, *force, *dryRun)
58+
results, err := flow_manager.Manage(*githubToken, owner, repo, sourceBranch, destinationBranch, expression, specificCheckName, *commitsNumber, *force, *dryRun)
5459
if nil != err {
5560
fmt.Println(err.Error())
5661
os.Exit(1)

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, lastCommitsNumber int, force, dryRun bool) ([]evaluationResult, error) {
13+
func Manage(githubToken, owner, repo, sourceBranch, destinationBranch, expression string, specificCheckName 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)
16+
commits, err := gm.GetCommits(owner, repo, sourceBranch, lastCommitsNumber, specificCheckName)
1717
if nil != err {
1818
return nil, err
1919
}
2020
firstParentCommits := github.PickFirstParentCommits(commits)
2121

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

github/commit.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ package github
33
import "time"
44

55
type Commit struct {
6-
SHA string
7-
Message string
8-
Parents []Commit
9-
StatusSuccess bool
10-
PushedDate time.Time
6+
SHA string
7+
Message string
8+
Parents []Commit
9+
StatusSuccess bool
10+
PushedDate time.Time
11+
SpecificCheckPassed bool
1112
}

github/github-query.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,18 @@ type githubQuery struct {
2727
TotalCount githubql.Int
2828
} `graphql:"contexts(first: $parentsNumber)"`
2929
}
30+
CheckSuites struct {
31+
Nodes []struct {
32+
CheckRuns struct {
33+
Nodes []struct {
34+
Name githubql.String
35+
Status githubql.String
36+
Title githubql.String
37+
Conclusion githubql.String
38+
}
39+
} `graphql:"checkRuns(first: 100)"`
40+
}
41+
} `graphql:"checkSuites(first: 10)"`
3042
}
3143
}
3244
} `graphql:"history(first: $commitsNumber)"`

github/manager.go

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func New(githubAccessToken string) *githubManager {
2626
return &githubManager{Context: ctx, Client: client, HttpClient: httpClient}
2727
}
2828

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

48-
return hydrateCommits(q), nil
48+
return hydrateCommits(q, specificCheckName), nil
4949
}
5050

5151
func PickFirstParentCommits(fullCommitsList []Commit) []Commit {
@@ -96,7 +96,7 @@ func (gm *githubManager) ChangeBranchHead(owner, repo, branch, sha string, force
9696
return nil
9797
}
9898

99-
func hydrateCommits(q *githubQuery) []Commit {
99+
func hydrateCommits(q *githubQuery, specificCheckName string) []Commit {
100100
var fullCommitsList []Commit
101101
for _, edge := range q.Repository.Ref.Target.Commit.History.Edges {
102102
var parents []Commit
@@ -107,11 +107,25 @@ func hydrateCommits(q *githubQuery) []Commit {
107107
})
108108
}
109109

110+
statusSuccess := false
111+
// In case a commit check name is specified, it override and get priority over the commit cumulative status
112+
if specificCheckName != "" {
113+
for _, checkSuite := range edge.Node.CheckSuites.Nodes {
114+
for _, checkRuns := range checkSuite.CheckRuns.Nodes {
115+
if githubql.String(specificCheckName) == checkRuns.Name {
116+
statusSuccess = checkRuns.Conclusion == githubql.String(githubql.StatusStateSuccess)
117+
}
118+
}
119+
}
120+
} else {
121+
statusSuccess = bool(edge.Node.StatusCheckRollup.State == githubql.String(githubql.StatusStateSuccess))
122+
}
123+
110124
fullCommitsList = append(fullCommitsList, Commit{
111125
SHA: string(edge.Node.Oid),
112126
Message: string(edge.Node.Message),
113127
Parents: parents,
114-
StatusSuccess: bool(edge.Node.StatusCheckRollup.State == githubql.String(githubql.StatusStateSuccess)),
128+
StatusSuccess: statusSuccess,
115129
PushedDate: edge.Node.PushedDate.Time,
116130
})
117131
}

0 commit comments

Comments
 (0)