Skip to content

Commit

Permalink
do not upload all attachments, when nothing has changed
Browse files Browse the repository at this point in the history
Signed-off-by: Martin Schurz <Martin.Schurz@t-systems.com>
  • Loading branch information
schurzi committed May 3, 2023
1 parent bddab13 commit 5deb72a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
7 changes: 7 additions & 0 deletions markdown-to-confluence.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,12 @@ def parse_args():
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 @@ -319,6 +325,7 @@ def main():
cookie=args.cookie,
headers=args.headers,
dry_run=args.dry_run,
optimizeattachments=args.optimizeattachments,
minoredit=args.minoredit)

if args.save_cookie:
Expand Down
18 changes: 17 additions & 1 deletion 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 @@ -33,6 +34,7 @@ def __init__(self,
headers=None,
dry_run=False,
minoredit=True,
optimizeattachments=True,
_client=None):
"""Creates a new Confluence API client.
Expand All @@ -54,6 +56,7 @@ def __init__(self,
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 @@ -299,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={'minorEdit': self.minoredit, '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

0 comments on commit 5deb72a

Please sign in to comment.