|
| 1 | +import argparse |
| 2 | +import logging |
| 3 | +import os |
| 4 | + |
| 5 | +import requests |
| 6 | +from OBAS_utils.release_utils import check_release, closeRelease |
| 7 | + |
| 8 | +logging.basicConfig(encoding="utf-8", level=logging.INFO) |
| 9 | + |
| 10 | +parser = argparse.ArgumentParser("release") |
| 11 | +parser.add_argument( |
| 12 | + "branch_client_python", help="The new version number of the release.", type=str |
| 13 | +) |
| 14 | +parser.add_argument( |
| 15 | + "previous_version", help="The previous version number of the release.", type=str |
| 16 | +) |
| 17 | +parser.add_argument( |
| 18 | + "new_version", help="The new version number of the release.", type=str |
| 19 | +) |
| 20 | +parser.add_argument( |
| 21 | + "github_token", help="The github token to use for the release note.", type=str |
| 22 | +) |
| 23 | +parser.add_argument( |
| 24 | + "--dev", help="Flag to prevent pushing the release.", action="store_false" |
| 25 | +) |
| 26 | +args = parser.parse_args() |
| 27 | + |
| 28 | +previous_version = args.previous_version |
| 29 | +new_version = args.new_version |
| 30 | +branch_client_python = args.branch_client_python |
| 31 | +github_token = args.github_token |
| 32 | + |
| 33 | +os.environ["DRONE_COMMIT_AUTHOR"] = "Filigran-Automation" |
| 34 | +os.environ["GIT_AUTHOR_NAME"] = "Filigran Automation" |
| 35 | +os. environ[ "GIT_AUTHOR_EMAIL"] = "[email protected]" |
| 36 | +os.environ["GIT_COMMITTER_NAME"] = "Filigran Automation" |
| 37 | +os. environ[ "GIT_COMMITTER_EMAIL"] = "[email protected]" |
| 38 | + |
| 39 | +# Python library release |
| 40 | +logging.info("[client-python] Starting the release") |
| 41 | +with open("./pyobas/__init__.py", "r") as file: |
| 42 | + filedata = file.read() |
| 43 | +filedata = filedata.replace(previous_version, new_version) |
| 44 | +with open("./pyobas/__init__.py", "w") as file: |
| 45 | + file.write(filedata) |
| 46 | +with open("./pyobas/_version.py", "r") as file: |
| 47 | + filedata = file.read() |
| 48 | +filedata = filedata.replace(previous_version, new_version) |
| 49 | +with open("./pyobas/_version.py", "w") as file: |
| 50 | + file.write(filedata) |
| 51 | + |
| 52 | +# Commit the change |
| 53 | +logging.info("[client-python] Pushing to " + branch_client_python) |
| 54 | +os.system('git commit -a -m "[client] Release ' + new_version + '" > /dev/null 2>&1') |
| 55 | +if not args.dev: |
| 56 | + os.system("git push origin " + branch_client_python + " > /dev/null 2>&1") |
| 57 | + |
| 58 | +logging.info("[client-python] Tagging") |
| 59 | +os.system("git tag -f " + new_version) |
| 60 | +if not args.dev: |
| 61 | + os.system("git push -f --tags > /dev/null 2>&1") |
| 62 | + |
| 63 | +logging.info("[client-python] Generating release") |
| 64 | +os.system("gren release > /dev/null 2>&1") |
| 65 | + |
| 66 | +# Modify the release note |
| 67 | +logging.info("[client-python] Getting the current release note") |
| 68 | +release = requests.get( |
| 69 | + "https://api.github.com/repos/OpenBAS-Platform/client-python/releases/latest", |
| 70 | + headers={ |
| 71 | + "Accept": "application/vnd.github+json", |
| 72 | + "Authorization": "Bearer " + github_token, |
| 73 | + "X-GitHub-Api-Version": "2022-11-28", |
| 74 | + }, |
| 75 | +) |
| 76 | +release_data = release.json() |
| 77 | +release_body = release_data["body"] |
| 78 | + |
| 79 | +logging.info("[client-python] Generating the new release note") |
| 80 | +if not args.dev: |
| 81 | + github_release_note = requests.post( |
| 82 | + "https://api.github.com/repos/OpenBAS-Platform/client-python/releases/generate-notes", |
| 83 | + headers={ |
| 84 | + "Accept": "application/vnd.github+json", |
| 85 | + "Authorization": "Bearer " + github_token, |
| 86 | + "X-GitHub-Api-Version": "2022-11-28", |
| 87 | + }, |
| 88 | + json={"tag_name": new_version, "previous_tag_name": previous_version}, |
| 89 | + ) |
| 90 | + github_release_note_data = github_release_note.json() |
| 91 | + github_release_note_data_body = github_release_note_data["body"] |
| 92 | + if "Full Changelog" not in release_body: |
| 93 | + new_release_note = ( |
| 94 | + release_body |
| 95 | + + "\n" |
| 96 | + + github_release_note_data_body.replace( |
| 97 | + "## What's Changed", "#### Pull Requests:\n" |
| 98 | + ).replace("## New Contributors", "#### New Contributors:\n") |
| 99 | + ) |
| 100 | + else: |
| 101 | + new_release_note = release_body |
| 102 | + |
| 103 | + logging.info("[client-python] Updating the release") |
| 104 | + requests.patch( |
| 105 | + "https://api.github.com/repos/OpenBAS-Platform/client-python/releases/" |
| 106 | + + str(release_data["id"]), |
| 107 | + headers={ |
| 108 | + "Accept": "application/vnd.github+json", |
| 109 | + "Authorization": "Bearer " + github_token, |
| 110 | + "X-GitHub-Api-Version": "2022-11-28", |
| 111 | + }, |
| 112 | + json={"body": new_release_note}, |
| 113 | + ) |
| 114 | + |
| 115 | +if not args.dev: |
| 116 | + closeRelease( |
| 117 | + "https://api.github.com/repos/OpenBAS-Platform/client-python", |
| 118 | + new_version, |
| 119 | + github_token, |
| 120 | + ) |
| 121 | + |
| 122 | +logging.info( |
| 123 | + "[client-python] Release done! Waiting 10 minutes for CI/CD and publication..." |
| 124 | +) |
| 125 | + |
| 126 | +if not args.dev: |
| 127 | + check_release("https://pypi.org/simple/pyobas/", "pyobas-" + new_version, 10) |
0 commit comments