-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub.go
More file actions
72 lines (66 loc) · 1.74 KB
/
Copy pathgithub.go
File metadata and controls
72 lines (66 loc) · 1.74 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
package main
import (
"encoding/json"
"fmt"
"os/exec"
"strings"
"time"
)
// PR is an open pull request authored by the current user.
type PR struct {
Number int
Title string
URL string
RepoNWO string // repository nameWithOwner, e.g. "acme/web"
CreatedAt time.Time
UpdatedAt time.Time
IsDraft bool
}
type ghPR struct {
Number int `json:"number"`
Title string `json:"title"`
URL string `json:"url"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
IsDraft bool `json:"isDraft"`
Repository struct {
NameWithOwner string `json:"nameWithOwner"`
} `json:"repository"`
}
// FetchPRs returns every open pull request authored by the authenticated user
// across GitHub, via the gh CLI. Callers filter these down to local repos.
func FetchPRs() ([]PR, error) {
cmd := exec.Command("gh", "search", "prs",
"--author=@me", "--state=open",
"--json", "number,title,url,createdAt,updatedAt,repository,isDraft",
"--limit", "300",
)
out, err := cmd.Output()
if err != nil {
if ee, ok := err.(*exec.ExitError); ok {
msg := strings.TrimSpace(string(ee.Stderr))
if msg == "" {
msg = err.Error()
}
return nil, fmt.Errorf("gh search prs: %s", msg)
}
return nil, fmt.Errorf("running gh (is it installed and authenticated?): %w", err)
}
var raw []ghPR
if err := json.Unmarshal(out, &raw); err != nil {
return nil, fmt.Errorf("parsing gh output: %w", err)
}
prs := make([]PR, 0, len(raw))
for _, r := range raw {
prs = append(prs, PR{
Number: r.Number,
Title: r.Title,
URL: r.URL,
RepoNWO: r.Repository.NameWithOwner,
CreatedAt: r.CreatedAt,
UpdatedAt: r.UpdatedAt,
IsDraft: r.IsDraft,
})
}
return prs, nil
}