Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added the "behaviour" parameter #81

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
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 === "update" && 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