github.com/yrj2011/jx-test-infra@v0.0.0-20190529031832-7a2065ee98eb/prow/cmd/mkpj/main.go (about) 1 /* 2 Copyright 2017 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package main 18 19 import ( 20 "errors" 21 "flag" 22 "fmt" 23 "os" 24 "strings" 25 26 "github.com/ghodss/yaml" 27 "github.com/sirupsen/logrus" 28 29 "k8s.io/test-infra/prow/config" 30 "k8s.io/test-infra/prow/kube" 31 "k8s.io/test-infra/prow/pjutil" 32 ) 33 34 type options struct { 35 jobName string 36 configPath string 37 jobConfigPath string 38 39 baseRef string 40 baseSha string 41 pullNumber int 42 pullSha string 43 pullAuthor string 44 } 45 46 func (o *options) Validate() error { 47 if o.jobName == "" { 48 return errors.New("required flag --job was unset") 49 } 50 51 if o.configPath == "" { 52 return errors.New("required flag --config-path was unset") 53 } 54 55 return nil 56 } 57 58 func gatherOptions() options { 59 o := options{} 60 flag.StringVar(&o.jobName, "job", "", "Job to run.") 61 flag.StringVar(&o.configPath, "config-path", "", "Path to config.yaml.") 62 flag.StringVar(&o.jobConfigPath, "job-config-path", "", "Path to prow job configs.") 63 flag.StringVar(&o.baseRef, "base-ref", "", "Git base ref under test") 64 flag.StringVar(&o.baseSha, "base-sha", "", "Git base SHA under test") 65 flag.IntVar(&o.pullNumber, "pull-number", 0, "Git pull number under test") 66 flag.StringVar(&o.pullSha, "pull-sha", "", "Git pull SHA under test") 67 flag.StringVar(&o.pullAuthor, "pull-author", "", "Git pull author under test") 68 flag.Parse() 69 return o 70 } 71 72 func main() { 73 o := gatherOptions() 74 if err := o.Validate(); err != nil { 75 logrus.Fatalf("Invalid options: %v", err) 76 } 77 78 conf, err := config.Load(o.configPath, o.jobConfigPath) 79 if err != nil { 80 logrus.WithError(err).Fatal("Error loading config.") 81 } 82 83 var pjs kube.ProwJobSpec 84 var labels map[string]string 85 var found bool 86 var needsBaseRef bool 87 var needsPR bool 88 for fullRepoName, ps := range conf.Presubmits { 89 org, repo, err := splitRepoName(fullRepoName) 90 if err != nil { 91 logrus.WithError(err).Fatal("Invalid repo name.") 92 } 93 for _, p := range ps { 94 if p.Name == o.jobName { 95 pjs = pjutil.PresubmitSpec(p, kube.Refs{ 96 Org: org, 97 Repo: repo, 98 BaseRef: o.baseRef, 99 BaseSHA: o.baseSha, 100 Pulls: []kube.Pull{{ 101 Author: o.pullAuthor, 102 Number: o.pullNumber, 103 SHA: o.pullSha, 104 }}, 105 }) 106 labels = p.Labels 107 found = true 108 needsBaseRef = true 109 needsPR = true 110 } 111 } 112 } 113 for fullRepoName, ps := range conf.Postsubmits { 114 org, repo, err := splitRepoName(fullRepoName) 115 if err != nil { 116 logrus.WithError(err).Fatal("Invalid repo name.") 117 } 118 for _, p := range ps { 119 if p.Name == o.jobName { 120 pjs = pjutil.PostsubmitSpec(p, kube.Refs{ 121 Org: org, 122 Repo: repo, 123 BaseRef: o.baseRef, 124 BaseSHA: o.baseSha, 125 }) 126 labels = p.Labels 127 found = true 128 needsBaseRef = true 129 } 130 } 131 } 132 for _, p := range conf.Periodics { 133 if p.Name == o.jobName { 134 pjs = pjutil.PeriodicSpec(p) 135 labels = p.Labels 136 found = true 137 } 138 } 139 if !found { 140 logrus.Fatalf("Job %s not found.", o.jobName) 141 } 142 if needsBaseRef { 143 if pjs.Refs.BaseRef == "" { 144 fmt.Fprint(os.Stderr, "Base ref (e.g. master): ") 145 fmt.Scanln(&pjs.Refs.BaseRef) 146 } 147 if pjs.Refs.BaseSHA == "" { 148 fmt.Fprint(os.Stderr, "Base SHA (e.g. 72bcb5d80): ") 149 fmt.Scanln(&pjs.Refs.BaseSHA) 150 } 151 } 152 if needsPR { 153 if pjs.Refs.Pulls[0].Number == 0 { 154 fmt.Fprint(os.Stderr, "PR Number: ") 155 fmt.Scanln(&pjs.Refs.Pulls[0].Number) 156 } 157 if pjs.Refs.Pulls[0].Author == "" { 158 fmt.Fprint(os.Stderr, "PR author: ") 159 fmt.Scanln(&pjs.Refs.Pulls[0].Author) 160 } 161 if pjs.Refs.Pulls[0].SHA == "" { 162 fmt.Fprint(os.Stderr, "PR SHA (e.g. 72bcb5d80): ") 163 fmt.Scanln(&pjs.Refs.Pulls[0].SHA) 164 } 165 } 166 pj := pjutil.NewProwJob(pjs, labels) 167 b, err := yaml.Marshal(&pj) 168 if err != nil { 169 logrus.WithError(err).Fatal("Error marshalling YAML.") 170 } 171 fmt.Print(string(b)) 172 } 173 174 func splitRepoName(repo string) (string, string, error) { 175 s := strings.Split(repo, "/") 176 if len(s) != 2 { 177 return "", "", fmt.Errorf("repo %s cannot be split into org/repo", repo) 178 } 179 return s[0], s[1], nil 180 }