forked from jenkins-x/test-infra
/
welcome.go
122 lines (102 loc) · 3.33 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
/*
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"
"github.com/sirupsen/logrus"
"k8s.io/test-infra/prow/github"
"k8s.io/test-infra/prow/pluginhelp"
"k8s.io/test-infra/prow/plugins"
)
const (
pluginName = "welcome"
)
// PRInfo contains info used provided to the welcome message template
type PRInfo struct {
Org string
Repo string
AuthorLogin string
AuthorName string
}
func init() {
plugins.RegisterPullRequestHandler(pluginName, handlePullRequest, helpProvider)
}
func helpProvider(config *plugins.Configuration, enabledRepos []string) (*pluginhelp.PluginHelp, error) {
// The {WhoCanUse, Usage, Examples} fields are omitted because this plugin is not triggered with commands.
return &pluginhelp.PluginHelp{
Description: "The welcome plugin posts a welcoming message when it detects a user's first contribution to a repo.",
Config: map[string]string{
"": fmt.Sprintf(
"The welcome plugin is configured to post using following welcome template: %s.",
config.Welcome.MessageTemplate,
),
},
},
nil
}
type githubClient interface {
CreateComment(owner, repo string, number int, comment string) error
FindIssues(query, sort string, asc bool) ([]github.Issue, error)
}
type client struct {
GitHubClient githubClient
Logger *logrus.Entry
}
func getClient(pc plugins.PluginClient) client {
return client{
GitHubClient: pc.GitHubClient,
Logger: pc.Logger,
}
}
func handlePullRequest(pc plugins.PluginClient, pre github.PullRequestEvent) error {
return handlePR(getClient(pc), pre, pc.PluginConfig.Welcome.MessageTemplate)
}
func handlePR(c client, pre github.PullRequestEvent, welcomeTemplate string) error {
// Only consider newly opened PRs
if pre.Action != github.PullRequestActionOpened {
return nil
}
// search for PRs from the author in this repo
org := pre.PullRequest.Base.Repo.Owner.Login
repo := pre.PullRequest.Base.Repo.Name
user := pre.PullRequest.User.Login
query := fmt.Sprintf("is:pr repo:%s/%s author:%s", org, repo, user)
issues, err := c.GitHubClient.FindIssues(query, "", false)
if err != nil {
return err
}
// if there is exactly one result, this is the first! post the welcome comment
if len(issues) == 1 {
// 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.User.Name,
})
if err != nil {
return err
}
// actually post the comment
return c.GitHubClient.CreateComment(org, repo, pre.PullRequest.Number, msgBuffer.String())
}
return nil
}