-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
177 lines (146 loc) · 5.03 KB
/
main.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
package internal
import (
"context"
"encoding/json"
"errors"
"fmt"
"os"
"strings"
"time"
"github.com/google/go-github/v33/github"
"github.com/prince-chrismc/conan-center-index-pending-review/v2/internal/format"
"github.com/prince-chrismc/conan-center-index-pending-review/v2/internal/stats"
"github.com/prince-chrismc/conan-center-index-pending-review/v2/internal/validate"
"github.com/prince-chrismc/conan-center-index-pending-review/v2/pkg/pending_review"
"golang.org/x/oauth2"
)
// Run the analysis
func Run(token string, dryRun bool) error {
tokenService := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: token},
)
context := context.Background()
client := pending_review.NewClient(oauth2.NewClient(context, tokenService))
// Get Rate limit information
rateLimit, _, err := client.RateLimits(context)
if err != nil {
fmt.Printf("Problem getting rate limit information %v\n", err)
os.Exit(1)
}
// We have not exceeded the limit so we can continue
fmt.Printf("Limit: %d \nRemaining %d \n", rateLimit.Limit, rateLimit.Remaining)
repo, _, err := client.Repository.GetSummary(context, "conan-io", "conan-center-index")
if err != nil {
fmt.Printf("Problem in getting repository information %v\n", err)
os.Exit(1)
}
fmt.Printf("%+v\n-----\n", repo)
var stats stats.Stats
var retval []*pending_review.PullRequestSummary
opt := &github.PullRequestListOptions{
Sort: "created",
Direction: "asc",
ListOptions: github.ListOptions{
Page: 0,
PerPage: 100,
},
}
for {
pulls, resp, err := client.PullRequests.List(context, "conan-io", "conan-center-index", opt)
if err != nil {
fmt.Printf("Problem getting pull request list %v\n", err)
os.Exit(1)
}
out, s := gatherReviewStatus(context, client, pulls)
retval = append(retval, out...)
stats.Add(s)
if resp.NextPage == 0 {
break
}
opt.Page = resp.NextPage
}
bytes, err := json.MarshalIndent(retval, "", " ")
if err != nil {
fmt.Printf("Problem formating result to JSON %v\n", err)
os.Exit(1)
}
// https://github.com/prince-chrismc/conan-center-index-pending-review/issues/5#issuecomment-754112342
isDifferent, err := validateContentIsDifferent(context, client, string(bytes))
if err != nil {
fmt.Printf("Problem getting original issue content %v\n", err)
os.Exit(1)
}
if !isDifferent {
fmt.Println("the obtained content is identical to the new result.")
return nil // The published results are the same, no need to update the table.
}
commentBody := `## :sparkles: Summary of Pull Requests Pending Review!
### :ballot_box_with_check: Selection Criteria:
- There has been at least one approval (at any point)
- No reviews and commited to in the last 24hrs
- No labels with exception to "bump version" and "docs"
#### :world_map: Legend
:new: - Adding a recipe which does not yet exist
:arrow_up: - Version bump <sup>[1]</sup>
:memo: - Modification to an existing recipe
:green_book: - Documentation change <sup>[1]</sup>
:warning: - The merge commit status does **not** indicate success <sup>[2]</sup>
<sup>[1]</sup>: _closely_ matches the label
<sup>[2]</sup>: (only displayed when ready to merge)` +
format.UnderReview(retval) + format.ReadyToMerge(retval) + format.Statistics(stats) +
"\n\n<details><summary>Raw JSON data</summary>\n\n```json\n" + string(bytes) + "\n```\n\n</details>"
if dryRun {
fmt.Println(commentBody)
return nil
}
_, _, err = client.Issues.Edit(context, "prince-chrismc", "conan-center-index-pending-review", 1, &github.IssueRequest{
Body: github.String(commentBody),
})
if err != nil {
fmt.Printf("Problem editing issue %v\n", err)
os.Exit(1)
}
return nil
}
func validateContentIsDifferent(context context.Context, client *pending_review.Client, expected string) (bool, error) {
issue, _, err := client.Issues.Get(context, "prince-chrismc", "conan-center-index-pending-review", 1)
if err != nil {
return false, err
}
content := issue.GetBody()
rawStart := strings.Index(content, "```json\n")
rawEnd := strings.Index(content, "\n```\n")
obtained := content[rawStart+len("```json\n") : rawEnd]
return obtained != expected, nil
}
func gatherReviewStatus(context context.Context, client *pending_review.Client, prs []*pending_review.PullRequest) ([]*pending_review.PullRequestSummary, stats.Stats) {
var stats stats.Stats
var out []*pending_review.PullRequestSummary
for _, pr := range prs {
stats.Age.Append(time.Now().Sub(pr.GetCreatedAt()))
stats.Open++
if pr.GetDraft() {
stats.Draft++
continue // Let's skip these
}
valid := validate.OnlyAcceptableLabels(pr.Labels, &stats)
if !valid {
continue
}
review, _, err := client.PullRequest.GetReviewSummary(context, "conan-io", "conan-center-index", pr)
if errors.Is(err, pending_review.ErrNoReviews) || errors.Is(err, pending_review.ErrInvalidChange) {
continue
} else if err != nil {
fmt.Printf("Problem getting list of reviews %v\n", err)
os.Exit(1)
}
if review.Summary.IsApproved() {
stats.Merge++
} else {
stats.Review++
}
fmt.Printf("%+v\n", review)
out = append(out, review)
}
return out, stats
}