forked from zengchen1024/robot-gitee-review-trigger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ci.go
130 lines (99 loc) · 2.66 KB
/
ci.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
package main
import (
"fmt"
"github.com/opensourceways/go-gitee/gitee"
"github.com/sirupsen/logrus"
)
type ciConfig struct {
// NoCI is the tag which indicates the repo is not set CI.
NoCI bool `json:"no_ci,omitempty"`
Job *jobConfig `json:"job,omitempty"`
// NumberOfTestCases is the number of test cases for PR
NumberOfTestCases int `json:"number_of_test_cases,omitempty"`
// LabelForCIPassed is the label name for org/repos indicating
// the CI test cases have passed
LabelForCIPassed string `json:"label_for_ci_passed,omitempty"`
}
func (c *ciConfig) setDefault() {}
func (c *ciConfig) validate() error {
if c == nil {
return nil
}
if c.NoCI {
return nil
}
if c.NumberOfTestCases <= 0 {
return fmt.Errorf("number_of_test_cases must be begger than 0")
}
if c.LabelForCIPassed == "" {
return fmt.Errorf("missing label_for_ci_passed")
}
if c.Job == nil {
return fmt.Errorf("missing job")
}
return c.Job.validate()
}
func canHandleCIEvent(e *gitee.NoteEvent, cfg ciConfig) (bool, error) {
if cfg.NoCI {
return false, nil
}
return cfg.Job.isCISuccess(e.GetComment().GetBody(), cfg.NumberOfTestCases)
}
func (bot *robot) handleCIStatusComment(e *gitee.NoteEvent, cfg *botConfig, log *logrus.Entry) error {
if b, err := canHandleCIEvent(e, cfg.CI); !b {
return err
}
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
}
info, err := bot.getReviewInfo(prInfo)
if err != nil {
return err
}
stats := &reviewStats{
pr: &pr,
cfg: cfg.Review,
reviewers: owner.AllReviewers(),
}
rs, r := info.doStats(stats, bot.botName)
if rs.IsEmpty() {
return bot.readyToReview(prInfo, cfg, log)
}
pa := PostAction{
c: bot.client,
cfg: cfg,
owner: owner,
log: log,
pr: &pr,
isStartingReview: true,
}
return pa.do(info.reviewGuides(bot.botName), "", rs, r, bot.botName)
}
type prInfoOnNoteEvent struct {
e *gitee.NoteEvent
}
func (pr prInfoOnNoteEvent) getOrgAndRepo() (string, string) {
return pr.e.GetOrgRepo()
}
func (pr prInfoOnNoteEvent) getNumber() int32 {
return pr.e.GetPRNumber()
}
func (pr prInfoOnNoteEvent) getTargetBranch() string {
return pr.e.GetPRBaseRef()
}
func (pr prInfoOnNoteEvent) hasLabel(l string) bool {
return pr.e.GetPRLabelSet().Has(l)
}
func (pr prInfoOnNoteEvent) getAuthor() string {
return pr.e.GetPRAuthor()
}
func (pr prInfoOnNoteEvent) getHeadSHA() string {
return pr.e.GetPRHeadSha()
}