/
gh.go
68 lines (62 loc) · 1.56 KB
/
gh.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
package cmd
import (
exec "golang.org/x/sys/execabs"
"strconv"
"strings"
"github.com/rs/zerolog/log"
"github.com/thoas/go-funk"
)
type PullRequest struct {
Title string
Number int
AuthorBranch string
State string
}
func ListGHPullRequests() []*PullRequest {
if !GHCliExistsAndLoggedIn() {
return []*PullRequest{
{AuthorBranch: "Either no PRs or GH CLI not installed"},
}
}
msg, err := execCommand("gh", "pr", "list", "--limit=200", "--state=all").CombinedOutput()
if err != nil {
log.Debug().Err(err).Send()
}
prsraw := strings.Split(strings.TrimSpace(string(msg)), "\n")
return funk.Map(prsraw, func(raw string) *PullRequest {
fields := strings.Split(raw, "\t")
number, _ := strconv.Atoi(fields[0])
return &PullRequest{
Number: number,
Title: fields[1],
AuthorBranch: fields[2],
State: fields[3],
}
}).([]*PullRequest)
}
func checkoutPullRequest(pr int) {
if !GHCliExistsAndLoggedIn() {
return
}
_, err := execCommand("gh", "pr", "checkout", strconv.Itoa(pr)).CombinedOutput()
if err != nil {
log.Debug().Err(err).Send()
}
}
func GHCliExistsAndLoggedIn() bool {
_, err := exec.LookPath("gh")
if err != nil {
log.Debug().Msg("GitHub CLI doesn't exist,")
return false
}
//msg , err := execCommand("gh", "auth", "status").CombinedOutput()
//if err != nil {
// log.Debug().Msg(string(msg) + err.Error())
// return false
//}
//if strings.Contains(string(msg), "You are not logged into any GitHub") {
// log.Debug().Msg("not logged into to Github")
// return false
//}
return true
}