This repository has been archived by the owner on Apr 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfluence.go
288 lines (235 loc) · 7.46 KB
/
confluence.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
package main
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"log"
"net/http"
"time"
"gopkg.in/go-playground/validator.v9"
)
// cURL sample:
// url="https://<account>.atlassian.net"/wiki/rest/api/content/"
// curl -X POST -u "<email>:<apikey>" -H "Content-Type: application/json" $url --data @- << EOF
// {
// "type":"blogpost",
// "space":{"key":"RN"},
// "status": "current",
// "id":"20220101001",
// "title":"2022-01-01 Our Project v1.5.0 - Release Notes",
// "body":{
// "storage":{
// "value":"<h1>Breaking Changes</h1><ul><li><b>nothing</b> 123</li></ul>",
// "representation":"storage"}
// }
// }
// EOF
// ConfluencePost represents Confluence Blog Post object
type ConfluencePost struct {
Type string `json:"type"`
Space ConfluenceSpace `json:"space"`
Status string `json:"status"`
// ID is optional, it could be created to match e.g. YYYYMMDDHHMMSS
// ID string `json:"id"`
Title string `json:"title"`
Body ConfluenceBody `json:"body"`
Metadata ConfluenceMetadata `json:"metadata"`
}
type ConfluenceSpace struct {
Key string `json:"key"`
}
type ConfluenceBody struct {
Storage ConfluenceStorage `json:"storage"`
}
type ConfluenceStorage struct {
Value string `json:"value"`
Representation string `json:"representation"`
}
type ConfluenceBlogPost struct {
ID string `json:"id"`
Type string `json:"type"`
Status string `json:"status"`
Title string `json:"title"`
}
type ConfluenceMetadata struct {
Labels []ConfluenceLabel `json:"labels"`
}
type ConfluenceLabel struct {
Name string `json:"name"`
}
func NewConfluencePost(spaceKey string, title string, content string, label string) *ConfluencePost {
return &ConfluencePost{
Type: "blogpost",
Space: ConfluenceSpace{Key: spaceKey},
Status: "current",
Title: title,
Body: ConfluenceBody{
Storage: ConfluenceStorage{
Representation: "editor2",
Value: content,
},
},
Metadata: ConfluenceMetadata{
Labels: []ConfluenceLabel{
{Name: label},
},
},
}
}
func publishReleaseNotesToConfluence(cfg *Config, timestamp time.Time, title string, pipeline string, version string, notes *Notes) (*ConfluenceBlogPost, error) {
// see https://developer.atlassian.com/cloud/confluence/rest/api-group-content/#api-api-content-post
apiURL := fmt.Sprintf("%s/wiki/rest/api/content/", cfg.JiraUrl)
log.Printf("Calling %s", apiURL)
// NOTE: this is a magical string for the YYYY-MM-DD format
date := timestamp.Format("2006-01-02")
postTitle := fmt.Sprintf("%s Release Notes %s - %s", title, version, date)
content, err := createConfluenceContentHTML(cfg, notes)
if err != nil {
return nil, err
}
post := NewConfluencePost(cfg.ConfluenceSpaceKey, postTitle, content, pipeline)
jsonStr, _ := json.Marshal(post)
req, err := http.NewRequest(http.MethodPost, apiURL, bytes.NewBuffer(jsonStr))
if err != nil {
return nil, err
}
// create a token here: https://id.atlassian.com/manage-profile/security/api-tokens
req.SetBasicAuth(cfg.JiraUser, cfg.JiraApiKey)
req.Header.Add("Content-Type", "application/json")
resp, err := cfg.Client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
response, _ := ioutil.ReadAll(resp.Body)
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("failed to post to Confluence %v %v", err, string(response))
}
// NOTE: for debugging
// os.WriteFile("./sample-data/confluence-post.json", response, 0644)
blogPost, err := parseConfluenceBlogPost(response)
if err != nil {
return nil, err
}
return blogPost, nil
}
func createConfluenceContentHTML(cfg *Config, notes *Notes) (string, error) {
var buf bytes.Buffer
for k, v := range notes.Groups {
buf.WriteString(fmt.Sprintf("\nh1. %s\n", k))
for _, l := range v {
buf.Write([]byte(l + "\n"))
}
}
result := buf.Bytes()
// NOTE: I've tried this approach initially,
// but it didn't work well with JIRA/Confluence URL format or JIRA/Confluence macros
// html := markdown.ToHTML([]byte(result), nil, nil)
// have I missed some trick?
confluenceFormat, err := convertToConfluenceFormat(cfg, result)
if err != nil {
return "", err
}
return string(confluenceFormat), nil
}
func convertToConfluenceFormat(cfg *Config, text []byte) ([]byte, error) {
// then convert the remaining wiki markup to Confluence storage format
// see https://developer.atlassian.com/server/confluence/confluence-rest-api-examples/#convert-wiki-markup-to-storage-format
apiURL := fmt.Sprintf("%s/wiki/rest/api/contentbody/convert/editor2", cfg.JiraUrl)
log.Printf("Calling %s", apiURL)
post := &ConfluenceStorage{
Value: string(text),
Representation: "wiki",
}
jsonStr, _ := json.Marshal(post)
// NOTE: for debugging/testing
// os.WriteFile("./sample-data/confluence-convert-format.json", jsonStr, 0644)
req, err := http.NewRequest(http.MethodPost, apiURL, bytes.NewBuffer(jsonStr))
if err != nil {
return nil, err
}
// create a token here: https://id.atlassian.com/manage-profile/security/api-tokens
req.SetBasicAuth(cfg.JiraUser, cfg.JiraApiKey)
req.Header.Add("Content-Type", "application/json")
resp, err := cfg.Client.Do(req)
if err != nil {
return nil, err
}
if resp.StatusCode == http.StatusUnauthorized {
return nil, fmt.Errorf("401 Unauthorized")
}
if resp.StatusCode != http.StatusOK {
log.Fatalln(fmt.Errorf("failed to convert Confluence format: %v", err))
return nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
// NOTE: for debugging/testing
//os.WriteFile(fmt.Sprintf("./sample-data/jira-%s.json", key), body, 0644)
obj, err := parseConfluenceStorage(body)
if err != nil {
return nil, err
}
return []byte(obj.Value), nil
}
func validateConfluenceStorage(json ConfluenceStorage) error {
validate = validator.New()
err := validate.Struct(json)
if err != nil {
if _, ok := err.(*validator.InvalidValidationError); ok {
logger.Error(err)
return err
}
failures := []string{}
for _, err := range err.(validator.ValidationErrors) {
failures = append(failures, fmt.Sprintf("ns:%s tag:%s param:%s type:%s \n", err.Namespace(), err.Tag(), err.Param(), err.Type()))
}
return fmt.Errorf("data validation failed %v", failures)
}
return nil
}
func parseConfluenceStorage(jsonData []byte) (ConfluenceStorage, error) {
var data ConfluenceStorage
if !isJSON(jsonData) {
return data, errors.New("cannot create object - invalid json")
}
err := json.Unmarshal(jsonData, &data)
if err != nil {
return data, err
}
err = validateConfluenceStorage(data)
return data, err
}
func validateConfluenceBlogPost(json ConfluenceBlogPost) error {
validate = validator.New()
err := validate.Struct(json)
if err != nil {
if _, ok := err.(*validator.InvalidValidationError); ok {
logger.Error(err)
return err
}
failures := []string{}
for _, err := range err.(validator.ValidationErrors) {
failures = append(failures, fmt.Sprintf("ns:%s tag:%s param:%s type:%s \n", err.Namespace(), err.Tag(), err.Param(), err.Type()))
}
return fmt.Errorf("data validation failed %v", failures)
}
return nil
}
func parseConfluenceBlogPost(jsonData []byte) (*ConfluenceBlogPost, error) {
var data ConfluenceBlogPost
if !isJSON(jsonData) {
return &data, errors.New("cannot create object - invalid json")
}
err := json.Unmarshal(jsonData, &data)
if err != nil {
return &data, err
}
err = validateConfluenceBlogPost(data)
return &data, err
}