|
| 1 | +import os |
| 2 | +import subprocess |
| 3 | +from github import Github |
| 4 | + |
| 5 | +def get_previous_release(repo): |
| 6 | + releases = repo.get_releases() |
| 7 | + if releases.totalCount < 2: |
| 8 | + raise Exception("Not enough releases to determine previous release.") |
| 9 | + return releases[1].tag_name |
| 10 | + |
| 11 | +def replace_version_in_file(file_path, old_version, new_version): |
| 12 | + with open(file_path, 'r') as file: |
| 13 | + file_content = file.read() |
| 14 | + |
| 15 | + updated_content = file_content.replace(old_version, new_version) |
| 16 | + |
| 17 | + with open(file_path, 'w') as file: |
| 18 | + file.write(updated_content) |
| 19 | + |
| 20 | +def create_branch_and_push(branch_name): |
| 21 | + subprocess.run(['git', 'checkout', '-b', branch_name]) |
| 22 | + subprocess.run(['git', 'add', '.']) |
| 23 | + subprocess.run(['git', 'commit', '-m', f'bump Shifu to {new_version}']) |
| 24 | + subprocess.run(['git', 'push', '--set-upstream', 'origin', branch_name]) |
| 25 | + |
| 26 | +def create_pull_request(repo, branch_name, old_version, new_version): |
| 27 | + pr_body = ( |
| 28 | + f'**What type of PR is this?**\n\n' |
| 29 | + f'/kind documentation\n\n' |
| 30 | + f'**What this PR does / why we need it:**\n\n' |
| 31 | + f'This PR updates Shifu to {new_version}\n\n' |
| 32 | + f'**Which issue(s) this PR fixes**:\n\n' |
| 33 | + f'Fixes #\n\n' |
| 34 | + f'#### How is this PR tested:\n\n' |
| 35 | + f'- [ ] unit test\n' |
| 36 | + f'- [ ] e2e test\n' |
| 37 | + f'- [ ] other (please specify)\n\n' |
| 38 | + f'**Special notes for your reviewer:**\n\n' |
| 39 | + f'**Does this PR introduce a user-facing change?**\n\n' |
| 40 | + f'```\n' |
| 41 | + f'- bump Shifu from {old_version} to {new_version}\n' |
| 42 | + f'```\n\n' |
| 43 | + f'**Additional documentation e.g., KEPs (Kubernetes Enhancement Proposals), usage docs, etc.:**\n\n' |
| 44 | + f'```\n\n' |
| 45 | + f'```\n\n' |
| 46 | + ) |
| 47 | + pr = repo.create_pull( |
| 48 | + title=f'bump Shifu to {new_version}', |
| 49 | + body=pr_body, |
| 50 | + head=branch_name, |
| 51 | + base='main' |
| 52 | + ) |
| 53 | + print(f'Pull request created: {pr.html_url}') |
| 54 | + |
| 55 | +if __name__ == "__main__": |
| 56 | + new_version = os.getenv('GITHUB_REF').split('/')[-1] |
| 57 | + |
| 58 | + token = os.getenv('GITHUB_TOKEN') |
| 59 | + repo_name = os.getenv('GITHUB_REPOSITORY') |
| 60 | + g = Github(token) |
| 61 | + repo = g.get_repo(repo_name) |
| 62 | + |
| 63 | + previous_version = get_previous_release(repo) |
| 64 | + print(f'PREVIOUS VERSION: {previous_version}. CURRENT VERSION: {new_version}') |
| 65 | + |
| 66 | + files_to_update = [ |
| 67 | + 'docs/guides/install/install-shifu-prod.md', |
| 68 | + 'docs/guides/telemetryservice/installtelemetryservice.md', |
| 69 | + 'i18n/zh-Hans/docusaurus-plugin-content-docs/current/guides/install/install-shifu-prod.md', |
| 70 | + 'i18n/zh-Hans/docusaurus-plugin-content-docs/current/guides/telemetryservice/installtelemetryservice.md', |
| 71 | + 'src/components/home/News/index.js' |
| 72 | + ] |
| 73 | + |
| 74 | + for file in files_to_update: |
| 75 | + replace_version_in_file(file, previous_version, new_version) |
| 76 | + |
| 77 | + branch_name = f'tag-{new_version}' |
| 78 | + create_branch_and_push(branch_name) |
| 79 | + |
| 80 | + create_pull_request(repo, branch_name, previous_version, new_version) |
0 commit comments