-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfluence2
52 lines (41 loc) · 2.46 KB
/
confluence2
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
def fileToUpload = "<path_to_html_file>"
def confluenceUrl = "<confluence_url>"
def confluenceUsername = "<confluence_username>"
def confluenceCredentialsId = "<confluence_credentials_id>"
def parentPageId = "<parent_page_id>"
def childPageTitle = "<child_page_title>"
def credentials = com.cloudbees.plugins.credentials.CredentialsProvider.lookupCredentials(
com.cloudbees.plugins.credentials.common.UsernamePasswordCredentials.class,
Jenkins.instance
)
def confluenceCredentials = credentials.find { it.id == confluenceCredentialsId }
if (confluenceCredentials == null) {
throw new IllegalArgumentException("Confluence credentials with ID '${confluenceCredentialsId}' not found.")
}
def username = confluenceCredentials.username
def password = confluenceCredentials.password.getPlainText()
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=${childPageTitle}"
def existingChildPageCmd = "curl -u ${username}:${password} ${existingChildPageUrl}"
def existingChildPageProcess = existingChildPageCmd.execute()
existingChildPageProcess.waitFor()
if (existingChildPageProcess.exitValue() == 0) {
def existingChildPageJson = new groovy.json.JsonSlurper().parseText(existingChildPageProcess.in.text)
if (existingChildPageJson.size() > 0) {
existingChildPageId = existingChildPageJson.results[0].id
existingChildPageVersion = existingChildPageJson.results[0].version.number
}
}
def uploadUrl = existingChildPageId ? "${confluenceUrl}/rest/api/content/${existingChildPageId}" : "${confluenceUrl}/rest/api/content/${parentPageId}/child/page"
def uploadMethod = existingChildPageId ? "PUT" : "POST"
def fileContent = new File(fileToUpload).text
def uploadCmd = "curl -u ${username}:${password} -X ${uploadMethod} -H 'Content-Type: application/json' -d '{\"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\"}}}' ${uploadUrl}"
def uploadProcess = uploadCmd.execute()
uploadProcess.waitFor()
if (uploadProcess.exitValue() == 0) {
println "HTML file uploaded successfully!"
} else {
println "Failed to upload HTML file. Error: ${uploadProcess.err.text}"
}