forked from kubernetes-retired/contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
check-labels.go
135 lines (114 loc) · 3.96 KB
/
check-labels.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
/*
Copyright 2015 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 mungers
import (
"fmt"
"os"
"k8s.io/contrib/mungegithub/features"
githubhelper "k8s.io/contrib/mungegithub/github"
"k8s.io/contrib/mungegithub/mungers/mungerutil"
"k8s.io/kubernetes/pkg/util/sets"
"k8s.io/kubernetes/pkg/util/yaml"
"bytes"
"github.com/golang/glog"
"github.com/google/go-github/github"
"github.com/spf13/cobra"
"io/ioutil"
)
type labelAccessor interface {
AddLabel(label *github.Label) error
GetLabels() ([]*github.Label, error)
}
// CheckLabelsMunger will check that the labels specified in the labels yaml file
// are created.
type CheckLabelsMunger struct {
LabelFilePath string
prevHash string
labelAccessor labelAccessor
features *features.Features
readFunc func() ([]byte, error)
}
func init() {
RegisterMungerOrDie(&CheckLabelsMunger{})
}
// Name is the name usable in --pr-mungers
func (c *CheckLabelsMunger) Name() string { return "check-labels" }
// RequiredFeatures is a slice of 'features' that must be provided.
func (c *CheckLabelsMunger) RequiredFeatures() []string { return []string{features.RepoFeatureName} }
// Initialize will initialize the munger.
func (c *CheckLabelsMunger) Initialize(config *githubhelper.Config, features *features.Features) error {
if len(c.LabelFilePath) == 0 {
glog.Fatalf("No --label-file= supplied, cannot check labels")
}
c.labelAccessor = config
c.features = features
c.readFunc = func() ([]byte, error) {
bytes, err := ioutil.ReadFile(c.LabelFilePath)
if err != nil {
return []byte{}, fmt.Errorf("Unable to read label file: %v", err)
}
return bytes, nil
}
if _, err := os.Stat(c.LabelFilePath); os.IsNotExist(err) {
return fmt.Errorf("Failed to stat the check label config: %v", err)
}
return nil
}
// EachLoop is called at the start of every munge loop
func (c *CheckLabelsMunger) EachLoop() error {
fileContents, err := c.readFunc()
if err != nil {
glog.Errorf("Failed to read the check label config: %v", err)
return err
}
hash := mungerutil.GetHash(fileContents)
if c.prevHash != hash {
// Get all labels from file.
fileLabels := map[string][]*github.Label{}
if err := yaml.NewYAMLToJSONDecoder(bytes.NewReader(fileContents)).Decode(&fileLabels); err != nil {
return fmt.Errorf("Failed to decode the check label config: %v", err)
}
// Get all labels from repository.
repoLabels, err := c.labelAccessor.GetLabels()
if err != nil {
return err
}
c.addMissingLabels(repoLabels, fileLabels["labels"])
c.prevHash = hash
}
return nil
}
// addMissingLabels will not remove any labels. It will add those which are present in the yaml file and not in
// the repository.
func (c *CheckLabelsMunger) addMissingLabels(repoLabels, fileLabels []*github.Label) {
repoLabelSet := sets.NewString()
for _, repoLabel := range repoLabels {
repoLabelSet.Insert(*repoLabel.Name)
}
// Compare against labels in local file.
for _, label := range fileLabels {
if !repoLabelSet.Has(*label.Name) {
err := c.labelAccessor.AddLabel(label)
if err != nil {
glog.Errorf("Error %s in adding label %s", err, *label.Name)
}
}
}
}
// AddFlags will add any request flags to the cobra `cmd`.
func (c *CheckLabelsMunger) AddFlags(cmd *cobra.Command, config *githubhelper.Config) {
cmd.Flags().StringVar(&c.LabelFilePath, "label-file", "", "Path from repository root to file containing"+
" list of labels")
}
// Munge is unused by this munger.
func (c *CheckLabelsMunger) Munge(obj *githubhelper.MungeObject) {}