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

Merge branch 'darrenwee-implement-label-list-color'

......@@ -19,6 +19,7 @@ var (
Usage: `
issue [-a <ASSIGNEE>] [-c <CREATOR>] [-@ <USER>] [-s <STATE>] [-f <FORMAT>] [-M <MILESTONE>] [-l <LABELS>] [-d <DATE>] [-o <SORT_KEY> [-^]] [-L <LIMIT>]
issue create [-oc] [-m <MESSAGE>|-F <FILE>] [-a <USERS>] [-M <MILESTONE>] [-l <LABELS>]
issue labels [--color]
`,
Long: `Manage GitHub issues for the current project.
......@@ -29,6 +30,9 @@ With no arguments, show a list of open issues.
* _create_:
Open an issue in the current project.
* _labels_:
List the labels available in this repository.
## Options:
-a, --assignee <ASSIGNEE>
Display only issues assigned to <ASSIGNEE>.
......@@ -136,6 +140,9 @@ With no arguments, show a list of open issues.
--include-pulls
Include pull requests as well as issues.
--color
Enable colored output for labels list.
`,
}
......@@ -146,6 +153,13 @@ With no arguments, show a list of open issues.
Long: "Open an issue in the current project.",
}
cmdLabel = &Command{
Key: "labels",
Run: listLabels,
Usage: "issue labels [--color]",
Long: "List the labels available in this repository.",
}
flagIssueAssignee,
flagIssueState,
flagIssueFormat,
......@@ -170,6 +184,8 @@ With no arguments, show a list of open issues.
flagIssueLabels listFlag
flagIssueLimit int
flagLabelsColorize bool
)
func init() {
......@@ -195,7 +211,10 @@ func init() {
cmdIssue.Flag.BoolVarP(&flagIssueIncludePulls, "include-pulls", "", false, "INCLUDE_PULLS")
cmdIssue.Flag.IntVarP(&flagIssueLimit, "limit", "L", -1, "LIMIT")
cmdLabel.Flag.BoolVarP(&flagLabelsColorize, "color", "", false, "COLORIZE")
cmdIssue.Use(cmdCreateIssue)
cmdIssue.Use(cmdLabel)
CmdRunner.Use(cmdIssue)
}
......@@ -282,12 +301,7 @@ func formatIssuePlaceholders(issue github.Issue, colorize bool) map[string]strin
utils.Check(err)
}
textColor := 16
if color.Brightness() < 0.65 {
textColor = 15
}
labelStrings = append(labelStrings, fmt.Sprintf("\033[38;5;%d;48;2;%d;%d;%dm %s \033[m", textColor, color.Red, color.Green, color.Blue, label.Name))
labelStrings = append(labelStrings, colorizeLabel(label, color))
rawLabels = append(rawLabels, label.Name)
}
......@@ -431,3 +445,47 @@ text is the title and the rest is the description.`, project))
messageBuilder.Cleanup()
}
func listLabels(cmd *Command, args *Args) {
localRepo, err := github.LocalRepo()
utils.Check(err)
project, err := localRepo.MainProject()
utils.Check(err)
gh := github.NewClient(project.Host)
args.NoForward()
if args.Noop {
ui.Printf("Would request list of labels for %s\n", project)
return
}
labels, err := gh.FetchLabels(project)
utils.Check(err)
for _, label := range labels {
ui.Printf(formatLabel(label, flagLabelsColorize))
}
}
func formatLabel(label github.IssueLabel, colorize bool) string {
if colorize {
if color, err := utils.NewColor(label.Color); err == nil {
return fmt.Sprintf("%s\n", colorizeLabel(label, color))
}
}
return fmt.Sprintf("%s\n", label.Name)
}
func colorizeLabel(label github.IssueLabel, color *utils.Color) string {
return fmt.Sprintf("\033[38;5;%d;48;2;%d;%d;%dm %s \033[m",
getSuitableLabelTextColor(color), color.Red, color.Green, color.Blue, label.Name)
}
func getSuitableLabelTextColor(color *utils.Color) int {
if color.Brightness() < 0.65 {
return 15 // white text
}
return 16 // black text
}
......@@ -452,3 +452,24 @@ Feature: hub issue
"""
https://github.com/github/hub/issues/1337\n
"""
Scenario: Fetch issue labels
Given the GitHub API server:
"""
get('/repos/github/hub/labels') {
json [
{ :name => "bug",
:color => "ff0000",
},
{ :name => "feature",
:color => "00ff00",
},
]
}
"""
When I successfully run `hub issue labels`
Then the output should contain exactly:
"""
bug
feature\n
"""
......@@ -607,6 +607,34 @@ func (client *Client) UpdateIssue(project *Project, issueNumber int, params map[
return
}
func (client *Client) FetchLabels(project *Project) (labels []IssueLabel, err error) {
api, err := client.simpleApi()
if err != nil {
return
}
path := fmt.Sprintf("repos/%s/%s/labels?per_page=100", project.Owner, project.Name)
labels = []IssueLabel{}
var res *simpleResponse
for path != "" {
res, err = api.Get(path)
if err = checkStatus(200, "fetching labels", res, err); err != nil {
return
}
path = res.Link("next")
labelsPage := []IssueLabel{}
if err = res.Unmarshal(&labelsPage); err != nil {
return
}
labels = append(labels, labelsPage...)
}
return
}
func (client *Client) CurrentUser() (user *User, err error) {
api, err := client.simpleApi()
if err != nil {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册