-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrobot.go
124 lines (99 loc) · 3.32 KB
/
robot.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
package main
import (
"errors"
"fmt"
sdk "gitee.com/openeuler/go-gitee/gitee"
libconfig "github.com/opensourceways/community-robot-lib/config"
"github.com/opensourceways/community-robot-lib/giteeclient"
libplugin "github.com/opensourceways/community-robot-lib/giteeplugin"
libutils "github.com/opensourceways/community-robot-lib/utils"
"github.com/sirupsen/logrus"
)
const botName = "checkpr"
type iClient interface {
UpdatePullRequest(org, repo string, number int32, param sdk.PullRequestUpdateParam) (sdk.PullRequest, error)
GetGiteePullRequest(org, repo string, number int32) (sdk.PullRequest, error)
GetPRCommits(org, repo string, number int32) ([]sdk.PullRequestCommits, error)
AddPRLabel(org, repo string, number int32, label string) error
RemovePRLabel(org, repo string, number int32, label string) error
}
func newRobot(cli iClient) *robot {
return &robot{cli: cli}
}
type robot struct {
cli iClient
}
func (bot *robot) NewPluginConfig() libconfig.PluginConfig {
return &configuration{}
}
func (bot *robot) getConfig(cfg libconfig.PluginConfig) (*configuration, error) {
if c, ok := cfg.(*configuration); ok {
return c, nil
}
return nil, errors.New("can't convert to configuration")
}
func (bot *robot) RegisterEventHandler(p libplugin.HandlerRegitster) {
p.RegisterPullRequestHandler(bot.handlePREvent)
}
func (bot *robot) handlePREvent(e *sdk.PullRequestEvent, cfg libconfig.PluginConfig, log *logrus.Entry) error {
action := giteeclient.GetPullRequestAction(e)
if action == giteeclient.PRActionClosed {
return nil
}
config, err := bot.getConfig(cfg)
if err != nil {
return err
}
prInfo := giteeclient.GetPRInfoByPREvent(e)
pc := config.configFor(prInfo.Org, prInfo.Repo)
if pc == nil {
return fmt.Errorf("no %s plugin config for this repo:%s/%s", botName, prInfo.Org, prInfo.Repo)
}
mr := libutils.NewMultiErrors()
if err := bot.removeMinNumReviewerAndTester(prInfo, pc); err != nil {
mr.AddError(err)
}
if action == giteeclient.PRActionOpened || action == giteeclient.PRActionChangedSourceBranch {
if err := bot.handleCheckCommits(prInfo, pc); err != nil {
mr.AddError(err)
}
}
return mr.Err()
}
func (bot *robot) removeMinNumReviewerAndTester(prInfo giteeclient.PRInfo, cfg *botConfig) error {
if !cfg.needResetReviewerAndTester() {
return nil
}
org := prInfo.Org
repo := prInfo.Repo
number := prInfo.Number
pr, err := bot.cli.GetGiteePullRequest(org, repo, number)
if err != nil {
return err
}
if pr.AssigneesNumber == 0 && pr.TestersNumber == 0 {
return nil
}
changeNum := int32(0)
param := sdk.PullRequestUpdateParam{AssigneesNumber: &changeNum, TestersNumber: &changeNum}
_, err = bot.cli.UpdatePullRequest(org, repo, int32(number), param)
return err
}
func (bot *robot) handleCheckCommits(prInfo giteeclient.PRInfo, cfg *botConfig) error {
if !cfg.needCheckCommits() {
return nil
}
commits, err := bot.cli.GetPRCommits(prInfo.Org, prInfo.Repo, prInfo.Number)
if err != nil {
return err
}
exceeded := uint(len(commits)) > cfg.CommitsThreshold
hasSquashLabel := prInfo.HasLabel(cfg.SquashCommitLabel)
if exceeded && !hasSquashLabel {
return bot.cli.AddPRLabel(prInfo.Org, prInfo.Repo, prInfo.Number, cfg.SquashCommitLabel)
}
if !exceeded && hasSquashLabel {
return bot.cli.RemovePRLabel(prInfo.Org, prInfo.Repo, prInfo.Number, cfg.SquashCommitLabel)
}
return nil
}