Skip to content

Commit

Permalink
Merge pull request #13 from konradpabjan/konradpabjan/avoid-using-upd…
Browse files Browse the repository at this point in the history
…ate-check-run-api

Create annotations without using update check run API
  • Loading branch information
kenyonj authored Nov 13, 2024
2 parents 75b8d4f + 1549b90 commit ca700ea
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 71 deletions.
1 change: 0 additions & 1 deletion .github/workflows/integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ name: Autograding Tests
- push
- workflow_dispatch
permissions:
checks: write
actions: read
contents: read
jobs:
Expand Down
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ on:
- push
- workflow_dispatch
permissions:
checks: write
actions: read
contents: read
jobs:
Expand Down
78 changes: 9 additions & 69 deletions src/notify-classroom.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const core = require("@actions/core");
const github = require("@actions/github");

exports.NotifyClassroom = async function NotifyClassroom(runnerResults) {
// combine max score and total score from each {runner, results} pair
Expand All @@ -19,74 +18,15 @@ exports.NotifyClassroom = async function NotifyClassroom(runnerResults) {
);
if (!maxPoints) return;

// Our action will need to API access the repository so we require a token
// This will need to be set in the calling workflow, otherwise we'll exit
const token = process.env.GITHUB_TOKEN || core.getInput("token");
if (!token || token === "") return;

// Create the octokit client
const octokit = github.getOctokit(token);
if (!octokit) return;

// The environment contains a variable for current repository. The repository
// will be formatted as a name with owner (`nwo`); e.g., jeffrafter/example
// We'll split this into two separate variables for later use
const nwo = process.env.GITHUB_REPOSITORY || "/";
const [owner, repo] = nwo.split("/");
if (!owner) return;
if (!repo) return;

// We need the workflow run id
const runId = parseInt(process.env.GITHUB_RUN_ID || "");
if (Number.isNaN(runId)) return;

// Fetch the workflow run
const workflowRunResponse = await octokit.rest.actions.getWorkflowRun({
owner,
repo,
run_id: runId,
});

// Find the check suite run
console.log(`Workflow Run Response: ${workflowRunResponse.data.check_suite_url}`);
const checkSuiteUrl = workflowRunResponse.data.check_suite_url;
const checkSuiteId = parseInt(checkSuiteUrl.match(/[0-9]+$/)[0], 10);

const checkRunsResponse = await octokit.rest.checks.listForSuite({
owner,
repo,
check_name: "run-autograding-tests",
check_suite_id: checkSuiteId,
});

// Filter to find the check run named "Autograding Tests" for the specific workflow run ID
const checkRun = checkRunsResponse.data.total_count === 1 && checkRunsResponse.data.check_runs[0];
const text = `Points ${totalPoints}/${maxPoints}`;
const summary = JSON.stringify({ totalPoints, maxPoints })

if (!checkRun) return;
// create notice annotations with the final result and summary
core.notice(text, {
title: "Autograding complete",
})

// Update the checkrun, we'll assign the title, summary and text even though we expect
// the title and summary to be overwritten by GitHub Actions (they are required in this call)
// We'll also store the total in an annotation to future-proof
const text = `Points ${totalPoints}/${maxPoints}`;
await octokit.rest.checks.update({
owner,
repo,
check_run_id: checkRun.id,
output: {
title: "Autograding",
summary: text,
text: JSON.stringify({ totalPoints, maxPoints }),
annotations: [
{
// Using the `.github` path is what GitHub Actions does
path: ".github",
start_line: 1,
end_line: 1,
annotation_level: "notice",
message: text,
title: "Autograding complete",
},
],
},
});
core.notice(summary, {
title: "Autograding report",
})
};

0 comments on commit ca700ea

Please sign in to comment.