Skip to content

Commit a44973d

Browse files
committed
Added script to update changelog and package version on default branch
1 parent eedf1d7 commit a44973d

File tree

1 file changed

+92
-0
lines changed

1 file changed

+92
-0
lines changed
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
"""
2+
Creates a pull request to update the changelog 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 script performs the following actions:
7+
1. Reads the package version from the package.json file and updates the CHANGELOG.md file while also cleaning it from empty sections.
8+
2. Updates the package version in the package.json file by incrementing the patch version to represent the current package state.
9+
3. Commits the change and pushes to the branch.
10+
11+
Requirements:
12+
- A GITHUB TOKEN with 'repo' scope must be available as an environment variable.
13+
"""
14+
#!/usr/bin/env python3
15+
import os
16+
import sys
17+
from github import GithubException
18+
19+
UTILS_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), '../Utils'))
20+
sys.path.insert(0, UTILS_DIR)
21+
from general_utils import get_package_version_from_manifest, update_changelog, update_package_version_by_patch # nopep8
22+
from git_utils import get_local_repo, GithubUtils # nopep8
23+
from config import getNetcodePackageName, getPackageManifestPath, getNetcodeGithubRepo, getDefaultRepoBranch, getPackageChangelogPath # nopep8
24+
25+
def updateNetcodeChangelogAndPackageVersionAndPush():
26+
"""
27+
The function updates the changelog and package version for NGO in anticipation of a new release.
28+
This means that it will clean and update the changelog for the current package version, then it will add new Unreleased section template at the top
29+
and finally it will update the package version in the package.json file by incrementing the patch version to signify the current state of the package.
30+
31+
This assumes that at the same time you already branched off for the release. Otherwise it may be confusing
32+
"""
33+
34+
ngo_package_name = getNetcodePackageName()
35+
ngo_manifest_path = getPackageManifestPath()
36+
ngo_changelog_path = getPackageChangelogPath()
37+
ngo_package_version = get_package_version_from_manifest(ngo_manifest_path)
38+
ngo_github_repo = getNetcodeGithubRepo()
39+
ngo_default_repo_branch_to_push_to = getDefaultRepoBranch() # The branch to which the changes will be pushed. For our purposes it's a default repo branch.
40+
ngo_github_token = os.environ.get("GITHUB_TOKEN")
41+
42+
print(f"Using branch: {ngo_default_repo_branch_to_push_to} for pushing changes")
43+
44+
if not os.path.exists(ngo_manifest_path):
45+
print(f" Path does not exist: {ngo_manifest_path}")
46+
sys.exit(1)
47+
48+
if not os.path.exists(ngo_changelog_path):
49+
print(f" Path does not exist: {ngo_changelog_path}")
50+
sys.exit(1)
51+
52+
if ngo_package_version is None:
53+
print(f"Package version not found at {ngo_manifest_path}")
54+
sys.exit(1)
55+
56+
if not ngo_github_token:
57+
print("Error: GITHUB_TOKEN environment variable not set.", file=sys.stderr)
58+
sys.exit(1)
59+
60+
try:
61+
# Initialize PyGithub and get the repository object
62+
GithubUtils(ngo_github_token, ngo_github_repo)
63+
64+
commit_message = f"Updated changelog and package version for NGO in anticipation of v{ngo_package_version} release"
65+
66+
repo = get_local_repo()
67+
repo.git.fetch()
68+
repo.git.checkout(ngo_default_repo_branch_to_push_to)
69+
repo.git.pull("origin", ngo_default_repo_branch_to_push_to)
70+
71+
# Update the changelog file with adding new [Unreleased] section
72+
update_changelog(ngo_package_version, ngo_package_name, add_unreleased_template=True)
73+
# Update the package version by patch to represent the "current package state" after release
74+
update_package_version_by_patch(ngo_manifest_path)
75+
76+
repo.git.add(ngo_changelog_path)
77+
repo.git.add(ngo_manifest_path)
78+
repo.index.commit(commit_message, skip_hooks=True)
79+
repo.git.push("origin", ngo_default_repo_branch_to_push_to)
80+
81+
print(f"Successfully updated and pushed the changelog on branch: {ngo_default_repo_branch_to_push_to}")
82+
83+
except GithubException as e:
84+
print(f"An error occurred with the GitHub API: {e.status}", file=sys.stderr)
85+
print(f"Error details: {e.data}", file=sys.stderr)
86+
sys.exit(1)
87+
except Exception as e:
88+
print(f"An unexpected error occurred: {e}", file=sys.stderr)
89+
sys.exit(1)
90+
91+
if __name__ == "__main__":
92+
updateNetcodeChangelogAndPackageVersionAndPush()

0 commit comments

Comments
 (0)