Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 21 additions & 6 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,30 @@ function findIssueByNumber(context, token, issueNumber) {
return issue;
});
}
function addCommentToPr(context, token, prNumber, issueNumber) {
function appendFixesToPrBody(context, token, prNumber, issueNumber) {
return __awaiter(this, void 0, void 0, function* () {
const octokit = github.getOctokit(token);
const { owner, repo } = context.repo;
const res = yield octokit.rest.issues.createComment({
// Get the current PR body
const { data: pr } = yield octokit.rest.pulls.get({
owner,
repo,
issue_number: prNumber,
body: `Fixes #${issueNumber}`,
pull_number: prNumber,
});
let body = pr.body || "";
// Remove any existing 'Fixes #<issueNumber>' at the end
body = body.replace(new RegExp(`\n*Fixes #${issueNumber}$`), "");
// Append the new line
if (body.length > 0 && !body.endsWith("\n")) {
body += "\n";
}
body += `Fixes #${issueNumber}`;
// Update the PR body
const res = yield octokit.rest.pulls.update({
owner,
repo,
pull_number: prNumber,
body,
});
return res.data;
});
Expand All @@ -100,8 +115,8 @@ function run() {
core.setFailed("No pull request found in the context.");
return;
}
const comment = yield addCommentToPr(context, token, pullRequest.number, issueNumber);
core.info(`Commented on PR #${pullRequest.number}: ${comment.html_url}`);
const updatedPr = yield appendFixesToPrBody(context, token, pullRequest.number, issueNumber);
core.info(`Updated PR #${pullRequest.number} body with 'Fixes #${issueNumber}'`);
});
}
function main() {
Expand Down
31 changes: 23 additions & 8 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,35 @@ async function findIssueByNumber(
return issue;
}

async function addCommentToPr(
async function appendFixesToPrBody(
context: Context,
token: string,
prNumber: number,
issueNumber: number
) {
const octokit = github.getOctokit(token);
const { owner, repo } = context.repo;

const res = await octokit.rest.issues.createComment({
// Get the current PR body
const { data: pr } = await octokit.rest.pulls.get({
owner,
repo,
issue_number: prNumber,
body: `Fixes #${issueNumber}`,
pull_number: prNumber,
});
let body = pr.body || "";
// Remove any existing 'Fixes #<issueNumber>' at the end
body = body.replace(new RegExp(`\n*Fixes #${issueNumber}$`), "");
// Append the new line
if (body.length > 0 && !body.endsWith("\n")) {
body += "\n";
}
body += `Fixes #${issueNumber}`;
// Update the PR body
const res = await octokit.rest.pulls.update({
owner,
repo,
pull_number: prNumber,
body,
});

return res.data;
}

Expand All @@ -70,13 +83,15 @@ async function run(): Promise<void> {
core.setFailed("No pull request found in the context.");
return;
}
const comment = await addCommentToPr(
const updatedPr = await appendFixesToPrBody(
context,
token,
pullRequest.number,
issueNumber
);
core.info(`Commented on PR #${pullRequest.number}: ${comment.html_url}`);
core.info(
`Updated PR #${pullRequest.number} body with 'Fixes #${issueNumber}'`
);
}

async function main() {
Expand Down