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+ # Skip the latest commit (the current PR commit)
78+ if [ -z "$LATEST_TAG" ]; then
79+ echo "No version tags found. Using all commits in branch except the latest."
80+ COMMITS=$(git log --pretty=format:"* %s" HEAD~1)
81+ else
82+ echo "Found latest tag: $LATEST_TAG"
83+ # Get all commits since the latest tag, excluding the latest commit
84+ COMMITS=$(git log --pretty=format:"* %s" $LATEST_TAG..HEAD~1)
85+ fi
86+
87+ # If no commits found, provide a clearer message
88+ if [ -z "$COMMITS" ]; then
89+ COMMITS="* No new commits since last release tag ($LATEST_TAG)"
90+ fi
91+
92+ # Create PR body content with the commits
93+ PR_BODY="## Release Changes\n\n"
94+ PR_BODY="${PR_BODY}${COMMITS}"
95+
96+ # Save to file to maintain formatting
97+ echo -e "$PR_BODY" > pr_body.txt
98+
99+ # Use delimiter for multiline output
100+ echo "pr_body<<EOF" >> $GITHUB_OUTPUT
101+ cat pr_body.txt >> $GITHUB_OUTPUT
102+ echo "EOF" >> $GITHUB_OUTPUT
103+
104+ - name : Update Pull Request
105+ if : contains(github.event.pull_request.labels.*.name, 'release') || steps.check_branch.outputs.result == 'true'
106+ uses : actions/github-script@v7
107+ with :
108+ github-token : ${{ secrets.GITHUB_TOKEN }}
109+ script : |
110+ const { issue: { number: issueNumber }, repo: { owner, repo } } = context;
111+
112+ const prBody = `${{ steps.generate_content.outputs.pr_body }}`;
113+
114+ github.rest.issues.update({
115+ owner,
116+ repo,
117+ issue_number: issueNumber,
118+ body: prBody
119+ });
0 commit comments