From 2557e1ab7795b66d0c529755cb1959c68ba36b8d Mon Sep 17 00:00:00 2001 From: Jean-Christophe Fillion-Robin Date: Sun, 12 Feb 2017 16:48:05 -0500 Subject: [PATCH] gh_asset_upload: Handle assets with same name already uploaded If assets with same name are already uploaded, trying to upload new ones with matching name would give a HTTP error 422. This commit updates the function to report a message if matching asset are already associated with the release. For example: ``` $ githubrelease asset jcfr/sandbox upload nightly "dist/*" uploading 'nightly' release asset(s) (found 4): uploading dist/sandbox-2.0.0-cp35-cp35m-manylinux1_x86_64.whl skipping (asset with same name already exists) download_url: https://github.com/jcfr/sandbox/releases/download/nightly/sandbox-2.0.0-cp35-cp35m-manylinux1_x86_64.whl uploading dist/sandbox-2.0.0-cp36-cp36m-manylinux1_x86_64.whl download_url: https://github.com/jcfr/sandbox/releases/download/nightly/sandbox-2.0.0-cp36-cp36m-manylinux1_x86_64.whl uploading dist/sandbox-2.0.0-cp27-cp27m-manylinux1_x86_64.whl download_url: https://github.com/jcfr/sandbox/releases/download/nightly/sandbox-2.0.0-cp27-cp27m-manylinux1_x86_64.whl uploading dist/sandbox-2.0.0-cp34-cp34m-manylinux1_x86_64.whl download_url: https://github.com/jcfr/sandbox/releases/download/nightly/sandbox-2.0.0-cp34-cp34m-manylinux1_x86_64.whl ``` Additionally, this commit also improves the message reported when no files are found. For example: ``` $ githubrelease asset jcfr/sandbox upload nightly "dist/*invalid*" skipping upload of 'nightly' release assets (no files match pattern(s): dist/*invalid*) ``` --- github_release.py | 39 ++++++++++++++++++++++++++++----------- 1 file changed, 28 insertions(+), 11 deletions(-) diff --git a/github_release.py b/github_release.py index bf57c40..410605d 100755 --- a/github_release.py +++ b/github_release.py @@ -364,6 +364,7 @@ def print_asset_info(i, asset, indent=""): def gh_asset_upload(repo_name, tag_name, pattern, dry_run=False, verbose=False): release = get_release_info(repo_name, tag_name) uploaded = False + already_uploaded = False upload_url = release["upload_url"] if "{" in upload_url: upload_url = upload_url[:upload_url.index("{")] @@ -376,33 +377,49 @@ def gh_asset_upload(repo_name, tag_name, pattern, dry_run=False, verbose=False): else: filenames = glob.glob(pattern) - prefix = "uploading '{0}' release assets: ".format(tag_name) - if len(filenames) > 1: - print(prefix) - prefix = " " + if len(filenames) > 0: + print("uploading '%s' release asset(s) " + "(found %s):" % (tag_name, len(filenames))) for filename in filenames: - print(prefix + 'uploading ' + filename) + print(" uploading %s" % filename) + basename = os.path.basename(filename) + # Skip if an asset with same name has already been uploaded + # Trying to upload would give a HTTP error 422 + download_url = None + for asset in release["assets"]: + if asset["name"] == basename: + download_url = asset["browser_download_url"] + break + if download_url: + already_uploaded = True + print(" skipping (asset with same name already exists)") + print(" download_url: %s" % download_url) + print("") + continue if dry_run: uploaded = True + print(" download_url: Unknown (dry_run)") + print("") continue + # Attempt upload with open(filename, 'rb') as f: - basename = os.path.basename(filename) url = '{0}?name={1}'.format(upload_url, basename) if verbose: - print(prefix + 'upload_url: ' + url) + print(" upload_url: %s" % url) response = _request( 'POST', url, headers={'Content-Type': 'application/octet-stream'}, data=f.read()) response.raise_for_status() asset = response.json() - print(prefix + 'download_url: ' + asset["browser_download_url"]) + print(" download_url: %s" % asset["browser_download_url"]) print("") uploaded = True - if not uploaded: - print("release {0}: skipping upload: " - "there are no files matching '{1}'".format(tag_name, pattern)) + if not uploaded and not already_uploaded: + print("skipping upload of '%s' release assets (" + "no files match pattern(s): %s)" % (tag_name, pattern)) + print("") gh_asset_upload.description = {