/
welcome.go
168 lines (142 loc) · 4.81 KB
/
welcome.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
/*
Copyright 2018 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// Package welcome implements a prow plugin to welcome new contributors
package welcome
import (
"bytes"
"fmt"
"html/template"
"strings"
"github.com/jenkins-x/go-scm/scm"
"github.com/sirupsen/logrus"
"github.com/jenkins-x/lighthouse/pkg/plugins"
"k8s.io/apimachinery/pkg/util/sets"
)
const (
pluginName = "welcome"
defaultWelcomeMessage = "Welcome @{{.AuthorLogin}}! It looks like this is your first PR to {{.Org}}/{{.Repo}} 🎉"
)
// PRInfo contains info used provided to the welcome message template
type PRInfo struct {
Org string
Repo string
AuthorLogin string
AuthorName string
}
func init() {
plugins.RegisterPlugin(
pluginName,
plugins.Plugin{
Description: "The welcome plugin posts a welcoming message when it detects a user's first contribution to a repo.",
ConfigHelpProvider: configHelp,
PullRequestHandler: handlePullRequest,
},
)
}
func configHelp(config *plugins.Configuration, enabledRepos []string) (map[string]string, error) {
welcomeConfig := map[string]string{}
for _, repo := range enabledRepos {
parts := strings.Split(repo, "/")
var messageTemplate string
switch len(parts) {
case 1:
messageTemplate = welcomeMessageForRepo(config, repo, "")
case 2:
messageTemplate = welcomeMessageForRepo(config, parts[0], parts[1])
default:
return nil, fmt.Errorf("invalid repo in enabledRepos: %q", repo)
}
welcomeConfig[repo] = fmt.Sprintf("The welcome plugin is configured to post using following welcome template: %s.", messageTemplate)
}
// The {WhoCanUse, Usage, Examples} fields are omitted because this plugin is not triggered with commands.
return welcomeConfig, nil
}
type scmProviderClient interface {
CreateComment(owner, repo string, number int, pr bool, comment string) error
FindPullRequestsByAuthor(owner, repo string, author string) ([]*scm.PullRequest, error)
}
type client struct {
SCMProviderClient scmProviderClient
Logger *logrus.Entry
}
func getClient(pc plugins.Agent) client {
return client{
SCMProviderClient: pc.SCMProviderClient,
Logger: pc.Logger,
}
}
func handlePullRequest(pc plugins.Agent, pre scm.PullRequestHook) error {
return handlePR(getClient(pc), pre, welcomeMessageForRepo(pc.PluginConfig, pre.Repo.Namespace, pre.Repo.Name))
}
func handlePR(c client, pre scm.PullRequestHook, welcomeTemplate string) error {
// Only consider newly opened PRs
if pre.Action != scm.ActionOpen {
return nil
}
// search for PRs from the author in this repo
org := pre.PullRequest.Base.Repo.Namespace
repo := pre.PullRequest.Base.Repo.Name
user := pre.PullRequest.Author.Login
issues, err := c.SCMProviderClient.FindPullRequestsByAuthor(org, repo, user)
if err != nil {
return err
}
// if there are no results, this is the first! post the welcome comment
if len(issues) == 0 || len(issues) == 1 && issues[0].Number == pre.PullRequest.Number {
// load the template, and run it over the PR info
parsedTemplate, err := template.New("welcome").Parse(welcomeTemplate)
if err != nil {
return err
}
var msgBuffer bytes.Buffer
err = parsedTemplate.Execute(&msgBuffer, PRInfo{
Org: org,
Repo: repo,
AuthorLogin: user,
AuthorName: pre.PullRequest.Author.Name,
})
if err != nil {
return err
}
// actually post the comment
return c.SCMProviderClient.CreateComment(org, repo, pre.PullRequest.Number, true, msgBuffer.String())
}
return nil
}
func welcomeMessageForRepo(config *plugins.Configuration, org, repo string) string {
opts := optionsForRepo(config, org, repo)
if opts.MessageTemplate != "" {
return opts.MessageTemplate
}
return defaultWelcomeMessage
}
// optionsForRepo gets the plugins.Welcome struct that is applicable to the indicated repo.
func optionsForRepo(config *plugins.Configuration, org, repo string) *plugins.Welcome {
fullName := fmt.Sprintf("%s/%s", org, repo)
// First search for repo config
for _, c := range config.Welcome {
if !sets.NewString(c.Repos...).Has(fullName) {
continue
}
return &c
}
// If you don't find anything, loop again looking for an org config
for _, c := range config.Welcome {
if !sets.NewString(c.Repos...).Has(org) {
continue
}
return &c
}
// Return an empty config, and default to defaultWelcomeMessage
return &plugins.Welcome{}
}