Skip to content

Commit

Permalink
Merge pull request #7 from schurzi/attachment_reupload
Browse files Browse the repository at this point in the history
Improve content synchronization
  • Loading branch information
mrporcles authored Aug 16, 2023
2 parents 072fe85 + 06d00e9 commit 2ef1f4a
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 9 deletions.
18 changes: 12 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,15 @@ docker run -it -v ~/hugo-site:/site markdown-to-confluence:v1.0 --api_url "CONFL
# Usage

```
usage: markdown-to-confluence.py [-h] [--api_url API_URL] [--space SPACE] [--username USERNAME] [--password PASSWORD]
[--dir DIR] [--git GIT] [--global_label GLOBAL_LABEL] [--header HEADER]
[--dry-run] [--force] [--save-cookie SAVE_COOKIE] [--cookie-file COOKIE]
[posts ...]
usage: markdown-to-confluence.py [-h] [--api_url API_URL] [--space SPACE]
[--username USERNAME] [--password PASSWORD]
[--dir DIR] [--git GIT]
[--global_label GLOBAL_LABEL]
[--header HEADER] [--dry-run] [--force]
[--no-minoredit] [--no-optimizeattachments]
[--save-cookie SAVE_COOKIE]
[--cookie-file COOKIE]
[posts [posts ...]]
Converts and deploys a single or directory of markdown page/s to Confluence
Expand All @@ -57,10 +61,12 @@ optional arguments:
--header HEADER Extra header to include in the request when sending HTTP to a server. May be specified multiple times. (default: env('CONFLUENCE_HEADER_<NAME>'))
--dry-run Print requests that would be sent - don't actually make requests against Confluence (note: we return empty responses, so this might impact accuracy)
--force Can be used with --git flag. Upload pages without checking for changes
--no-minoredit Don't use minorEdit flag when creating content and trigger notifications for all changes
--no-optimizeattachments
Upload all attachments everytime
--save-cookie SAVE_COOKIE
File system location to write cookie
--cookie-file COOKIE Instead of using a user name and password use a cookie created with --save-cookie
```

## What Posts are Deployed?
Expand Down
16 changes: 15 additions & 1 deletion markdown-to-confluence.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,18 @@ def parse_args():
action='store_true',
help='Can be used with --git flag. Upload pages without checking for changes'
)
parser.add_argument(
'--no-minoredit',
dest='minoredit',
action='store_false',
help='Don\'t use minorEdit flag when creating content and trigger notifications for all changes'
)
parser.add_argument(
'--no-optimizeattachments',
dest='optimizeattachments',
action='store_false',
help='Upload all attachments everytime'
)
parser.add_argument(
'--save-cookie',
dest='save_cookie',
Expand Down Expand Up @@ -312,7 +324,9 @@ def main():
password=args.password,
cookie=args.cookie,
headers=args.headers,
dry_run=args.dry_run)
dry_run=args.dry_run,
optimizeattachments=args.optimizeattachments,
minoredit=args.minoredit)

if args.save_cookie:
log.info('Attempting to save cookie. Input files will be ignored')
Expand Down
23 changes: 21 additions & 2 deletions markdown_to_confluence/confluence.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import logging
import requests
import hashlib
import os
import pickle
import sys
Expand Down Expand Up @@ -32,6 +33,8 @@ def __init__(self,
cookie=None,
headers=None,
dry_run=False,
minoredit=True,
optimizeattachments=True,
_client=None):
"""Creates a new Confluence API client.
Expand All @@ -41,6 +44,7 @@ def __init__(self,
password {str} -- The Confluence service account password
headers {list(str)} -- The HTTP headers which will be set for all requests
dry_run {str} -- The Confluence service account password
minoredit {bool} -- Flag for minorEdit in Confluence
"""
# A common gotcha will be given a URL that doesn't end with a /, so we
# can account for this
Expand All @@ -51,6 +55,8 @@ def __init__(self,
self.username = username
self.password = password
self.dry_run = dry_run
self.minoredit = minoredit
self.optimizeattachments = optimizeattachments

if _client is None:
_client = requests.Session()
Expand Down Expand Up @@ -296,10 +302,23 @@ def upload_attachment(self, post_id=None, attachment_path=None):
log.info(
'Uploading attachment {attachment_path} to post {post_id}'.format(
attachment_path=attachment_path, post_id=post_id))
shahash = None
if self.optimizeattachments:
response = self.get(path="content/{}/child/attachment".format(post_id),
params= {'filename': os.path.basename(attachment_path),
'expand': 'version'})
shahash = hashlib.sha256(open(attachment_path, 'rb').read()).hexdigest()
try:
if len(response['results']) == 1 and \
shahash == response['results'][0]['version']['message']:
log.info('Not Uploaded {} to post ID {} - no changes in file'.format(attachment_path, post_id))
return
except KeyError:
pass
if not self.dry_run:
self.post(path=path,
params={'allowDuplicated': 'true'},
files={'file': open(attachment_path, 'rb')})
files={'comment': shahash, 'minorEdit': self.minoredit, 'file': open(attachment_path, 'rb')})
log.info('Uploaded {} to post ID {}'.format(attachment_path, post_id))

def get_author(self, username):
Expand Down Expand Up @@ -424,7 +443,7 @@ def update(self,
# Increment the version number, as required by the Confluence API
# https://docs.atlassian.com/ConfluenceServer/rest/7.1.0/#api/content-update
new_version = page['version']['number'] + 1
new_page['version'] = {'number': new_version}
new_page['version'] = {'minorEdit': self.minoredit, 'number': new_version}

# With the attachments uploaded, and our new page structure created,
# we can upload the final content up to Confluence.
Expand Down

0 comments on commit 2ef1f4a

Please sign in to comment.