Skip to content

Commit 1d57074

Browse files
committed
auto create PR when new release
1 parent 7c6497c commit 1d57074

File tree

2 files changed

+115
-0
lines changed

2 files changed

+115
-0
lines changed

.github/scripts/update_files.py

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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)

.github/workflows/update_version.yml

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
name: Create Pull Request on Release
2+
3+
on:
4+
release:
5+
types: [published]
6+
7+
jobs:
8+
update-version:
9+
runs-on: ubuntu-latest
10+
11+
steps:
12+
- name: Checkout repository
13+
uses: actions/checkout@v2
14+
15+
- name: Set up Python
16+
uses: actions/setup-python@v2
17+
with:
18+
python-version: '3.x'
19+
20+
- name: Install dependencies
21+
run: |
22+
python -m pip install --upgrade pip
23+
pip install PyGithub
24+
25+
- name: Configure Git
26+
run: |
27+
git config --global user.email "github-actions[bot]@users.noreply.github.com"
28+
git config --global user.name "github-actions[bot]"
29+
30+
- name: Run update script
31+
env:
32+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
33+
GITHUB_REPOSITORY: ${{ github.repository }}
34+
GITHUB_REF: ${{ github.ref }}
35+
run: python .github/scripts/update_files.py

0 commit comments

Comments
 (0)