-
Notifications
You must be signed in to change notification settings - Fork 6
/
robot.go
199 lines (160 loc) · 4.86 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
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package main
import (
"fmt"
"strings"
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/opensourceways/community-robot-lib/utils"
"github.com/sirupsen/logrus"
)
const (
botName = "welcome"
welcomeMessage = `
Hi ***%s***, welcome to the %s Community.
I'm the Bot here serving you. You can find the instructions on how to interact with me at **[Here](%s)**.
If you have any questions, please contact the SIG: [%s](https://gitee.com/openeuler/community/tree/master/sig/%s), and any of the maintainers: @%s`
)
type iClient interface {
CreatePRComment(owner, repo string, number int32, comment string) error
CreateIssueComment(owner, repo string, number string, comment string) error
GetPathContent(org, repo, path, ref string) (sdk.Content, error)
CreateRepoLabel(org, repo, label, color string) error
GetRepoLabels(owner, repo string) ([]sdk.Label, error)
AddPRLabel(org, repo string, number int32, label string) error
AddIssueLabel(org, repo, number, label string) error
ListCollaborators(org, repo string) ([]sdk.ProjectMember, 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, 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 libplugin.HandlerRegitster) {
p.RegisterIssueHandler(bot.handleIssueEvent)
p.RegisterPullRequestHandler(bot.handlePREvent)
}
func (bot *robot) handlePREvent(e *sdk.PullRequestEvent, pc libconfig.PluginConfig, log *logrus.Entry) error {
action := giteeclient.GetPullRequestAction(e)
if action != giteeclient.PRActionOpened {
return nil
}
org, repo := e.GetRepository().GetOwnerAndRepo()
pr := e.GetPullRequest()
number := pr.Number
cfg, err := bot.getConfig(pc, org, repo)
if err != nil {
return err
}
return bot.handle(
org, repo, pr.GetUser().GetLogin(), cfg, log,
func(c string) error {
return bot.cli.CreatePRComment(org, repo, number, c)
},
func(label string) error {
return bot.cli.AddPRLabel(org, repo, number, label)
},
)
}
func (bot *robot) handleIssueEvent(e *sdk.IssueEvent, pc libconfig.PluginConfig, log *logrus.Entry) error {
ew := giteeclient.NewIssueEventWrapper(e)
if giteeclient.StatusOpen != ew.GetAction() {
return nil
}
org, repo := ew.GetOrgRep()
cfg, err := bot.getConfig(pc, org, repo)
if err != nil {
return err
}
author := ew.GetIssueAuthor()
number := ew.GetIssueNumber()
return bot.handle(
org, repo, author, cfg, log,
func(c string) error {
return bot.cli.CreateIssueComment(org, repo, number, c)
},
func(label string) error {
return bot.cli.AddIssueLabel(org, repo, number, label)
},
)
}
func (bot *robot) handle(
org, repo, author string,
cfg *botConfig, log *logrus.Entry,
addMsg, addLabel func(string) error,
) error {
sigName, comment, err := bot.genComment(org, repo, author, cfg)
if err != nil {
return err
}
mErr := utils.NewMultiErrors()
if err := addMsg(comment); err != nil {
mErr.AddError(err)
}
label := fmt.Sprintf("sig/%s", sigName)
if err := bot.createLabelIfNeed(org, repo, label); err != nil {
log.Errorf("create repo label:%s, err:%s", label, err.Error())
}
if err := addLabel(label); err != nil {
mErr.AddError(err)
}
return mErr.Err()
}
func (bot robot) genComment(org, repo, author string, cfg *botConfig) (string, string, error) {
sigName, err := bot.getSigOfRepo(repo, cfg)
if err != nil {
return "", "", err
}
if sigName == "" {
return "", "", fmt.Errorf("cant get sig name of repo: %s/%s", org, repo)
}
maintainers, err := bot.getMaintainers(org, repo)
if err != nil {
return "", "", err
}
return sigName, fmt.Sprintf(
welcomeMessage, author, cfg.CommunityName, cfg.CommandLink,
sigName, sigName, strings.Join(maintainers, " , @"),
), nil
}
func (bot *robot) getMaintainers(org, repo string) ([]string, error) {
v, err := bot.cli.ListCollaborators(org, repo)
if err != nil {
return nil, err
}
r := make([]string, 0, len(v))
for i := range v {
p := v[i].Permissions
if p != nil && (p.Admin || p.Push) {
r = append(r, v[i].Login)
}
}
return r, nil
}
func (bot *robot) createLabelIfNeed(org, repo, label string) error {
repoLabels, err := bot.cli.GetRepoLabels(org, repo)
if err != nil {
return err
}
for _, v := range repoLabels {
if v.Name == label {
return nil
}
}
return bot.cli.CreateRepoLabel(org, repo, label, "")
}