forked from review-robot/robot-gitee-tide
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrobot.go
More file actions
169 lines (136 loc) · 4.46 KB
/
Copy pathrobot.go
File metadata and controls
169 lines (136 loc) · 4.46 KB
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
package main
import (
"fmt"
"regexp"
"github.com/opensourceways/community-robot-lib/config"
"github.com/opensourceways/community-robot-lib/giteeclient"
"github.com/opensourceways/community-robot-lib/robot-gitee-framework"
sdk "github.com/opensourceways/go-gitee/gitee"
"github.com/sirupsen/logrus"
"k8s.io/apimachinery/pkg/util/sets"
)
const botName = "tide"
var (
checkPRRe = regexp.MustCompile(`(?mi)^/check-pr\s*$`)
tideNotification = "@%s, This pr is not mergeable."
tideNotificationRe = regexp.MustCompile(fmt.Sprintf(tideNotification, "(.*)"))
)
type iClient interface {
CreatePRComment(owner, repo string, number int32, comment string) error
DeletePRComment(org, repo string, ID int32) error
ListPRComments(org, repo string, number int32) ([]sdk.PullRequestComments, error)
MergePR(owner, repo string, number int32, opt sdk.PullRequestMergePutParam) error
ListPROperationLogs(org, repo string, number int32) ([]sdk.OperateLog, error)
GetBot() (sdk.User, error)
UpdatePullRequest(org, repo string, number int32, param sdk.PullRequestUpdateParam) (sdk.PullRequest, error)
}
func newRobot(cli iClient, botName string) *robot {
return &robot{cli: cli, botName: botName}
}
type robot struct {
cli iClient
botName string
}
func (bot *robot) NewConfig() config.Config {
return &configuration{}
}
func (bot *robot) getConfig(cfg config.Config, org, repo string) (*botConfig, error) {
c, ok := cfg.(*configuration)
if !ok {
return nil, fmt.Errorf("can't convert to configuration")
}
if bc := c.configFor(org, repo); bc != nil {
return bc, nil
}
return nil, fmt.Errorf("no config for this repo:%s/%s", org, repo)
}
func (bot *robot) RegisterEventHandler(f framework.HandlerRegitster) {
f.RegisterPullRequestHandler(bot.handlePREvent)
f.RegisterNoteEventHandler(bot.handleNoteEvent)
}
func (bot *robot) handlePREvent(e *sdk.PullRequestEvent, c config.Config, log *logrus.Entry) error {
if e.GetPullRequest().GetState() != sdk.StatusOpen {
return nil
}
if sdk.GetPullRequestAction(e) != sdk.PRActionUpdatedLabel {
return nil
}
org, repo := e.GetOrgRepo()
// In order to avoid adding two or more comments in the concurrence of webhook,
// do pre-check first. Besides, this will enhance the user experience.
// Because if it doesn't leave a comment, developer has to comment /check-pr
// to get details.
return bot.handle(org, repo, e.GetPullRequest(), c, log, areAllLabelsReady)
}
func (bot *robot) handleNoteEvent(e *sdk.NoteEvent, c config.Config, log *logrus.Entry) error {
if !e.IsCreatingCommentEvent() || !e.IsPullRequest() {
return nil
}
if !checkPRRe.MatchString(e.GetComment().GetBody()) {
return nil
}
org, repo := e.GetOrgRepo()
if !e.GetPullRequest().GetMergeable() {
return bot.writeComment(
org, repo, e.GetPRNumber(), e.GetPRAuthor(),
" Because it conflicts to the target branch.",
)
}
return bot.handle(org, repo, e.GetPullRequest(), c, log, nil)
}
func (bot *robot) handle(
org, repo string,
pr *sdk.PullRequestHook,
c config.Config,
log *logrus.Entry,
preCheck func(labels sets.String, cfg *botConfig) bool,
) error {
cfg, err := bot.getConfig(c, org, repo)
if err != nil {
return err
}
if preCheck != nil && !preCheck(pr.LabelsToSet(), cfg) {
return nil
}
number := pr.GetNumber()
ops, err := bot.cli.ListPROperationLogs(org, repo, number)
if err != nil {
return err
}
if details := checkPRLabel(pr.LabelsToSet(), ops, cfg, log); details != "" {
return bot.writeComment(org, repo, number, pr.GetUser().GetLogin(), "\n\n"+details)
}
if pr.GetNeedTest() || pr.GetNeedReview() {
v := int32(0)
p := sdk.PullRequestUpdateParam{
AssigneesNumber: &v,
TestersNumber: &v,
}
if _, err := bot.cli.UpdatePullRequest(org, repo, number, p); err != nil {
return err
}
}
return bot.cli.MergePR(
org, repo, number,
sdk.PullRequestMergePutParam{
MergeMethod: string(cfg.getMergeMethod(pr.GetBase().GetRef())),
},
)
}
func (bot *robot) writeComment(org, repo string, number int32, prAuthor, c string) error {
_ = bot.deleteOldComments(org, repo, number)
return bot.cli.CreatePRComment(
org, repo, number,
fmt.Sprintf(tideNotification, prAuthor)+c,
)
}
func (bot *robot) deleteOldComments(org, repo string, number int32) error {
comments, err := bot.cli.ListPRComments(org, repo, number)
if err != nil {
return err
}
for _, c := range giteeclient.FindBotComment(comments, bot.botName, tideNotificationRe.MatchString) {
_ = bot.cli.DeletePRComment(org, repo, c.CommentID)
}
return nil
}