|
| 1 | +""" |
| 2 | +Creates a direct commit to specified branch (in the config) to update the changelog, package version and validation exceptions for a new release using the GitHub API. |
| 3 | +Quite often the changelog gets distorted between the time we branch for the release and the time we will branch back. |
| 4 | +To mitigate this we want to create changelog update PR straight away and merge it fast while proceeding with the release. |
| 5 | +
|
| 6 | +This will also allow us to skip any PRs after releasing, unless, we made some changes on this branch. |
| 7 | +
|
| 8 | +""" |
| 9 | +#!/usr/bin/env python3 |
| 10 | +import os |
| 11 | +import sys |
| 12 | +from github import GithubException |
| 13 | +from git import Actor |
| 14 | + |
| 15 | +PARENT_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), '../')) |
| 16 | +sys.path.insert(0, PARENT_DIR) |
| 17 | + |
| 18 | +from ReleaseAutomation.release_config import ReleaseConfig |
| 19 | +from Utils.general_utils import get_package_version_from_manifest, update_changelog, update_package_version_by_patch, update_validation_exceptions |
| 20 | +from Utils.git_utils import get_local_repo |
| 21 | + |
| 22 | +def commitChangelogAndPackageVersionUpdates(config: ReleaseConfig): |
| 23 | + """ |
| 24 | + The function updates the changelog and package version of the package in anticipation of a new release. |
| 25 | + This means that it will |
| 26 | + 1) Clean and update the changelog for the current package version. |
| 27 | + 2) Add new Unreleased section template at the top. |
| 28 | + 3) Update the package version in the package.json file by incrementing the patch version to signify the current state of the package. |
| 29 | + 4) Update package version in the validation exceptions to match the new package version. |
| 30 | +
|
| 31 | + This assumes that at the same time you already branched off for the release. Otherwise it may be confusing |
| 32 | + """ |
| 33 | + |
| 34 | + try: |
| 35 | + if not config.github_manager.is_branch_present(config.default_repo_branch): |
| 36 | + print(f"Branch '{config.default_repo_branch}' does not exist. Exiting.") |
| 37 | + sys.exit(1) |
| 38 | + |
| 39 | + repo = get_local_repo() |
| 40 | + repo.git.fetch() |
| 41 | + repo.git.checkout(config.default_repo_branch) |
| 42 | + repo.git.pull("origin", config.default_repo_branch) |
| 43 | + |
| 44 | + # Update the changelog file with adding new [Unreleased] section |
| 45 | + update_changelog(config.changelog_path, config.package_version, add_unreleased_template=True) |
| 46 | + # Update the package version by patch to represent the "current package state" after release |
| 47 | + updated_package_version = update_package_version_by_patch(config.manifest_path) |
| 48 | + update_validation_exceptions(config.validation_exceptions_path, updated_package_version) |
| 49 | + |
| 50 | + repo.git.add(config.changelog_path) |
| 51 | + repo.git.add(config.manifest_path) |
| 52 | + repo.git.add(config.validation_exceptions_path) |
| 53 | + |
| 54 | + author = Actor(config.commiter_name, config.commiter_email) |
| 55 | + committer = Actor(config.commiter_name, config.commiter_email) |
| 56 | + |
| 57 | + repo.index.commit(config.commit_message, author=author, committer=committer, skip_hooks=True) |
| 58 | + repo.git.push("origin", config.default_repo_branch) |
| 59 | + |
| 60 | + print(f"Successfully updated and pushed the changelog on branch: {config.default_repo_branch}") |
| 61 | + |
| 62 | + except GithubException as e: |
| 63 | + print(f"An error occurred with the GitHub API: {e.status}", file=sys.stderr) |
| 64 | + print(f"Error details: {e.data}", file=sys.stderr) |
| 65 | + sys.exit(1) |
| 66 | + except Exception as e: |
| 67 | + print(f"An unexpected error occurred: {e}", file=sys.stderr) |
| 68 | + sys.exit(1) |
0 commit comments