-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrobot.go
73 lines (59 loc) · 1.91 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
package main
import (
"errors"
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"
"github.com/sirupsen/logrus"
)
const (
botName = "assign"
)
type iClient interface {
ListCollaborators(org, repo string) ([]sdk.ProjectMember, error)
AssignPR(owner, repo string, number int32, logins []string) error
UnassignPR(owner, repo string, number int32, logins []string) error
AssignGiteeIssue(org, repo string, number string, login string) error
UnassignGiteeIssue(org, repo string, number string, login string) error
CreateIssueComment(org, repo string, number string, comment string) error
CreatePRComment(org, repo string, number int32, comment string) error
UpdateIssue(owner, number string, param sdk.IssueUpdateParam) (sdk.Issue, 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.RegisterNoteEventHandler(bot.handleNoteEvent)
}
func (bot *robot) handleNoteEvent(e *sdk.NoteEvent, cfg libconfig.PluginConfig, log *logrus.Entry) error {
ew := giteeclient.NewNoteEventWrapper(e)
if !ew.IsCreatingCommentEvent() {
log.Debug("Event is not a creation of a comment, skipping.")
return nil
}
config, err := bot.getConfig(cfg)
if err != nil {
return err
}
bCfg := config.configFor(ew.GetOrgRep())
if bCfg == nil {
return nil
}
if h := newHandler(bot.cli, ew, bCfg, log); h != nil {
return h.handle()
}
return nil
}