-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
176 lines (159 loc) · 3.54 KB
/
main.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package main
import (
"bytes"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/http/cookiejar"
"net/url"
"os"
"os/exec"
"regexp"
"strings"
jira "github.com/andygrunwald/go-jira"
)
func main() {
// Don't modify if a pre-existing message exists
if len(os.Args) > 2 {
return
}
jsessionid, err := getGitConfig("jira.jsessionid")
if err != nil {
log.Fatal("Please set jira.jsessionid in you git config")
}
jurl, err := getGitConfig("jira.url")
if err != nil {
log.Fatal("Please set jira.url in your git config")
}
jregex, err := getGitConfig("jira.regexp")
if err != nil {
log.Fatal("Please set jira.regexp in your git config")
}
re, err := regexp.Compile(jregex)
if err != nil {
log.Fatal(err)
}
// Get the name from the branch
out, err := exec.Command("git", "rev-parse", "--abbrev-ref", "HEAD").CombinedOutput()
if err != nil {
log.Fatal("Could not get branch name", err)
}
name := string(re.Find(out))
if name == "" {
// We still want to allow regular committing
fmt.Println("No matching branch found")
return
}
issue, err := fetchIssue(name, jsessionid, jurl)
if err != nil {
// We still want to allow regular committing
fmt.Println(err)
return
}
err = ioutil.WriteFile(os.Args[1], formatForGit(issue), 0644)
if err != nil {
log.Fatal(err)
}
}
func getGitConfig(key string) (string, error) {
out, err := exec.Command("git", "config", "--get", key).CombinedOutput()
return string(bytes.TrimSpace(out)), err
}
const lineLen = 76
// 1) Puts the summary at the top
// 2) wraps long lines at 76 characters
// 3) prettifies some jira formatting
// - {noformat} gets indented by 4 spaces
// - TODO more things like tables, quotes, and numbered lists
func formatForGit(i *jira.Issue) []byte {
var buf bytes.Buffer
// Summary
fmt.Fprintf(&buf, "%v: %v\n\n", i.Key, i.Fields.Summary)
// Wrap some lines
str := strings.Replace(i.Fields.Description, "\r", "", -1)
pars := strings.Split(str, "\n")
count := 0
noformat := false
for i := range pars {
if noformat {
if pars[i] == "{noformat}" {
noformat = false
} else {
fmt.Fprintf(&buf, " %v\n", pars[i])
}
continue
}
if pars[i] == "{noformat}" {
if count != 0 {
buf.WriteString("\n\n")
count = 0
}
noformat = true
continue
}
if len(pars[i]) == 0 {
// Double newline
if count != 0 {
buf.WriteString("\n")
count = 0
}
buf.WriteString("\n")
continue
}
firstChar := pars[i][0]
switch {
case firstChar >= 'a' && firstChar <= 'z':
case firstChar >= 'A' && firstChar <= 'Z':
case firstChar >= '0' && firstChar <= '9':
default:
if count != 0 {
buf.WriteString("\n")
}
buf.WriteString(pars[i])
buf.WriteString("\n")
count = 0
continue
}
words := strings.Fields(pars[i])
for _, word := range words {
if count+len(word) > lineLen {
buf.WriteString("\n")
count = 0
}
fmt.Fprintf(&buf, "%v ", word)
count += len(word)
}
}
return buf.Bytes()
}
func fetchIssue(name, jsessionid, jurl string) (*jira.Issue, error) {
jar, err := cookiejar.New(nil)
if err != nil {
return nil, err
}
jurlp, err := url.Parse(jurl)
if err != nil {
return nil, err
}
jar.SetCookies(jurlp, []*http.Cookie{{
Name: "JSESSIONID",
Value: jsessionid,
}})
cl := &http.Client{
Jar: jar,
}
c, err := jira.NewClient(cl, jurl)
if err != nil {
return nil, err
}
issue, resp, err := c.Issue.Get(name)
if err != nil {
if resp != nil {
body, _ := ioutil.ReadAll(resp.Body)
err = fmt.Errorf("%v %v", err, string(body))
}
return nil, err
}
return issue, nil
}