Simplify parsing API issues
This commit is contained in:
parent
eabcae84c7
commit
a650cbe002
5 changed files with 61 additions and 106 deletions
|
|
@ -12,30 +12,23 @@ type IssuesPayload struct {
|
|||
}
|
||||
|
||||
type Issue struct {
|
||||
Number int
|
||||
Title string
|
||||
URL string
|
||||
Labels []string
|
||||
TotalLabelCount int
|
||||
Number int
|
||||
Title string
|
||||
URL string
|
||||
|
||||
Labels struct {
|
||||
Nodes []IssueLabel
|
||||
TotalCount int
|
||||
}
|
||||
}
|
||||
|
||||
type IssueLabel struct {
|
||||
Name string
|
||||
}
|
||||
|
||||
type apiIssues struct {
|
||||
Issues struct {
|
||||
Edges []struct {
|
||||
Node struct {
|
||||
Number int
|
||||
Title string
|
||||
URL string
|
||||
Labels struct {
|
||||
Edges []struct {
|
||||
Node struct {
|
||||
Name string
|
||||
}
|
||||
}
|
||||
TotalCount int
|
||||
}
|
||||
}
|
||||
}
|
||||
Nodes []Issue
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -43,11 +36,10 @@ const fragments = `
|
|||
fragment issue on Issue {
|
||||
number
|
||||
title
|
||||
url
|
||||
labels(first: 3) {
|
||||
edges {
|
||||
node {
|
||||
name
|
||||
}
|
||||
nodes {
|
||||
name
|
||||
}
|
||||
totalCount
|
||||
}
|
||||
|
|
@ -104,28 +96,22 @@ func IssueStatus(client *Client, ghRepo Repo, currentUsername string) (*IssuesPa
|
|||
query($owner: String!, $repo: String!, $since: DateTime!, $viewer: String!, $per_page: Int = 10) {
|
||||
assigned: repository(owner: $owner, name: $repo) {
|
||||
issues(filterBy: {assignee: $viewer, states: OPEN}, first: $per_page, orderBy: {field: CREATED_AT, direction: DESC}) {
|
||||
edges {
|
||||
node {
|
||||
...issue
|
||||
}
|
||||
nodes {
|
||||
...issue
|
||||
}
|
||||
}
|
||||
}
|
||||
mentioned: repository(owner: $owner, name: $repo) {
|
||||
issues(filterBy: {mentioned: $viewer, states: OPEN}, first: $per_page, orderBy: {field: CREATED_AT, direction: DESC}) {
|
||||
edges {
|
||||
node {
|
||||
...issue
|
||||
}
|
||||
nodes {
|
||||
...issue
|
||||
}
|
||||
}
|
||||
}
|
||||
recent: repository(owner: $owner, name: $repo) {
|
||||
issues(filterBy: {since: $since, states: OPEN}, first: $per_page, orderBy: {field: CREATED_AT, direction: DESC}) {
|
||||
edges {
|
||||
node {
|
||||
...issue
|
||||
}
|
||||
nodes {
|
||||
...issue
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -148,14 +134,10 @@ func IssueStatus(client *Client, ghRepo Repo, currentUsername string) (*IssuesPa
|
|||
return nil, err
|
||||
}
|
||||
|
||||
assigned := convertAPIToIssues(resp.Assigned)
|
||||
mentioned := convertAPIToIssues(resp.Mentioned)
|
||||
recent := convertAPIToIssues(resp.Recent)
|
||||
|
||||
payload := IssuesPayload{
|
||||
assigned,
|
||||
mentioned,
|
||||
recent,
|
||||
Assigned: resp.Assigned.Issues.Nodes,
|
||||
Mentioned: resp.Mentioned.Issues.Nodes,
|
||||
Recent: resp.Recent.Issues.Nodes,
|
||||
}
|
||||
|
||||
return &payload, nil
|
||||
|
|
@ -191,10 +173,8 @@ func IssueList(client *Client, ghRepo Repo, state string, labels []string, assig
|
|||
query($owner: String!, $repo: String!, $limit: Int, $states: [IssueState!] = OPEN, $labels: [String!], $assignee: String) {
|
||||
repository(owner: $owner, name: $repo) {
|
||||
issues(first: $limit, orderBy: {field: CREATED_AT, direction: DESC}, states: $states, labels: $labels, filterBy: {assignee: $assignee}) {
|
||||
edges {
|
||||
node {
|
||||
...issue
|
||||
}
|
||||
nodes {
|
||||
...issue
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -221,27 +201,9 @@ func IssueList(client *Client, ghRepo Repo, state string, labels []string, assig
|
|||
return nil, err
|
||||
}
|
||||
|
||||
issues := convertAPIToIssues(resp.Repository)
|
||||
return issues, nil
|
||||
}
|
||||
|
||||
func convertAPIToIssues(i apiIssues) []Issue {
|
||||
var issues []Issue
|
||||
for _, edge := range i.Issues.Edges {
|
||||
var labels []string
|
||||
for _, labelEdge := range edge.Node.Labels.Edges {
|
||||
labels = append(labels, labelEdge.Node.Name)
|
||||
}
|
||||
|
||||
issue := Issue{
|
||||
Number: edge.Node.Number,
|
||||
Title: edge.Node.Title,
|
||||
URL: edge.Node.URL,
|
||||
Labels: labels,
|
||||
TotalLabelCount: edge.Node.Labels.TotalCount,
|
||||
}
|
||||
issues := []Issue{}
|
||||
for _, issue := range resp.Repository.Issues.Nodes {
|
||||
issues = append(issues, issue)
|
||||
}
|
||||
|
||||
return issues
|
||||
return issues, nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -245,14 +245,27 @@ func issueCreate(cmd *cobra.Command, args []string) error {
|
|||
func printIssues(prefix string, issues ...api.Issue) {
|
||||
for _, issue := range issues {
|
||||
number := utils.Green("#" + strconv.Itoa(issue.Number))
|
||||
var coloredLabels string
|
||||
if len(issue.Labels) > 0 {
|
||||
var ellipse string
|
||||
if issue.TotalLabelCount > len(issue.Labels) {
|
||||
ellipse = "…"
|
||||
}
|
||||
coloredLabels = utils.Gray(fmt.Sprintf(" (%s%s)", strings.Join(issue.Labels, ", "), ellipse))
|
||||
coloredLabels := labelList(issue)
|
||||
if coloredLabels != "" {
|
||||
coloredLabels = utils.Gray(fmt.Sprintf(" (%s)", coloredLabels))
|
||||
}
|
||||
fmt.Printf("%s%s %s %s\n", prefix, number, truncate(70, issue.Title), coloredLabels)
|
||||
fmt.Printf("%s%s %s%s\n", prefix, number, truncate(70, issue.Title), coloredLabels)
|
||||
}
|
||||
}
|
||||
|
||||
func labelList(issue api.Issue) string {
|
||||
if len(issue.Labels.Nodes) == 0 {
|
||||
return ""
|
||||
}
|
||||
|
||||
labelNames := []string{}
|
||||
for _, label := range issue.Labels.Nodes {
|
||||
labelNames = append(labelNames, label.Name)
|
||||
}
|
||||
|
||||
list := strings.Join(labelNames, ", ")
|
||||
if issue.Labels.TotalCount > len(issue.Labels.Nodes) {
|
||||
list += ", …"
|
||||
}
|
||||
return list
|
||||
}
|
||||
|
|
|
|||
|
|
@ -55,9 +55,9 @@ func TestIssueList(t *testing.T) {
|
|||
}
|
||||
|
||||
expectedIssues := []*regexp.Regexp{
|
||||
regexp.MustCompile(`#1.*won`),
|
||||
regexp.MustCompile(`#2.*too`),
|
||||
regexp.MustCompile(`#4.*fore`),
|
||||
regexp.MustCompile(`(?m)^1\t.*won`),
|
||||
regexp.MustCompile(`(?m)^2\t.*too`),
|
||||
regexp.MustCompile(`(?m)^4\t.*fore`),
|
||||
}
|
||||
|
||||
for _, r := range expectedIssues {
|
||||
|
|
|
|||
20
test/fixtures/issueList.json
vendored
20
test/fixtures/issueList.json
vendored
|
|
@ -2,57 +2,45 @@
|
|||
"data": {
|
||||
"repository": {
|
||||
"issues": {
|
||||
"edges": [
|
||||
"nodes": [
|
||||
{
|
||||
"node": {
|
||||
"number": 1,
|
||||
"title": "number won",
|
||||
"url": "https://wow.com",
|
||||
"labels": {
|
||||
"edges": [
|
||||
"nodes": [
|
||||
{
|
||||
"node": {
|
||||
"name": "label"
|
||||
}
|
||||
}
|
||||
],
|
||||
"totalCount": 1
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"node": {
|
||||
"number": 2,
|
||||
"title": "number too",
|
||||
"url": "https://wow.com",
|
||||
"labels": {
|
||||
"edges": [
|
||||
"nodes": [
|
||||
{
|
||||
"node": {
|
||||
"name": "label"
|
||||
}
|
||||
}
|
||||
],
|
||||
"totalCount": 1
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"node": {
|
||||
"number": 4,
|
||||
"title": "number fore",
|
||||
"url": "https://wow.com",
|
||||
"labels": {
|
||||
"edges": [
|
||||
"nodes": [
|
||||
{
|
||||
"node": {
|
||||
"name": "label"
|
||||
}
|
||||
}
|
||||
],
|
||||
"totalCount": 1
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
|
|
|
|||
14
test/fixtures/issueStatus.json
vendored
14
test/fixtures/issueStatus.json
vendored
|
|
@ -2,43 +2,35 @@
|
|||
"data": {
|
||||
"assigned": {
|
||||
"issues": {
|
||||
"edges": [
|
||||
"nodes": [
|
||||
{
|
||||
"node": {
|
||||
"number": 9,
|
||||
"title": "corey thinks squash tastes bad"
|
||||
}
|
||||
},
|
||||
{
|
||||
"node": {
|
||||
"number": 10,
|
||||
"title": "broccoli is a superfood"
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
"mentioned": {
|
||||
"issues": {
|
||||
"edges": [
|
||||
"nodes": [
|
||||
{
|
||||
"node": {
|
||||
"number": 8,
|
||||
"title": "rabbits eat carrots"
|
||||
}
|
||||
},
|
||||
{
|
||||
"node": {
|
||||
"number": 11,
|
||||
"title": "swiss chard is neutral"
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
"recent": {
|
||||
"issues": {
|
||||
"edges": []
|
||||
"nodes": []
|
||||
}
|
||||
},
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue