-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathgit-open-pull.go
More file actions
362 lines (323 loc) · 9.57 KB
/
Copy pathgit-open-pull.go
File metadata and controls
362 lines (323 loc) · 9.57 KB
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
package main
import (
"context"
_ "embed"
"flag"
"fmt"
"log"
"os"
"os/exec"
"runtime"
"strconv"
"strings"
"time"
"github.com/google/go-github/v60/github"
"github.com/jehiah/agentdetection"
"github.com/jehiah/git-open-pull/internal/input"
"golang.org/x/oauth2"
)
//go:embed SKILL.md
var skillDoc string
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)
}
}
if interactive {
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)
}
return detected, nil
}
func printUsage(settings *Settings) {
out := flag.CommandLine.Output()
fmt.Fprintln(out, "git-open-pull creates an issue, renames the local branch to include that issue number, pushes the renamed branch and finally converts the issue into a pull request against the renamed branch.")
fmt.Fprintln(out, "Functionally similar to 'gh pr create'.")
if settings != nil && settings.User != "" && settings.BaseAccount != "" && settings.BaseRepo != "" {
fmt.Fprintf(out, "By default, code is pushed to %s/%s and the pull request targets %s/%s branch %s.\n", settings.User, settings.BaseRepo, settings.BaseAccount, settings.BaseRepo, settings.BaseBranch)
}
fmt.Fprintln(out)
if settings != nil {
if hints := settings.RequiredHints(); len(hints) > 0 {
fmt.Fprintln(out, "Required configuration missing:")
for _, h := range hints {
fmt.Fprintf(out, " - %s\n", h)
}
fmt.Fprintln(out)
}
}
if agentdetection.IsAgent() {
fmt.Fprintln(out, "Agent Hint: run --list-labels first to inspect the valid repository labels before passing --labels.")
fmt.Fprintln(out, "Do not push code prior to running, as the branch will be renamed to include the issue number.")
fmt.Fprintln(out, "Run --skill for full agent usage documentation, or redirect to a file: git-open-pull --skill > SKILL.md")
fmt.Fprintln(out)
}
fmt.Fprintf(out, "Usage of %s:\n", os.Args[0])
flag.PrintDefaults()
}
func main() {
ctx := context.Background()
preSettings, _ := readSettingsConfig(ctx)
flag.Usage = func() {
printUsage(preSettings)
}
description := flag.String("description-file", "", "Path to PR description file")
listLabels := flag.Bool("list-labels", false, "List available issue labels and exit")
skill := flag.Bool("skill", false, "Print agent skill documentation and exit")
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", true, "Open PR in draft mode (non-interactive only)")
flag.Parse()
if *version {
fmt.Printf("git-open-pull v%s %s\n", Version, runtime.Version())
os.Exit(0)
}
if *skill {
fmt.Print(skillDoc)
return
}
var settings *Settings
var err error
if *interactive {
settings, err = LoadSettings(ctx)
if err != nil {
log.Fatal(err)
}
} else {
settings, err = readSettingsConfig(ctx)
if err != nil {
log.Fatal(err)
}
if hints := settings.RequiredHints(); len(hints) > 0 {
for _, h := range hints {
fmt.Fprintln(os.Stderr, h)
}
os.Exit(1)
}
}
client := SetupClient(ctx, settings)
if *listLabels {
labels, err := Labels(ctx, client, settings)
if err != nil {
log.Fatal(err)
}
for _, label := range labels {
fmt.Println(label)
}
return
}
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 := os.ReadFile(*description)
if err != nil {
log.Fatalf("error reading description file: %v", err)
}
descriptionString = string(fileContent)
}
// Validate flag combinations
if !*interactive && *description != "" && *title == "" {
log.Fatal("--title is required when using --description-file with --interactive=false")
}
branch, err := GitFeatureBranch(ctx)
if err != nil {
log.Fatal(err)
}
fmt.Printf("current branch %s\n", branch)
switch branch {
case "main", "master":
yn, err := input.Ask(fmt.Sprintf("Are you sure you want to make a pull request from %s? [y/N]", branch), "")
if err != nil {
log.Fatal(err)
}
if yn != "y" && yn != "Y" {
os.Exit(1)
}
}
detectedIssueNumber := DetectIssueNumber(branch)
// 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")
}
}
if *interactive {
yn, err := input.Ask("Open as draft [Y/n]", "")
if err != nil {
log.Fatal(err)
}
switch yn {
case "", "Y", "y":
*draft = true
case "n", "N":
*draft = false
}
}
// 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 := os.CreateTemp("", 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)
}
}
}