forked from kubernetes/test-infra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdateconfig.go
140 lines (111 loc) · 3.5 KB
/
updateconfig.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
/*
Copyright 2017 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 updateconfig
import (
"fmt"
"github.com/sirupsen/logrus"
"k8s.io/test-infra/prow/github"
"k8s.io/test-infra/prow/kube"
"k8s.io/test-infra/prow/plugins"
)
const (
pluginName = "config-updater"
)
func init() {
plugins.RegisterPullRequestHandler(pluginName, handlePullRequest)
}
type githubClient interface {
CreateComment(owner, repo string, number int, comment string) error
GetPullRequestChanges(org, repo string, number int) ([]github.PullRequestChange, error)
GetFile(org, repo, filepath, commit string) ([]byte, error)
}
type kubeClient interface {
ReplaceConfigMap(name string, config kube.ConfigMap) (kube.ConfigMap, error)
}
func handlePullRequest(pc plugins.PluginClient, pre github.PullRequestEvent) error {
configFile := pc.PluginConfig.ConfigUpdater.ConfigFile
pluginFile := pc.PluginConfig.ConfigUpdater.PluginFile
return handle(pc.GitHubClient, pc.KubeClient, pc.Logger, pre, configFile, pluginFile)
}
func handleConfig(gc githubClient, kc kubeClient, org, repo, commit, configFile string) error {
content, err := gc.GetFile(org, repo, configFile, commit)
if err != nil {
return err
}
c := kube.ConfigMap{
Metadata: kube.ObjectMeta{
Name: "config",
},
Data: map[string]string{
"config": string(content),
},
}
_, err = kc.ReplaceConfigMap("config", c)
return err
}
func handlePlugin(gc githubClient, kc kubeClient, org, repo, commit, pluginFile string) error {
content, err := gc.GetFile(org, repo, pluginFile, commit)
if err != nil {
return err
}
c := kube.ConfigMap{
Metadata: kube.ObjectMeta{
Name: "plugins",
},
Data: map[string]string{
"plugins": string(content),
},
}
_, err = kc.ReplaceConfigMap("plugins", c)
return err
}
func handle(gc githubClient, kc kubeClient, log *logrus.Entry, pre github.PullRequestEvent, configFile, pluginFile string) error {
// Only consider newly merged PRs
if pre.Action != github.PullRequestActionClosed {
return nil
}
pr := pre.PullRequest
if !pr.Merged || pr.MergeSHA == nil {
return nil
}
org := pr.Base.Repo.Owner.Login
repo := pr.Base.Repo.Name
// Process change to prow/config.yaml and prow/plugin.yaml
changes, err := gc.GetPullRequestChanges(org, repo, pr.Number)
if err != nil {
return err
}
var msg string
for _, change := range changes {
if change.Filename == configFile {
if err := handleConfig(gc, kc, org, repo, *pr.MergeSHA, configFile); err != nil {
return err
}
msg += fmt.Sprintf("I updated Prow config for you!")
} else if change.Filename == pluginFile {
if err := handlePlugin(gc, kc, org, repo, *pr.MergeSHA, pluginFile); err != nil {
return err
}
if msg != "" {
msg += "\n--------------------------\n"
}
msg += fmt.Sprintf("I updated Prow plugins config for you!")
}
}
if msg != "" {
if err := gc.CreateComment(org, repo, pr.Number, plugins.FormatResponseRaw(pr.Body, pr.HTMLURL, pr.User.Login, msg)); err != nil {
return fmt.Errorf("comment err: %v", err)
}
}
return nil
}