diff --git a/__tests__/aggregate-results.test.js b/__tests__/aggregate-results.test.js index 2e55dcba..87cf82cd 100644 --- a/__tests__/aggregate-results.test.js +++ b/__tests__/aggregate-results.test.js @@ -1,28 +1,28 @@ -const { getTableTotals } = require("../src/aggregate-results"); -const { totalPercentageReducer } = require("../src/helpers/test-helpers"); +const {getTableTotals} = require('../src/aggregate-results') +const {totalPercentageReducer} = require('../src/helpers/test-helpers') -test("test getTableTotals", function (done) { +test('test getTableTotals', function (done) { const mockRunnerResults = [ { - runner: "Test Runner 1", + runner: 'Test Runner 1', results: { version: 1, - status: "pass", + status: 'pass', tests: [ { - name: "Test 1", - status: "pass", + name: 'Test 1', + status: 'pass', message: null, line_no: null, - execution_time: "0ms", + execution_time: '0ms', score: 1, }, { - name: "Test 2", - status: "fail", - message: "Expected output not matched", + name: 'Test 2', + status: 'fail', + message: 'Expected output not matched', line_no: 10, - execution_time: "5ms", + execution_time: '5ms', score: 0, }, ], @@ -30,121 +30,121 @@ test("test getTableTotals", function (done) { }, }, { - runner: "Test Runner 2", + runner: 'Test Runner 2', results: { version: 2, - status: "fail", + status: 'fail', tests: [ { - name: "Test 3", - status: "pass", + name: 'Test 3', + status: 'pass', message: null, line_no: null, - execution_time: "0ms", + execution_time: '0ms', score: 1, }, { - name: "Test 4", - status: "fail", - message: "Expected output not matched", + name: 'Test 4', + status: 'fail', + message: 'Expected output not matched', line_no: 10, - execution_time: "5ms", + execution_time: '5ms', score: 0, }, ], max_score: 2, }, }, - ]; + ] const mockPushToTable = (row) => { - console.log(row); - }; - const result = getTableTotals(mockRunnerResults, mockPushToTable); + console.log(row) + } + const result = getTableTotals(mockRunnerResults, mockPushToTable) expect(result).toStrictEqual([ { score: 1, maxScore: 2, - weight: "50.00", + weight: '50.00', }, { score: 1, maxScore: 2, - weight: "50.00", + weight: '50.00', }, - ]); - const totalPercent = result.reduce(totalPercentageReducer, 0); - expect(totalPercent).toBe(50); + ]) + const totalPercent = result.reduce(totalPercentageReducer, 0) + expect(totalPercent).toBe(50) - done(); -}); + done() +}) -test("test getTableTotals where weight is weird", function (done) { +test('test getTableTotals where weight is weird', function (done) { const mockRunnerResults = [ { - runner: "Runner 1", + runner: 'Runner 1', results: { version: 1, - status: "pass", + status: 'pass', tests: [ { - name: "Test 1", - status: "pass", + name: 'Test 1', + status: 'pass', message: null, line_no: null, - execution_time: "0ms", + execution_time: '0ms', }, { - name: "Test 2", - status: "fail", - message: "Expected output not matched", + name: 'Test 2', + status: 'fail', + message: 'Expected output not matched', line_no: 10, - execution_time: "5ms", + execution_time: '5ms', }, ], max_score: 2.5, // weird max_score }, }, { - runner: "Runner 2", + runner: 'Runner 2', results: { version: 2, - status: "fail", + status: 'fail', tests: [ { - name: "Test 3", - status: "pass", + name: 'Test 3', + status: 'pass', message: null, line_no: null, - execution_time: "0ms", + execution_time: '0ms', }, { - name: "Test 4", - status: "fail", - message: "Expected output not matched", + name: 'Test 4', + status: 'fail', + message: 'Expected output not matched', line_no: 10, - execution_time: "5ms", + execution_time: '5ms', }, ], max_score: 3.3, // weird max_score }, }, - ]; + ] const mockPushToTable = (row) => { - console.log(row); - }; - const result = getTableTotals(mockRunnerResults, mockPushToTable); + console.log(row) + } + const result = getTableTotals(mockRunnerResults, mockPushToTable) expect(result).toStrictEqual([ { score: 1.25, maxScore: 2.5, - weight: "43.00", + weight: '43.00', }, { score: 1.65, maxScore: 3.3, - weight: "57.00", + weight: '57.00', }, - ]); - done(); -}); + ]) + done() +}) diff --git a/__tests__/main.test.js b/__tests__/main.test.js index 6fdeba78..c6bd6f33 100644 --- a/__tests__/main.test.js +++ b/__tests__/main.test.js @@ -1,83 +1,103 @@ -const process = require("process"); -const cp = require("child_process"); -const path = require("path"); +const process = require('process') +const cp = require('child_process') +const path = require('path') -const node = process.execPath; -const ip = path.join(__dirname, "..", "src", "main.js"); +const node = process.execPath +const ip = path.join(__dirname, '..', 'src', 'main.js') const options = { env: process.env, - encoding: "utf-8", -}; - -const { parseRunnerResults } = require("../src/main"); - -test("test runs", () => { - process.env.RESULT1_RESULTS = Buffer.from('{ "tests": [{ "name": "Test 1", "status": "pass", "message": null }] }').toString("base64"); - process.env.INPUT_RUNNERS = "result1"; - - const child = cp.spawnSync(node, [ip], options); - const stdout = child.stdout.toString(); - expect(stdout).toContain(`✅ Test 1`); -}); - -test("test fails", () => { - process.env.RESULT1_RESULTS = Buffer.from('{ "tests": [{ "name": "Test 1", "status": "fail", "message": "Test failed with non-zero exit code." }] }').toString("base64"); - process.env.INPUT_RUNNERS = "result1"; - - const child = cp.spawnSync(node, [ip], options); - const stdout = child.stdout.toString(); - expect(stdout).toContain(`❌ Test 1`); -}); - -test("test errors out", () => { - process.env.RESULT1_RESULTS = Buffer.from('{ "tests": [{ "name": "Test 1", "status": "error", "message": "Test failed to execute." }] }').toString("base64"); - process.env.INPUT_RUNNERS = "result1"; - - const child = cp.spawnSync(node, [ip], options); - const stdout = child.stdout.toString(); - expect(stdout).toContain(`Error: Test failed to execute.`); -}); - -test("fails to run if input not in the right format", () => { - process.env.RESULT1_RESULTS = Buffer.from('{ "tests": [{ "name": "Test 1", "status": "pass", "message": null }] }').toString("base64"); - process.env.RESULT2_RESULTS = Buffer.from('{ "tests": [{ "name": "Test 2", "status": "pass", "message": null }] }').toString("base64"); - process.env.INPUT_RUNNERS = "[result1,result2]"; - - const child = cp.spawnSync(node, [ip], options); - const stderr = child.stderr.toString(); - expect(stderr).toContain(`The runners input must be a comma-separated list of strings.`); -}); - -// test('test runs with multiple runners', () => { -// process.env.RESULT1_RESULTS = Buffer.from('{ "tests": [{ "name": "Test 1", "status": "pass", "message": null }] }').toString('base64') -// process.env.RESULT2_RESULTS = Buffer.from('{ "tests": [{ "name": "Test 2", "status": "pass", "message": null }] }').toString('base64') -// process.env.INPUT_RUNNERS = 'result1,result2' - -// const child = cp.spawnSync(node, [ip], options) -// const stdout = child.stdout.toString() -// console.log(stdout) -// expect(stdout).toBe('pass') -// }) - -// test('test fails with multiple runners', () => { -// process.env.RESULT1_RESULTS = Buffer.from('{ "tests": [{ "name": "Test 1", "status": "fail", "message": null }] }').toString('base64') -// process.env.RESULT2_RESULTS = Buffer.from('{ "tests": [{ "name": "Test 2", "status": "pass", "message": null }] }').toString('base64') -// process.env.INPUT_RUNNERS = 'result1,result2' - -// const child = cp.spawnSync(node, [ip], options) -// const stdout = child.stdout.toString() -// console.log(stdout) -// expect(stdout).toBe('fail') -// }) - -test("test autograding-output.parseRunnerResults", function () { - process.env.INPUT_RUNNERS = "result1,result2"; - - let testResults1 = parseRunnerResults(process.env.INPUT_RUNNERS); - expect(testResults1.length).toBe(2); - expect(testResults1[0].runner).toBe("result1"); - expect(testResults1[1].runner).toBe("result2"); - - process.env.INPUT_RUNNERS = "[result1,result2]"; - expect(() => parseRunnerResults(process.env.INPUT_RUNNERS)).toThrow("The runners input must be a comma-separated list of strings."); -}); + encoding: 'utf-8', +} + +const {parseRunnerResults} = require('../src/main') + +test('test runs', () => { + process.env.RESULT1_RESULTS = Buffer.from( + '{ "tests": [{ "name": "Test 1", "status": "pass", "message": null }] }', + ).toString('base64') + process.env.INPUT_RUNNERS = 'result1' + + const child = cp.spawnSync(node, [ip], options) + const stdout = child.stdout.toString() + expect(stdout).toContain(`✅ Test 1`) +}) + +test('test fails', () => { + process.env.RESULT1_RESULTS = Buffer.from( + '{ "tests": [{ "name": "Test 1", "status": "fail", "message": "Test failed with non-zero exit code." }] }', + ).toString('base64') + process.env.INPUT_RUNNERS = 'result1' + + const child = cp.spawnSync(node, [ip], options) + const stdout = child.stdout.toString() + expect(stdout).toContain(`❌ Test 1`) +}) + +test('test errors out', () => { + process.env.RESULT1_RESULTS = Buffer.from( + '{ "tests": [{ "name": "Test 1", "status": "error", "message": "Test failed to execute." }] }', + ).toString('base64') + process.env.INPUT_RUNNERS = 'result1' + + const child = cp.spawnSync(node, [ip], options) + const stdout = child.stdout.toString() + expect(stdout).toContain(`Error: Test failed to execute.`) +}) + +test('fails to run if input not in the right format', () => { + process.env.RESULT1_RESULTS = Buffer.from( + '{ "tests": [{ "name": "Test 1", "status": "pass", "message": null }] }', + ).toString('base64') + process.env.RESULT2_RESULTS = Buffer.from( + '{ "tests": [{ "name": "Test 2", "status": "pass", "message": null }] }', + ).toString('base64') + process.env.INPUT_RUNNERS = '[result1,result2]' + + const child = cp.spawnSync(node, [ip], options) + const stderr = child.stderr.toString() + expect(stderr).toContain(`The runners input must be a comma-separated list of strings.`) +}) + +test('test runs with multiple runners', () => { + process.env.RESULT1_RESULTS = Buffer.from( + '{ "tests": [{ "name": "Test 1", "status": "pass", "message": null }] }', + ).toString('base64') + process.env.RESULT2_RESULTS = Buffer.from( + '{ "tests": [{ "name": "Test 2", "status": "pass", "message": null }] }', + ).toString('base64') + process.env.INPUT_RUNNERS = 'result1,result2' + + const child = cp.spawnSync(node, [ip], options) + const stdout = child.stdout.toString() + console.log(stdout) + expect(stdout).toContain('✅') +}) + +test('test fails with multiple runners', () => { + process.env.RESULT1_RESULTS = Buffer.from( + '{ "tests": [{ "name": "Test 1", "status": "fail", "message": null }] }', + ).toString('base64') + process.env.RESULT2_RESULTS = Buffer.from( + '{ "tests": [{ "name": "Test 2", "status": "pass", "message": null }] }', + ).toString('base64') + process.env.INPUT_RUNNERS = 'result1,result2' + + const child = cp.spawnSync(node, [ip], options) + const stdout = child.stdout.toString() + console.log(stdout) + expect(stdout).toContain('❌') +}) + +test('test autograding-output.parseRunnerResults', function () { + process.env.INPUT_RUNNERS = 'result1,result2' + + const testResults1 = parseRunnerResults(process.env.INPUT_RUNNERS) + expect(testResults1.length).toBe(2) + expect(testResults1[0].runner).toBe('result1') + expect(testResults1[1].runner).toBe('result2') + + process.env.INPUT_RUNNERS = '[result1,result2]' + expect(() => parseRunnerResults(process.env.INPUT_RUNNERS)).toThrow( + 'The runners input must be a comma-separated list of strings.', + ) +}) diff --git a/src/aggregate-results.js b/src/aggregate-results.js index 4420fe26..421db849 100644 --- a/src/aggregate-results.js +++ b/src/aggregate-results.js @@ -1,46 +1,52 @@ -const { COLORS } = require("./colors"); -const Table = require("cli-table3"); -const { getTotalMaxScore, getTestWeight, getTestScore, totalPercentageReducer, getMaxScoreForTest } = require("./helpers/test-helpers"); +const {COLORS} = require('./colors') +const Table = require('cli-table3') +const { + getTotalMaxScore, + getTestWeight, + getTestScore, + totalPercentageReducer, + getMaxScoreForTest, +} = require('./helpers/test-helpers') function getTableTotals(runnerResults, pushToTable) { - const totalMaxScore = getTotalMaxScore(runnerResults); + const totalMaxScore = getTotalMaxScore(runnerResults) - return runnerResults.map(({ runner, results }) => { - const maxScore = getMaxScoreForTest(results); - const weight = getTestWeight(maxScore, totalMaxScore); - const score = getTestScore(results); - const testName = runner.trim(); + return runnerResults.map(({runner, results}) => { + const maxScore = getMaxScoreForTest(results) + const weight = getTestWeight(maxScore, totalMaxScore) + const score = getTestScore(results) + const testName = runner.trim() - pushToTable([testName, score, maxScore, weight]); + pushToTable([testName, score, maxScore, weight]) return { score, maxScore, weight, - }; - }); + } + }) } function AggregateResults(runnerResults) { try { const table = new Table({ - head: ["Test Runner Name", "Test Score", "Max Score", "Weight"], + head: ['Test Runner Name', 'Test Score', 'Max Score', 'Weight'], colWidths: [20, 13, 13, 10], - }); + }) - console.log(COLORS.magenta, "Test runner summary", COLORS.reset); + console.log(COLORS.magenta, 'Test runner summary', COLORS.reset) - const totals = getTableTotals(runnerResults, (row) => table.push(row)); + const totals = getTableTotals(runnerResults, (row) => table.push(row)) - const totalPercent = totals.reduce(totalPercentageReducer, 0).toFixed(2) + "%"; + const totalPercent = totals.reduce(totalPercentageReducer, 0).toFixed(2) + '%' - table.push(["Total: ", "----", "----", totalPercent]); + table.push(['Total: ', '----', '----', totalPercent]) - console.log(table.toString()); + console.log(table.toString()) } catch (error) { - throw new Error(error.message); + throw new Error(error.message) } } -exports.AggregateResults = AggregateResults; -exports.getTableTotals = getTableTotals; +exports.AggregateResults = AggregateResults +exports.getTableTotals = getTableTotals diff --git a/src/colors.js b/src/colors.js index e8af2ceb..fcc01a65 100644 --- a/src/colors.js +++ b/src/colors.js @@ -1,10 +1,10 @@ const COLORS = { - reset: "\x1b[0m", - cyan: "\x1b[36m", - green: "\x1b[32m", - red: "\x1b[31m", - yellow: "\x1b[33m", - magenta: "\x1b[35m", -}; + reset: '\x1b[0m', + cyan: '\x1b[36m', + green: '\x1b[32m', + red: '\x1b[31m', + yellow: '\x1b[33m', + magenta: '\x1b[35m', +} -exports.COLORS = COLORS; +exports.COLORS = COLORS diff --git a/src/console-results.js b/src/console-results.js index ee28e9b7..9a52620d 100644 --- a/src/console-results.js +++ b/src/console-results.js @@ -1,45 +1,45 @@ -const { COLORS } = require("./colors"); +const {COLORS} = require('./colors') exports.ConsoleResults = function ConsoleResults(runnerResults) { try { - let grandTotalPassedTests = 0; - let grandTotalTests = 0; + let grandTotalPassedTests = 0 + let grandTotalTests = 0 - runnerResults.forEach(({ runner, results }, index) => { + runnerResults.forEach(({runner, results}, index) => { // Fun transition to new runner if (index > 0) { - console.log(`${COLORS.magenta}🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀${COLORS.reset}\n`); + console.log(`${COLORS.magenta}🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀${COLORS.reset}\n`) } - console.log(`🔄 Processing: ${runner}`); - let passedTests = 0; - const totalTests = results.tests.length; + console.log(`🔄 Processing: ${runner}`) + let passedTests = 0 + const totalTests = results.tests.length results.tests.forEach((test) => { - if (test.status === "pass") { - console.log(`${COLORS.green}✅ ${test.name}\n${COLORS.reset}`); - passedTests += 1; - } else if (test.status === "error") { - console.log(`Error: ${test.message || `Failed to run test '${test.name}'`}\n${COLORS.reset}`); + if (test.status === 'pass') { + console.log(`${COLORS.green}✅ ${test.name}\n${COLORS.reset}`) + passedTests += 1 + } else if (test.status === 'error') { + console.log(`Error: ${test.message || `Failed to run test '${test.name}'`}\n${COLORS.reset}`) } else { - console.log(`${COLORS.red}❌ ${test.name}\n`); - console.log(`${test.message || ""}\n${COLORS.reset}`); + console.log(`${COLORS.red}❌ ${test.name}\n`) + console.log(`${test.message || ''}\n${COLORS.reset}`) } - }); + }) // Update grand totals - grandTotalPassedTests += passedTests; - grandTotalTests += totalTests; + grandTotalPassedTests += passedTests + grandTotalTests += totalTests // Calculate and display points for the current runner - const points = (passedTests / totalTests) * 100; - console.log(`Total points for ${runner}: ${points.toFixed(2)}/100\n`); - }); + const points = (passedTests / totalTests) * 100 + console.log(`Total points for ${runner}: ${points.toFixed(2)}/100\n`) + }) // Calculate and display grand total points - const grandTotalPoints = (grandTotalPassedTests / grandTotalTests) * 100; - console.log(`${COLORS.cyan}🏆 Grand total points: ${grandTotalPoints.toFixed(2)}/100${COLORS.reset}\n`); + const grandTotalPoints = (grandTotalPassedTests / grandTotalTests) * 100 + console.log(`${COLORS.cyan}🏆 Grand total points: ${grandTotalPoints.toFixed(2)}/100${COLORS.reset}\n`) } catch (error) { - throw new Error(error.message); + throw new Error(error.message) } -}; +} diff --git a/src/helpers/test-helpers.js b/src/helpers/test-helpers.js index f1fd4edf..79b30c15 100644 --- a/src/helpers/test-helpers.js +++ b/src/helpers/test-helpers.js @@ -1,33 +1,33 @@ -const getMaxScoreForTest = (runnerResult) => runnerResult.max_score || 0; +const getMaxScoreForTest = (runnerResult) => runnerResult.max_score || 0 const getTotalMaxScore = (runnerResults) => { - return runnerResults.reduce((acc, { results }) => acc + results.max_score, 0); -}; + return runnerResults.reduce((acc, {results}) => acc + results.max_score, 0) +} -const totalPercentageReducer = (acc, { score, weight, maxScore }) => { - return acc + ((score || 0) / (maxScore || 1)) * weight; -}; +const totalPercentageReducer = (acc, {score, weight, maxScore}) => { + return acc + ((score || 0) / (maxScore || 1)) * weight +} const getTestScore = (runnerResult) => { - const { tests } = runnerResult; - const score = runnerResult.tests.reduce((acc, { status }) => { - return status === "pass" ? acc + 1 : acc; - }, 0); + const {tests} = runnerResult + const score = runnerResult.tests.reduce((acc, {status}) => { + return status === 'pass' ? acc + 1 : acc + }, 0) - return (score / tests.length) * (getMaxScoreForTest(runnerResult) || 1); -}; + return (score / tests.length) * (getMaxScoreForTest(runnerResult) || 1) +} const getTestWeight = (maxScore, allMaxScores) => { if (maxScore === 0) { - return (0).toFixed(1); + return (0).toFixed(1) } - const weight = allMaxScores !== 0 ? (maxScore / allMaxScores) * 100 : 0; + const weight = allMaxScores !== 0 ? (maxScore / allMaxScores) * 100 : 0 - return Math.round(weight).toFixed(2); -}; + return Math.round(weight).toFixed(2) +} -exports.getMaxScoreForTest = getMaxScoreForTest; -exports.getTotalMaxScore = getTotalMaxScore; -exports.totalPercentageReducer = totalPercentageReducer; -exports.getTestScore = getTestScore; -exports.getTestWeight = getTestWeight; +exports.getMaxScoreForTest = getMaxScoreForTest +exports.getTotalMaxScore = getTotalMaxScore +exports.totalPercentageReducer = totalPercentageReducer +exports.getTestScore = getTestScore +exports.getTestWeight = getTestWeight diff --git a/src/main.js b/src/main.js index 3d252c34..5fa4a5c6 100644 --- a/src/main.js +++ b/src/main.js @@ -1,45 +1,45 @@ -const core = require("@actions/core"); -const { ConsoleResults } = require("./console-results"); -const { NotifyClassroom } = require("./notify-classroom"); -const { AggregateResults } = require("./aggregate-results"); +const core = require('@actions/core') +const {ConsoleResults} = require('./console-results') +const {NotifyClassroom} = require('./notify-classroom') +const {AggregateResults} = require('./aggregate-results') function parseRunnerResults(runners) { try { - const returnRunners = runners.split(",").map((runner) => { - const encodedResults = process.env[`${runner.trim().toUpperCase()}_RESULTS`]; - const json = Buffer.from(encodedResults, "base64").toString("utf-8"); - return { runner: runner.trim(), results: JSON.parse(json) }; - }); - return returnRunners; + const returnRunners = runners.split(',').map((runner) => { + const encodedResults = process.env[`${runner.trim().toUpperCase()}_RESULTS`] + const json = Buffer.from(encodedResults, 'base64').toString('utf-8') + return {runner: runner.trim(), results: JSON.parse(json)} + }) + return returnRunners } catch (error) { - throw new Error("The runners input must be a comma-separated list of strings."); + throw new Error('The runners input must be a comma-separated list of strings.') } } -exports.parseRunnerResults = parseRunnerResults; +exports.parseRunnerResults = parseRunnerResults async function main() { try { - const runnerResults = parseRunnerResults(core.getInput("runners")); + const runnerResults = parseRunnerResults(core.getInput('runners')) - ConsoleResults(runnerResults); - AggregateResults(runnerResults); + ConsoleResults(runnerResults) + AggregateResults(runnerResults) try { - console.log("Before NotifyClassroom") - await NotifyClassroom(runnerResults); + console.log('Before NotifyClassroom') + await NotifyClassroom(runnerResults) } catch (error) { - console.error("Error in NotifyClassroom:", error); + console.error('Error in NotifyClassroom:', error) } - if (runnerResults.some((r) => r.results.status === "fail")) { - core.setFailed("Some tests failed."); + if (runnerResults.some((r) => r.results.status === 'fail')) { + core.setFailed('Some tests failed.') } else if (runnerResults.some((r) => r.results.status === 'error')) { - core.setFailed("Some tests errored."); + core.setFailed('Some tests errored.') } } catch (error) { - console.error(error.message); - core.setFailed(error.message); + console.error(error.message) + core.setFailed(error.message) } } -main(); +main() diff --git a/src/notify-classroom.js b/src/notify-classroom.js index 013e0e8c..a2cde722 100644 --- a/src/notify-classroom.js +++ b/src/notify-classroom.js @@ -1,96 +1,96 @@ -const core = require("@actions/core"); -const github = require("@actions/github"); +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 // if max_score is greater than 0 run the rest of this code - const { totalPoints, maxPoints } = runnerResults.reduce( - (acc, { results }) => { - if (!results.max_score) return acc; + const {totalPoints, maxPoints} = runnerResults.reduce( + (acc, {results}) => { + if (!results.max_score) return acc - acc.maxPoints += results.max_score; - results.tests.forEach(({ score }) => { - acc.totalPoints += score; - }); + acc.maxPoints += results.max_score + results.tests.forEach(({score}) => { + acc.totalPoints += score + }) - return acc; + return acc }, - { totalPoints: 0, maxPoints: 0 } - ); - console.log(`max score: ${maxPoints}`); - if (!maxPoints) return; + {totalPoints: 0, maxPoints: 0}, + ) + console.log(`max score: ${maxPoints}`) + 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"); - console.log(`Token: ${token}`); - if (!token || token === "") return; + const token = process.env.GITHUB_TOKEN || core.getInput('token') + console.log(`Token: ${token}`) + if (!token || token === '') return // Create the octokit client - const octokit = github.getOctokit(token); - console.log(`Octokit: ${octokit}`); - if (!octokit) return; + const octokit = github.getOctokit(token) + console.log(`Octokit: ${octokit}`) + 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; + 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 || ""); - console.log(`Run ID: ${runId}`); - if (Number.isNaN(runId)) return; + const runId = parseInt(process.env.GITHUB_RUN_ID || '') + console.log(`Run ID: ${runId}`) + 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); + 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_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 checkRun = checkRunsResponse.data.total_count === 1 && checkRunsResponse.data.check_runs[0] - if (!checkRun) return; + if (!checkRun) return // 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}`; + const text = `Points ${totalPoints}/${maxPoints}` await octokit.rest.checks.update({ owner, repo, check_run_id: checkRun.id, output: { - title: "Autograding", + title: 'Autograding', summary: text, - text: JSON.stringify({ totalPoints, maxPoints }), + text: JSON.stringify({totalPoints, maxPoints}), annotations: [ { // Using the `.github` path is what GitHub Actions does - path: ".github", + path: '.github', start_line: 1, end_line: 1, - annotation_level: "notice", + annotation_level: 'notice', message: text, - title: "Autograding complete", + title: 'Autograding complete', }, ], }, - }); -}; + }) +}