-
Notifications
You must be signed in to change notification settings - Fork 20
/
git-open-pull.go
276 lines (248 loc) · 7.02 KB
/
git-open-pull.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
package main
import (
"context"
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"os/exec"
"runtime"
"strconv"
"strings"
"time"
"github.com/google/go-github/v60/github"
"github.com/jehiah/git-open-pull/internal/input"
"golang.org/x/oauth2"
)
func RenameBranch(ctx context.Context, branch string, issueNumber int) (string, error) {
branch = fmt.Sprintf("%s_%d", branch, issueNumber)
_, err := RunGit(ctx, "branch", "-m", branch)
if err != nil {
return "", err
}
return branch, nil
}
func SetupClient(ctx context.Context, s *Settings) *github.Client {
if s == nil {
panic("missing settings")
}
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: s.Token},
)
tc := oauth2.NewClient(ctx, ts)
client := github.NewClient(tc)
client.UserAgent = fmt.Sprintf("git-open-pull/%s (+http://github.com/jehiah/git-open-pull)", Version)
return client
}
// GetIssueNumber prompts to create a new issue, or confirmation of auto-detected issue number
func GetIssueNumber(ctx context.Context, client *github.Client, settings *Settings, detected int, interactive bool, title, description string, labels []string) (int, error) {
var issue int
if detected == 0 {
var err error
n := "c"
if interactive {
n, err = input.Ask("enter issue number (or 'c' to create)", "")
if err != nil {
return issue, err
}
}
switch n {
case "", "c", "C":
return NewIssue(ctx, client, settings, interactive, title, description, labels)
default:
return strconv.Atoi(n)
}
}
n, err := input.Ask(fmt.Sprintf("issue number [%d]", detected), "")
if err != nil {
log.Fatal(err)
}
if n == "" {
return detected, nil
}
return strconv.Atoi(n)
}
func main() {
description := flag.String("description-file", "", "Path to PR description file")
labels := flag.String("labels", "", "Comma separated PR Labels")
title := flag.String("title", "", "PR Title")
interactive := flag.Bool("interactive", true, "Toggles interactive mode")
version := flag.Bool("version", false, "Prints current version")
draft := flag.Bool("draft", false, "Open PR in draft mode")
flag.Parse()
if *version {
fmt.Printf("git-open-pull v%s %s\n", Version, runtime.Version())
os.Exit(0)
}
ctx := context.Background()
// Load and initialize settings
settings, err := LoadSettings(ctx)
if err != nil {
log.Fatal(err)
}
var labelSlice []string
if *labels != "" {
labelSlice = strings.Split(*labels, ",")
for idx := range labelSlice {
labelSlice[idx] = strings.TrimSpace(labelSlice[idx])
}
}
var descriptionString string
if *description != "" {
fileContent, err := ioutil.ReadFile(*description)
if err != nil {
log.Fatal(err)
}
descriptionString = string(fileContent)
}
branch, err := GitFeatureBranch(ctx)
if err != nil {
log.Fatal(err)
}
fmt.Printf("current branch %s\n", branch)
if branch == "master" {
yn, err := input.Ask("Are you sure you want to make a pull request from master? [y/N]", "")
if err != nil {
log.Fatal(err)
}
if yn != "y" && yn != "Y" {
os.Exit(1)
}
}
detectedIssueNumber := DetectIssueNumber(branch)
client := SetupClient(ctx, settings)
// create issue if needed
issueNumber, err := GetIssueNumber(ctx, client, settings, detectedIssueNumber, *interactive, *title, descriptionString, labelSlice)
if err != nil {
log.Fatal(err)
}
if issueNumber == 0 {
log.Fatal("expected issue number")
}
// Do we need/want to rename the branch?
if issueNumber != detectedIssueNumber {
if *interactive {
yn, err := input.Ask(fmt.Sprintf("rename branch to %s_%d [Y/n]", branch, issueNumber), "")
if err != nil {
log.Fatal(err)
}
switch yn {
case "", "y", "Y":
branch, err = RenameBranch(ctx, branch, issueNumber)
if err != nil {
log.Fatal(err)
}
case "n", "N":
default:
log.Fatalf("unknown response %q", yn)
}
} else {
branch, err = RenameBranch(ctx, branch, issueNumber)
if err != nil {
log.Fatal(err)
}
}
}
// confirm issue number is valid and issue is open
issue, _, err := client.Issues.Get(ctx, settings.BaseAccount, settings.BaseRepo, issueNumber)
if err != nil {
log.Fatalf("error verifying issue %d %s", issueNumber, err)
}
if *issue.State != "open" {
log.Fatalf("error: Issue %s/%s#%d is %s (%s)", settings.BaseAccount, settings.BaseRepo, issueNumber, *issue.State, *issue.Title)
}
fmt.Printf("pushing branch %s to %s\n", branch, settings.User)
_, err = RunGit(ctx, "push", "-u", settings.User, branch)
if err != nil {
log.Fatal(err)
}
// GitHub needs a variable amount of time before a new branch
// can be used to open a pull request. This is usually enough.
time.Sleep(2 * time.Second)
// check branch exists on remote
branches, _, err := client.Repositories.ListBranches(ctx, settings.User, settings.BaseRepo, &github.BranchListOptions{ListOptions: github.ListOptions{PerPage: 100}})
if err != nil {
log.Fatal(err)
}
var foundBranch bool
for _, b := range branches {
if *b.Name == branch {
foundBranch = true
}
}
if !foundBranch {
fmt.Printf("Error: branch %s does not exist in %s/%s\n", branch, settings.User, settings.BaseRepo)
if len(branches) > 1 {
fmt.Printf("valid branches are:")
for i, b := range branches {
if i > 0 {
fmt.Print(", ")
}
fmt.Printf("%s", *b.Name)
}
}
os.Exit(1)
}
fmt.Printf("Issue: %d (%s)\n", issueNumber, *issue.Title)
head := fmt.Sprintf("%s:%s", settings.User, branch)
fmt.Printf("pulling from %s into %s/%s branch %s\n", head, settings.BaseAccount, settings.BaseRepo, settings.BaseBranch)
if *interactive {
yn, err := input.Ask("confirm [y/n]", "")
if err != nil {
log.Fatal(err)
}
if strings.ToLower(yn) != "y" {
log.Fatal("exiting")
}
yn, err = input.Ask("Open as draft [Y/n]", "")
if err != nil {
log.Fatal(err)
}
switch yn {
case "", "Y", "y":
*draft = true
}
}
// convert Issue to PR
params := &github.NewPullRequest{
Issue: &issueNumber,
Base: &settings.BaseBranch,
Head: &head,
MaintainerCanModify: &settings.MaintainersCanModify,
Draft: draft,
}
_, _, err = client.PullRequests.Create(ctx, settings.BaseAccount, settings.BaseRepo, params)
if err != nil {
log.Fatal(err)
}
fmt.Printf("%s\n", issue.GetHTMLURL())
// set asignee (if needed) ?
if settings.Callback != "" {
// fetch the json of the current issue
tempFile, err := ioutil.TempFile("", fmt.Sprintf("issue-%d", issueNumber))
if err != nil {
log.Fatal(err)
}
defer os.Remove(tempFile.Name())
req, err := client.NewRequest("GET", fmt.Sprintf("/repos/%s/%s/pulls/%d", settings.BaseAccount, settings.BaseRepo, issueNumber), nil)
if err != nil {
log.Fatal(err)
}
resp, err := client.Do(ctx, req, tempFile)
tempFile.Sync()
tempFile.Close()
if err != nil {
log.Fatal(err)
}
if resp.StatusCode != 200 {
log.Fatalf("got unexpected response code %d", resp.StatusCode)
}
cmd := exec.CommandContext(ctx, settings.Callback, tempFile.Name())
out, err := cmd.CombinedOutput()
if err != nil {
log.Printf("error on callback: %s:\n %s", settings.Callback, out)
log.Fatal(err)
}
}
}