forked from kubernetes/test-infra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmungegithub.go
135 lines (117 loc) · 3.96 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
/*
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"
utilflag "k8s.io/kubernetes/pkg/util/flag"
"k8s.io/test-infra/mungegithub/features"
github_util "k8s.io/test-infra/mungegithub/github"
"k8s.io/test-infra/mungegithub/mungers"
"k8s.io/test-infra/mungegithub/reports"
"github.com/golang/glog"
"github.com/spf13/cobra"
"k8s.io/test-infra/mungegithub/mungers/mungerutil"
)
var (
_ = fmt.Print
)
type mungeConfig struct {
github_util.Config
MinIssueNumber int
PRMungersList []string
IssueReportsList []string
Once bool
Period time.Duration
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().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)
}
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)
}
if config.GithubKey != "" {
config.HookHandler = github_util.NewWebHookAndListen(config.GithubKey, config.Address)
}
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)
}
}