提交 df7b30a0 编写于 作者: M Mislav Marohnić

[ci-status] Add support for `--format` string

上级 cadc8d28
......@@ -20,6 +20,19 @@ var cmdCiStatus = &Command{
-v, --verbose
Print detailed report of all status checks and their URLs.
-f, --format <FORMAT>
Pretty print all status checks using <FORMAT> (implies '--verbose'). See the
"PRETTY FORMATS" section of git-log(1) for some additional details on how
placeholders are used in format. The available placeholders for issues are:
%U: the URL of this status check
%S: check state (e.g. "success", "failure")
%sC: set color to red, green, or yellow, depending on state
%t: name of the status check
<COMMIT>
A commit SHA or branch name (default: "HEAD").
......@@ -107,8 +120,9 @@ func ciStatus(cmd *Command, args *Args) {
exitCode = 3
}
if args.Flag.Bool("--verbose") && len(response.Statuses) > 0 {
verboseFormat(response.Statuses)
verbose := args.Flag.Bool("--verbose") || args.Flag.HasReceived("--format")
if verbose && len(response.Statuses) > 0 {
verboseFormat(response.Statuses, args.Flag.Value("--format"))
} else {
if state != "" {
ui.Println(state)
......@@ -121,7 +135,7 @@ func ciStatus(cmd *Command, args *Args) {
}
}
func verboseFormat(statuses []github.CIStatus) {
func verboseFormat(statuses []github.CIStatus, formatString string) {
colorize := ui.IsTerminal(os.Stdout)
contextWidth := 0
......@@ -153,15 +167,26 @@ func verboseFormat(statuses []github.CIStatus) {
color = 33
}
placeholders := map[string]string{
"S": status.State,
"sC": "",
"t": status.Context,
"U": status.TargetUrl,
}
if colorize {
stateMarker = fmt.Sprintf("\033[%dm%s\033[0m", color, stateMarker)
placeholders["sC"] = fmt.Sprintf("\033[%dm", color)
}
if status.TargetUrl == "" {
ui.Printf("%s\t%s\n", stateMarker, status.Context)
} else {
ui.Printf("%s\t%-*s\t%s\n", stateMarker, contextWidth, status.Context, status.TargetUrl)
format := formatString
if format == "" {
if status.TargetUrl == "" {
format = fmt.Sprintf("%%sC%s%%Creset\t%%t\n", stateMarker)
} else {
format = fmt.Sprintf("%%sC%s%%Creset\t%%<(%d)%%t\t%%U\n", stateMarker, contextWidth)
}
}
ui.Print(ui.Expand(format, placeholders, colorize))
}
}
......
......@@ -55,6 +55,40 @@ Feature: hub ci-status
"""
And the exit status should be 1
Scenario: Multiple statuses with format string
Given there is a commit named "the_sha"
Given the remote commit states of "michiels/pencilbox" "the_sha" are:
"""
{ :state => "error",
:statuses => [
{ :state => "success",
:context => "continuous-integration/travis-ci/push",
:target_url => "https://travis-ci.org/michiels/pencilbox/builds/1234567" },
{ :state => "success",
:context => "continuous-integration/travis-ci/ants",
:target_url => "https://travis-ci.org/michiels/pencilbox/builds/1234568" },
{ :state => "pending",
:context => "continuous-integration/travis-ci/merge",
:target_url => nil },
{ :state => "error",
:context => "whatevs!" },
{ :state => "failure",
:context => "GitHub CLA",
:target_url => "https://cla.github.com/michiels/pencilbox/accept/mislav" },
]
}
"""
When I run `hub ci-status the_sha --format '%S: %t (%U)%n'`
Then the output should contain exactly:
"""
failure: GitHub CLA (https://cla.github.com/michiels/pencilbox/accept/mislav)
error: whatevs! ()
pending: continuous-integration/travis-ci/merge ()
success: continuous-integration/travis-ci/ants (https://travis-ci.org/michiels/pencilbox/builds/1234568)
success: continuous-integration/travis-ci/push (https://travis-ci.org/michiels/pencilbox/builds/1234567)\n
"""
And the exit status should be 1
Scenario: Exit status 1 for 'error' and 'failure'
Given the remote commit state of "michiels/pencilbox" "HEAD" is "error"
When I run `hub ci-status`
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册