forked from zengchen1024/robot-gitee-review-trigger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
note_event.go
105 lines (83 loc) · 2.54 KB
/
note_event.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
package main
import (
"fmt"
"strings"
"github.com/opensourceways/community-robot-lib/giteeclient"
sdk "github.com/opensourceways/go-gitee/gitee"
"github.com/sirupsen/logrus"
)
func (bot *robot) processNoteEvent(e *sdk.NoteEvent, cfg *botConfig, log *logrus.Entry) error {
if !e.IsPullRequest() || !e.IsPROpen() {
return nil
}
if e.IsCreatingCommentEvent() && e.GetCommenter() != bot.botName {
if cmds := parseReviewCommand(e.GetComment().GetBody()); len(cmds) > 0 {
return bot.handleReviewComment(e, cfg, log)
}
}
return bot.handleCIStatusComment(e, cfg, log)
}
func (bot *robot) handleReviewComment(e *sdk.NoteEvent, cfg *botConfig, log *logrus.Entry) error {
org, repo := e.GetOrgRepo()
owner, err := bot.genRepoOwner(org, repo, e.GetPRBaseRef(), cfg.Owner, log)
if err != nil {
return err
}
prInfo := prInfoOnNoteEvent{e}
pr, err := bot.genPullRequest(prInfo, getAssignees(e.GetPullRequest()), owner)
if err != nil {
return err
}
stats := &reviewStats{
pr: &pr,
cfg: cfg.Review,
reviewers: owner.AllReviewers(),
}
cmd, validReview := bot.isValidReview(cfg.commandsEndpoint, stats, e, log)
if !validReview {
return nil
}
info, err := bot.getReviewInfo(prInfo)
if err != nil {
return err
}
canReview := cfg.CI.NoCI || stats.pr.info.hasLabel(cfg.CI.LabelForCIPassed)
pa := PostAction{
c: bot.client,
cfg: cfg,
owner: owner,
log: log,
pr: &pr,
isStartingReview: canReview,
}
oldTips := info.reviewGuides(bot.botName)
rs, rr := info.doStats(stats, bot.botName)
return pa.do(oldTips, cmd, rs, rr, bot.botName)
}
func (bot *robot) isValidReview(
commandEndpoint string, stats *reviewStats, e *sdk.NoteEvent, log *logrus.Entry,
) (string, bool) {
commenter := normalizeLogin(e.GetCommenter())
cmd, invalidCmd := getReviewCommand(e.GetComment().GetBody(), commenter, stats.genCheckCmdFunc())
validReview := cmd != "" && stats.isReviewer(commenter)
if !validReview {
log.Infof(
"It can't handle note event, because cmd(%s) is empty or commenter(%s) is not a reviewer. There are %d reviewers.",
cmd, commenter, stats.numberOfReviewers(),
)
}
if invalidCmd != "" {
info := stats.pr.info
org, repo := info.getOrgAndRepo()
s := fmt.Sprintf(
"You can't comment `/%s`. Please see the [*Command Usage*](%s) to get detail.",
strings.ToLower(invalidCmd),
commandEndpoint,
)
bot.client.CreatePRComment(
org, repo, info.getNumber(),
giteeclient.GenResponseWithReference(e, s),
)
}
return cmd, validReview
}