Skip to content

Commit 9ecb9de

Browse files
committed
DQSAAS-862 add a new input parameter to check the commit status based on a specific commit check
1 parent 7a0644c commit 9ecb9de

File tree

5 files changed

+49
-19
lines changed

5 files changed

+49
-19
lines changed

cmd/root.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@ import (
44
"fmt"
55
"os"
66

7-
"github.com/Docplanner/github-flow-manager/flow-manager"
8-
"github.com/olekukonko/tablewriter"
9-
"github.com/spf13/cobra"
107
"strings"
118
"time"
9+
10+
flow_manager "github.com/Docplanner/github-flow-manager/flow-manager"
11+
"github.com/olekukonko/tablewriter"
12+
"github.com/spf13/cobra"
1213
)
1314

1415
var commitsNumber *int
@@ -22,17 +23,19 @@ const SYMBOL_FAIL = "✖"
2223

2324
var rootCmd = &cobra.Command{
2425
Args: cobra.MinimumNArgs(5),
25-
Use: "github-flow-manager [OWNER] [REPOSITORY] [SOURCE_BRANCH] [DESTINATION_BRANCH] [EXPRESSION]",
26+
Use: "github-flow-manager [OWNER] [REPOSITORY] [SOURCE_BRANCH] [DESTINATION_BRANCH] [EXPRESSION] [SPECIFIC_COMMIT_CHECK_NAME]",
2627
Short: "GitHub Flow Manager",
2728
Long: `Main goal for that app is to push commits between branches
2829
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"`,
30+
Example use case "push all commits pushed to branch develop more than 30 minutes ago to branch master"
31+
If a SPECIFIC_COMMIT_CHECK_NAME is specified, the StatusSuccess will be calculated based ONLY on the result of that specific commit check`,
3032
Run: func(cmd *cobra.Command, args []string) {
3133
owner := args[0]
3234
repo := args[1]
3335
sourceBranch := args[2]
34-
destinationBrnach := args[3]
36+
destinationBranch := args[3]
3537
expression := strings.Join(args[4:], " ")
38+
specificCheckName := args[5]
3639

3740
for _, a := range args {
3841
if len(a) < 1 {
@@ -50,7 +53,7 @@ Example use case "push all commits pushed to branch develop more than 30 minutes
5053
}
5154
}
5255

53-
results, err := flow_manager.Manage(*githubToken, owner, repo, sourceBranch, destinationBrnach, expression, *commitsNumber, *force, *dryRun)
56+
results, err := flow_manager.Manage(*githubToken, owner, repo, sourceBranch, destinationBranch, expression, specificCheckName, *commitsNumber, *force, *dryRun)
5457
if nil != err {
5558
fmt.Println(err.Error())
5659
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 := bool(edge.Node.StatusCheckRollup.State == githubql.String(githubql.StatusStateSuccess))
111+
112+
// In case a commit check name is specified, it override and get priority over the commit cumulative status
113+
if specificCheckName != "" {
114+
statusSuccess = false
115+
for _, checkSuite := range edge.Node.CheckSuites.Nodes {
116+
for _, checkRuns := range checkSuite.CheckRuns.Nodes {
117+
if githubql.String(specificCheckName) == checkRuns.Name {
118+
statusSuccess = checkRuns.Conclusion == githubql.String(githubql.StatusStateSuccess)
119+
}
120+
}
121+
}
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)