forked from kubernetes-retired/contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mungegithub.go
141 lines (122 loc) · 4.19 KB
/
mungegithub.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
/*
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 main
import (
"fmt"
"os"
"path/filepath"
"time"
"k8s.io/contrib/mungegithub/features"
github_util "k8s.io/contrib/mungegithub/github"
"k8s.io/contrib/mungegithub/mungers"
"k8s.io/contrib/mungegithub/reports"
utilflag "k8s.io/kubernetes/pkg/util/flag"
"github.com/golang/glog"
"github.com/spf13/cobra"
"k8s.io/contrib/mungegithub/mungers/fsm"
"k8s.io/contrib/mungegithub/mungers/mungerutil"
)
var (
_ = fmt.Print
)
type mungeConfig struct {
github_util.Config
MinIssueNumber int
PRMungersList []string
IssueReportsList []string
Once bool
Period time.Duration
StateMachineEnabled bool
features.Features
}
func addMungeFlags(config *mungeConfig, cmd *cobra.Command) {
cmd.Flags().BoolVar(&config.Once, "once", false, "If true, run one loop and exit")
cmd.Flags().BoolVar(&config.StateMachineEnabled, "state-machine-enabled", false, "If true, run the state machine after all mungers are run.")
cmd.Flags().StringSliceVar(&config.PRMungersList, "pr-mungers", []string{}, "A list of pull request mungers to run")
cmd.Flags().StringSliceVar(&config.IssueReportsList, "issue-reports", []string{}, "A list of issue reports to run. If set, will run the reports and exit.")
cmd.Flags().DurationVar(&config.Period, "period", 10*time.Minute, "The period for running mungers")
}
func doMungers(config *mungeConfig) error {
for {
nextRunStartTime := time.Now().Add(config.Period)
glog.Infof("Running mungers")
config.NextExpectedUpdate(nextRunStartTime)
config.Features.EachLoop()
mungers.EachLoop()
if err := config.ForEachIssueDo(mungers.MungeIssue); err != nil {
glog.Errorf("Error munging PRs: %v", err)
}
if config.StateMachineEnabled {
if err := config.ForEachIssueDo(fsm.ComputeState); err != nil {
glog.Errorf("Error computing state: %v", err)
}
}
config.ResetAPICount()
if config.Once {
break
}
if nextRunStartTime.After(time.Now()) {
sleepDuration := nextRunStartTime.Sub(time.Now())
glog.Infof("Sleeping for %v\n", sleepDuration)
time.Sleep(sleepDuration)
} else {
glog.Infof("Not sleeping as we took more than %v to complete one loop\n", config.Period)
}
}
return nil
}
func main() {
config := &mungeConfig{}
root := &cobra.Command{
Use: filepath.Base(os.Args[0]),
Short: "A program to add labels, check tests, and generally mess with outstanding PRs",
RunE: func(_ *cobra.Command, _ []string) error {
glog.Info(mungerutil.PrettyString(config))
if err := config.PreExecute(); err != nil {
return err
}
if len(config.IssueReportsList) > 0 {
return reports.RunReports(&config.Config, config.IssueReportsList...)
}
if len(config.PRMungersList) == 0 {
glog.Fatalf("must include at least one --pr-mungers")
}
if err := mungers.RegisterMungers(config.PRMungersList); err != nil {
glog.Fatalf("unable to find requested mungers: %v", err)
}
requestedFeatures := mungers.RequestedFeatures()
if err := config.Features.Initialize(&config.Config, requestedFeatures); err != nil {
return err
}
if err := mungers.InitializeMungers(&config.Config, &config.Features); err != nil {
glog.Fatalf("unable to initialize mungers: %v", err)
}
return doMungers(config)
},
}
root.SetGlobalNormalizationFunc(utilflag.WordSepNormalizeFunc)
config.AddRootFlags(root)
addMungeFlags(config, root)
config.Features.AddFlags(root)
allMungers := mungers.GetAllMungers()
for _, m := range allMungers {
m.AddFlags(root, &config.Config)
}
allReports := reports.GetAllReports()
for _, r := range allReports {
r.AddFlags(root, &config.Config)
}
if err := root.Execute(); err != nil {
glog.Fatalf("%v\n", err)
}
}