-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
210 lines (179 loc) · 6.12 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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package main
import (
"fmt"
"github.com/codegangsta/cli"
"github.com/google/go-github/github"
"github.com/nlopes/slack"
"io/ioutil"
"log"
"os"
"os/exec"
"strconv"
"strings"
)
func gitRun(args ...string) error {
cmd := exec.Command("git", args...)
output, err := cmd.CombinedOutput()
if len(output) > 0 {
log.Println(string(output))
}
return err
}
func postReviewMessageOnSlack(caseNumber string, defendantName string) (string, string, error) {
api := slack.New(Config.SlackToken)
params := slack.PostMessageParameters{}
courtroomRepo := "https://github.com/" + Config.Owner + "/" + Config.Repo + "/pull/" + caseNumber
attachment := slack.Attachment{
Pretext: "Case " + caseNumber,
Text: fmt.Sprintf("Defendant's Name: *%s*\nCase: %s", defendantName, courtroomRepo),
MarkdownIn: []string{"text", "pretext"},
}
params.Attachments = []slack.Attachment{attachment}
params.Markdown = true
params.AsUser = true
msg := fmt.Sprintf(`@channel All rise! Members of the jury, you are instructed to review %s's case.
As jurors you are not to be swayed by sympathy. Be insightful though.`, defendantName)
channelID, timestamp, err := api.PostMessage(Config.SlackChannel, msg, params)
return channelID, timestamp, err
}
func open(c *cli.Context) {
var err error
courtroomRepo := "git@github.com:" + Config.Owner + "/" + Config.Repo + ".git"
branch := c.GlobalString("branch")
targetRepo := c.Args().Get(0)
defendant := c.Args().Get(1)
if targetRepo == "" || defendant == "" {
log.Fatal("USAGE: courtroom open [repo url] [defendant]")
}
if strings.Contains(defendant, " ") {
log.Fatal("The defendant's name should also be a valid branch name. Sorry.")
}
tempDir, err := ioutil.TempDir("/tmp", "courtroom-"+defendant)
if err != nil {
log.Fatal("Could not open temp directory")
}
log.Println("Cloning", targetRepo, "branch", branch, "into", tempDir)
if gitRun("clone", "-b", branch, targetRepo, tempDir) != nil {
log.Fatal("Could not clone repository into ", tempDir)
}
if os.Chdir(tempDir) != nil {
log.Fatal("Could not enter cloned repository ", tempDir)
}
if gitRun("remote", "add", "courtroom", courtroomRepo) != nil {
log.Fatal("Could not add remote")
}
if gitRun("fetch", "courtroom") != nil {
log.Fatal("Could not fetch target repo ", courtroomRepo)
}
if gitRun("merge", "courtroom/master") != nil {
log.Fatal("Could not rebase from master branch of ", courtroomRepo)
}
if gitRun("push", "courtroom", branch+":"+defendant) != nil {
log.Fatal("Could not add branch ", defendant, " to ", courtroomRepo)
}
log.Println("Added branch ", defendant, " to ", courtroomRepo)
canonicalRepoName := Config.Owner + "/" + Config.Repo
title := defendant + "'s case"
head := defendant
base := "master"
body := "Auto-generated pull request from benchlabs/bailiff. For code review purposes only - do not merge."
pullRequest := github.NewPullRequest{
Title: &title,
Head: &head,
Base: &base,
Body: &body,
}
pr, _, err := GithubClient.PullRequests.Create(Config.Owner, Config.Repo, &pullRequest)
if err != nil {
log.Fatal("Could not create new pull request in ", canonicalRepoName, err)
}
log.Println("Created new pull request for branch", defendant, "in", canonicalRepoName)
channelID, timestamp, err := postReviewMessageOnSlack(strconv.Itoa(*pr.Number), defendant)
if err != nil {
log.Fatal("Could not post review message on Slack ", canonicalRepoName, err)
}
log.Println("Message successfully posted to channel %s at %s", channelID, timestamp)
}
func hired(c *cli.Context) {
defendant := c.Args().Get(0)
if defendant == "" {
log.Fatal("USAGE: bailiff hired [defendant]")
}
opt := &github.PullRequestListOptions{
Head: Config.Owner + ":" + defendant,
}
pullRequests, _, err := GithubClient.PullRequests.List(Config.Owner, Config.Repo, opt)
if len(pullRequests) == 0 || err != nil {
log.Fatal("Could not list pull requests: ", err)
}
prNumber := *pullRequests[0].Number
log.Println("Found pull request", prNumber, "for defendant", defendant)
comments, _, err := GithubClient.Issues.ListComments(Config.Owner, Config.Repo, prNumber, nil)
if err != nil {
log.Fatal("Could not list comments for issue", prNumber, err)
}
for i := 0; i < len(comments); i++ {
_, err = GithubClient.Issues.DeleteComment(Config.Owner, Config.Repo, *comments[i].ID)
if err != nil {
log.Fatal("Could not delete comment", err)
}
}
log.Println("Deleted comments in issue", prNumber)
prComments, _, err := GithubClient.PullRequests.ListComments(Config.Owner, Config.Repo, prNumber, nil)
if err != nil {
log.Fatal("Could not list comments for pull request", prNumber, err)
}
for i := 0; i < len(prComments); i++ {
_, err = GithubClient.PullRequests.DeleteComment(Config.Owner, Config.Repo, *prComments[i].ID)
if err != nil {
log.Fatal("Could not delete comment", err)
}
}
log.Println("Deleted comments in pull request", prNumber)
log.Println("Deleted all comments for defendant", defendant, "in pull request", prNumber)
closed := "closed"
thisPr := &pullRequests[0]
thisPr.State = &closed
_, _, err = GithubClient.PullRequests.Edit(Config.Owner, Config.Repo, prNumber, thisPr)
if err != nil {
log.Fatal("Could not close pull request", prNumber, err)
}
log.Print("Closed pull request")
branch := "heads/" + defendant
_, err = GithubClient.Git.DeleteRef(Config.Owner, Config.Repo, branch)
if err != nil {
log.Fatal("Could not delete branch", defendant, err)
}
log.Print("Deleted branch ", defendant)
}
func main() {
app := cli.NewApp()
app.Name = "bailiff"
app.Usage = "Send a defendant to your courtroom."
app.Authors = []cli.Author{
{Name: "jeffling", Email: "jeff.ummu@gmail.com"},
{Name: "abylaw", Email: "andrea@bench.co"},
}
app.Commands = []cli.Command{
{
Name: "open",
Aliases: []string{"o"},
Usage: "[targetRepo] [defendant] - Open a new case",
Action: open,
},
{
Name: "hired",
Aliases: []string{"hi"},
Usage: "[defendant] - Delete all comments on defendant's case",
Action: hired,
},
}
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "branch, b",
Value: "master",
Usage: "Defendant's branch to be pulled into court",
},
}
app.Run(os.Args)
}