-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.go
260 lines (215 loc) · 5.96 KB
/
app.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
// Package main is the main package for the app
package main
import (
"context"
"fmt"
"sort"
"strings"
"time"
"github.com/bakkerme/github-pr-manager/githubservice"
)
type config struct {
GithubUsername string
GithubAccessToken string
Frameless bool
OrgFilter *string
}
// App struct
type App struct {
ctx context.Context
gh githubservice.GithubService
cfg config
}
// NewApp creates a new App application struct
func NewApp(gh githubservice.GithubService, cfg config) *App {
return &App{
gh: gh,
cfg: cfg,
}
}
// startup is called when the app starts. The context is saved
// so we can call the runtime methods
func (a *App) startup(ctx context.Context) {
a.ctx = ctx
}
// GetAssignedPullRequestsForUser returns the pull requests to review for the given user
func (a *App) GetAssignedPullRequestsForUser() ([]githubservice.PullRequest, error) {
issues, err := a.GetAssignedReviews()
if err != nil {
return nil, err
}
pullRequests, err := processPullRequests(a.gh, issues, sortKeyUpdated, orderDirectionDesc, a.cfg.OrgFilter)
if err != nil {
return []githubservice.PullRequest{}, err
}
return pullRequests, nil
}
// GetReviewedPullRequestsForUser returns the pull requests to review for the given user
func (a *App) GetReviewedPullRequestsForUser() ([]githubservice.PullRequest, error) {
issues, err := a.GetCompletedReviews()
if err != nil {
return nil, err
}
pullRequests, err := processPullRequests(a.gh, issues, sortKeyUpdated, orderDirectionDesc, a.cfg.OrgFilter)
if err != nil {
return []githubservice.PullRequest{}, err
}
return pullRequests, nil
}
// GetAssignedReviews returns the assigned reviews for user
func (a *App) GetAssignedReviews() ([]githubservice.Issue, error) {
assignedReviews, err := a.gh.GetAssignedReviews(a.cfg.GithubUsername)
if err != nil {
return nil, err
}
return assignedReviews, nil
}
// GetCompletedReviews returns the completed reviews for user
func (a *App) GetCompletedReviews() ([]githubservice.Issue, error) {
completedReviews, err := a.gh.GetCompletedReviews(a.cfg.GithubUsername)
if err != nil {
return nil, err
}
return completedReviews, nil
}
// GetPullRequest returns the pull request for the given owner, repo and number
func (a *App) GetPullRequest(owner, repo string, number int) (*githubservice.PullRequest, error) {
pr, err := a.gh.GetPullRequest(owner, repo, number)
if err != nil {
return nil, err
}
return &pr, nil
}
// IsFrameless returns if frameless window is turned on
func (a *App) IsFrameless() bool {
return a.cfg.Frameless
}
type userAndRepo struct {
user string
repo string
}
func getUserAndRepoFromRepoURL(url string) (*userAndRepo, error) {
withoutDomain := strings.Replace(url, "https://api.github.com/repos/", "", 1)
urlParts := strings.Split(withoutDomain, "/")
if len(urlParts) != 2 {
return nil, fmt.Errorf("invalid repo url: %s", url)
}
return &userAndRepo{
user: urlParts[0],
repo: urlParts[1],
}, nil
}
func getReviewStateForUsers(reviews []githubservice.PullRequestReview) []githubservice.UserReviewState {
userToReview := map[string]githubservice.UserReviewState{}
for _, review := range reviews {
if reviewEntry, ok := userToReview[*review.User.Login]; ok {
reviewEntry.ReviewState = review.State
} else {
userToReview[*review.User.Login] = githubservice.UserReviewState{
User: review.User,
ReviewState: review.State,
}
}
}
reviewsForUser := []githubservice.UserReviewState{}
for _, review := range userToReview {
reviewsForUser = append(reviewsForUser, review)
}
return reviewsForUser
}
type orderDirection string
type sortKey string
const (
orderDirectionAsc orderDirection = "asc"
orderDirectionDesc orderDirection = "desc"
sortKeyUpdated sortKey = "updated"
sortKeyCreated sortKey = "created"
)
func processPullRequests(
gh githubservice.GithubService,
issues []githubservice.Issue,
sortfield sortKey,
order orderDirection,
orgFilter *string,
) ([]githubservice.PullRequest, error) {
type prAndError struct {
pr *githubservice.PullRequest
err error
}
var ch = make(chan prAndError)
pullRequests := []githubservice.PullRequest{}
for _, issue := range issues {
go func(ch chan (prAndError), issue githubservice.Issue) {
issueNo := *issue.Number
userRepo, err := getUserAndRepoFromRepoURL(*issue.RepositoryURL)
if err != nil {
ch <- prAndError{err: err}
return
}
if orgFilter != nil && userRepo.user != *orgFilter {
ch <- prAndError{
pr: nil,
}
return
}
pr, err := gh.GetPullRequest(userRepo.user, userRepo.repo, issueNo)
if err != nil {
ch <- prAndError{err: err}
return
}
reviews, err := gh.GetPullRequestReviews(userRepo.user, userRepo.repo, issueNo)
if err != nil {
ch <- prAndError{err: err}
return
}
pr.Reviews = reviews
reviewState := getReviewStateForUsers(reviews)
pr.ReviewStateForUser = reviewState
comments, err := gh.GetIssueComments(userRepo.user, userRepo.repo, issueNo)
if err != nil {
ch <- prAndError{err: err}
return
}
pr.Comments = comments
ch <- prAndError{
pr: &pr,
}
}(ch, issue)
}
for i := 0; i < len(issues); i++ {
pr := <-ch
if pr.err != nil {
return nil, pr.err
}
if pr.pr != nil {
pullRequests = append(pullRequests, *pr.pr)
}
}
sort.Slice(
pullRequests,
func(p, q int) bool {
currPR := pullRequests[p]
otherPR := pullRequests[q]
var fieldToCompare *time.Time
var otherFieldToCompare *time.Time
switch sortfield {
case sortKeyUpdated:
fieldToCompare = currPR.UpdatedAt
otherFieldToCompare = otherPR.UpdatedAt
break
case sortKeyCreated:
fieldToCompare = currPR.CreatedAt
otherFieldToCompare = otherPR.CreatedAt
break
}
if fieldToCompare == nil || otherFieldToCompare == nil {
return false
}
if order == orderDirectionAsc {
return fieldToCompare.Before(*otherFieldToCompare)
}
return fieldToCompare.After(*otherFieldToCompare)
},
)
return pullRequests, nil
}