forked from review-robot/robot-gitee-label
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrobot.go
More file actions
107 lines (82 loc) · 2.9 KB
/
Copy pathrobot.go
File metadata and controls
107 lines (82 loc) · 2.9 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
package main
import (
"fmt"
"github.com/opensourceways/community-robot-lib/config"
framework "github.com/opensourceways/community-robot-lib/robot-gitee-framework"
"github.com/opensourceways/community-robot-lib/utils"
sdk "github.com/opensourceways/go-gitee/gitee"
"github.com/sirupsen/logrus"
)
const botName = "label"
type iClient interface {
GetRepoLabels(owner, repo string) ([]sdk.Label, error)
GetIssueLabels(org, repo, number string) ([]sdk.Label, error)
GetPRLabels(org, repo string, number int32) ([]sdk.Label, error)
AddIssueLabel(org, repo, number, label string) error
RemoveIssueLabels(org, repo, number string, label []string) error
AddMultiIssueLabel(org, repo, number string, label []string) error
AddMultiPRLabel(org, repo string, number int32, label []string) error
AddPRLabel(org, repo string, number int32, label string) error
RemovePRLabel(org, repo string, number int32, label string) error
RemovePRLabels(org, repo string, number int32, labels []string) error
CreateRepoLabel(org, repo, label, color string) error
CreatePRComment(org, repo string, number int32, comment string) error
CreateIssueComment(org, repo string, number string, comment string) error
IsCollaborator(owner, repo, login string) (bool, error)
}
func newRobot(cli iClient) *robot {
return &robot{cli: cli}
}
type robot struct {
cli iClient
}
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(p framework.HandlerRegitster) {
p.RegisterPullRequestHandler(bot.handlePREvent)
p.RegisterNoteEventHandler(bot.handleNoteEvent)
}
func (bot *robot) handlePREvent(e *sdk.PullRequestEvent, pc config.Config, log *logrus.Entry) error {
org, repo := e.GetOrgRepo()
cfg, err := bot.getConfig(pc, org, repo)
if err != nil {
return err
}
merr := utils.NewMultiErrors()
if err = bot.handleClearLabel(e, cfg); err != nil {
merr.AddError(err)
}
commits := uint(e.GetPullRequest().GetCommits())
if err = bot.handleSquashLabel(e, commits, cfg.SquashConfig); err != nil {
merr.AddError(err)
}
return merr.Err()
}
func (bot *robot) handleNoteEvent(e *sdk.NoteEvent, pc config.Config, log *logrus.Entry) error {
if !e.IsCreatingCommentEvent() {
log.Debug("Event is not a creation of a comment, skipping.")
return nil
}
org, repo := e.GetOrgRepo()
cfg, err := bot.getConfig(pc, org, repo)
if err != nil {
return err
}
toAdd, toRemove := getMatchedLabels(e.GetComment().GetBody())
if len(toAdd) == 0 && len(toRemove) == 0 {
log.Debug("invalid comment, skipping.")
return nil
}
return bot.handleLabels(e, toAdd, toRemove, cfg, log)
}