Skip to content

Commit

Permalink
GH Actions backport
Browse files Browse the repository at this point in the history
  • Loading branch information
Youssef1313 committed Jan 9, 2025
1 parent 2cf523d commit 4231021
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 56 deletions.
115 changes: 60 additions & 55 deletions .github/workflows/backport-base.yml
Original file line number Diff line number Diff line change
Expand Up @@ -164,74 +164,74 @@ jobs:
// download and apply patch
await exec.exec(`curl -sSL "${context.payload.issue.pull_request.patch_url}" --output changes.patch`);
const git_am_command = "git am --3way --empty=keep --ignore-whitespace --keep-non-patch changes.patch";
let git_am_output = `$ ${git_am_command}\n\n`;
let git_am_failed = false;
try {
await exec.exec(git_am_command, [], {
listeners: {
stdout: function stdout(data) { git_am_output += data; },
stderr: function stderr(data) { git_am_output += data; }
}
});
} catch (error) {
git_am_output += error;
git_am_failed = true;
}
// const git_am_command = "git am --3way --empty=keep --ignore-whitespace --keep-non-patch changes.patch";
// let git_am_output = `$ ${git_am_command}\n\n`;
// let git_am_failed = false;
// try {
// await exec.exec(git_am_command, [], {
// listeners: {
// stdout: function stdout(data) { git_am_output += data; },
// stderr: function stderr(data) { git_am_output += data; }
// }
// });
// } catch (error) {
// git_am_output += error;
// git_am_failed = true;
// }
if (git_am_failed) {
const git_am_failed_body = `@${context.payload.comment.user.login} backporting to ${target_branch} failed, the patch most likely resulted in conflicts:\n\n\`\`\`shell\n${git_am_output}\n\`\`\`\n\nPlease backport manually!`;
await github.rest.issues.createComment({
owner: repo_owner,
repo: repo_name,
issue_number: pr_number,
body: git_am_failed_body
});
core.setFailed("Error: git am failed, most likely due to a merge conflict.");
return;
}
else {
// push the temp branch to the repository
await exec.exec(`git push --force --set-upstream origin HEAD:${temp_branch}`);
}
// if (git_am_failed) {
// const git_am_failed_body = `@${context.payload.comment.user.login} backporting to ${target_branch} failed, the patch most likely resulted in conflicts:\n\n\`\`\`shell\n${git_am_output}\n\`\`\`\n\nPlease backport manually!`;
// await github.rest.issues.createComment({
// owner: repo_owner,
// repo: repo_name,
// issue_number: pr_number,
// body: git_am_failed_body
// });
// core.setFailed("Error: git am failed, most likely due to a merge conflict.");
// return;
// }
// else {
// // push the temp branch to the repository
// await exec.exec(`git push --force --set-upstream origin HEAD:${temp_branch}`);
// }
if (!should_open_pull_request) {
console.log("Backport temp branch already exists, skipping opening a PR.");
return;
}
// if (!should_open_pull_request) {
// console.log("Backport temp branch already exists, skipping opening a PR.");
// return;
// }
// prepare the GitHub PR details
// get users to cc (append PR author if different from user who issued the backport command)
let cc_users = `@${comment_user}`;
if (comment_user != context.payload.issue.user.login) cc_users += ` @${context.payload.issue.user.login}`;
// let cc_users = `@${comment_user}`;
// if (comment_user != context.payload.issue.user.login) cc_users += ` @${context.payload.issue.user.login}`;
// replace the special placeholder tokens with values
const { BACKPORT_PR_TITLE_TEMPLATE, BACKPORT_PR_DESCRIPTION_TEMPLATE } = process.env
// const { BACKPORT_PR_TITLE_TEMPLATE, BACKPORT_PR_DESCRIPTION_TEMPLATE } = process.env
const backport_pr_title = BACKPORT_PR_TITLE_TEMPLATE
.replace(/%target_branch%/g, target_branch)
.replace(/%source_pr_title%/g, context.payload.issue.title)
.replace(/%source_pr_number%/g, context.payload.issue.number)
.replace(/%cc_users%/g, cc_users);
// const backport_pr_title = BACKPORT_PR_TITLE_TEMPLATE
// .replace(/%target_branch%/g, target_branch)
// .replace(/%source_pr_title%/g, context.payload.issue.title)
// .replace(/%source_pr_number%/g, context.payload.issue.number)
// .replace(/%cc_users%/g, cc_users);
const backport_pr_description = BACKPORT_PR_DESCRIPTION_TEMPLATE
.replace(/%target_branch%/g, target_branch)
.replace(/%source_pr_title%/g, context.payload.issue.title)
.replace(/%source_pr_number%/g, context.payload.issue.number)
.replace(/%cc_users%/g, cc_users);
// const backport_pr_description = BACKPORT_PR_DESCRIPTION_TEMPLATE
// .replace(/%target_branch%/g, target_branch)
// .replace(/%source_pr_title%/g, context.payload.issue.title)
// .replace(/%source_pr_number%/g, context.payload.issue.number)
// .replace(/%cc_users%/g, cc_users);
// open the GitHub PR
await github.rest.pulls.create({
owner: repo_owner,
repo: repo_name,
title: backport_pr_title,
body: backport_pr_description,
head: temp_branch,
base: target_branch
});
// await github.rest.pulls.create({
// owner: repo_owner,
// repo: repo_name,
// title: backport_pr_title,
// body: backport_pr_description,
// head: temp_branch,
// base: target_branch
// });
console.log("Successfully opened the GitHub PR.");
// console.log("Successfully opened the GitHub PR.");
} catch (error) {
core.setFailed(error);
Expand All @@ -246,6 +246,11 @@ jobs:
});
}
- uses: peter-evans/create-pull-request@67ccf781d68cd99b580ae25a5c18a1cc84ffff1f # v7
with:
token: ${{ secrets.BACKPORT_MACHINE_USER_PAT }}
push-to-fork: youssef-backport-bot/testfx

- name: Re-lock PR comments
uses: actions/github-script@v7
if: ${{ github.event.issue.locked == true && (success() || failure()) }}
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/backport.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ jobs:
pr_description_template: |
Backport of #%source_pr_number% to %target_branch%
/cc %cc_users%
/cc %cc_users%

0 comments on commit 4231021

Please sign in to comment.