Skip to content

Commit 0e18db2

Browse files
authored
Merge branch 'cli:trunk' into browse-commit
2 parents 0a49631 + 161de77 commit 0e18db2

File tree

8 files changed

+98
-20
lines changed

8 files changed

+98
-20
lines changed

pkg/cmd/browse/browse.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,8 @@ func runBrowse(opts *BrowseOptions) error {
157157
}
158158

159159
if opts.NoBrowserFlag {
160-
_, err := fmt.Fprintf(opts.IO.Out, "%s\n", url)
161-
return err
160+
fmt.Fprintf(opts.IO.Out, "%s\n", url)
161+
return nil
162162
} else {
163163
if opts.IO.IsStdoutTTY() {
164164
fmt.Fprintf(opts.IO.Out, "now opening %s in browser\n", url)

pkg/cmd/issue/shared/display.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func PrintIssues(io *iostreams.IOStreams, prefix string, totalCount int, issues
2222
issueNum = "#" + issueNum
2323
}
2424
issueNum = prefix + issueNum
25-
labels := IssueLabelList(issue)
25+
labels := IssueLabelList(issue, cs)
2626
if labels != "" && table.IsTTY() {
2727
labels = fmt.Sprintf("(%s)", labels)
2828
}
@@ -56,14 +56,14 @@ func truncateLabels(w int, t string) string {
5656
return fmt.Sprintf("(%s)", truncated)
5757
}
5858

59-
func IssueLabelList(issue api.Issue) string {
59+
func IssueLabelList(issue api.Issue, cs *iostreams.ColorScheme) string {
6060
if len(issue.Labels.Nodes) == 0 {
6161
return ""
6262
}
6363

6464
labelNames := make([]string, 0, len(issue.Labels.Nodes))
6565
for _, label := range issue.Labels.Nodes {
66-
labelNames = append(labelNames, label.Name)
66+
labelNames = append(labelNames, cs.HexToRGB(label.Color, label.Name))
6767
}
6868

6969
return strings.Join(labelNames, ", ")

pkg/cmd/issue/view/view.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ func viewRun(opts *ViewOptions) error {
125125
return nil
126126
}
127127

128-
return printRawIssuePreview(opts.IO.Out, issue)
128+
return printRawIssuePreview(opts.IO.Out, issue, opts.IO.ColorScheme())
129129
}
130130

131131
func findIssue(client *http.Client, baseRepoFn func() (ghrepo.Interface, error), selector string, loadComments bool) (*api.Issue, error) {
@@ -141,9 +141,9 @@ func findIssue(client *http.Client, baseRepoFn func() (ghrepo.Interface, error),
141141
return issue, err
142142
}
143143

144-
func printRawIssuePreview(out io.Writer, issue *api.Issue) error {
144+
func printRawIssuePreview(out io.Writer, issue *api.Issue, cs *iostreams.ColorScheme) error {
145145
assignees := issueAssigneeList(*issue)
146-
labels := shared.IssueLabelList(*issue)
146+
labels := shared.IssueLabelList(*issue, cs)
147147
projects := issueProjectList(*issue)
148148

149149
// Print empty strings for empty values so the number of metadata lines is consistent when
@@ -193,7 +193,7 @@ func printHumanIssuePreview(opts *ViewOptions, issue *api.Issue) error {
193193
fmt.Fprint(out, cs.Bold("Assignees: "))
194194
fmt.Fprintln(out, assignees)
195195
}
196-
if labels := shared.IssueLabelList(*issue); labels != "" {
196+
if labels := shared.IssueLabelList(*issue, cs); labels != "" {
197197
fmt.Fprint(out, cs.Bold("Labels: "))
198198
fmt.Fprintln(out, labels)
199199
}

pkg/cmd/pr/view/view.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ func printRawPrPreview(io *iostreams.IOStreams, pr *api.PullRequest) error {
139139

140140
reviewers := prReviewerList(*pr, cs)
141141
assignees := prAssigneeList(*pr)
142-
labels := prLabelList(*pr)
142+
labels := prLabelList(*pr, cs)
143143
projects := prProjectList(*pr)
144144

145145
fmt.Fprintf(out, "title:\t%s\n", pr.Title)
@@ -197,7 +197,7 @@ func printHumanPrPreview(opts *ViewOptions, pr *api.PullRequest) error {
197197
fmt.Fprint(out, cs.Bold("Assignees: "))
198198
fmt.Fprintln(out, assignees)
199199
}
200-
if labels := prLabelList(*pr); labels != "" {
200+
if labels := prLabelList(*pr, cs); labels != "" {
201201
fmt.Fprint(out, cs.Bold("Labels: "))
202202
fmt.Fprintln(out, labels)
203203
}
@@ -367,14 +367,14 @@ func prAssigneeList(pr api.PullRequest) string {
367367
return list
368368
}
369369

370-
func prLabelList(pr api.PullRequest) string {
370+
func prLabelList(pr api.PullRequest, cs *iostreams.ColorScheme) string {
371371
if len(pr.Labels.Nodes) == 0 {
372372
return ""
373373
}
374374

375375
labelNames := make([]string, 0, len(pr.Labels.Nodes))
376376
for _, label := range pr.Labels.Nodes {
377-
labelNames = append(labelNames, label.Name)
377+
labelNames = append(labelNames, cs.HexToRGB(label.Color, label.Name))
378378
}
379379

380380
list := strings.Join(labelNames, ", ")

pkg/cmd/run/watch/watch.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,9 @@ func watchRun(opts *WatchOptions) error {
123123

124124
if run.Status == shared.Completed {
125125
fmt.Fprintf(out, "Run %s (%s) has already completed with '%s'\n", cs.Bold(run.Name), cs.Cyanf("%d", run.ID), run.Conclusion)
126+
if opts.ExitStatus && run.Conclusion != shared.Success {
127+
return cmdutil.SilentError
128+
}
126129
return nil
127130
}
128131

pkg/cmd/run/watch/watch_test.go

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,21 @@ func TestWatchRun(t *testing.T) {
190190
},
191191
wantOut: "Run failed (1234) has already completed with 'failure'\n",
192192
},
193+
{
194+
name: "already completed, exit status",
195+
opts: &WatchOptions{
196+
RunID: "1234",
197+
ExitStatus: true,
198+
},
199+
httpStubs: func(reg *httpmock.Registry) {
200+
reg.Register(
201+
httpmock.REST("GET", "repos/OWNER/REPO/actions/runs/1234"),
202+
httpmock.JSONResponse(shared.FailedRun))
203+
},
204+
wantOut: "Run failed (1234) has already completed with 'failure'\n",
205+
wantErr: true,
206+
errMsg: "SilentError",
207+
},
193208
{
194209
name: "prompt, no in progress runs",
195210
tty: true,
@@ -307,13 +322,8 @@ func TestWatchRun(t *testing.T) {
307322
t.Run(tt.name, func(t *testing.T) {
308323
err := watchRun(tt.opts)
309324
if tt.wantErr {
310-
assert.Error(t, err)
311-
assert.Equal(t, tt.errMsg, err.Error())
312-
if !tt.opts.ExitStatus {
313-
return
314-
}
315-
}
316-
if !tt.opts.ExitStatus {
325+
assert.EqualError(t, err, tt.errMsg)
326+
} else {
317327
assert.NoError(t, err)
318328
}
319329
assert.Equal(t, tt.wantOut, stdout.String())

pkg/iostreams/color.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package iostreams
33
import (
44
"fmt"
55
"os"
6+
"strconv"
67
"strings"
78

89
"github.com/mgutz/ansi"
@@ -202,3 +203,14 @@ func (c *ColorScheme) ColorFromString(s string) func(string) string {
202203

203204
return fn
204205
}
206+
207+
func (c *ColorScheme) HexToRGB(hex string, x string) string {
208+
if !c.enabled || !c.is256enabled {
209+
return x
210+
}
211+
212+
r, _ := strconv.ParseInt(hex[0:2], 16, 64)
213+
g, _ := strconv.ParseInt(hex[2:4], 16, 64)
214+
b, _ := strconv.ParseInt(hex[4:6], 16, 64)
215+
return fmt.Sprintf("\033[38;2;%d;%d;%dm%s\033[0m", r, g, b, x)
216+
}

pkg/iostreams/color_test.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package iostreams
33
import (
44
"os"
55
"testing"
6+
7+
"github.com/stretchr/testify/assert"
68
)
79

810
func TestEnvColorDisabled(t *testing.T) {
@@ -143,3 +145,54 @@ func TestEnvColorForced(t *testing.T) {
143145
})
144146
}
145147
}
148+
149+
func Test_HextoRGB(t *testing.T) {
150+
tests := []struct {
151+
name string
152+
hex string
153+
arg string
154+
expectedOutput string
155+
expectedError bool
156+
cs *ColorScheme
157+
}{
158+
{
159+
name: "Colored red enabled color",
160+
hex: "fc0303",
161+
arg: "red",
162+
expectedOutput: "\033[38;2;252;3;3mred\033[0m",
163+
cs: NewColorScheme(true, true),
164+
},
165+
{
166+
name: "Failed colored red enabled color",
167+
hex: "fc0303",
168+
arg: "red",
169+
expectedOutput: "\033[38;2;252;2;3mred\033[0m",
170+
expectedError: true,
171+
cs: NewColorScheme(true, true),
172+
},
173+
{
174+
name: "Colored red disabled color",
175+
hex: "fc0303",
176+
arg: "red",
177+
expectedOutput: "red",
178+
cs: NewColorScheme(false, false),
179+
},
180+
{
181+
name: "Failed colored red disabled color",
182+
hex: "fc0303",
183+
arg: "red",
184+
expectedOutput: "\033[38;2;252;3;3mred\033[0m",
185+
expectedError: true,
186+
cs: NewColorScheme(false, false),
187+
},
188+
}
189+
190+
for _, tt := range tests {
191+
output := tt.cs.HexToRGB(tt.hex, tt.arg)
192+
if tt.expectedError {
193+
assert.NotEqual(t, tt.expectedOutput, output)
194+
} else {
195+
assert.Equal(t, tt.expectedOutput, output)
196+
}
197+
}
198+
}

0 commit comments

Comments
 (0)