1+ name : ' Update Release PR'
2+
3+ on :
4+ pull_request :
5+ branches :
6+ - master
7+
8+ jobs :
9+ update-pr :
10+ runs-on : ubuntu-latest
11+ steps :
12+ - uses : actions/checkout@v4
13+ with :
14+ fetch-depth : 0
15+ ref : ${{ github.event.pull_request.head.ref }}
16+
17+ - name : Check if branch is release branch and add label if needed
18+ uses : actions/github-script@v7
19+ id : check_branch
20+ with :
21+ github-token : ${{ secrets.GITHUB_TOKEN }}
22+ script : |
23+ const { issue: { number: issueNumber }, repo: { owner, repo } } = context;
24+ const branchName = '${{ github.event.pull_request.head.ref }}';
25+
26+ console.log(`Branch name: ${branchName}`);
27+
28+ // Check if branch follows 'release/xxx' pattern
29+ const isReleaseBranch = branchName.startsWith('release/');
30+
31+ if (isReleaseBranch) {
32+ console.log('This is a release branch');
33+
34+ // Get current labels
35+ const { data: currentLabels } = await github.rest.issues.listLabelsOnIssue({
36+ owner,
37+ repo,
38+ issue_number: issueNumber
39+ });
40+
41+ // Check if 'release' label is already applied
42+ const hasReleaseLabel = currentLabels.some(label => label.name === 'release');
43+
44+ if (!hasReleaseLabel) {
45+ console.log('Adding release label');
46+ await github.rest.issues.addLabels({
47+ owner,
48+ repo,
49+ issue_number: issueNumber,
50+ labels: ['release']
51+ });
52+ } else {
53+ console.log('Release label already exists');
54+ }
55+
56+ return true;
57+ } else {
58+ console.log('Not a release branch');
59+ return false;
60+ }
61+
62+ - name : Fetch base branch
63+ if : contains(github.event.pull_request.labels.*.name, 'release') || steps.check_branch.outputs.result == 'true'
64+ run : |
65+ git fetch origin ${{ github.event.pull_request.base.ref }}
66+
67+ - name : Generate PR content
68+ if : contains(github.event.pull_request.labels.*.name, 'release') || steps.check_branch.outputs.result == 'true'
69+ id : generate_content
70+ run : |
71+ # Fetch all tags
72+ git fetch --tags
73+
74+ # Find the most recent version tag (vx.x.x format)
75+ LATEST_TAG=$(git tag -l "v*.*.*" | sort -V | tail -n1)
76+
77+ if [ -z "$LATEST_TAG" ]; then
78+ echo "No version tags found. Using all commits in branch."
79+ COMMITS=$(git log --pretty=format:"* %s" HEAD)
80+ else
81+ echo "Found latest tag: $LATEST_TAG"
82+ # Get all commits since the latest tag
83+ COMMITS=$(git log --pretty=format:"* %s" $LATEST_TAG..HEAD)
84+ fi
85+
86+ # If no commits found, provide a clearer message
87+ if [ -z "$COMMITS" ]; then
88+ COMMITS="* No new commits since last release tag ($LATEST_TAG)"
89+ fi
90+
91+ # Create PR body content with the commits
92+ PR_BODY="## Release Changes\n\n"
93+ PR_BODY="${PR_BODY}${COMMITS}"
94+
95+ # Save to file to maintain formatting
96+ echo -e "$PR_BODY" > pr_body.txt
97+
98+ # Use delimiter for multiline output
99+ echo "pr_body<<EOF" >> $GITHUB_OUTPUT
100+ cat pr_body.txt >> $GITHUB_OUTPUT
101+ echo "EOF" >> $GITHUB_OUTPUT
102+
103+ - name : Update Pull Request
104+ if : contains(github.event.pull_request.labels.*.name, 'release') || steps.check_branch.outputs.result == 'true'
105+ uses : actions/github-script@v7
106+ with :
107+ github-token : ${{ secrets.GITHUB_TOKEN }}
108+ script : |
109+ const { issue: { number: issueNumber }, repo: { owner, repo } } = context;
110+
111+ const prBody = `${{ steps.generate_content.outputs.pr_body }}`;
112+
113+ github.rest.issues.update({
114+ owner,
115+ repo,
116+ issue_number: issueNumber,
117+ body: prBody
118+ });
0 commit comments