hub/commands/ci_status.go

68 строки
1.2 KiB
Go
Исходник Обычный вид История

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
)
var cmdCiStatus = &Command{
Run: ciStatus,
Usage: "ci-status [COMMIT]",
2013-05-29 22:58:46 +04:00
Short: "Show CI status of a commit",
Long: `Looks up the SHA for COMMIT in GitHub Status API and displays the latest
2013-06-02 17:37:26 +04:00
status. If no COMMIT is provided, HEAD will be used. Exits with one of:
2013-05-29 22:58:46 +04:00
success (0), error (1), failure (1), pending (2), no status (3)
`,
}
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() {
ref = args.FirstParam()
2013-05-29 22:58:46 +04:00
}
ref, err := git.Ref(ref)
utils.Check(err)
2013-05-30 06:10:18 +04:00
gh := github.New()
2013-06-09 03:42:34 +04:00
status, err := gh.CiStatus(ref)
2013-05-30 06:10:18 +04:00
utils.Check(err)
var state string
2013-06-11 08:49:45 +04:00
var targetURL string
2013-05-30 06:10:18 +04:00
var desc string
var exitCode int
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
desc = status.Description
}
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
}
fmt.Println(state)
2013-06-11 08:49:45 +04:00
if targetURL != "" {
fmt.Println(targetURL)
2013-05-30 06:10:18 +04:00
}
if desc != "" {
fmt.Println(desc)
}
os.Exit(exitCode)
2013-05-29 22:58:46 +04:00
}