Skip to content

Commit

Permalink
Added the "behaviour" parameter, fixes #8
Browse files Browse the repository at this point in the history
  • Loading branch information
Razican committed Oct 23, 2022
1 parent adfd3a9 commit 7a07055
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 27 deletions.
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ inputs:
description: "Enable the default features of a crate while running the benchmark, usually the default features are enabled"
required: false
default: true
behaviour:
description: "Whether to `update` an existing comment (default) or to `create` a new comment with the results"
required: false
default: "update"
runs:
using: "node16"
main: "dist/index.js"
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

82 changes: 56 additions & 26 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ async function main() {
benchName: core.getInput("benchName"),
features: core.getInput("features"),
defaultFeatures: core.getInput("defaultFeatures"),
behaviour: core.getInput("behaviour"),
};
core.debug(`Inputs: ${inspect(inputs)}`);

Expand Down Expand Up @@ -75,32 +76,7 @@ async function main() {

const resultsAsMarkdown = convertToMarkdown(myOutput);

// An authenticated instance of `@octokit/rest`
const octokit = github.getOctokit(inputs.token);

const contextObj = { ...context.issue };

try {
const { data: comment } = await octokit.rest.issues.createComment({
owner: contextObj.owner,
repo: contextObj.repo,
issue_number: contextObj.number,
body: resultsAsMarkdown,
});
core.info(
`Created comment id '${comment.id}' on issue '${contextObj.number}' in '${contextObj.repo}'.`
);
core.setOutput("comment-id", comment.id);
} catch (err) {
core.warning(`Failed to comment: ${err}`);
core.info("Commenting is not possible from forks.");

// If we can't post to the comment, display results here.
// forkedRepos only have READ ONLY access on GITHUB_TOKEN
// https://github.community/t5/GitHub-Actions/quot-Resource-not-accessible-by-integration-quot-for-adding-a/td-p/33925
const resultsAsObject = convertToTableObject(myOutput);
console.table(resultsAsObject);
}
await saveComment(inputs, resultsAsMarkdown, myOutput);

core.debug("Succesfully run!");
}
Expand Down Expand Up @@ -288,6 +264,60 @@ function convertToTableObject(results) {
return benchResults;
}

async function saveComment(inputs, markdown, fallbackOutput) {
const octokit = github.getOctokit(inputs.token);
const contextObj = { ...context.issue };

const commentToken = `<!-- criterion-benchmark-compare: ${
contextObj.repo + contextObj.number || "unknown"
} -->`;

const { data: comments } = await octokit.rest.issues.listComments({
...contextObj.repo,
issue_number: contextObj.number,
});
const existingComment = comments.find((comment) =>
comment.body?.includes(commentToken)
);

try {
if (inputs.behaviour === "" && existingComment) {
core.debug(`Updating existing comment #${existingComment.id}`);

await octokit.rest.issues.updateComment({
owner: contextObj.owner,
repo: contextObj.repo,
issue_number: contextObj.number,
body: markdown,
comment_id: existingComment.id,
});
} else {
core.debug("Creating a new comment");

const { data: comment } = await octokit.rest.issues.createComment({
owner: contextObj.owner,
repo: contextObj.repo,
issue_number: contextObj.number,
body: markdown,
});

core.debug(
`Created comment id '${comment.id}' on issue '${contextObj.number}' in '${contextObj.repo}'.`
);
core.setOutput("comment-id", comment.id);
}
} catch (err) {
core.warning(`Failed to comment: ${err}`);
core.info("Commenting is not possible from forks.");

// If we can't post to the comment, display results here.
// forkedRepos only have READ ONLY access on GITHUB_TOKEN
// https://github.community/t5/GitHub-Actions/quot-Resource-not-accessible-by-integration-quot-for-adding-a/td-p/33925
const resultsAsObject = convertToTableObject(fallbackOutput);
console.table(resultsAsObject);
}
}

// IIFE to be able to use async/await
(async () => {
try {
Expand Down

0 comments on commit 7a07055

Please sign in to comment.