Skip to content

Commit 4ead5b6

Browse files
authored
Merge pull request #74 from gonchik/master
Confluence cleaner updates
2 parents f6400f3 + 07634fa commit 4ead5b6

File tree

4 files changed

+40
-7
lines changed

4 files changed

+40
-7
lines changed

atlassian/confluence.py

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,24 @@ def get_all_draft_pages_from_space(self, space, start=0, limit=500, status='draf
194194
status=status)
195195
return (self.get(url) or {}).get('results')
196196

197+
def get_all_draft_pages_from_space_through_cql(self, space, start=0, limit=500, status='draft'):
198+
"""
199+
Search list of draft pages by space key
200+
Use case is cleanup old drafts from Confluence
201+
:param space: Space Key
202+
:param status: Can be changed
203+
:param start: OPTIONAL: The start point of the collection to return. Default: None (0).
204+
:param limit: OPTIONAL: The limit of the number of pages to return, this may be restricted by
205+
fixed system limits. Default: 500
206+
:return:
207+
"""
208+
url = 'rest/api/content?cql=space=spaceKey={space} and status={status}'.format(space=space,
209+
status=status)
210+
params = {}
211+
params['limit'] = limit
212+
params['start'] = start
213+
return (self.get(url, params=params) or {}).get('results')
214+
197215
def get_all_restictions_for_content(self, content_id):
198216
"""
199217
Returns info about all restrictions by operation.
@@ -209,16 +227,28 @@ def remove_page_from_trash(self, page_id):
209227
:param page_id:
210228
:return:
211229
"""
212-
url = 'rest/api/content/{page_id}?status=trashed'.format(page_id=page_id)
213-
return self.delete(url)
230+
return self.remove_page(page_id=page_id, status='trashed')
214231

215-
def remove_page(self, page_id):
232+
def remove_page_as_draft(self, page_id):
233+
"""
234+
This method removed page from trash
235+
:param page_id:
236+
:return:
237+
"""
238+
return self.remove_page(page_id=page_id, status='draft')
239+
240+
def remove_page(self, page_id, status=None):
216241
"""
217242
This method removed page
218243
:param page_id:
244+
:param status: OPTIONAL: type of page
219245
:return:
220246
"""
221-
url = 'rest/api/content/{page_id}'.format(page_id=page_id)
247+
if status is None:
248+
url = 'rest/api/content/{page_id}'.format(page_id=page_id)
249+
else:
250+
url = 'rest/api/content/{page_id}?status={status}'.format(page_id=page_id, status=status)
251+
222252
return self.delete(url)
223253

224254
def create_page(self, space, title, body, parent_id=None, type='page'):

atlassian/rest_client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def log_curl_debug(self, method, path, data=None, headers=None, level=logging.DE
3131
password=self.password,
3232
headers=' -H '.join(["'{0}: {1}'".format(key, value) for key, value in headers.items()]),
3333
data='' if not data else "--data '{0}'".format(json.dumps(data)),
34-
url='{0}{1}'.format(self.url, path))
34+
url='{0}'.format(urljoin(self.url, path)))
3535
log.log(level=level, msg=message)
3636

3737
def resource_url(self, resource):

examples/confluence-draft-page-cleaner.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ def clean_draft_pages_from_space(confluence, space_key, count, date_now):
2727
last_date = datetime.datetime.strptime(last_date_string.replace(".000", "")[:-6], "%Y-%m-%dT%H:%M:%S")
2828
if (date_now - last_date) > datetime.timedelta(days=DRAFT_DAYS):
2929
count += 1
30-
print("Removed page with date {}".format(last_date_string))
30+
print("Removing page with page id: " + page_id)
31+
confluence.remove_page_as_draft(page_id=page_id)
32+
print("Removed page with date " + last_date_string)
3133
return count
3234

3335

examples/confluence-trash-cleaner.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@ def clean_pages_from_space(confluence, space_key, limit=500):
2424
flag = False
2525
print("For space {} trash is empty".format(space_key))
2626
else:
27+
print("Founded in space {} pages as trashed {}".format(space_key, len(values)))
2728
for value in values:
28-
print(value['title'])
29+
print("Removing page with title: " + value['title'])
2930
confluence.remove_page_from_trash(value['id'])
3031

3132

0 commit comments

Comments
 (0)