-
-
Notifications
You must be signed in to change notification settings - Fork 227
/
ConfluenceClient.groovy
128 lines (106 loc) · 5.25 KB
/
ConfluenceClient.groovy
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
package org.docToolchain.atlassian.clients
import groovyx.net.http.EncoderRegistry
import groovyx.net.http.HttpResponseException
import groovyx.net.http.Method
import groovyx.net.http.RESTClient
import org.apache.http.entity.ContentType
import org.docToolchain.configuration.ConfigService
abstract class ConfluenceClient {
protected final String API_V1_DEFAULT_PATH = "/wiki/rest/api/"
protected final String API_V2_DEFAULT_PATH = "/wiki/api/v2/"
protected final String editorVersion
protected String baseApiUrl
Map headers
RESTClient restClient
ConfluenceClient(ConfigService configService) {
this.baseApiUrl = configService.getConfigProperty('confluence.api')
this.restClient = new RESTClient(baseApiUrl)
restClient.setEncoderRegistry(new EncoderRegistry( charset: 'utf-8' ))
this.headers = ['X-Atlassian-Token':'no-check']
this.editorVersion = determineEditorVersion(configService)
if(configService.getFlatConfigSubTree('confluence.proxy')){
def proxy = configService.getFlatConfigSubTree('confluence.proxy')
restClient.setProxy(proxy.host as String, proxy.port as Integer, proxy.schema as String?: 'http')
}
if(configService.getConfigProperty('confluence.bearerToken')){
headers.put('Authorization', 'Bearer ' + configService.getConfigProperty('confluence.bearerToken'))
println 'Start using bearer auth'
} else {
headers.put('Authorization', 'Basic ' + configService.getConfigProperty('confluence.credentials'))
//Add api key and value to REST API request header if configured - required for authentification.
if (configService.getConfigProperty('confluence.apikey')){
headers.put('keyid', configService.getConfigProperty('confluence.apikey'))
}
}
}
def addHeader(key, value) {
this.headers.put(key, value)
}
abstract verifyCredentials()
abstract addLabel(pageId, label)
abstract getAttachment(pageId, filename)
abstract updateAttachment(String pageId, String attachmentId, InputStream inputStream, String fileName, String note, String localHash)
abstract createAttachment(String pageId, InputStream inputStream, String fileName, String note, String localHash)
protected uploadAttachment(uri, InputStream inputStream, String fileName, note, localHash) {
restClient.request(uri, Method.POST, ContentType.MULTIPART_FORM_DATA){ req ->
def multiPartContent = MultipartEntityBuilder.create()
.setMode(HttpMultipartMode.BROWSER_COMPATIBLE)
.addBinaryBody("file", inputStream, ContentType.APPLICATION_OCTET_STREAM, fileName)
.addTextBody("comment", note + "\r\n#" + localHash + "#")
.build()
req.setEntity(multiPartContent)
headers.each { key, value ->
req.addHeader(key, value)
}
}
}
abstract fetchPagesBySpaceKey(String spaceKey, Integer pageLimit)
abstract fetchPagesByAncestorId(List<String> pageIds, Integer pageLimit)
abstract fetchPageByPageId(String id)
abstract updatePage(String pageId, String title, String confluenceSpaceKey, Object localPage, Integer pageVersion, String pageVersionComment, String parentId)
abstract createPage(String title, String confluenceSpaceKey, Object localPage, String pageVersionComment, String parentId)
protected abstract fetchPageIdByName(String name, String spaceKey)
def retrieveFullPageById(String pageId) {
trythis {
fetchPageByPageId(pageId).data
} ?: [:]
}
def retrievePageIdByName(String name, String spaceKey){
trythis {
fetchPageIdByName(name, spaceKey)
} ?: null
}
// for getting better error message from the REST-API
// LuisMuniz: return the action's result, if successful.
protected def trythis(Closure action) {
try {
action.call()
} catch (HttpResponseException error) {
println "something went wrong - got an http response code "+error.response.status+":"
switch (error.response.status) {
case '401':
println (error.response.data.toString().replaceAll("^.*Reason","Reason"))
println "please check your confluence credentials in config file or passed parameters"
throw new Exception("missing authentication credentials")
break
case '400':
println error.response.data
println "please check the ancestorId in your config file"
throw new Exception("Parent does not exist")
break
default:
println error.response.data
}
null
}
}
private String determineEditorVersion(ConfigService configService){
if(configService.getConfigProperty("confluence.enforceNewEditor")
&& configService.getConfigProperty("confluence.enforceNewEditor").toBoolean() == true){
println "WARNING: You are using the new editor version v2. This is not yet fully supported by docToolchain."
return "v2"
} else {
return "v1"
}
}
}