-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfluence3
120 lines (101 loc) · 4.32 KB
/
confluence3
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
// confluence.groovy
import groovy.json.JsonSlurper
import groovy.json.JsonOutput
import groovy.text.SimpleTemplateEngine
import javax.xml.bind.DatatypeConverter
import java.net.URL
import java.net.HttpURLConnection
def username = "your_confluence_username"
def password = "your_confluence_password"
def confluenceBaseUrl = "https://your-confluence-instance.com"
def spaceKey = "your-space-key"
def pageTitle = "your-page-title"
def itemName = "Item1"
def newItemContent = "New content for Item1"
def authHeaderValue = authenticateConfluence(username, password)
def pageContent = downloadConfluencePage(authHeaderValue, confluenceBaseUrl, spaceKey, pageTitle)
def tableContent = parseTableContent(pageContent)
def item = findItemInArray(tableContent, itemName)
if (item) {
println "Found item: ${item}"
tableContent = replaceItemContent(tableContent, itemName, newItemContent)
println "Updated table content: ${tableContent}"
} else {
println "Item not found."
}
def requestPAT(username, password) {
def url = "https://your-confluence-instance.com/rest/api/access-tokens/impersonation"
def body = [
"accountId": "userAccountId",
"idType": "user"
]
def connection = url.toURL().openConnection() as HttpURLConnection
connection.setRequestMethod("POST")
connection.setRequestProperty("Authorization", "Basic ${encodeCredentials(username, password)}")
connection.setRequestProperty("Content-Type", "application/json")
connection.setRequestProperty("Accept", "application/json")
connection.doOutput = true
connection.outputStream.write(JsonOutput.toJson(body).getBytes())
def response = connection.inputStream.getText()
def json = new JsonSlurper().parseText(response)
return json?.token
}
def encodeCredentials(username, password) {
def auth = "${username}:${password}"
return DatatypeConverter.printBase64Binary(auth.getBytes())
}
def authenticateConfluence(username, password) {
def pat = requestPAT(username, password)
return "Bearer ${pat}"
}
def downloadConfluencePage(authHeaderValue, confluenceBaseUrl, spaceKey, pageTitle) {
def url = "${confluenceBaseUrl}/rest/api/content"
def query = [
"spaceKey": spaceKey,
"title": pageTitle
]
def contentId = null
def connection = url.toURL().openConnection()
connection.setRequestProperty("Authorization", authHeaderValue)
connection.setRequestProperty("Accept", "application/json")
def response = new JsonSlurper().parseText(connection.getInputStream().getText())
if (response.size() > 0) {
contentId = response[0].id
}
if (contentId) {
def contentUrl = "${confluenceBaseUrl}/rest/api/content/${contentId}/?expand=body.view"
def contentConnection = new URL(contentUrl).openConnection()
contentConnection.setRequestProperty("Authorization", authHeaderValue)
contentConnection.setRequestProperty("Accept", "application/json")
def contentResponse = new JsonSlurper().parseText(contentConnection.getInputStream().getText())
return contentResponse.body.view.value
} else {
return null
}
}
def parseTableContent(tableHtml) {
def engine = new SimpleTemplateEngine()
def template = engine.createTemplate(tableHtml)
def result = template.make().toString()
// Add your logic here to parse the HTML table and convert it into an array of objects
// For demonstration, let's assume the table content is already in the desired format
return [
["Environment": "Prod", "Version": "1.0", "GitUrl": "https://example.com", "Date": "2024-03-21"],
["Environment": "Dev", "Version": "1.1", "GitUrl": "https://example.com/dev", "Date": "2024-03-20"],
["Environment": "Test", "Version": "1.2", "GitUrl": "https://example.com/test", "Date": "2024-03-19"]
]
}
def findEnvironmentInArray(itemArray, environmentName) {
return itemArray.find { it.Environment == environmentName }
}
def updateEnvironment(itemArray, environmentInfo) {
def environment = findEnvironmentInArray(itemArray, environmentInfo.Environment)
if (environment) {
environment.Version = environmentInfo.Version
environment.GitUrl = environmentInfo.GitUrl
environment.Date = environmentInfo.Date
} else {
itemArray.add(environmentInfo)
}
return itemArray
}