github.com/yrj2011/jx-test-infra@v0.0.0-20190529031832-7a2065ee98eb/prow/external-plugins/cherrypicker/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 "net/http" 22 "net/url" 23 "os/signal" 24 "strconv" 25 "syscall" 26 27 "github.com/sirupsen/logrus" 28 29 "k8s.io/test-infra/prow/config" 30 "k8s.io/test-infra/prow/flagutil" 31 "k8s.io/test-infra/prow/git" 32 "k8s.io/test-infra/prow/github" 33 "k8s.io/test-infra/prow/pluginhelp/externalplugins" 34 ) 35 36 var ( 37 port = flag.Int("port", 8888, "Port to listen on.") 38 dryRun = flag.Bool("dry-run", true, "Dry run for testing. Uses API tokens but does not mutate.") 39 githubEndpoint = flagutil.NewStrings("https://api.github.com") 40 githubTokenFile = flag.String("github-token-file", "/etc/github/oauth", "Path to the file containing the GitHub OAuth secret.") 41 webhookSecretFile = flag.String("hmac-secret-file", "/etc/webhook/hmac", "Path to the file containing the GitHub HMAC secret.") 42 prowAssignments = flag.Bool("use-prow-assignments", true, "Use prow commands to assign cherrypicked PRs.") 43 allowAll = flag.Bool("allow-all", false, "Allow anybody to use automated cherrypicks by skipping Github organization membership checks.") 44 ) 45 46 func init() { 47 flag.Var(&githubEndpoint, "github-endpoint", "GitHub's API endpoint.") 48 } 49 50 func main() { 51 flag.Parse() 52 logrus.SetFormatter(&logrus.JSONFormatter{}) 53 // TODO: Use global option from the prow config. 54 logrus.SetLevel(logrus.DebugLevel) 55 log := logrus.StandardLogger().WithField("plugin", "cherrypick") 56 57 // Ignore SIGTERM so that we don't drop hooks when the pod is removed. 58 // We'll get SIGTERM first and then SIGKILL after our graceful termination 59 // deadline. 60 signal.Ignore(syscall.SIGTERM) 61 62 secretAgent := &config.SecretAgent{} 63 if err := secretAgent.Start([]string{*githubTokenFile, *webhookSecretFile}); err != nil { 64 logrus.WithError(err).Fatal("Error starting secrets agent.") 65 } 66 67 var err error 68 for _, ep := range githubEndpoint.Strings() { 69 _, err = url.ParseRequestURI(ep) 70 if err != nil { 71 logrus.WithError(err).Fatalf("Invalid --endpoint URL %q.", ep) 72 } 73 } 74 75 githubClient := github.NewClient(secretAgent.GetTokenGenerator(*githubTokenFile), githubEndpoint.Strings()...) 76 if *dryRun { 77 githubClient = github.NewDryRunClient(secretAgent.GetTokenGenerator(*githubTokenFile), githubEndpoint.Strings()...) 78 } 79 80 gitClient, err := git.NewClient() 81 if err != nil { 82 log.WithError(err).Fatal("Error getting git client.") 83 } 84 85 // The bot name is used to determine to what fork we can push cherry-pick branches. 86 botName, err := githubClient.BotName() 87 if err != nil { 88 log.WithError(err).Fatal("Error getting bot name.") 89 } 90 email, err := githubClient.Email() 91 if err != nil { 92 log.WithError(err).Fatal("Error getting bot e-mail.") 93 } 94 // The bot needs to be able to push to its own Github fork and potentially pull 95 // from private repos. 96 gitClient.SetCredentials(botName, secretAgent.GetTokenGenerator(*githubTokenFile)) 97 98 repos, err := githubClient.GetRepos(botName, true) 99 if err != nil { 100 log.WithError(err).Fatal("Error listing bot repositories.") 101 } 102 103 server := &Server{ 104 tokenGenerator: secretAgent.GetTokenGenerator(*webhookSecretFile), 105 botName: botName, 106 email: email, 107 108 gc: gitClient, 109 ghc: githubClient, 110 log: log, 111 112 prowAssignments: *prowAssignments, 113 allowAll: *allowAll, 114 115 bare: &http.Client{}, 116 patchURL: "https://patch-diff.githubusercontent.com", 117 118 repos: repos, 119 } 120 121 http.Handle("/", server) 122 externalplugins.ServeExternalPluginHelp(http.DefaultServeMux, log, HelpProvider) 123 logrus.Fatal(http.ListenAndServe(":"+strconv.Itoa(*port), nil)) 124 }