diff --git a/tip.py b/tip.py index 63ee01b..7d8d7ef 100755 --- a/tip.py +++ b/tip.py @@ -14,19 +14,19 @@ print("· Get list of artifacts to be uploaded") args = [] files = [] -if 'INPUT_FILES' in environ: - args = environ['INPUT_FILES'].split() +if "INPUT_FILES" in environ: + args = environ["INPUT_FILES"].split() if len(argv) > 1: args = args + argv[1:] -if len(args) == 1 and args[0] == 'none': +if len(args) == 1 and args[0] == "none": files = [] print("! Skipping 'files' because it's set to 'none") else: if len(args) == 0: stdout.flush() - raise(Exception("Glob patterns need to be provided as positional arguments or through envvar 'INPUT_FILES'!")) + raise (Exception("Glob patterns need to be provided as positional arguments or through envvar 'INPUT_FILES'!")) for item in args: items = [fname for fname in glob(item, recursive=True) if not Path(fname).is_dir()] @@ -39,61 +39,65 @@ else: if len(files) < 1: stdout.flush() - raise(Exception('Empty list of files to upload/update!')) + raise (Exception("Empty list of files to upload/update!")) print("· Get GitHub API handler (authenticate)") -if 'GITHUB_TOKEN' in environ: +if "GITHUB_TOKEN" in environ: gh = Github(environ["GITHUB_TOKEN"]) -elif 'INPUT_TOKEN' in environ: +elif "INPUT_TOKEN" in environ: gh = Github(environ["INPUT_TOKEN"]) else: - if 'GITHUB_USER' not in environ or 'GITHUB_PASS' not in environ: + if "GITHUB_USER" not in environ or "GITHUB_PASS" not in environ: stdout.flush() - raise(Exception("Need credentials to authenticate! Please, provide 'GITHUB_TOKEN', 'INPUT_TOKEN', or 'GITHUB_USER' and 'GITHUB_PASS'")) + raise ( + Exception( + "Need credentials to authenticate! Please, provide 'GITHUB_TOKEN', 'INPUT_TOKEN', or 'GITHUB_USER' and 'GITHUB_PASS'" + ) + ) gh = Github(environ["GITHUB_USER"], environ["GITHUB_PASS"]) print("· Get Repository handler") -if 'GITHUB_REPOSITORY' not in environ: +if "GITHUB_REPOSITORY" not in environ: stdout.flush() - raise(Exception("Repository name not defined! Please set 'GITHUB_REPOSITORY")) + raise (Exception("Repository name not defined! Please set 'GITHUB_REPOSITORY")) -gh_repo = gh.get_repo(environ['GITHUB_REPOSITORY']) +gh_repo = gh.get_repo(environ["GITHUB_REPOSITORY"]) print("· Get Release handler") -tag = getenv('INPUT_TAG', 'tip') +tag = getenv("INPUT_TAG", "tip") env_tag = None -gh_ref = environ['GITHUB_REF'] +gh_ref = environ["GITHUB_REF"] is_prerelease = True is_draft = False -if gh_ref[0:10] == 'refs/tags/': +if gh_ref[0:10] == "refs/tags/": env_tag = gh_ref[10:] if env_tag != tag: rexp = r"^(?P0|[1-9]\d*)\.(?P0|[1-9]\d*)\.(?P0|[1-9]\d*)(?:-(?P(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+(?P[0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$" semver = re.search(rexp, env_tag) - if semver == None and env_tag[0] == 'v': + if semver == None and env_tag[0] == "v": semver = re.search(rexp, env_tag[1:]) tag = env_tag if semver == None: - print(f'! Could not get semver from {gh_ref!s}') + print(f"! Could not get semver from {gh_ref!s}") print(f"! Treat tag '{tag!s}' as a release") is_prerelease = False else: - if semver.group('prerelease') is None: + if semver.group("prerelease") is None: # is a regular semver compilant tag is_prerelease = False - elif getenv('INPUT_SNAPSHOTS', 'true') == 'true': + elif getenv("INPUT_SNAPSHOTS", "true") == "true": # is semver compilant prerelease tag, thus a snapshot (we skip it) print("! Skipping snapshot prerelease") sys.exit() gh_tag = None try: - gh_tag = gh_repo.get_git_ref(f'tags/{tag!s}') + gh_tag = gh_repo.get_git_ref(f"tags/{tag!s}") except Exception as e: stdout.flush() pass @@ -106,13 +110,15 @@ if gh_tag: pass else: err_msg = f"Tag/release '{tag!s}' does not exist and could not create it!" - if 'GITHUB_SHA' not in environ: - raise(Exception(err_msg)) + if "GITHUB_SHA" not in environ: + raise (Exception(err_msg)) try: - gh_release = gh_repo.create_git_tag_and_release(tag, "", tag, "", environ['GITHUB_SHA'], 'commit', draft=True, prerelease=is_prerelease) + gh_release = gh_repo.create_git_tag_and_release( + tag, "", tag, "", environ["GITHUB_SHA"], "commit", draft=True, prerelease=is_prerelease + ) is_draft = True except Exception as e: - raise(Exception(err_msg)) + raise (Exception(err_msg)) print("· Cleanup and/or upload artifacts") @@ -120,7 +126,7 @@ artifacts = files assets = gh_release.get_assets() -if getenv('INPUT_RM', 'false') == 'true': +if getenv("INPUT_RM", "false") == "true": print("· RM set. All previous assets are being cleared...") for asset in assets: print(" ", asset.name) @@ -132,7 +138,7 @@ else: aname = str(Path(artifact).name) if asset.name == aname: print(" - uploading tmp...") - new_asset = gh_release.upload_asset(artifact, name=f'tmp.{aname!s}') + new_asset = gh_release.upload_asset(artifact, name=f"tmp.{aname!s}") print(" - removing...") asset.delete_asset() print(" - renaming tmp...") @@ -157,10 +163,10 @@ if is_draft: draft=False, prerelease=is_prerelease, tag_name=gh_release.tag_name, - target_commitish=gh_release.target_commitish + target_commitish=gh_release.target_commitish, ) -if ('GITHUB_SHA' in environ) and (env_tag is None): - sha = environ['GITHUB_SHA'] +if ("GITHUB_SHA" in environ) and (env_tag is None): + sha = environ["GITHUB_SHA"] print(f" > Force-push '{tag!s}' to {sha!s}") - gh_repo.get_git_ref(f'tags/{tag!s}').edit(sha) + gh_repo.get_git_ref(f"tags/{tag!s}").edit(sha)