forked from kubernetes-retired/contrib
/
issues.go
97 lines (83 loc) · 2.75 KB
/
issues.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
/*
Copyright 2015 The Kubernetes Authors All rights reserved.
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 issues
import (
"fmt"
github_util "k8s.io/contrib/mungegithub/github"
"github.com/golang/glog"
github_api "github.com/google/go-github/github"
"github.com/spf13/cobra"
)
// IssueMunger is the interface which all mungers must implement to register
type IssueMunger interface {
MungeIssue(config *github_util.Config, issue *github_api.Issue)
AddFlags(cmd *cobra.Command, config *github_util.Config)
Initialize(*github_util.Config) error
Name() string
}
var (
mungerMap = map[string]IssueMunger{}
mungers = []IssueMunger{}
)
// GetAllMungers returns a slice of all registered mungers. This list is
// completely independant of the mungers selected at runtime in --pr-mungers.
// This is all possible mungers.
func GetAllMungers() []IssueMunger {
out := []IssueMunger{}
for _, munger := range mungerMap {
out = append(out, munger)
}
return out
}
// InitializeMungers will call munger.Initialize() for all mungers
// requested in --issue-mungers
func InitializeMungers(requestedMungers []string, config *github_util.Config) error {
for _, name := range requestedMungers {
munger, found := mungerMap[name]
if !found {
return fmt.Errorf("couldn't find a munger named: %s", name)
}
if err := munger.Initialize(config); err != nil {
return err
}
mungers = append(mungers, munger)
}
return nil
}
// RegisterMunger should be called in `init()` by each munger to make itself
// available by name
func RegisterMunger(munger IssueMunger) error {
if _, found := mungerMap[munger.Name()]; found {
return fmt.Errorf("a munger with that name (%s) already exists", munger.Name())
}
mungerMap[munger.Name()] = munger
glog.Infof("Registered %#v at %s", munger, munger.Name())
return nil
}
func mungeIssue(config *github_util.Config, issue *github_api.Issue) error {
for _, munger := range mungers {
munger.MungeIssue(config, issue)
}
return nil
}
// MungeIssues is the main function which asks that each munger be called
// for each Issue
func MungeIssues(config *github_util.Config) error {
mfunc := func(issue *github_api.Issue) error {
return mungeIssue(config, issue)
}
if err := config.ForEachIssueDo([]string{}, mfunc); err != nil {
return err
}
return nil
}