-
Notifications
You must be signed in to change notification settings - Fork 1
/
gogh.go
141 lines (130 loc) · 3.29 KB
/
gogh.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
package main
import (
"bufio"
"flag"
"fmt"
"log"
"os"
"path"
"strings"
"github.com/google/go-github/github"
"golang.org/x/oauth2"
)
var (
user *string
upstream *string
debug *bool
outdated *bool
issueComments *bool
)
func init() {
user = flag.String("user", os.Getenv("USER"), "Github username")
upstream = flag.String("upstream", "origin", "Github user or org for upstream fork")
debug = flag.Bool("debug", false, "Enable debug logging")
outdated = flag.Bool("outdated", false, "Show outdated comments")
issueComments = flag.Bool("issue_comments", false, "Show issue comments")
flag.Parse()
}
type tokenSource struct {
token *oauth2.Token
}
func (t *tokenSource) Token() (*oauth2.Token, error) {
return t.token, nil
}
func main() {
remotes, _ := getRemotes()
// shas, _ := getSHA()
branch, _ := getBranch()
if *debug {
log.Println("branch", branch)
//for _, sha := range shas {
//log.Println("SHA:", sha)
//}
}
var remote *Remote
for _, r := range remotes {
if r.LocalName == *upstream {
remote = r
break
}
}
if remote == nil {
log.Fatalln("Couldn't find upstream remote named", *upstream)
}
keyfile, err := os.Open(os.ExpandEnv("$HOME/.github.key"))
if err != nil {
log.Fatalln(err)
}
r := bufio.NewReader(keyfile)
key, err := r.ReadString('\n')
if err != nil {
log.Fatalln(err)
}
ts := &tokenSource{
&oauth2.Token{AccessToken: key},
}
tc := oauth2.NewClient(oauth2.NoContext, ts)
client := github.NewClient(tc)
data, _, _ := client.PullRequests.List(remote.User, remote.Repo, nil)
for _, pr := range data {
if *pr.Head.Ref == branch {
if *debug {
log.Println("Found the PR:", *pr.URL)
}
getCommentsForPullRequest(client, remote, pr)
os.Exit(0)
}
}
fmt.Println("Couldn't find Pull Request matching this branch")
os.Exit(1)
}
func getCommentsForPullRequest(client *github.Client, remote *Remote, pr github.PullRequest) {
comments, _, err := client.PullRequests.ListComments(remote.User, remote.Repo, *pr.Number, nil)
if err != nil {
log.Fatalln(err)
}
diff := getDiffFromURL(*pr.URL)
diffMap := processDiffIntoDiffMap(diff)
basePath, _ := getTopLevelPath()
if *debug {
log.Println("# Comments:", len(comments))
}
for _, comment := range comments {
p := basePath
if comment.Path != nil {
p = path.Join(basePath, *comment.Path)
}
pos := 1
if comment.Position != nil && comment.Path != nil {
if *debug {
fmt.Println(*comment.Path, *comment.Position)
fmt.Println(diffMap[*comment.Path])
}
if len(diffMap[*comment.Path]) == 0 {
continue
}
diffLine := diffMap[*comment.Path][*comment.Position]
fmt.Println(diffLine.Line)
pos = diffLine.RightIndex
} else if !*outdated {
continue
}
fmt.Printf("%s:%d:%s (@%s)\n", p, pos, *comment.Body, *comment.User.Login)
}
if *issueComments {
commit, _, err := client.Git.GetCommit(remote.User, remote.Repo, *pr.Head.SHA)
if err != nil {
log.Fatalln(err)
}
prComments, _, err := client.Issues.ListComments(remote.User, remote.Repo, *pr.Number, nil)
if err != nil {
log.Fatalln(err)
}
for _, comment := range prComments {
if commit.Author.Date.Before(*comment.UpdatedAt) {
body := strings.Replace(*comment.Body, "\r\n", "\n* ", -1)
fmt.Printf(":: (@%s) %s\n\n", *comment.User.Login, body)
}
}
}
}