-
Notifications
You must be signed in to change notification settings - Fork 2
/
confluence.lib.js
171 lines (139 loc) · 6.3 KB
/
confluence.lib.js
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
let HttpURLConnection= Java.type("java.net.HttpURLConnection")
let BufferedReader= Java.type("java.io.BufferedReader")
let InputStreamReader = Java.type("java.io.InputStreamReader")
let DataOutputStream= Java.type("java.io.DataOutputStream")
let URL = Java.type("java.net.URL")
let Base64 = Java.type("java.util.Base64")
let JString = Java.type("java.lang.String")
var confluence = {
"_readResponseToString": function(streamData) {
let reader
let result = ""
try {
reader = new BufferedReader(new InputStreamReader(streamData))
let line
while ( (line = reader.readLine()) != null) {
result += line
}
} finally {
if (reader)
reader.close()
}
return result
},
"imageDataBytesFromBase64": function(base64Image) {
return Base64.getDecoder().decode(base64Image)
},
"_encodedCredentials": function(userCredentials) {
let creds = new JString(userCredentials).getBytes()
return new JString(Base64.getEncoder().encode(creds))
},
"_statusCodeIsSuccess": function(statusCode) {
return (statusCode >= 200 && statusCode <= 300)
},
"attachImage": function(confluenceSettings, pageId, imageName, imageBytes, opts) {
let urlString = (confluenceSettings.baseUrl + "/rest/api/content/" + pageId + "/child/attachment")
let connectionAddress = new URL(urlString)
let httpConnection = connectionAddress.openConnection()
let basicAuth = "Basic " + this._encodedCredentials(confluenceSettings.username + ":" + confluenceSettings.apiToken)
httpConnection.setRequestProperty("Authorization", basicAuth)
httpConnection.setRequestProperty("X-Atlassian-Token", "nocheck")
let boundary = "*****"
let crlf = "\r\n"
let twoHypens = "--"
httpConnection.setUseCaches(false)
httpConnection.setDoInput(true)
httpConnection.setDoOutput(true)
httpConnection.setRequestMethod("PUT")
httpConnection.setRequestProperty("Connection", "Keep-Alive")
httpConnection.setRequestProperty("file", imageName)
httpConnection.setRequestProperty("Cache-Control", "no-cache")
httpConnection.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary)
httpConnection.connect()
let request = new DataOutputStream(httpConnection.getOutputStream())
request.writeBytes(twoHypens + boundary + crlf)
request.writeBytes("Content-Disposition: form-data; name=\"file\";" + "filename=\"" + imageName + "\"" + crlf)
request.writeBytes("Content-Type: image/png" + crlf)
request.writeBytes("Content-Transfer-Encoding: binary" + crlf)
request.writeBytes(crlf)
// write image
request.write(Java.to(imageBytes, "byte[]"))
// end content wrapper
request.writeBytes(crlf)
request.writeBytes(twoHypens + boundary + twoHypens + crlf)
// flush
request.flush()
request.close()
let statusCode = httpConnection.getResponseCode()
if (!this._statusCodeIsSuccess(statusCode))
throw new Error(this._readResponseToString(httpConnection.getErrorStream()))
if (opts && opts.debug == true)
console.log(this._readResponseToString(httpConnection.getInputStream()))
},
"updateConfluencePageContents": function(confluenceSettings, pageId, pageTitle, pageVersion, pageContents, opts) {
let connectionAddress = new URL(confluenceSettings.baseUrl + "/rest/api/content/" + pageId)
let httpConnection = connectionAddress.openConnection()
let basicAuth = "Basic " + this._encodedCredentials(confluenceSettings.username + ":" + confluenceSettings.apiToken)
httpConnection.setRequestProperty("Authorization", basicAuth)
httpConnection.setRequestProperty("X-Atlassian-Token", "nocheck")
httpConnection.setUseCaches(false)
httpConnection.setDoInput(true)
httpConnection.setDoOutput(true)
httpConnection.setRequestMethod("PUT")
httpConnection.setRequestProperty("Connection", "Keep-Alive")
httpConnection.setRequestProperty("Cache-Control", "no-cache")
httpConnection.setRequestProperty("Content-Type", "application/json")
httpConnection.setRequestProperty("Accept", "application/json")
httpConnection.connect()
let request = new DataOutputStream(httpConnection.getOutputStream())
let requestData = {
"id" : pageId,
"type" : "page",
"space": {
"key": confluenceSettings.spaceKey
},
"title": pageTitle,
"version": {
"number": pageVersion
},
"body": {
"storage": {
"value": pageContents,
"representation": "storage"
}
}
}
// write image
request.writeBytes(JSON.stringify(requestData))
// flush output
request.flush()
request.close()
let statusCode = httpConnection.getResponseCode()
if (!this._statusCodeIsSuccess(statusCode))
throw new Error(this._readResponseToString(httpConnection.getErrorStream()))
if (opts && opts.debug == true)
console.log(this._readResponseToString(httpConnection.getInputStream()))
},
"confluencePageInformation": function (confluenceSettings, pageTitle, opts) {
let urlString = (confluenceSettings.baseUrl + "/rest/api/content?title=" + encodeURIComponent(pageTitle) + "&spaceKey=" + confluenceSettings.spaceKey + "&expand=version")
let connectionAddress = new URL(urlString)
let httpConnection= connectionAddress.openConnection()
let basicAuth = "Basic " + this._encodedCredentials(confluenceSettings.username + ":" + confluenceSettings.apiToken)
httpConnection.setRequestProperty("Authorization", basicAuth)
httpConnection.setRequestMethod("GET")
httpConnection.connect()
let statusCode = httpConnection.getResponseCode()
if (!this._statusCodeIsSuccess(statusCode))
throw new Error(this._readResponseToString(httpConnection.getErrorStream()))
let httpResponse = this._readResponseToString(httpConnection.getInputStream())
if (opts && opts.debug == true)
console.log(httpResponse)
let jsonResponse = JSON.parse(httpResponse)
if (jsonResponse["results"].length == 0)
throw new Error("Cannot find page in space '" + confluenceSettings.spaceKey + "' with title '" + pageTitle + "'.")
let pageId = jsonResponse["results"][0]["id"]
let versionNum = jsonResponse["results"][0]["version"]["number"]
let pageVersion = parseInt(versionNum)
return { "pageId": pageId, "pageVersion": pageVersion }
}
}