forked from kubernetes/test-infra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
migratestatus.go
147 lines (121 loc) · 4.67 KB
/
migratestatus.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
142
143
144
145
146
147
/*
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 main
import (
"errors"
"flag"
"fmt"
"net/url"
"os"
"regexp"
"github.com/sirupsen/logrus"
"k8s.io/test-infra/maintenance/migratestatus/migrator"
prowflagutil "k8s.io/test-infra/prow/flagutil"
"k8s.io/test-infra/prow/logrusutil"
)
type options struct {
org, repo string
copyContext, moveContext, retireContext, destContext string
descriptionURL string
continueOnError, dryRun bool
github prowflagutil.GitHubOptions
branchFilterRaw string
branchFilter *regexp.Regexp
}
func gatherOptions() options {
o := options{}
fs := flag.NewFlagSet(os.Args[0], flag.ExitOnError)
fs.StringVar(&o.org, "org", "", "The organization that owns the repo.")
fs.StringVar(&o.repo, "repo", "", "The repo needing status migration.")
fs.BoolVar(&o.dryRun, "dry-run", true, "Run in dry-run mode, performing no modifying actions.")
fs.BoolVar(&o.continueOnError, "continue-on-error", false, "Indicates that the migration should continue if context migration fails for an individual PR.")
fs.StringVar(&o.copyContext, "copy", "", "Indicates copy mode and specifies the context to copy.")
fs.StringVar(&o.moveContext, "move", "", "Indicates move mode and specifies the context to move.")
fs.StringVar(&o.retireContext, "retire", "", "Indicates retire mode and specifies the context to retire.")
fs.StringVar(&o.destContext, "dest", "", "The destination context to copy or move to. For retire mode this is the context that replaced the retired context.")
fs.StringVar(&o.descriptionURL, "description", "", "A URL to a page explaining why a context was migrated or retired. (Optional)")
fs.StringVar(&o.branchFilterRaw, "branch-filter", "", "A regular expression which the PR target branch must match to be modified. (Optional)")
o.github.AddFlags(fs)
fs.Parse(os.Args[1:])
return o
}
func (o *options) Validate() error {
if o.org == "" {
return errors.New("'--org' must be set.\n")
}
if o.repo == "" {
return errors.New("'--repo' must be set.\n")
}
if o.destContext == "" && o.retireContext == "" {
return errors.New("'--dest' is required unless using '--retire' mode.\n")
}
if o.descriptionURL != "" {
_, err := url.ParseRequestURI(o.descriptionURL)
if err != nil {
return fmt.Errorf("'--description' URL '%s' is not valid: %w", o.descriptionURL, err)
}
}
if o.descriptionURL != "" && o.copyContext != "" {
return errors.New("'--description' URL is not applicable to '--copy' mode")
}
var optionCount int
if o.copyContext != "" {
optionCount++
}
if o.moveContext != "" {
optionCount++
}
if o.retireContext != "" {
optionCount++
}
if optionCount != 1 {
return errors.New("Exactly one mode must be specified [--copy|--retire|--move].")
}
if err := o.github.Validate(o.dryRun); err != nil {
return err
}
expr, err := regexp.Compile(o.branchFilterRaw)
if err != nil {
return fmt.Errorf("invalid --branch-filter regular expression: %w", err)
}
o.branchFilter = expr
return nil
}
func main() {
logrusutil.ComponentInit()
o := gatherOptions()
if err := o.Validate(); err != nil {
logrus.WithError(err).Fatal("Invalid options")
}
githubClient, err := o.github.GitHubClient(o.dryRun)
if err != nil {
logrus.WithError(err).Fatal("Error getting GitHub client.")
}
var mode *migrator.Mode
if o.copyContext != "" {
mode = migrator.CopyMode(o.copyContext, o.destContext)
}
if o.moveContext != "" {
mode = migrator.MoveMode(o.moveContext, o.destContext, o.descriptionURL)
}
if o.retireContext != "" {
mode = migrator.RetireMode(o.retireContext, o.destContext, o.descriptionURL)
}
// Note that continueOnError is false by default so that errors can be addressed when they occur
// instead of blindly continuing to the next PR, possibly continuing to error.
m := migrator.New(*mode, githubClient, o.org, o.repo, o.branchFilter.MatchString, o.continueOnError)
if err := m.Migrate(); err != nil {
logrus.WithError(err).Fatal("Error during status migration")
}
os.Exit(0)
}