/
issue.go
98 lines (84 loc) · 2.22 KB
/
issue.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package main
import (
"encoding/json"
"fmt"
"strconv"
"strings"
"time"
"github.com/fatih/color"
)
// Issue is the internal representation of an issue
type Issue struct {
ID string `json:"id"`
Number int `json:"number"`
Status string `json:"status"`
Title string `json:"title,omitempty"`
Body string `json:"body,omitempty"`
Assignee string `json:"assignee,omitempty"`
Comments []Comment `json:"comments,omitempty"`
URL string `json:"url,omitempty"`
Repo string `json:"repo"`
Owner string `json:"owner"`
}
func (i *Issue) Path() string {
s := strconv.Itoa(i.Number)
return i.Owner + "/" + i.Repo + "/" + s
}
// Comments is a collection/slice of comments
type Comments []Comment
// Comment is the internal representation of an issue comment
type Comment struct {
ID int `json:"id,omitempty"`
Body string `json:"body,omitempty"`
User string `json:"user,omitempty"`
CreatedAt time.Time `json:"created_at,omitempty"`
UpdatedAt time.Time `json:"updated_at,omitempty"`
URL string `json:"url,omitempty"`
HTMLURL string `json:"html_url,omitempty"`
IssueURL string `json:"issue_url,omitempty"`
}
// Issues is a collection/slice of issues
type Issues []*Issue
type Error struct {
Message string `json:"error"`
}
func printIssuesList(body []byte) error {
issues := Issues{}
err := json.Unmarshal(body, &issues)
if err != nil {
return err
}
prev := ""
for _, issue := range issues {
if issue.Repo != prev {
repo := strings.Replace(issue.Repo, "\n", "", 1)
if prev != "" {
fmt.Println("\n")
}
color.Blue("\nRepo: " + issue.Owner + "/" + repo + ":")
prev = issue.Repo
}
title := color.YellowString(issue.Title)
fmt.Printf("\n - " + title + " - [#" + issue.Path() + "]")
}
fmt.Printf("\n\n")
return nil
}
func printIssueDetails(body []byte) {
issue := Issue{}
err := json.Unmarshal(body, &issue)
if err != nil {
color.Red(err.Error())
return
}
if issue.Title != "" {
color.Green("[ #" + issue.Path() + "] " + issue.Title)
fmt.Println(issue.Body)
color.Blue(issue.URL)
fmt.Println(issue.Status)
} else {
err := Error{}
json.Unmarshal(body, &err)
color.Red("ERROR: " + err.Message)
}
}