2013-05-29 22:58:46 +04:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
2013-05-30 06:10:18 +04:00
|
|
|
"fmt"
|
2013-05-29 22:58:46 +04:00
|
|
|
"github.com/jingweno/gh/git"
|
2013-05-30 06:10:18 +04:00
|
|
|
"github.com/jingweno/gh/github"
|
2013-05-29 22:58:46 +04:00
|
|
|
"github.com/jingweno/gh/utils"
|
2013-05-30 06:10:18 +04:00
|
|
|
"os"
|
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
|
|
|
|
|
|
|
If "-v" is given, additionally print the URL to CI build results.
|
2013-05-29 22:58:46 +04:00
|
|
|
`,
|
|
|
|
}
|
|
|
|
|
2013-12-08 22:32:00 +04:00
|
|
|
var flagCiStatusVerbose bool
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
cmdCiStatus.Flag.BoolVar(&flagCiStatusVerbose, "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)
|
|
|
|
> One of: success (0), error (1), failure (1), pending (2), no status (3)
|
|
|
|
|
2013-12-08 22:37:01 +04:00
|
|
|
$ gh ci-status -v
|
|
|
|
> (prints CI state of HEAD, the URL to the CI build results and exits with appropriate code)
|
|
|
|
> One of: success (0), error (1), failure (1), pending (2), no status (3)
|
|
|
|
|
2013-07-06 00:45:22 +04:00
|
|
|
$ gh ci-status BRANCH
|
|
|
|
> (prints CI state of BRANCH and exits with appropriate code)
|
|
|
|
> One of: success (0), error (1), failure (1), pending (2), no status (3)
|
|
|
|
|
|
|
|
$ gh ci-status SHA
|
|
|
|
> (prints CI state of SHA and exits with appropriate code)
|
|
|
|
> One of: success (0), error (1), failure (1), pending (2), no status (3)
|
|
|
|
*/
|
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
|
|
|
}
|
|
|
|
|
2013-12-08 22:32:00 +04:00
|
|
|
localRepo := github.LocalRepo()
|
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 {
|
2013-12-08 22:32:00 +04:00
|
|
|
fmt.Printf("Would request CI status for %s\n", sha)
|
2013-07-06 00:45:22 +04:00
|
|
|
} else {
|
2013-12-28 13:14:07 +04:00
|
|
|
state, targetURL, exitCode, err := fetchCiStatus(project, sha)
|
2013-07-06 00:45:22 +04:00
|
|
|
utils.Check(err)
|
2013-12-08 22:32:00 +04:00
|
|
|
if flagCiStatusVerbose && targetURL != "" {
|
|
|
|
fmt.Printf("%s: %s\n", state, targetURL)
|
|
|
|
} else {
|
|
|
|
fmt.Println(state)
|
2013-07-06 00:45:22 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
os.Exit(exitCode)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-08 22:32:00 +04:00
|
|
|
func fetchCiStatus(p *github.Project, sha string) (state, targetURL string, exitCode int, err error) {
|
2013-12-17 19:45:48 +04:00
|
|
|
gh := github.NewClient(p.Host)
|
|
|
|
status, err := gh.CIStatus(p, sha)
|
2013-07-06 00:45:22 +04:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2013-05-30 06:10:18 +04:00
|
|
|
|
2013-06-09 03:30:01 +04:00
|
|
|
if status == nil {
|
2013-05-30 06:10:18 +04:00
|
|
|
state = "no status"
|
|
|
|
} else {
|
|
|
|
state = status.State
|
2013-06-11 08:49:45 +04:00
|
|
|
targetURL = status.TargetURL
|
2013-05-30 06:10:18 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
switch state {
|
|
|
|
case "success":
|
|
|
|
exitCode = 0
|
|
|
|
case "failure", "error":
|
|
|
|
exitCode = 1
|
|
|
|
case "pending":
|
|
|
|
exitCode = 2
|
2013-06-02 17:35:30 +04:00
|
|
|
default:
|
|
|
|
exitCode = 3
|
2013-05-30 06:10:18 +04:00
|
|
|
}
|
|
|
|
|
2013-07-06 00:45:22 +04:00
|
|
|
return
|
2013-05-29 22:58:46 +04:00
|
|
|
}
|