|
| 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 | + |
| 17 | +module.exports = async ({ github, context, core }) => { |
| 18 | + const commentBody = context.payload.comment.body; |
| 19 | + const prNumber = context.payload.issue.number; |
| 20 | + |
| 21 | + core.info(`Processing comment: ${commentBody}`); |
| 22 | + |
| 23 | + // Parse comment for /cherry-pick branches |
| 24 | + const cherryPickPattern = /^\/cherry-pick\s+(.+)$/m; |
| 25 | + const match = commentBody.match(cherryPickPattern); |
| 26 | + |
| 27 | + if (!match) { |
| 28 | + core.warning('Comment does not match /cherry-pick pattern'); |
| 29 | + return { success: false, message: 'Invalid format' }; |
| 30 | + } |
| 31 | + |
| 32 | + // Extract all release branches (space-separated) |
| 33 | + const branchesText = match[1].trim(); |
| 34 | + const branchPattern = /release-\d+\.\d+(?:\.\d+)?/g; |
| 35 | + const branches = branchesText.match(branchPattern) || []; |
| 36 | + |
| 37 | + if (branches.length === 0) { |
| 38 | + core.warning('No valid release branches found in comment'); |
| 39 | + await github.rest.reactions.createForIssueComment({ |
| 40 | + owner: context.repo.owner, |
| 41 | + repo: context.repo.repo, |
| 42 | + comment_id: context.payload.comment.id, |
| 43 | + content: 'confused' |
| 44 | + }); |
| 45 | + return { success: false, message: 'No valid branches found' }; |
| 46 | + } |
| 47 | + |
| 48 | + core.info(`Found branches: ${branches.join(', ')}`); |
| 49 | + |
| 50 | + // Add labels to PR |
| 51 | + const labels = branches.map(branch => `cherry-pick/${branch}`); |
| 52 | + |
| 53 | + try { |
| 54 | + await github.rest.issues.addLabels({ |
| 55 | + owner: context.repo.owner, |
| 56 | + repo: context.repo.repo, |
| 57 | + issue_number: prNumber, |
| 58 | + labels: labels |
| 59 | + }); |
| 60 | + core.info(`Added labels: ${labels.join(', ')}`); |
| 61 | + } catch (error) { |
| 62 | + core.error(`Failed to add labels: ${error.message}`); |
| 63 | + await github.rest.reactions.createForIssueComment({ |
| 64 | + owner: context.repo.owner, |
| 65 | + repo: context.repo.repo, |
| 66 | + comment_id: context.payload.comment.id, |
| 67 | + content: '-1' |
| 68 | + }); |
| 69 | + return { success: false, message: error.message }; |
| 70 | + } |
| 71 | + |
| 72 | + // React with checkmark emoji |
| 73 | + await github.rest.reactions.createForIssueComment({ |
| 74 | + owner: context.repo.owner, |
| 75 | + repo: context.repo.repo, |
| 76 | + comment_id: context.payload.comment.id, |
| 77 | + content: '+1' |
| 78 | + }); |
| 79 | + |
| 80 | + // Check if PR is already merged |
| 81 | + const { data: pullRequest } = await github.rest.pulls.get({ |
| 82 | + owner: context.repo.owner, |
| 83 | + repo: context.repo.repo, |
| 84 | + pull_number: prNumber |
| 85 | + }); |
| 86 | + |
| 87 | + if (pullRequest.merged) { |
| 88 | + core.info('PR is already merged - triggering backport immediately'); |
| 89 | + |
| 90 | + // Set branches in environment and trigger backport |
| 91 | + process.env.BRANCHES_JSON = JSON.stringify(branches); |
| 92 | + |
| 93 | + // Run backport script |
| 94 | + const backportScript = require('./backport.js'); |
| 95 | + const results = await backportScript({ github, context, core }); |
| 96 | + |
| 97 | + return { |
| 98 | + success: true, |
| 99 | + message: `Labels added and backport triggered for: ${branches.join(', ')}`, |
| 100 | + backportResults: results |
| 101 | + }; |
| 102 | + } else { |
| 103 | + core.info('PR not yet merged - labels added, backport will trigger on merge'); |
| 104 | + return { |
| 105 | + success: true, |
| 106 | + message: `Labels added for: ${branches.join(', ')}. Backport will trigger on merge.` |
| 107 | + }; |
| 108 | + } |
| 109 | +}; |
| 110 | + |
0 commit comments