-
Notifications
You must be signed in to change notification settings - Fork 0
/
github.go
112 lines (100 loc) · 2.42 KB
/
github.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
package main
import (
"crypto/tls"
"encoding/json"
"fmt"
"log"
"net/http"
"time"
)
const (
githubURL = "https://api.github.com"
)
// GitHubRepository retrieves pull requests for given repositories.
type GitHubRepository struct {
URL string
Insecure bool
Token string
Repositories []string
Filter struct {
MinAge Duration
IgnoreAssigned bool
}
}
// PullRequests returns all pull requests of added repositories.
func (r *GitHubRepository) PullRequests() []*PullRequest {
baseURL := r.URL
if baseURL == "" {
baseURL = githubURL
}
client := http.Client{}
if r.Insecure {
// Skip verifying GitHub server certificate
client.Transport = &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
}
}
var pulls []*PullRequest
for _, n := range r.Repositories {
url := fmt.Sprintf("%s/repos/%s/pulls", baseURL, n)
pulls = append(pulls, r.pullRequests(&client, url)...)
}
return pulls
}
func (r *GitHubRepository) pullRequests(client *http.Client, url string) []*PullRequest {
req, err := http.NewRequest("GET", url, nil)
if err != nil {
log.Fatal(err)
}
if r.Token != "" {
req.Header.Set("Authorization", "token "+r.Token)
}
log.Println("getting pull requests from", url)
resp, err := client.Do(req)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
log.Fatalf("unexpected github status code: %d", resp.StatusCode)
}
decoder := json.NewDecoder(resp.Body)
pulls := make([]gitHubPulls, 0, 5)
err = decoder.Decode(&pulls)
if err != nil {
log.Fatal(err)
}
// log.Printf("%+v\n", pulls)
pr := make([]*PullRequest, 0, len(pulls))
for i := range pulls {
p := &pulls[i]
if r.shouldIgnore(p) {
log.Printf("skipped pull request %s", p.HTMLURL)
continue
}
pr = append(pr, &PullRequest{
URL: p.HTMLURL,
Title: p.Title,
Author: p.User.Login,
CreatedTime: p.CreatedAt,
})
}
return pr
}
func (r *GitHubRepository) shouldIgnore(p *gitHubPulls) bool {
return (r.Filter.MinAge.Duration > 0 && time.Now().Sub(p.CreatedAt) < r.Filter.MinAge.Duration) ||
(r.Filter.IgnoreAssigned && p.Assignee.Login != "")
}
type gitHubPulls struct {
HTMLURL string `json:"html_url"`
Title string `json:"title"`
User struct {
Login string `json:"login"`
} `json:"user"`
Assignee struct {
Login string `json:"login"`
} `json:"assignee"`
CreatedAt time.Time `json:"created_at"`
}