2013-05-29 22:58:46 +04:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
2013-05-30 06:10:18 +04:00
|
|
|
"fmt"
|
2014-04-02 00:40:02 +04:00
|
|
|
"os"
|
|
|
|
|
2014-02-10 20:22:36 +04:00
|
|
|
"github.com/github/hub/git"
|
|
|
|
"github.com/github/hub/github"
|
2015-02-10 11:26:45 +03:00
|
|
|
"github.com/github/hub/ui"
|
2014-02-10 20:22:36 +04:00
|
|
|
"github.com/github/hub/utils"
|
2013-05-29 22:58:46 +04:00
|
|
|
)
|
|
|
|
|
2013-06-30 00:54:40 +04:00
|
|
|
var cmdCiStatus = &Command{
|
|
|
|
Run: ciStatus,
|
2013-12-08 22:37:01 +04:00
|
|
|
Usage: "ci-status [-v] [COMMIT]",
|
2013-05-29 22:58:46 +04:00
|
|
|
Short: "Show CI status of a commit",
|
2013-12-08 22:37:01 +04:00
|
|
|
Long: `Looks up the SHA for <COMMIT> in GitHub Status API and displays the latest
|
|
|
|
status. Exits with one of:
|
2013-05-29 22:58:46 +04:00
|
|
|
success (0), error (1), failure (1), pending (2), no status (3)
|
2013-12-08 22:37:01 +04:00
|
|
|
|
2015-09-27 02:04:56 +03:00
|
|
|
If "-v" is given, additionally print detailed report of all checks and their URLs.
|
2013-05-29 22:58:46 +04:00
|
|
|
`,
|
|
|
|
}
|
|
|
|
|
2013-12-08 22:32:00 +04:00
|
|
|
var flagCiStatusVerbose bool
|
|
|
|
|
|
|
|
func init() {
|
2013-12-30 03:07:58 +04:00
|
|
|
cmdCiStatus.Flag.BoolVarP(&flagCiStatusVerbose, "verbose", "v", false, "VERBOSE")
|
2013-12-30 02:18:14 +04:00
|
|
|
|
|
|
|
CmdRunner.Use(cmdCiStatus)
|
2013-12-08 22:32:00 +04:00
|
|
|
}
|
|
|
|
|
2013-07-06 00:45:22 +04:00
|
|
|
/*
|
|
|
|
$ gh ci-status
|
|
|
|
> (prints CI state of HEAD and exits with appropriate code)
|
|
|
|
|
2013-12-08 22:37:01 +04:00
|
|
|
$ gh ci-status -v
|
2015-09-27 02:04:56 +03:00
|
|
|
> (prints CI states and URLs to CI build results for HEAD and exits with appropriate code)
|
2013-12-08 22:37:01 +04:00
|
|
|
|
2013-07-06 00:45:22 +04:00
|
|
|
$ gh ci-status BRANCH
|
|
|
|
> (prints CI state of BRANCH and exits with appropriate code)
|
|
|
|
|
|
|
|
$ gh ci-status SHA
|
|
|
|
> (prints CI state of SHA and exits with appropriate code)
|
|
|
|
*/
|
2013-06-30 00:54:40 +04:00
|
|
|
func ciStatus(cmd *Command, args *Args) {
|
2013-05-30 06:10:18 +04:00
|
|
|
ref := "HEAD"
|
2013-07-02 22:56:45 +04:00
|
|
|
if !args.IsParamsEmpty() {
|
2013-07-06 00:45:22 +04:00
|
|
|
ref = args.RemoveParam(0)
|
2013-05-29 22:58:46 +04:00
|
|
|
}
|
|
|
|
|
2014-04-02 00:40:02 +04:00
|
|
|
localRepo, err := github.LocalRepo()
|
|
|
|
utils.Check(err)
|
|
|
|
|
2013-12-28 13:14:07 +04:00
|
|
|
project, err := localRepo.MainProject()
|
2013-12-08 22:32:00 +04:00
|
|
|
utils.Check(err)
|
|
|
|
|
|
|
|
sha, err := git.Ref(ref)
|
|
|
|
if err != nil {
|
|
|
|
err = fmt.Errorf("Aborted: no revision could be determined from '%s'", ref)
|
|
|
|
}
|
2013-05-29 22:58:46 +04:00
|
|
|
utils.Check(err)
|
|
|
|
|
2013-07-06 00:45:22 +04:00
|
|
|
if args.Noop {
|
2015-02-10 11:26:45 +03:00
|
|
|
ui.Printf("Would request CI status for %s\n", sha)
|
2013-07-06 00:45:22 +04:00
|
|
|
} else {
|
2015-09-27 02:04:56 +03:00
|
|
|
gh := github.NewClient(project.Host)
|
|
|
|
response, err := gh.FetchCIStatus(project, sha)
|
2013-07-06 00:45:22 +04:00
|
|
|
utils.Check(err)
|
2015-09-27 02:04:56 +03:00
|
|
|
|
|
|
|
state := response.State
|
|
|
|
if len(response.Statuses) == 0 {
|
|
|
|
state = ""
|
|
|
|
}
|
|
|
|
|
|
|
|
var exitCode int
|
|
|
|
switch state {
|
|
|
|
case "success":
|
|
|
|
exitCode = 0
|
|
|
|
case "failure", "error":
|
|
|
|
exitCode = 1
|
|
|
|
case "pending":
|
|
|
|
exitCode = 2
|
|
|
|
default:
|
|
|
|
exitCode = 3
|
|
|
|
}
|
|
|
|
|
|
|
|
if flagCiStatusVerbose && len(response.Statuses) > 0 {
|
|
|
|
verboseFormat(response.Statuses)
|
2013-12-08 22:32:00 +04:00
|
|
|
} else {
|
2015-09-27 02:04:56 +03:00
|
|
|
if state != "" {
|
|
|
|
ui.Println(state)
|
|
|
|
} else {
|
|
|
|
ui.Println("no status")
|
|
|
|
}
|
2013-07-06 00:45:22 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
os.Exit(exitCode)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-27 02:04:56 +03:00
|
|
|
func verboseFormat(statuses []*github.CIStatus) {
|
|
|
|
colorize := github.IsTerminal(os.Stdout)
|
2013-05-30 06:10:18 +04:00
|
|
|
|
2015-09-27 02:04:56 +03:00
|
|
|
contextWidth := 0
|
|
|
|
for _, status := range statuses {
|
|
|
|
if len(status.Context) > contextWidth {
|
|
|
|
contextWidth = len(status.Context)
|
|
|
|
}
|
2013-05-30 06:10:18 +04:00
|
|
|
}
|
|
|
|
|
2015-09-27 02:04:56 +03:00
|
|
|
for _, status := range statuses {
|
|
|
|
var color int
|
|
|
|
var stateMarker string
|
|
|
|
switch status.State {
|
|
|
|
case "success":
|
|
|
|
stateMarker = "✔︎"
|
|
|
|
color = 32
|
|
|
|
case "failure", "error":
|
|
|
|
stateMarker = "✖︎"
|
|
|
|
color = 31
|
|
|
|
case "pending":
|
|
|
|
stateMarker = "●"
|
|
|
|
color = 33
|
|
|
|
}
|
2013-05-30 06:10:18 +04:00
|
|
|
|
2015-09-27 02:04:56 +03:00
|
|
|
if colorize {
|
|
|
|
stateMarker = fmt.Sprintf("\033[%dm%s\033[0m", color, stateMarker)
|
|
|
|
}
|
|
|
|
|
|
|
|
ui.Printf("%s\t%-*s\t%s\n", stateMarker, contextWidth, status.Context, status.TargetUrl)
|
|
|
|
}
|
2013-05-29 22:58:46 +04:00
|
|
|
}
|