Skip to content

Commit

Permalink
Implement update-page
Browse files Browse the repository at this point in the history
  • Loading branch information
droberts2013 committed Sep 16, 2017
1 parent 4b4a3ff commit 4a58d41
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 2 deletions.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# xlr-confluence-plugin v1.0.1
# xlr-confluence-plugin v1.0.4

### Functionality ###

Expand All @@ -10,6 +10,14 @@ The page text should be formatted with HTML tags.

![screenshot of add-page](images/addpage.png)

#### Task: Update one or more wiki pages ####

The specified pages will be updated with the new body text. The new text will replace the prior contents of the page.

The page text should be formatted with HTML tags.

![screenshot of add-page](images/updatepage.png)

#### Task: Add a comment to one or more existing wiki pages ####

A comment will be added to all pages matching the page titles and page numbers within the space key.
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ apply plugin: 'idea'
apply plugin: 'eclipse'
apply plugin: 'maven'

version="1.0.1"
version="1.0.4"

license {
header rootProject.file('src/main/license/xebialabs_community.license')
Expand Down
Binary file added images/updatepage.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
38 changes: 38 additions & 0 deletions src/main/resources/confluence/ConfluenceClient.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,17 @@
class ConfluenceClient(object):

def __init__(self, httpConnection, username=None, password=None):
print "Executing __init__() in ConfluenceClient\n"
self.httpConnection = httpConnection
self.httpRequest = HttpRequestPlus(httpConnection, username, password)

@staticmethod
def createClient(httpConnection, username=None, password=None):
print "Executing createClient() in ConfluenceClient\n"
return ConfluenceClient(httpConnection, username, password)

def addComment(self, pageId, comment):
print "Executing addComment() in ConfluenceClient\n"
contentType = "application/json"
headers = {'Accept' : 'application/json'}
addCommentUrl = '/rest/api/content'
Expand All @@ -31,8 +34,10 @@ def addComment(self, pageId, comment):
response = self.httpRequest.post(addCommentUrl, json.dumps(payload), contentType=contentType, headers=headers)
if response.getStatus() not in HTTP_SUCCESS:
self.throw_error(response)
print "Success. The comment has been added to page %s.\n" % pageId

def addPage(self, spaceKey, parentPageId, pageTitle, pageText):
print "Executing addPage() in ConfluenceClient\n"
contentType = "application/json"
headers = {'Accept' : 'application/json'}
addPageUrl = '/rest/api/content'
Expand All @@ -44,8 +49,21 @@ def addPage(self, spaceKey, parentPageId, pageTitle, pageText):
response = self.httpRequest.post(addPageUrl, json.dumps(payload), contentType=contentType, headers=headers)
if response.getStatus() not in HTTP_SUCCESS:
self.throw_error(response)
print "Success. The page %s has been added.\n" % pageTitle


def getPage(self, pageId):
print "Executing getPage() in ConfluenceClient\n"
contentType = "application/json"
headers = {'Accept' : 'application/json', 'Content-Type' : 'application/json'}
getPageUrl = '/rest/api/content/%s' % pageId
response = self.httpRequest.get(getPageUrl, contentType=contentType, headers=headers)
if response.getStatus() not in HTTP_SUCCESS:
self.throw_error(response)
return json.loads(response.response)

def getPageNumbersByTitle(self, spaceKey, pageTitles):
print "Executing getPageNumbersByTitle() in ConfluenceClient\n"
contentType = "application/json"
headers = {'Accept' : 'application/json'}
pageIdList = []
Expand All @@ -59,3 +77,23 @@ def getPageNumbersByTitle(self, spaceKey, pageTitles):
pageIdList.append(page['id'])
return pageIdList

def updatePage(self, spaceKey, pageId, pageTitle, pageText):
print "Executing updatePage() in ConfluenceClient\n"
contentType = "application/json"
headers = {'Accept' : 'application/json', 'Content-Type' : 'application/json'}
newVersionNumber = self.getPage(pageId)['version']['number'] + 1
updatePageUrl = '/rest/api/content/%s' % pageId
payload = json.loads('{"type":"page", "title":"", "space":{"key":""}, "version":{"number":0}, "body":{"storage":{"representation":"storage","value":""}}}')
payload['title'] = pageTitle
payload['space']['key'] = spaceKey
payload['version']['number'] = newVersionNumber
payload['body']['storage']['value'] = pageText
response = self.httpRequest.put(updatePageUrl, json.dumps(payload), contentType=contentType, headers=headers)
if response.getStatus() not in HTTP_SUCCESS:
self.throw_error(response)
print "Success. Page %s has been updated.\n" % pageId

def throw_error(self, response):
print "Error from Confluence, HTTP Return: %s\n" % (response.getStatus())
print response.response
sys.exit(1)
21 changes: 21 additions & 0 deletions src/main/resources/confluence/updateWikiPages.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#
# THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS
# FOR A PARTICULAR PURPOSE. THIS CODE AND INFORMATION ARE NOT SUPPORTED BY XEBIALABS.
#

from confluence.ConfluenceClientUtil import ConfluenceClientUtil

print "Executing updateWikiPages.py\n"

if confluenceServer is None:
print "No server provided\n"
sys.exit(1)

credentials = CredentialsFallback(confluenceServer, username, password).getCredentials()

confluenceClient = ConfluenceClientUtil.createConfluenceClient(confluenceServer, credentials['username'], credentials['password'])

for pageId in pageIds:
confluenceClient.updatePage(spaceKey, pageId, pageTitle, pageText)

7 changes: 7 additions & 0 deletions src/main/resources/synthetic.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,11 @@
<property name="pageText" category="input" />
</type>

<type type="confluence.updateWikiPages" extends="confluence.Task">
<property name="spaceKey" category="input" />
<property name="pageIds" category="input" kind="list_of_string" />
<property name="pageTitle" category="input" />
<property name="pageText" category="input" />
</type>

</synthetic>

0 comments on commit 4a58d41

Please sign in to comment.