forked from abhinababasu/devops
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrepo.go
186 lines (155 loc) · 4.86 KB
/
repo.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
package main
// Docs
// https://docs.microsoft.com/en-us/rest/api/azure/devops/git/pull%20requests/get%20pull%20requests?view=azure-devops-server-rest-4.1
import (
"fmt"
"net/url"
"sort"
"strconv"
"strings"
"time"
az "github.com/benmatselby/go-azuredevops/azuredevops"
)
type AzureDevopsRepo struct {
client *az.Client
Repo Repository
PullRequests []PullRequest
// if an operation resulted in an error, it should be stored here
// so that it can be displayed
err error
}
type PullRequestsResponse struct {
PullRequests []PullRequest `json:"value"`
Count int `json:"count"`
}
type PullRequest struct {
ID int `json:"pullRequestId,omitempty"`
Title string `json:"title"`
Description string `json:"description"`
Status string `json:"status"`
Created string `json:"creationDate"`
CreatedBy User `json:"createdBy"`
ClosedDate time.Time `json:closedDate`
Repo az.PullRequestRepo `json:"repository"`
URL string `json:"url"`
RemoteURL string `json:"remoteUrl"`
Reviewers []User `json:"reviewers"`
}
type ReviewerStat struct {
Name string
Count int
}
// Repository represents a repository used by a build definition
type Repository struct {
ID string `json:"id,omitempty"`
Type string `json:"type,omitempty"`
Name string `json:"name,omitempty"`
URL string `json:"url,omitempty"`
RootFolder string `json:"root_folder"`
Properties map[string]interface{} `json:"properties"`
Clean string `json:"clean"`
DefaultBranch string `json:"default_branch"`
CheckoutSubmodules bool `json:"checkout_submodules"`
RemoteUrl string `json:"remoteUrl"`
}
type User struct {
Vote int `json:"vote,omitempty"`
ID string `json:"id"`
DisplayName string `json:"displayName"`
UniqueName string `json:"uniqueName"`
IsAadIdentity bool `json:"isAadIdentity"`
IsContainer bool `json:"isContainer"`
ImageUrl string `json:imageUrl`
}
func NewRepo(account, project, token, repoName string) (r *AzureDevopsRepo) {
r = &AzureDevopsRepo{}
r.client = constructClientFromConfig(account, project, token)
URL := fmt.Sprintf(
"_apis/git/repositories/%s?api-version=4.1",
url.PathEscape(repoName),
)
var azrepo Repository
request, err := r.client.NewRequest("GET", URL, nil)
if err != nil {
r.err = err
return
}
_, err = r.client.Execute(request, &azrepo)
if err != nil {
r.err = err
}
r.Repo = azrepo
return
}
func (r *AzureDevopsRepo) Refresh(count int) {
var errs []error
if err := r.loadPullRequests(count); err != nil {
errs = append(errs, err)
}
if len(errs) != 0 {
r.err = fmt.Errorf("Error(s) occurred: %v", errs)
}
return
}
func (r *AzureDevopsRepo) GetPullRequestReviewsByUser(count int) ([]ReviewerStat, int) {
Info.Printf("Processing %v completed PRs", count)
r.Refresh(count)
prs := r.PullRequests
Info.Println("PRs from", prs[len(prs)-1].ClosedDate)
// Iterate and create a map of reviewers[review-count]
review := make(map[string]int)
for _, pr := range prs {
for _, rv := range pr.Reviewers {
// filter for specific user and ensure we do not count PR creater approving their own PR
if !strings.Contains(rv.DisplayName, "AzLinux SAP HANA RP Devs") && rv.Vote != 0 && rv.DisplayName != pr.CreatedBy.DisplayName {
review[rv.DisplayName]++
}
}
}
// Sort the PRs by review count, by stuffing into a slice
max := 0
var reviewerStat []ReviewerStat
for k, v := range review {
reviewerStat = append(reviewerStat, ReviewerStat{k, v})
if v > max {
max = v
}
}
sort.Slice(reviewerStat, func(i, j int) bool {
return reviewerStat[i].Count > reviewerStat[j].Count
})
return reviewerStat, max
}
func (r *AzureDevopsRepo) loadPullRequests(count int) error {
params := url.Values{}
params.Add("searchCriteria.repositoryId", r.Repo.ID)
params.Add("searchCriteria.status", "completed")
params.Add("$top", strconv.Itoa(count))
URL := fmt.Sprintf(
"/_apis/git/pullrequests?%s&%s",
"api-version=4.1",
params.Encode(),
)
request, err := r.client.NewRequest("GET", URL, nil)
if err != nil {
return err
}
var response PullRequestsResponse
_, err = r.client.Execute(request, &response)
if err != nil {
return err
}
r.PullRequests = response.PullRequests
return nil
}
func constructClientFromConfig(account, project, token string) *az.Client {
return az.NewClient(account, project, token)
}
func containsUser(name string, Users ...User) bool {
for _, user := range Users {
if user.DisplayName == name || user.UniqueName == name {
return true
}
}
return false
}