-
Notifications
You must be signed in to change notification settings - Fork 0
/
configfiletobash
57 lines (46 loc) · 2.12 KB
/
configfiletobash
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
def fileToUpload = "<path_to_html_file>"
def confluenceUrl = "<confluence_url>"
def confluenceCredentialsId = "<confluence_credentials_id>"
def parentPageId = "<parent_page_id>"
def childPageTitle = "<child_page_title>"
def credentials = credentials(confluenceCredentialsId)
if (credentials == null) {
throw new IllegalArgumentException("Confluence credentials with ID '${confluenceCredentialsId}' not found.")
}
def username = credentials.username
def password = credentials.password
def existingChildPageId = null
def existingChildPageVersion = 0
// Check if a child page with the same title already exists
def existingChildPageUrl = "${confluenceUrl}/rest/api/content/${parentPageId}/child/page?title=${URLEncoder.encode(childPageTitle, "UTF-8")}"
def existingChildPageCmd = "curl -u ${username}:${password} ${existingChildPageUrl}"
def existingChildPageJson = sh(script: existingChildPageCmd, returnStdout: true).trim()
if (existingChildPageJson) {
def existingChildPageData = readJSON text: existingChildPageJson
if (existingChildPageData.size() > 0) {
existingChildPageId = existingChildPageData.results[0].id
existingChildPageVersion = existingChildPageData.results[0].version.number
}
}
def fileContent = readFile(fileToUpload).trim()
// Additional properties to append
def additionalProperties = """
{
"version": {
"number": ${existingChildPageVersion + 1}
},
"title": "${childPageTitle}",
"type": "page",
"body": {
"storage": {
"value": "<ac:structured-macro ac:name=\\\"html\\\"><ac:plain-text-body><![CDATA[${fileContent}]]></ac:plain-text-body></ac:structured-macro>",
"representation": "storage"
}
}
}
"""
def uploadUrl = existingChildPageId ? "${confluenceUrl}/rest/api/content/${existingChildPageId}" : "${confluenceUrl}/rest/api/content/${parentPageId}/child/page"
def uploadMethod = existingChildPageId ? "PUT" : "POST"
def uploadCmd = "curl -u ${username}:${password} -X ${uploadMethod} -H 'Content-Type: application/json' -d '${additionalProperties.replaceAll("'", "\\'")}' --data-binary @${fileToUpload} ${uploadUrl}"
sh(uploadCmd)
println "HTML file uploaded successfully!"