-
Notifications
You must be signed in to change notification settings - Fork 26
/
utils.go
159 lines (143 loc) · 4.24 KB
/
utils.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package main
import (
"bytes"
"fmt"
"github.com/Netflix-Skunkworks/go-jira"
ui "github.com/gizak/termui"
"gopkg.in/coryb/yaml.v2"
"io/ioutil"
"os"
"strings"
)
func countLabelsFromQuery(query string) map[string]int {
counts := make(map[string]int)
data, _ := runJiraQuery(query)
issues := data.(map[string]interface{})["issues"].([]interface{})
for _, issue := range issues {
issueLabels := issue.(map[string]interface{})["fields"].(map[string]interface{})["labels"]
for _, v := range issueLabels.([]interface{}) {
label := v.(string)
counts[label] = counts[label] + 1
}
}
return counts
}
func runJiraCmdEdit(ticketId string) {
opts := getJiraOpts()
c := jira.New(opts)
ui.Close()
c.CmdEdit(ticketId)
log.Notice("Regrettably, need to exit after edit. See https://github.com/mikepea/go-jira-ui/issues/8")
os.Exit(0)
}
func runJiraCmdComment(ticketId string) {
opts := getJiraOpts()
c := jira.New(opts)
ui.Close()
c.CmdComment(ticketId)
log.Notice("Regrettably, need to exit after comment. See https://github.com/mikepea/go-jira-ui/issues/8")
os.Exit(0)
}
func runJiraQuery(query string) (interface{}, error) {
opts := getJiraOpts()
opts["query"] = query
c := jira.New(opts)
return c.FindIssues()
}
func JiraQueryAsStrings(query string) []string {
opts := getJiraOpts()
opts["query"] = query
c := jira.New(opts)
data, _ := c.FindIssues()
buf := new(bytes.Buffer)
// TODO: this is a nasty hack, make it less so
// template must start {key} and (for labels view) end with '-- labels()'
template := c.GetTemplate("jira_ui_list")
if template == "" {
template = default_list_template
}
jira.RunTemplate(template, data, buf)
return strings.Split(strings.TrimSpace(buf.String()), "\n")
}
func JiraTicketAsStrings(id string) []string {
opts := getJiraOpts()
c := jira.New(opts)
data, _ := c.ViewIssue(id)
buf := new(bytes.Buffer)
jira.RunTemplate(c.GetTemplate("view"), data, buf)
return strings.Split(strings.TrimSpace(buf.String()), "\n")
}
func parseYaml(file string, v map[string]interface{}) {
if fh, err := ioutil.ReadFile(file); err == nil {
log.Debug("Parsing YAML file: %s", file)
yaml.Unmarshal(fh, &v)
}
}
func loadConfigs(opts map[string]interface{}) {
paths := jira.FindParentPaths(".jira.d/jira-ui-config.yml")
paths = append(jira.FindParentPaths(".jira.d/config.yml"), paths...)
paths = append([]string{"/etc/go-jira-ui.yml", "/etc/go-jira.yml"}, paths...)
// iterate paths in reverse
for i := len(paths) - 1; i >= 0; i-- {
file := paths[i]
if _, err := os.Stat(file); err == nil {
tmp := make(map[string]interface{})
parseYaml(file, tmp)
for k, v := range tmp {
if _, ok := opts[k]; !ok {
log.Debug("Setting %q to %#v from %s", k, v, file)
opts[k] = v
}
}
}
}
}
func doLogin(opts map[string]interface{}) error {
c := jira.New(opts)
fmt.Printf("Logging in as %s:\n", opts["user"])
return c.CmdLogin()
}
func ensureLoggedIntoJira() error {
homeDir := os.Getenv("HOME")
opts := getJiraOpts()
testSessionQuery := fmt.Sprintf("reporter = %s", opts["user"])
if _, err := os.Stat(fmt.Sprintf("%s/.jira.d/cookies.js", homeDir)); err != nil {
return doLogin(opts)
} else if data, err := runJiraQuery(testSessionQuery); err != nil {
return doLogin(opts)
} else if val, ok := data.(map[string]interface{})["errorMessages"]; ok {
if len(val.([]interface{})) > 0 {
return doLogin(opts)
}
}
return nil
}
func getJiraOpts() map[string]interface{} {
user := os.Getenv("USER")
home := os.Getenv("HOME")
defaultQueryFields := "summary,created,updated,priority,status,reporter,assignee,labels"
defaultSort := "priority asc, created"
defaultMaxResults := 1000
opts := make(map[string]interface{})
defaults := map[string]interface{}{
"user": user,
"endpoint": os.Getenv("JIRA_ENDPOINT"),
"queryfields": defaultQueryFields,
"directory": fmt.Sprintf("%s/.jira.d/templates", home),
"sort": defaultSort,
"max_results": defaultMaxResults,
"method": "GET",
"quiet": true,
}
loadConfigs(opts)
for k, v := range defaults {
if _, ok := opts[k]; !ok {
log.Debug("Setting %q to %#v from defaults", k, v)
opts[k] = v
}
}
return opts
}
func lastLineDisplayed(ls *ui.List, firstLine int, correction int) int {
return firstLine + ls.Height - correction
}