Skip to content

Commit

Permalink
Improve comment to have running message
Browse files Browse the repository at this point in the history
  • Loading branch information
emcfarlane committed Jul 5, 2024
1 parent 35540d2 commit b250414
Show file tree
Hide file tree
Showing 3 changed files with 163 additions and 44 deletions.
136 changes: 96 additions & 40 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -45601,47 +45601,97 @@ const commentTag = "<!-- Buf results -->";
// summary should be a markdown formatted string. This function returns true if
// the comment was successfully created or updated. On failure, it returns
// false but does not throw an error.
async function commentOnPR(context, github, summary) {
const comment = `The latest Buf updates on your PR.\n\n${summary}`;
try {
const { owner, repo } = context.repo;
const prNumber = context.payload.pull_request?.number;
if (!prNumber) {
core.info("This is not a PR, skipping commenting");
return false;
}
const content = {
owner: owner,
repo: repo,
body: comment + commentTag,
};
// Check if a comment already exists and update it.
const comments = await github.paginate(github.rest.issues.listComments, {
owner: owner,
repo: repo,
issue_number: prNumber,
});
const previousComment = comments.find((comment) => comment.body?.includes(commentTag));
if (previousComment) {
await github.rest.issues.updateComment({
...content,
comment_id: previousComment.id,
});
core.info(`Updated comment ${previousComment.id} on PR #${prNumber}`);
}
else {
await github.rest.issues.createComment({
...content,
issue_number: prNumber,
});
core.info(`Commented on PR #${prNumber}`);
}
return true;
/*export async function commentOnPR(
context: Context,
github: InstanceType<typeof GitHub>,
summary: string,
): Promise<boolean> {
const comment = `The latest Buf updates on your PR.\n\n${summary}`;
try {
const { owner, repo } = context.repo;
const prNumber = context.payload.pull_request?.number;
if (!prNumber) {
core.info("This is not a PR, skipping commenting");
return false;
}
const content = {
owner: owner,
repo: repo,
body: comment + commentTag,
};
// Check if a comment already exists and update it.
const comments = await github.paginate(github.rest.issues.listComments, {
owner: owner,
repo: repo,
issue_number: prNumber,
});
const previousComment = comments.find((comment) =>
comment.body?.includes(commentTag),
);
if (previousComment) {
await github.rest.issues.updateComment({
...content,
comment_id: previousComment.id,
});
core.info(`Updated comment ${previousComment.id} on PR #${prNumber}`);
} else {
await github.rest.issues.createComment({
...content,
issue_number: prNumber,
});
core.info(`Commented on PR #${prNumber}`);
}
catch (error) {
core.info(`Error occurred while commenting on PR: ${error}`);
return false;
return true;
} catch (error) {
core.info(`Error occurred while commenting on PR: ${error}`);
return false;
}
}*/
async function findCommentOnPR(context, github) {
const { owner, repo } = context.repo;
const prNumber = context.payload.pull_request?.number;
if (!prNumber) {
core.info("This is not a PR, skipping finding comment");
return undefined;
}
const comments = await github.paginate(github.rest.issues.listComments, {
owner: owner,
repo: repo,
issue_number: prNumber,
});
const previousComment = comments.find((comment) => comment.body?.includes(commentTag));
if (previousComment) {
core.info(`Found previous comment ${previousComment.id}`);
return previousComment.id;
}
return undefined;
}
async function commentOnPR(context, github, commentID, body) {
const { owner, repo } = context.repo;
const prNumber = context.payload.pull_request?.number;
if (!prNumber) {
core.info("This is not a PR, skipping commenting");
return undefined;
}
const content = {
owner: owner,
repo: repo,
body: body + commentTag,
};
if (commentID) {
await github.rest.issues.updateComment({
...content,
comment_id: commentID,
});
core.info(`Updated comment ${commentID} on PR #${prNumber}`);
return commentID;
}
const comment = await github.rest.issues.createComment({
...content,
issue_number: prNumber,
});
core.info(`Commented ${comment.data.id} on PR #${prNumber}`);
return comment.data.id;
}

;// CONCATENATED MODULE: ./src/config.ts
Expand Down Expand Up @@ -45725,13 +45775,19 @@ async function main() {
core.info("Setup only, skipping steps");
return;
}
// Find the comment on the PR, and update it with a running message.
let commentID;
if (inputs.pr_comment) {
commentID = await findCommentOnPR(lib_github.context, github);
commentID = await commentOnPR(lib_github.context, github, commentID, "Running...");
}
// Run the buf workflow.
const steps = await runWorkflow(bufPath, inputs);
// Create a summary of the steps.
const summary = createSummary(inputs, steps);
// Comment on the PR with the summary, if requested.
if (inputs.pr_comment) {
await commentOnPR(lib_github.context, github, summary.stringify());
await commentOnPR(lib_github.context, github, commentID, summary.stringify());
}
// Write the summary to a file defined by GITHUB_STEP_SUMMARY.
// NB: Write empties the buffer and must be after the comment.
Expand Down
60 changes: 59 additions & 1 deletion src/comment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const commentTag = "<!-- Buf results -->";
// summary should be a markdown formatted string. This function returns true if
// the comment was successfully created or updated. On failure, it returns
// false but does not throw an error.
export async function commentOnPR(
/*export async function commentOnPR(
context: Context,
github: InstanceType<typeof GitHub>,
summary: string,
Expand Down Expand Up @@ -69,4 +69,62 @@ export async function commentOnPR(
core.info(`Error occurred while commenting on PR: ${error}`);
return false;
}
}*/

export async function findCommentOnPR(
context: Context,
github: InstanceType<typeof GitHub>,
): Promise<number | undefined> {
const { owner, repo } = context.repo;
const prNumber = context.payload.pull_request?.number;
if (!prNumber) {
core.info("This is not a PR, skipping finding comment");
return undefined;
}
const comments = await github.paginate(github.rest.issues.listComments, {
owner: owner,
repo: repo,
issue_number: prNumber,
});
const previousComment = comments.find((comment) =>
comment.body?.includes(commentTag),
);
if (previousComment) {
core.info(`Found previous comment ${previousComment.id}`);
return previousComment.id;
}
return undefined;
}

export async function commentOnPR(
context: Context,
github: InstanceType<typeof GitHub>,
commentID: number | undefined,
body: string,
): Promise<number | undefined> {
const { owner, repo } = context.repo;
const prNumber = context.payload.pull_request?.number;
if (!prNumber) {
core.info("This is not a PR, skipping commenting");
return undefined;
}
const content = {
owner: owner,
repo: repo,
body: body + commentTag,
};
if (commentID) {
await github.rest.issues.updateComment({
...content,
comment_id: commentID,
});
core.info(`Updated comment ${commentID} on PR #${prNumber}`);
return commentID;
}
const comment = await github.rest.issues.createComment({
...content,
issue_number: prNumber,
});
core.info(`Commented ${comment.data.id} on PR #${prNumber}`);
return comment.data.id;
}
11 changes: 8 additions & 3 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import { LabelRef } from "@buf/bufbuild_registry.bufbuild_es/buf/registry/module
import { getInputs, Inputs, getEnv } from "./inputs";
import { Outputs } from "./outputs";
import { installBuf } from "./installer";
import { commentOnPR } from "./comment";
import { findCommentOnPR, commentOnPR } from "./comment";
import { parseModuleNames, ModuleName } from "./config";

// main is the entrypoint for the action.
Expand All @@ -34,18 +34,23 @@ async function main() {
const [bufPath, bufVersion] = await installBuf(github, inputs.version);
core.setOutput(Outputs.BufVersion, bufVersion);
await login(bufPath, inputs);

if (inputs.setup_only) {
core.info("Setup only, skipping steps");
return;
}
// Find the comment on the PR, and update it with a running message.
let commentID;
if (inputs.pr_comment) {
commentID = await findCommentOnPR(context, github);
commentID = await commentOnPR(context, github, commentID, "Running...");
}
// Run the buf workflow.
const steps = await runWorkflow(bufPath, inputs);
// Create a summary of the steps.
const summary = createSummary(inputs, steps);
// Comment on the PR with the summary, if requested.
if (inputs.pr_comment) {
await commentOnPR(context, github, summary.stringify());
await commentOnPR(context, github, commentID, summary.stringify());
}
// Write the summary to a file defined by GITHUB_STEP_SUMMARY.
// NB: Write empties the buffer and must be after the comment.
Expand Down

0 comments on commit b250414

Please sign in to comment.