Skip to content

Commit 7dbcd48

Browse files
Address reviewer comments: styling changes + readability
Signed-off-by: Karthik Vetrivel <[email protected]>
1 parent e1ae960 commit 7dbcd48

File tree

3 files changed

+50
-20
lines changed

3 files changed

+50
-20
lines changed

.github/scripts/backport.js

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
/**
2+
* Copyright 2025 NVIDIA CORPORATION
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
117
module.exports = async ({ github, context, core }) => {
218
const branches = JSON.parse(process.env.BRANCHES_JSON || '[]');
319

@@ -70,7 +86,7 @@ for (const targetBranch of branches) {
7086
// Create backport branch from target release branch
7187
core.info(`Creating branch ${backportBranch} from ${targetBranch}`);
7288
execSync(`git fetch origin ${targetBranch}:${targetBranch}`, { stdio: 'inherit' });
73-
execSync(`git checkout -b ${backportBranch} ${targetBranch}`, { stdio: 'inherit' });
89+
execSync(`git checkout ${backportBranch} || git checkout -b ${backportBranch} ${targetBranch}`, { stdio: 'inherit' });
7490
// Cherry-pick each commit from the PR
7591
let hasConflicts = false;
7692
for (let i = 0; i < commits.length; i++) {
@@ -107,10 +123,12 @@ for (const targetBranch of branches) {
107123
execSync(`git push origin ${backportBranch}`, { stdio: 'inherit' });
108124
// Create pull request
109125
const commitList = commits.map(c => `- \`${c.sha.substring(0, 7)}\` ${c.commit.message.split('\n')[0]}`).join('\n');
110-
const prBody = hasConflicts
111-
? `🤖 **Automated backport of #${prNumber} to \`${targetBranch}\`**
112-
113-
⚠️ **This PR has merge conflicts that need manual resolution.**
126+
127+
// Build PR body based on conflict status
128+
let prBody = `🤖 **Automated backport of #${prNumber} to \`${targetBranch}\`**\n\n`;
129+
130+
if (hasConflicts) {
131+
prBody += `⚠️ **This PR has merge conflicts that need manual resolution.**
114132
115133
Original PR: #${prNumber}
116134
Original Author: @${prAuthor}
@@ -122,7 +140,7 @@ ${commitList}
122140
1. Review the conflicts in the "Files changed" tab
123141
2. Check out this branch locally: \`git fetch origin ${backportBranch} && git checkout ${backportBranch}\`
124142
3. Resolve conflicts manually
125-
4. Push the resolution: \`git push origin ${backportBranch}\`
143+
4. Push the resolution: \`git push -f origin ${backportBranch}\`
126144
127145
---
128146
<details>
@@ -134,12 +152,11 @@ git checkout ${backportBranch}
134152
# Resolve conflicts in your editor
135153
git add .
136154
git commit
137-
git push origin ${backportBranch}
155+
git push -f origin ${backportBranch}
138156
\`\`\`
139-
</details>`
140-
: `🤖 **Automated backport of #${prNumber} to \`${targetBranch}\`**
141-
142-
✅ Cherry-pick completed successfully with no conflicts.
157+
</details>`;
158+
} else {
159+
prBody += `✅ Cherry-pick completed successfully with no conflicts.
143160
144161
Original PR: #${prNumber}
145162
Original Author: @${prAuthor}
@@ -148,6 +165,7 @@ Original Author: @${prAuthor}
148165
${commitList}
149166
150167
This backport was automatically created by the backport bot.`;
168+
}
151169

152170
const newPR = await github.rest.pulls.create({
153171
owner: context.repo.owner,
@@ -218,5 +236,3 @@ for (const result of results) {
218236
}
219237
return results;
220238
};
221-
222-

.github/scripts/extract-branches.js

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
/**
2+
* Copyright 2025 NVIDIA CORPORATION
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
117
module.exports = async ({ github, context, core }) => {
218
let branches = [];
319

@@ -12,8 +28,8 @@ module.exports = async ({ github, context, core }) => {
1228
// Check PR body
1329
if (context.payload.pull_request?.body) {
1430
const prBody = context.payload.pull_request.body;
15-
// Enforce release-X.Y or release-X.Y.Z
16-
const bodyMatches = prBody.matchAll(/\/cherry-pick\s+(release-\d+\.\d+(?:\.\d+)?)/g);
31+
// Strict ASCII, anchored; allow X.Y or X.Y.Z
32+
const bodyMatches = prBody.matchAll(/^\/cherry-pick\s+(release-\d+\.\d+(?:\.\d+)?)/gmi);
1733
branches.push(...Array.from(bodyMatches, m => m[1]));
1834
}
1935

@@ -25,7 +41,7 @@ module.exports = async ({ github, context, core }) => {
2541
});
2642

2743
for (const comment of comments.data) {
28-
const commentMatches = comment.body.matchAll(/\/cherry-pick\s+(release-\d+\.\d+(?:\.\d+)?)/g);
44+
const commentMatches = comment.body.matchAll(/^\/cherry-pick\s+(release-\d+\.\d+(?:\.\d+)?)/gmi);
2945
branches.push(...Array.from(commentMatches, m => m[1]));
3046
}
3147

.github/workflows/cherrypick.yml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ jobs:
4343

4444
- name: Extract target branches from PR comments
4545
id: extract-branches
46-
uses: actions/github-script@v7
46+
uses: actions/github-script@v8
4747
with:
4848
script: |
4949
const run = require('./.github/scripts/extract-branches.js');
@@ -56,12 +56,10 @@ jobs:
5656
5757
- name: Backport to release branches
5858
id: backport
59-
uses: actions/github-script@v7
59+
uses: actions/github-script@v8
6060
env:
6161
BRANCHES_JSON: ${{ steps.extract-branches.outputs.result }}
6262
with:
6363
script: |
6464
const run = require('./.github/scripts/backport.js');
6565
return await run({ github, context, core });
66-
67-

0 commit comments

Comments
 (0)