Skip to content

Commit e7f7f86

Browse files
committed
[x] flow manager
[x] cli
1 parent 700500b commit e7f7f86

File tree

6 files changed

+303
-23
lines changed

6 files changed

+303
-23
lines changed

Gopkg.lock

Lines changed: 123 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Gopkg.toml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,15 @@
4040
[[constraint]]
4141
name = "github.com/google/go-github"
4242
version = "15.0.0"
43+
44+
[[constraint]]
45+
branch = "master"
46+
name = "github.com/araddon/qlbridge"
47+
48+
[[constraint]]
49+
name = "github.com/spf13/cobra"
50+
version = "0.0.2"
51+
52+
[[constraint]]
53+
branch = "master"
54+
name = "github.com/olekukonko/tablewriter"

cmd/root.go

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
package cmd
2+
3+
import (
4+
"fmt"
5+
"os"
6+
7+
"github.com/spf13/cobra"
8+
"strings"
9+
"github-flow-manager/flow-manager"
10+
"github.com/olekukonko/tablewriter"
11+
"time"
12+
)
13+
14+
var commitsNumber *int
15+
var githubToken *string
16+
var force *bool
17+
var verbose *bool
18+
var dryRun *bool
19+
20+
const SYMBOL_SUCCESS = "✔"
21+
const SYMBOL_FAIL = "✖"
22+
23+
var rootCmd = &cobra.Command{
24+
Args: cobra.MinimumNArgs(5),
25+
Use: "github-flow-manager [OWNER] [REPOSITORY] [SOURCE_BRANCH] [DESTINATION_BRANCH] [EXPRESSION]",
26+
Short: "GitHub Flow Manager",
27+
Long: `Main goal for that app is to push commits between branches
28+
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+
Run: func(cmd *cobra.Command, args []string) {
31+
owner := args[0]
32+
repo := args[1]
33+
sourceBranch := args[2]
34+
destinationBrnach := args[3]
35+
expression := strings.Join(args[4:], " ")
36+
37+
for _, a := range args {
38+
if len(a) < 1 {
39+
cmd.Help()
40+
return
41+
}
42+
}
43+
44+
if "" == *githubToken {
45+
*githubToken = os.Getenv("GITHUB_TOKEN")
46+
if "" == *githubToken {
47+
fmt.Println("Github token not set!")
48+
cmd.Help()
49+
return
50+
}
51+
}
52+
53+
results, err := flow_manager.Manage(*githubToken, owner, repo, sourceBranch, destinationBrnach, expression, *commitsNumber, *force, *dryRun)
54+
if nil != err {
55+
panic(err.Error())
56+
}
57+
58+
if !*verbose {
59+
return
60+
}
61+
62+
if len(results) < 1 {
63+
fmt.Println("No commits on source branch")
64+
return
65+
}
66+
67+
table := tablewriter.NewWriter(os.Stdout)
68+
table.SetRowLine(true)
69+
table.SetHeader([]string{"SHA", "MESSAGE", "PUSHED_AT", "IS_STATUS_SUCCESS", "EVALUATION"})
70+
71+
for _, res := range results {
72+
c := res.Commit
73+
74+
statusSign := SYMBOL_FAIL
75+
if c.StatusSuccess {
76+
statusSign = SYMBOL_SUCCESS
77+
}
78+
79+
resultSign := SYMBOL_FAIL
80+
if res.Result {
81+
resultSign = SYMBOL_SUCCESS
82+
}
83+
84+
table.Append([]string{c.SHA, c.Message, c.PushedDate.Format(time.RFC3339), statusSign, resultSign})
85+
}
86+
87+
table.Render()
88+
89+
endingMessage := "THERE IS NO COMMITS PASSING EVALUATION"
90+
if results[len(results)-1].Result {
91+
endingMessage = "NO MORE COMMITS WERE EXAMINED BECAUSE LAST ONE EVALUATED SUCCESSFULLY"
92+
}
93+
94+
fmt.Println("\t!!!!! " + endingMessage + " !!!!!")
95+
},
96+
}
97+
98+
func Execute() {
99+
if err := rootCmd.Execute(); err != nil {
100+
fmt.Println(err)
101+
os.Exit(1)
102+
}
103+
}
104+
105+
func init() {
106+
commitsNumber = rootCmd.Flags().IntP("commits-number", "c", 100, "Number of commits to get under evaluation (>0, <=100)")
107+
githubToken = rootCmd.Flags().StringP("github-token", "t", "", "GitHub token (can be passed also as GITHUB_TOKEN env variable")
108+
force = rootCmd.Flags().BoolP("force", "f", false, "Use the force Luke... - Changes branch HEAD with force")
109+
verbose = rootCmd.Flags().BoolP("verbose", "v", false, "Print table with commits evaluation status")
110+
dryRun = rootCmd.Flags().BoolP("dry-run", "d", false, "Don't modify repository")
111+
}

flow-manager/evaluation-result.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package flow_manager
2+
3+
import "github-flow-manager/github"
4+
5+
type evaluationResult struct {
6+
Commit github.Commit
7+
Result bool
8+
}

flow-manager/manager.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package flow_manager
2+
3+
import (
4+
"github-flow-manager/github"
5+
"github.com/araddon/qlbridge/expr/builtins"
6+
"github.com/araddon/qlbridge/datasource"
7+
"github.com/araddon/qlbridge/vm"
8+
"github.com/araddon/qlbridge/expr"
9+
)
10+
11+
func Manage(githubToken, owner, repo, sourceBranch, destinationBranch, expression string, lastCommitsNumber int, force, dryRun bool) ([]evaluationResult, error) {
12+
parsedExpression := expr.MustParse(expression)
13+
gm := github.New(githubToken)
14+
commits, err := gm.GetCommits(owner, repo, sourceBranch, lastCommitsNumber)
15+
if nil != err {
16+
return nil, err
17+
}
18+
firstParentCommits := github.PickFirstParentCommits(commits)
19+
20+
var evaluationResultList []evaluationResult
21+
builtins.LoadAllBuiltins()
22+
for _, commit := range firstParentCommits {
23+
evalContext := datasource.NewContextSimpleNative(map[string]interface{}{
24+
"SHA": commit.SHA,
25+
"Message": commit.Message,
26+
"PushedDate": commit.PushedDate,
27+
"StatusSuccess": commit.StatusSuccess,
28+
})
29+
30+
val, _ := vm.Eval(evalContext, parsedExpression)
31+
v := val.Value()
32+
33+
evaluationResultList = append(evaluationResultList, evaluationResult{Result: v.(bool), Commit: commit})
34+
35+
if true == v {
36+
if !dryRun {
37+
err = gm.ChangeBranchHead(owner, repo, destinationBranch, commit.SHA, force)
38+
if err != nil {
39+
return nil, err
40+
}
41+
}
42+
break
43+
}
44+
}
45+
46+
return evaluationResultList, nil
47+
}

github-flow-manager.go

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,9 @@
11
package main
22

33
import (
4-
"github-flow-manager/github"
5-
"os"
6-
"fmt"
4+
"github-flow-manager/cmd"
75
)
86

97
func main() {
10-
gm := github.New(os.Getenv("GITHUB_TOKEN"))
11-
commits, err := gm.GetCommits("DocPlanner", "github-flow-manager-test-repo", "master", 10)
12-
if nil != err {
13-
fmt.Println(err)
14-
panic("")
15-
}
16-
firstParentCommits := github.PickFirstParentCommits(commits)
17-
18-
masterHead := firstParentCommits[0]
19-
20-
err = gm.ChangeBranchHead("DocPlanner", "github-flow-manager-test-repo", "test", masterHead.SHA, false)
21-
if err != nil {
22-
fmt.Println(err)
23-
}
24-
25-
26-
//for _, c := range firstParentCommits {
27-
// fmt.Println(c.SHA, c.Message)
28-
//}
8+
cmd.Execute()
299
}

0 commit comments

Comments
 (0)