forked from minio/buzz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
github-pull-requests.go
111 lines (95 loc) · 2.85 KB
/
github-pull-requests.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
/*
* Buzz, (C) 2017 Minio, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package main
import (
"encoding/json"
"fmt"
"net/http"
"strings"
"github.com/google/go-github/github"
)
// GitPR - a single pull request
type GitPR struct {
Number int `json:"number"`
ID int64 `json:"id"`
Title string `json:"title"`
Labels []github.Label `json:"name"`
Sender string `json:"sender"`
Assignees string `json:"login"`
State string `json:"state"`
UpdatedAt int64 `json:"updated_at"`
Repo string `json:"repo_name"`
Link string `json:"html_url"`
ReviewState []ReviewState
}
// Retrieve the relevant pull request data from an owner/repo.
func getPullRequests(owner string, repo string) (pullRequests []GitPR, err error) {
opt := &github.PullRequestListOptions{
ListOptions: github.ListOptions{
PerPage: 50,
},
}
prs, _, err := buzzClient.PullRequests.List(ctx, owner, repo, opt)
if err != nil {
return nil, err
}
pullRequests = []GitPR{}
for _, elem := range prs {
pullRequest := GitPR{}
pullRequest.Number = *elem.Number
pullRequest.Title = *elem.Title
pullRequest.Sender = *elem.Head.User.Login
for _, assignee := range elem.Assignees {
pullRequest.Assignees += *assignee.Login + " "
}
pullRequest.UpdatedAt = elem.UpdatedAt.Unix()
pullRequest.Repo = repo
pullRequest.ID = *elem.ID
pullRequest.Link = *elem.HTMLURL
for _, label := range elem.Labels {
pullRequest.Labels = append(pullRequest.Labels, *label)
}
err, states := getReviewStatesForPR(owner, pullRequest.Repo, pullRequest.Number)
if err != nil {
return nil, err
}
pullRequest.ReviewState = states
pullRequests = append(pullRequests, pullRequest)
}
return pullRequests, nil
}
func getPRs(w http.ResponseWriter, req *http.Request) {
pullRequests := []GitPR{}
for _, locator := range config.RepoNames {
// It's in the format owner/repo.
parts := strings.SplitN(locator, "/", 2)
prs, err := getPullRequests(parts[0], parts[1])
if err != nil {
w.WriteHeader(501)
fmt.Fprint(w, err)
return
}
pullRequests = append(pullRequests, prs...)
}
js, err := json.Marshal(pullRequests)
if err != nil {
w.WriteHeader(501)
fmt.Fprint(w, err)
return
}
w.Header().Set("Content-Type", "application/json")
w.Write(js)
}