This repository has been archived by the owner on May 3, 2023. It is now read-only.
/
main.go
187 lines (148 loc) · 4.31 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package main
import (
"bufio"
"bytes"
"context"
"fmt"
"log"
"os"
"os/exec"
"path"
"regexp"
"strconv"
"strings"
"sync"
"github.com/google/go-github/github"
"github.com/rodaine/table"
)
const (
gitHubRoot = "http://github.com"
gc = "git"
)
func main() {
var err error
var cmdLog string
config := initConfig()
// Get command line arguments and store args as refs
f := parseFlags()
repopath := *f.Path
// Override the default slack channel if flag is passed in
if *f.SlackChannel != "" {
config.Slack.Channel = *f.SlackChannel
}
ref1, ref2 := parseArgs()
// Derive repo from the path
repo := path.Base(repopath)
// Determine that Git is installed
checkForGit()
// Auth with GitHub
client := authWithGitHub(config.Github.Token)
// define table output
tbl := table.New("PR", "Author", "Description", "URL")
// Check to ensure our path is a Git repo, if not exit!
checkPathIsGitRepo(repopath)
// Output what we are about to do
cmdLog = fmt.Sprintf("Determining merged branches between the following hashes: %s %s \n", ref1, ref2)
fmt.Println(cmdLog)
// Strict vs loose comparison based upon dev branch paradigm
comparison := "..."
if *f.Dev {
comparison = ".."
}
// Determine the merged branches between the two hashes
marg := fmt.Sprintf("%s%s%s", ref1, comparison, ref2)
c := exec.Command(gc, "-C", repopath, "log", "--merges", "--pretty=format:\"%s\"", marg)
out, err := c.StdoutPipe()
if err != nil {
log.Fatal(err)
}
err = c.Start()
if err != nil {
log.Fatal(err)
}
// iterate through matches, and pull out the issues id into a slice
var ids []int
s := bufio.NewScanner(out)
for s.Scan() {
t := s.Text()
r, _ := regexp.Compile("#([0-9]+)")
sm := r.FindStringSubmatch(t)
if len(sm) > 0 {
i, err := strconv.Atoi(sm[1])
if err == nil {
ids = append(ids, i)
}
}
}
err = c.Wait()
if err != nil {
log.Fatal(err)
}
// if the list is empty, error out
if len(ids) == 0 {
cmdLog = fmt.Sprintf("No merged PRs / GitHub issues found between: %s %s", ref1, ref2)
fmt.Println(cmdLog)
os.Exit(2)
}
// Get info from GitHub (experiment with concurrency)
pulls := processPullRequests(ids, client, config, repo)
for _, pull := range pulls {
i := fmt.Sprintf("#%d", *pull.Number)
u := fmt.Sprintf("@%s", *pull.User.Login)
t := fmt.Sprintf("%s", *pull.Title)
l := fmt.Sprintf("%s/%s/%s/pull/%d", gitHubRoot, config.Github.Org, repo, *pull.Number)
tbl.AddRow(i, u, t, l)
}
// Generate results output
output := new(bytes.Buffer)
tbl.WithWriter(output)
output.WriteString(fmt.Sprintf("%s: Merged GitHub PRs between the following refs: %s %s", strings.ToUpper(repo), ref1, ref2))
tbl.Print()
// Print the merge message to console and notifies slack
mergedMessage := output.String()
fmt.Println(mergedMessage)
if !*f.Test {
notifySlack(mergedMessage, config.Slack)
}
}
// Function to go to GetHub and process the passed pull requests
func processPullRequests(ids []int, client *github.Client, config Config, repo string) []*github.PullRequest {
var wg sync.WaitGroup
jobs := make(chan *github.PullRequest, len(ids))
wg.Add(len(ids))
// List of Pull Requests
pulls := []*github.PullRequest{}
// TODO: add backoff mechanism
for _, item := range ids {
go func(client *github.Client, org string, r string, id int) {
pr, _, err := client.PullRequests.Get(context.TODO(), org, r, id)
if err != nil {
log.Printf("Error retrieving pull request: %d, %v", id, err)
wg.Done()
return
}
jobs <- pr
wg.Done()
}(client, config.Github.Org, repo, item)
}
wg.Wait()
close(jobs)
for pull := range jobs {
pulls = append(pulls, pull)
}
return pulls
}
const usage = `Script can be used within a Git repository between any two hashes, tags, or branches
$ merged-prs <PREV> <NEW>
User should specify the older revision first ie. merging dev into master would necessitate that master is the older commit, and dev is the newer
$ merged-prs master dev
Determining merged branches between the following hashes: master dev
REPO: Merged GitHub PRs between the following refs: master dev
PR Author Description URL
#55 @lucas Typo 100 vs 1000 http://github.com/promiseofcake/foo/pull/55
#54 @lucas LRU Cache Store Results http://github.com/promiseofcake/foo/pull/54
`
func showUsage() {
fmt.Println(usage)
os.Exit(1)
}