github.com/trevoraustin/hub@v2.2.0-preview1.0.20141105230840-96d8bfc654cc+incompatible/commands/ci_status.go (about) 1 package commands 2 3 import ( 4 "fmt" 5 "os" 6 7 "github.com/github/hub/git" 8 "github.com/github/hub/github" 9 "github.com/github/hub/utils" 10 ) 11 12 var cmdCiStatus = &Command{ 13 Run: ciStatus, 14 Usage: "ci-status [-v] [COMMIT]", 15 Short: "Show CI status of a commit", 16 Long: `Looks up the SHA for <COMMIT> in GitHub Status API and displays the latest 17 status. Exits with one of: 18 success (0), error (1), failure (1), pending (2), no status (3) 19 20 If "-v" is given, additionally print the URL to CI build results. 21 `, 22 } 23 24 var flagCiStatusVerbose bool 25 26 func init() { 27 cmdCiStatus.Flag.BoolVarP(&flagCiStatusVerbose, "verbose", "v", false, "VERBOSE") 28 29 CmdRunner.Use(cmdCiStatus) 30 } 31 32 /* 33 $ gh ci-status 34 > (prints CI state of HEAD and exits with appropriate code) 35 > One of: success (0), error (1), failure (1), pending (2), no status (3) 36 37 $ gh ci-status -v 38 > (prints CI state of HEAD, the URL to the CI build results and exits with appropriate code) 39 > One of: success (0), error (1), failure (1), pending (2), no status (3) 40 41 $ gh ci-status BRANCH 42 > (prints CI state of BRANCH and exits with appropriate code) 43 > One of: success (0), error (1), failure (1), pending (2), no status (3) 44 45 $ gh ci-status SHA 46 > (prints CI state of SHA and exits with appropriate code) 47 > One of: success (0), error (1), failure (1), pending (2), no status (3) 48 */ 49 func ciStatus(cmd *Command, args *Args) { 50 ref := "HEAD" 51 if !args.IsParamsEmpty() { 52 ref = args.RemoveParam(0) 53 } 54 55 localRepo, err := github.LocalRepo() 56 utils.Check(err) 57 58 project, err := localRepo.MainProject() 59 utils.Check(err) 60 61 sha, err := git.Ref(ref) 62 if err != nil { 63 err = fmt.Errorf("Aborted: no revision could be determined from '%s'", ref) 64 } 65 utils.Check(err) 66 67 if args.Noop { 68 fmt.Printf("Would request CI status for %s\n", sha) 69 } else { 70 state, targetURL, exitCode, err := fetchCiStatus(project, sha) 71 utils.Check(err) 72 if flagCiStatusVerbose && targetURL != "" { 73 fmt.Printf("%s: %s\n", state, targetURL) 74 } else { 75 fmt.Println(state) 76 } 77 78 os.Exit(exitCode) 79 } 80 } 81 82 func fetchCiStatus(p *github.Project, sha string) (state, targetURL string, exitCode int, err error) { 83 gh := github.NewClient(p.Host) 84 status, err := gh.CIStatus(p, sha) 85 if err != nil { 86 return 87 } 88 89 if status == nil { 90 state = "no status" 91 } else { 92 state = status.State 93 targetURL = status.TargetURL 94 } 95 96 switch state { 97 case "success": 98 exitCode = 0 99 case "failure", "error": 100 exitCode = 1 101 case "pending": 102 exitCode = 2 103 default: 104 exitCode = 3 105 } 106 107 return 108 }