Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add options of github endpoints to auto-tools #172

Merged
merged 1 commit into from
Sep 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 21 additions & 16 deletions cmd/autobumper/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

"k8s.io/test-infra/experiment/autobumper/bumper"
"k8s.io/test-infra/prow/config/secret"
"k8s.io/test-infra/prow/github"
"k8s.io/test-infra/prow/flagutil"
)

const (
Expand All @@ -24,33 +24,35 @@ var extraFiles = map[string]bool{
}

type options struct {
dryRun bool
githubLogin string
githubToken string
gitName string
gitEmail string
targetDir string
assign string
flagutil.GitHubOptions
}

func parseOptions() options {
var o options
flag.StringVar(&o.githubLogin, "github-login", githubLogin, "The GitHub username to use.")
flag.StringVar(&o.githubToken, "github-token", "", "The path to the GitHub token file.")
flag.StringVar(&o.gitName, "git-name", "", "The name to use on the git commit. Requires --git-email. If not specified, uses the system default.")
flag.StringVar(&o.gitEmail, "git-email", "", "The email to use on the git commit. Requires --git-name. If not specified, uses the system default.")
flag.StringVar(&o.targetDir, "target-dir", "", "The directory containing the target repo.")
flag.StringVar(&o.assign, "assign", githubTeam, "The github username or group name to assign the created pull request to.")
flag.Parse()
fs := flag.NewFlagSet(os.Args[0], flag.ExitOnError)
fs.BoolVar(&o.dryRun, "dry-run", true, "Whether to actually create the pull request with github client")
fs.StringVar(&o.githubLogin, "github-login", githubLogin, "The GitHub username to use.")
fs.StringVar(&o.gitName, "git-name", "", "The name to use on the git commit. Requires --git-email. If not specified, uses the system default.")
fs.StringVar(&o.gitEmail, "git-email", "", "The email to use on the git commit. Requires --git-name. If not specified, uses the system default.")
fs.StringVar(&o.targetDir, "target-dir", "", "The directory containing the target repo.")
fs.StringVar(&o.assign, "assign", githubTeam, "The github username or group name to assign the created pull request to.")
o.AddFlagsWithoutDefaultGitHubTokenPath(fs)
if err := fs.Parse(os.Args[1:]); err != nil {
logrus.WithError(err).Errorf("cannot parse args: '%s'", os.Args[1:])
}
return o
}

func validateOptions(o options) error {
if o.githubLogin == "" {
return fmt.Errorf("--github-login cannot be empty string")
}
if o.githubToken == "" {
return fmt.Errorf("--github-token is mandatory")
}
if (o.gitEmail == "") != (o.gitName == "") {
return fmt.Errorf("--git-name and --git-email must be specified together")
}
Expand All @@ -60,7 +62,7 @@ func validateOptions(o options) error {
if o.assign == "" {
return fmt.Errorf("--assign is mandatory")
}
return nil
return o.GitHubOptions.Validate(o.dryRun)
}

func main() {
Expand All @@ -70,11 +72,14 @@ func main() {
}

sa := &secret.Agent{}
if err := sa.Start([]string{o.githubToken}); err != nil {
if err := sa.Start([]string{o.GitHubOptions.TokenPath}); err != nil {
logrus.WithError(err).Fatal("Failed to start secrets agent")
}

gc := github.NewClient(sa.GetTokenGenerator(o.githubToken), sa.Censor, github.DefaultGraphQLEndpoint, github.DefaultAPIEndpoint)
gc, err := o.GitHubOptions.GitHubClient(sa, o.dryRun)
if err != nil {
logrus.WithError(err).Fatal("error getting GitHub client")
}

logrus.Infof("Changing working directory to '%s' ...", o.targetDir)
if err := os.Chdir(o.targetDir); err != nil {
Expand All @@ -90,7 +95,7 @@ func main() {

remoteBranch := "autobump"
if err := bumper.MakeGitCommit(fmt.Sprintf("https://%s:%s@github.com/%s/%s.git", o.githubLogin,
string(sa.GetTokenGenerator(o.githubToken)()), o.githubLogin, githubRepo), remoteBranch, o.gitName, o.gitEmail, images, stdout, stderr); err != nil {
string(sa.GetTokenGenerator(o.GitHubOptions.TokenPath)()), o.githubLogin, githubRepo), remoteBranch, o.gitName, o.gitEmail, images, stdout, stderr); err != nil {
logrus.WithError(err).Fatal("Failed to push changes.")
}

Expand Down
48 changes: 28 additions & 20 deletions cmd/autoconfigbrancher/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"k8s.io/test-infra/experiment/autobumper/bumper"
"k8s.io/test-infra/prow/config/secret"
"k8s.io/test-infra/prow/flagutil"
"k8s.io/test-infra/prow/github"
)

const (
Expand All @@ -28,40 +27,41 @@ var (
)

type options struct {
dryRun bool
githubLogin string
githubToken string
gitName string
gitEmail string
targetDir string
assign string
currentRelease string
futureReleases flagutil.Strings
debugMode bool
flagutil.GitHubOptions
}

func parseOptions() options {
var o options
flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ExitOnError)
flag.StringVar(&o.githubLogin, "github-login", githubLogin, "The GitHub username to use.")
flag.StringVar(&o.githubToken, "github-token", "", "The path to the GitHub token file.")
flag.StringVar(&o.gitName, "git-name", "", "The name to use on the git commit. Requires --git-email. If not specified, uses the system default.")
flag.StringVar(&o.gitEmail, "git-email", "", "The email to use on the git commit. Requires --git-name. If not specified, uses the system default.")
flag.StringVar(&o.targetDir, "target-dir", "", "The directory containing the target repo.")
flag.StringVar(&o.assign, "assign", githubTeam, "The github username or group name to assign the created pull request to.")
flag.StringVar(&o.currentRelease, "current-release", "", "Configurations targeting this release will get branched.")
flag.Var(&o.futureReleases, "future-release", "Configurations will get branched to target this release, provide one or more times.")
flag.BoolVar(&o.debugMode, "debug-mode", false, "Enable the DEBUG level of logs if true.")
flag.Parse()
fs := flag.NewFlagSet(os.Args[0], flag.ExitOnError)
fs.BoolVar(&o.dryRun, "dry-run", true, "Whether to actually create the pull request with github client")
fs.StringVar(&o.githubLogin, "github-login", githubLogin, "The GitHub username to use.")
fs.StringVar(&o.gitName, "git-name", "", "The name to use on the git commit. Requires --git-email. If not specified, uses the system default.")
fs.StringVar(&o.gitEmail, "git-email", "", "The email to use on the git commit. Requires --git-name. If not specified, uses the system default.")
fs.StringVar(&o.targetDir, "target-dir", "", "The directory containing the target repo.")
fs.StringVar(&o.assign, "assign", githubTeam, "The github username or group name to assign the created pull request to.")
fs.StringVar(&o.currentRelease, "current-release", "", "Configurations targeting this release will get branched.")
fs.Var(&o.futureReleases, "future-release", "Configurations will get branched to target this release, provide one or more times.")
fs.BoolVar(&o.debugMode, "debug-mode", false, "Enable the DEBUG level of logs if true.")
o.AddFlagsWithoutDefaultGitHubTokenPath(fs)
if err := fs.Parse(os.Args[1:]); err != nil {
logrus.WithError(err).Errorf("cannot parse args: '%s'", os.Args[1:])
}
return o
}

func validateOptions(o options) error {
if o.githubLogin == "" {
return fmt.Errorf("--github-login cannot be empty string")
}
if o.githubToken == "" {
return fmt.Errorf("--github-token is mandatory")
}
if (o.gitEmail == "") != (o.gitName == "") {
return fmt.Errorf("--git-name and --git-email must be specified together")
}
Expand All @@ -77,7 +77,12 @@ func validateOptions(o options) error {
if len(o.futureReleases.Strings()) == 0 {
return fmt.Errorf("--future-release is mandatory")
}
return nil
for _, rf := range o.futureReleases.Strings() {
if rf == "" {
return fmt.Errorf("--future-release cannot be empty")
}
}
return o.GitHubOptions.Validate(o.dryRun)
}

func hasChanges() (bool, error) {
Expand Down Expand Up @@ -132,11 +137,14 @@ func main() {
}

sa := &secret.Agent{}
if err := sa.Start([]string{o.githubToken}); err != nil {
if err := sa.Start([]string{o.GitHubOptions.TokenPath}); err != nil {
logrus.WithError(err).Fatal("Failed to start secrets agent")
}

gc := github.NewClient(sa.GetTokenGenerator(o.githubToken), sa.Censor, github.DefaultGraphQLEndpoint, github.DefaultAPIEndpoint)
gc, err := o.GitHubOptions.GitHubClient(sa, o.dryRun)
if err != nil {
logrus.WithError(err).Fatal("error getting GitHub client")
}

logrus.Infof("Changing working directory to '%s' ...", o.targetDir)
if err := os.Chdir(o.targetDir); err != nil {
Expand Down Expand Up @@ -177,7 +185,7 @@ func main() {
title := fmt.Sprintf("%s by auto-config-brancher job at %s", matchTitle, time.Now().Format(time.RFC1123))
cmd = "git"
args = []string{"push", "-f", fmt.Sprintf("https://%s:%s@github.com/%s/%s.git", o.githubLogin,
string(sa.GetTokenGenerator(o.githubToken)()), o.githubLogin, githubRepo),
string(sa.GetTokenGenerator(o.GitHubOptions.TokenPath)()), o.githubLogin, githubRepo),
fmt.Sprintf("HEAD:%s", remoteBranch)}
run(cmd, args...)

Expand Down
14 changes: 12 additions & 2 deletions cmd/autoowners/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,24 @@ $ ./autoowners -h
Usage of ./autoowners:
-assign string
The github username or group name to assign the created pull request to. (default "openshift/openshift-team-developer-productivity-test-platform")
-debug-mode
Enable the DEBUG level of logs if true.
-dry-run
Whether to actually create the pull request with github client (default true)
-git-email string
The email to use on the git commit. Requires --git-name. If not specified, uses the system default.
-git-name string
The name to use on the git commit. Requires --git-email. If not specified, uses the system default.
-github-endpoint value
GitHub's API endpoint (may differ for enterprise). (default https://api.github.com)
-github-graphql-endpoint string
GitHub GraphQL API endpoint (may differ for enterprise). (default "https://api.github.com/graphql")
-github-login string
The GitHub username to use. (default "openshift-bot")
-github-token string
The path to the GitHub token file.
-github-token-file string
DEPRECATED: use -github-token-path instead. -github-token-file may be removed anytime after 2019-01-01.
-github-token-path string
Path to the file containing the GitHub OAuth secret.
-ignore-repo value
The repo for which syncing OWNERS file is disabled.
-target-dir string
Expand Down
41 changes: 23 additions & 18 deletions cmd/autoowners/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,39 +254,40 @@ func pullOwners(gc github.Client, directory string, blacklist sets.String) error
}

type options struct {
dryRun bool
githubLogin string
githubToken string
gitName string
gitEmail string
assign string
targetDir string
blacklist flagutil.Strings
debugMode bool
flagutil.GitHubOptions
}

func parseOptions() options {
var o options
//To avoid flag from dependencies such as https://github.com/openshift/ci-tools/blob/5b5410293f7cd318540d1fb333c68b93ddab2b60/vendor/github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1/artifact_pvc.go#L30
flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ExitOnError)
flag.StringVar(&o.githubLogin, "github-login", githubLogin, "The GitHub username to use.")
flag.StringVar(&o.githubToken, "github-token", "", "The path to the GitHub token file.")
flag.StringVar(&o.gitName, "git-name", "", "The name to use on the git commit. Requires --git-email. If not specified, uses the system default.")
flag.StringVar(&o.gitEmail, "git-email", "", "The email to use on the git commit. Requires --git-name. If not specified, uses the system default.")
flag.StringVar(&o.assign, "assign", githubTeam, "The github username or group name to assign the created pull request to.")
flag.StringVar(&o.targetDir, "target-dir", "", "The directory containing the target repo.")
flag.Var(&o.blacklist, "ignore-repo", "The repo for which syncing OWNERS file is disabled.")
flag.BoolVar(&o.debugMode, "debug-mode", false, "Enable the DEBUG level of logs if true.")
flag.Parse()
fs := flag.NewFlagSet(os.Args[0], flag.ExitOnError)
fs.BoolVar(&o.dryRun, "dry-run", true, "Whether to actually create the pull request with github client")
fs.StringVar(&o.githubLogin, "github-login", githubLogin, "The GitHub username to use.")
fs.StringVar(&o.gitName, "git-name", "", "The name to use on the git commit. Requires --git-email. If not specified, uses the system default.")
fs.StringVar(&o.gitEmail, "git-email", "", "The email to use on the git commit. Requires --git-name. If not specified, uses the system default.")
fs.StringVar(&o.assign, "assign", githubTeam, "The github username or group name to assign the created pull request to.")
fs.StringVar(&o.targetDir, "target-dir", "", "The directory containing the target repo.")
fs.Var(&o.blacklist, "ignore-repo", "The repo for which syncing OWNERS file is disabled.")
fs.BoolVar(&o.debugMode, "debug-mode", false, "Enable the DEBUG level of logs if true.")
o.AddFlagsWithoutDefaultGitHubTokenPath(fs)
if err := fs.Parse(os.Args[1:]); err != nil {
logrus.WithError(err).Errorf("cannot parse args: '%s'", os.Args[1:])
}
return o
}

func validateOptions(o options) error {
if o.githubLogin == "" {
return fmt.Errorf("--github-login is mandatory")
}
if o.githubToken == "" {
return fmt.Errorf("--github-token is mandatory")
}
if (o.gitEmail == "") != (o.gitName == "") {
return fmt.Errorf("--git-name and --git-email must be specified together")
}
Expand All @@ -296,7 +297,7 @@ func validateOptions(o options) error {
if o.targetDir == "" {
return fmt.Errorf("--target-dir is mandatory")
}
return nil
return o.GitHubOptions.Validate(o.dryRun)
}

func getBody(directories []string, assign string) string {
Expand Down Expand Up @@ -359,10 +360,14 @@ func main() {
}

secretAgent := &secret.Agent{}
if err := secretAgent.Start([]string{o.githubToken}); err != nil {
if err := secretAgent.Start([]string{o.GitHubOptions.TokenPath}); err != nil {
logrus.WithError(err).Fatalf("Error starting secrets agent.")
}
gc := github.NewClient(secretAgent.GetTokenGenerator(o.githubToken), secretAgent.Censor, github.DefaultGraphQLEndpoint, github.DefaultAPIEndpoint)

gc, err := o.GitHubOptions.GitHubClient(secretAgent, o.dryRun)
if err != nil {
logrus.WithError(err).Fatal("error getting GitHub client")
}

logrus.Infof("Changing working directory to '%s' ...", o.targetDir)
if err := os.Chdir(o.targetDir); err != nil {
Expand All @@ -389,7 +394,7 @@ func main() {
matchTitle := "Sync OWNERS files"
title := getTitle(matchTitle, time.Now().Format(time.RFC1123))
if err := bumper.GitCommitAndPush(fmt.Sprintf("https://%s:%s@github.com/%s/%s.git", o.githubLogin,
string(secretAgent.GetTokenGenerator(o.githubToken)()), o.githubLogin, githubRepo),
string(secretAgent.GetTokenGenerator(o.GitHubOptions.TokenPath)()), o.githubLogin, githubRepo),
remoteBranch, o.gitName, o.gitEmail, title, stdout, stderr); err != nil {
logrus.WithError(err).Fatal("Failed to push changes.")
}
Expand Down