forked from zengchen1024/robot-gitee-review-trigger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stats.go
120 lines (96 loc) · 2.56 KB
/
stats.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
package main
import (
"sort"
"time"
sdk "github.com/opensourceways/go-gitee/gitee"
"k8s.io/apimachinery/pkg/util/sets"
)
type reviewComment struct {
author string
comment string
t time.Time
}
type reviewStats struct {
pr *pullRequest
cfg reviewConfig
reviewers sets.String
}
func (rs reviewStats) StatReview(
comments []sdk.PullRequestComments,
startTime time.Time,
botName string,
) (reviewSummary, reviewResult) {
commands := rs.filterComments(comments, startTime, botName)
if len(commands) == 0 {
return reviewSummary{}, reviewResult{}
}
r := genReviewSummary(commands)
return r, genReviewResult(r, rs.pr.areAllFilesApproved, rs.cfg)
}
func (rs reviewStats) filterComments(comments []sdk.PullRequestComments, startTime time.Time, botName string) []reviewCommand {
isValidCmd := rs.genCheckCmdFunc()
newComments := rs.preTreatComments(comments, startTime, botName)
n := len(newComments)
done := map[string]bool{}
commands := make([]reviewCommand, 0, n)
for i := n - 1; i >= 0; i-- {
c := &newComments[i]
if done[c.author] {
continue
}
if cmd, _ := getReviewCommand(c.comment, c.author, isValidCmd); cmd != "" {
commands = append(commands, reviewCommand{command: cmd, author: c.author})
done[c.author] = true
}
}
return commands
}
// first. filter comments and omit each one
// which is before the pr code update time
// or which is not a reviewer
// or which is commented by bot
//
// second sort the comments by updated time in aesc
func (rs reviewStats) preTreatComments(comments []sdk.PullRequestComments, startTime time.Time, botName string) []reviewComment {
r := make([]reviewComment, 0, len(comments))
for i := range comments {
c := &comments[i]
if c.User == nil || c.User.Login == botName {
continue
}
author := normalizeLogin(c.User.Login)
if !rs.isReviewer(author) {
continue
}
ut, err := time.Parse(time.RFC3339, c.UpdatedAt)
if err != nil || ut.Before(startTime) {
continue
}
r = append(r, reviewComment{
author: author,
t: ut,
comment: c.Body,
})
}
sort.SliceStable(r, func(i, j int) bool {
return r[i].t.Before(r[j].t)
})
return r
}
func (rs reviewStats) genCheckCmdFunc() func(cmd, author string) bool {
prAuthor := rs.pr.prAuthor()
return func(cmd, author string) bool {
return canApplyCmd(
cmd,
prAuthor == author,
rs.pr.isApprover(author),
rs.cfg.AllowSelfApprove,
)
}
}
func (rs reviewStats) numberOfReviewers() int {
return len(rs.reviewers)
}
func (rs reviewStats) isReviewer(author string) bool {
return rs.reviewers.Has(author)
}