github.com/shashidharatd/test-infra@v0.0.0-20171006011030-71304e1ca560/maintenance/migratestatus/migratestatus.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  	"io/ioutil"
    22  	"os"
    23  	"strings"
    24  
    25  	"github.com/golang/glog"
    26  	"github.com/google/go-github/github"
    27  	"k8s.io/test-infra/maintenance/migratestatus/migrator"
    28  )
    29  
    30  func main() {
    31  	var tokenFile, org, repo, copyContext, moveContext, retireContext, destContext string
    32  	var modify, continueOnError bool
    33  
    34  	flag.StringVar(&tokenFile, "tokenfile", "", "The file containing the token to use for authentication.")
    35  	flag.StringVar(&org, "org", "", "The organization that owns the repo.")
    36  	flag.StringVar(&repo, "repo", "", "The repo needing status migration.")
    37  	flag.BoolVar(&modify, "modify", false, "Perform modifying actions when set, otherwise dry-run.")
    38  	flag.BoolVar(&continueOnError, "continue-on-error", false, "Indicates that the migration should continue if context migration fails for an individual PR.")
    39  
    40  	flag.StringVar(&copyContext, "copy", "", "Indicates copy mode and specifies the context to copy.")
    41  	flag.StringVar(&moveContext, "move", "", "Indicates move mode and specifies the context to move.")
    42  	flag.StringVar(&retireContext, "retire", "", "Indicates retire mode and specifies the context to retire.")
    43  	flag.StringVar(&destContext, "dest", "", "The destination context to copy or move to. For retire mode this is the context that replaced the retired context.")
    44  	flag.Parse()
    45  
    46  	if org == "" {
    47  		errorfExit("'--org' must be set.\n")
    48  	}
    49  	if repo == "" {
    50  		errorfExit("'--repo' must be set.\n")
    51  	}
    52  	if tokenFile == "" {
    53  		errorfExit("'--tokenfile' must be set.\n")
    54  	}
    55  	tokenData, err := ioutil.ReadFile(tokenFile)
    56  	if err != nil {
    57  		errorfExit("Error loading token file: %v\n", err)
    58  	}
    59  
    60  	if destContext == "" && retireContext == "" {
    61  		errorfExit("'--dest' is required unless using '--retire' mode.\n")
    62  	}
    63  
    64  	var mode *migrator.Mode
    65  	modeMsg := "Exactly one mode must be specified [--copy|--retire|--move].\n"
    66  	if copyContext != "" {
    67  		mode = migrator.CopyMode(copyContext, destContext)
    68  	}
    69  	if moveContext != "" {
    70  		if mode != nil {
    71  			errorfExit(modeMsg)
    72  		}
    73  		mode = migrator.MoveMode(moveContext, destContext)
    74  	}
    75  	if retireContext != "" {
    76  		if mode != nil {
    77  			errorfExit(modeMsg)
    78  		}
    79  		mode = migrator.RetireMode(retireContext, destContext)
    80  	}
    81  	if mode == nil {
    82  		errorfExit(modeMsg)
    83  	}
    84  
    85  	// Note that continueOnError is false by default so that errors can be addressed when they occur
    86  	// instead of blindly continuing to the next PR, possibly continuing to error.
    87  	dryRun := !modify
    88  	m := migrator.New(*mode, strings.TrimSpace(string(tokenData)), org, repo, dryRun, continueOnError)
    89  
    90  	prOptions := &github.PullRequestListOptions{}
    91  	if err := m.Migrate(prOptions); err != nil {
    92  		errorfExit("Error during status migration: %v\n", err)
    93  	}
    94  	glog.Flush()
    95  	os.Exit(0)
    96  }
    97  
    98  func errorfExit(format string, args ...interface{}) {
    99  	glog.Errorf(format, args...)
   100  	glog.Flush()
   101  	os.Exit(1)
   102  }