github.com/shashidharatd/test-infra@v0.0.0-20171006011030-71304e1ca560/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 "flag" 21 "fmt" 22 "os" 23 "strings" 24 25 "github.com/ghodss/yaml" 26 "github.com/sirupsen/logrus" 27 28 "k8s.io/test-infra/prow/config" 29 "k8s.io/test-infra/prow/kube" 30 "k8s.io/test-infra/prow/pjutil" 31 ) 32 33 var ( 34 jobName = flag.String("job", "", "Job to run.") 35 configPath = flag.String("config-path", "", "Path to config.yaml.") 36 ) 37 38 func main() { 39 flag.Parse() 40 41 if *jobName == "" { 42 logrus.Fatal("Must specify --job.") 43 } 44 45 conf, err := config.Load(*configPath) 46 if err != nil { 47 logrus.WithError(err).Fatal("Error loading config.") 48 } 49 50 var pjs kube.ProwJobSpec 51 var found bool 52 var needsBaseRef bool 53 var needsPR bool 54 for fullRepoName, ps := range conf.Presubmits { 55 org, repo, err := splitRepoName(fullRepoName) 56 if err != nil { 57 logrus.WithError(err).Fatal("Invalid repo name.") 58 } 59 for _, p := range ps { 60 if p.Name == *jobName { 61 pjs = pjutil.PresubmitSpec(p, kube.Refs{ 62 Org: org, 63 Repo: repo, 64 Pulls: []kube.Pull{{}}, 65 }) 66 found = true 67 needsBaseRef = true 68 needsPR = true 69 } 70 } 71 } 72 for fullRepoName, ps := range conf.Postsubmits { 73 org, repo, err := splitRepoName(fullRepoName) 74 if err != nil { 75 logrus.WithError(err).Fatal("Invalid repo name.") 76 } 77 for _, p := range ps { 78 if p.Name == *jobName { 79 pjs = pjutil.PostsubmitSpec(p, kube.Refs{ 80 Org: org, 81 Repo: repo, 82 }) 83 found = true 84 needsBaseRef = true 85 } 86 } 87 } 88 for _, p := range conf.Periodics { 89 if p.Name == *jobName { 90 pjs = pjutil.PeriodicSpec(p) 91 found = true 92 } 93 } 94 if !found { 95 logrus.Fatalf("Job %s not found.", *jobName) 96 } 97 if needsBaseRef { 98 fmt.Fprint(os.Stderr, "Base ref (e.g. master): ") 99 fmt.Scanln(&pjs.Refs.BaseRef) 100 fmt.Fprint(os.Stderr, "Base SHA (e.g. 72bcb5d80): ") 101 fmt.Scanln(&pjs.Refs.BaseSHA) 102 } 103 if needsPR { 104 fmt.Fprint(os.Stderr, "PR Number: ") 105 fmt.Scanln(&pjs.Refs.Pulls[0].Number) 106 fmt.Fprint(os.Stderr, "PR author: ") 107 fmt.Scanln(&pjs.Refs.Pulls[0].Author) 108 fmt.Fprint(os.Stderr, "PR SHA (e.g. 72bcb5d80): ") 109 fmt.Scanln(&pjs.Refs.Pulls[0].SHA) 110 } 111 pj := pjutil.NewProwJob(pjs) 112 b, err := yaml.Marshal(&pj) 113 if err != nil { 114 logrus.WithError(err).Fatal("Error marshalling YAML.") 115 } 116 fmt.Print(string(b)) 117 } 118 119 func splitRepoName(repo string) (string, string, error) { 120 s := strings.Split(repo, "/") 121 if len(s) != 2 { 122 return "", "", fmt.Errorf("repo %s cannot be split into org/repo", repo) 123 } 124 return s[0], s[1], nil 125 }