-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReleaseNotes.go
More file actions
179 lines (147 loc) · 4.38 KB
/
Copy pathReleaseNotes.go
File metadata and controls
179 lines (147 loc) · 4.38 KB
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
177
178
179
package main
import (
"bytes"
"encoding/json"
"fmt"
"html"
"html/template"
"io/ioutil"
"net/http"
)
// Config structs
type ConfigContainer struct {
IssuesUrl string `json:"issuesUrl"`
Jql string `json:"jql"`
Fields []string `json:"fields"`
HighlightField string `json:"highlightField"`
HeadsUpField string `json:"headsUpField"`
MaxResults int64 `json:"maxResults"`
Username string `json:"username"`
Password string `json:"password"`
IssueTypesSortOrder []string `json:"issueTypesSortOrder"`
ReleaseDescriptionLabel string `json:"releaseDescriptionLabel"`
}
// Issues imports structs
type IssuesJson struct {
StartAt int64 `json:"startAt"`
MaxResult int64 `json:"maxResults"`
Total int64 `json:"total"`
Issues []Issues `json:"issues"`
}
// -- Issues structs
type Issues struct {
Key string
Self string
Fields Fields `json:"fields"`
}
type Fields interface {
}
// --- Output structs
type OutputData struct {
Version string
ReleaseDate string
IssueTypes map[string][]Issues
Dependencies []Dependencies
IssueTypesSortOrder []string
ReleaseDescription string
}
type Dependencies struct {
Dependency string `json:"dependency"`
Versions []string `json:"versions"`
}
func GenerateReleaseNotes(configData []byte, templateFile, version, releaseDate, dependenciesData string) {
var dependencies []Dependencies
var config ConfigContainer
configInfo, err2 := template.New("hello").Parse(string(configData))
assertError(err2)
processedConfig := new(bytes.Buffer)
type ConfigData struct {
Version string
Login string
Password string
}
err3 := configInfo.Execute(processedConfig, ConfigData{Version: version, Login: login, Password: password})
assertError(err3)
err := json.Unmarshal(processedConfig.Bytes(), &config)
assertError(err)
issues := getIssuesFromUrl(config)
if dependenciesData != "" {
err = json.Unmarshal([]byte(dependenciesData), &dependencies)
assertError(err)
}
var output = OutputData{
Version: version,
ReleaseDate: releaseDate,
IssueTypes: make(map[string][]Issues),
Dependencies: dependencies,
}
if len(config.IssueTypesSortOrder) == 0 {
config.IssueTypesSortOrder = getDefaultSortOrder()
}
for i := 0; i < len(issues); i++ {
issue := issues[i]
fields := issue.Fields.(map[string]interface{})
if fields["issuetype"] != nil {
issueType := fields["issuetype"].(map[string]interface{})
var typeKey = ""
if issueType["name"] != nil {
typeKey = string(issueType["name"].(string))
}
if output.IssueTypes[typeKey] == nil {
output.IssueTypes[typeKey] = []Issues{issue}
} else {
output.IssueTypes[typeKey] = append(output.IssueTypes[typeKey], issue)
}
} else {
output.IssueTypes[""] = []Issues{issue}
}
if fields["labels"] != nil {
if config.ReleaseDescriptionLabel != "" {
labels := fields["labels"].([]interface{})
for j := 0; j < len(labels); j++ {
if labels[j] == config.ReleaseDescriptionLabel {
if fields["description"] != nil {
output.ReleaseDescription = fields["description"].(string)
}
}
}
}
}
}
output.IssueTypesSortOrder = config.IssueTypesSortOrder
reportTemplate, err := template.New("report").Parse(templateFile)
assertError(err)
var out bytes.Buffer
err = reportTemplate.Execute(&out, output)
fmt.Println(html.UnescapeString(out.String()))
assertError(err)
}
func getDefaultSortOrder() []string {
return []string{"New Feature", "Task", "Bug", "Epic"}
}
func getIssuesFromUrl(config ConfigContainer) []Issues {
type JsonData struct {
Jql string `json:"jql"`
Fields []string `json:"fields"`
MaxResults int64 `json:"maxResults"`
}
var data = JsonData{
Jql: config.Jql,
Fields: config.Fields,
MaxResults: config.MaxResults,
}
body, err := json.Marshal(data)
assertError(err)
req, err := http.NewRequest("POST", config.IssuesUrl, bytes.NewBuffer(body))
assertError(err)
req.SetBasicAuth(config.Username, config.Password)
req.Header.Add("Content-Type", "application/json")
resp, err := http.DefaultClient.Do(req)
assertError(err)
defer resp.Body.Close()
responseBody, err := ioutil.ReadAll(resp.Body)
assertError(err)
var issues IssuesJson
err = json.Unmarshal(responseBody, &issues)
return issues.Issues
}