generated from actions/typescript-action
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
163 lines (161 loc) · 5.12 KB
/
main.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import * as core from '@actions/core'
import { exec } from '@actions/exec'
import { createHash } from 'crypto'
import { createWriteStream } from 'fs'
import { mkdir, readFile } from 'fs/promises'
import { Readable } from 'stream'
import { finished } from 'stream/promises'
import {
createSubmission,
submitFeedback
} from './api/adminServiceComponents.js'
import grade from './grader/Grader.js'
import { SummaryTableRow } from '@actions/core/lib/summary.js'
/**
* The main function for the action.
*
* @returns Resolves when the action is complete.
*/
export async function run(): Promise<void> {
try {
//Get an OIDC token
const token = await core.getIDToken()
if (!token) {
throw new Error(
'Unable to get OIDC token. Is workflow permission configured correctly?'
)
}
//Double check: is this the handout? If so, ignore the rest of the action and just log a warning
const handout = await core.getInput('handout_repo')
if (handout && handout === process.env.GITHUB_REPOSITORY) {
core.warning(
'This action appears to have been triggered by running in the handout repo. No submission has been created, and it will not be graded.'
)
return
}
const graderConfig = await createSubmission({
headers: {
Authorization: token
}
})
// Download the autograder
const file = await fetch(graderConfig.grader_url)
//Save to disk
if (!file.body) {
throw new Error('No body in response')
}
const fileStream = createWriteStream('grader.tgz')
await finished(Readable.fromWeb(file.body).pipe(fileStream))
//Calculate the sha256 hash of the file
const hash = createHash('sha256')
const fileContents = await readFile('grader.tgz')
hash.update(fileContents)
const graderSha = hash.digest('hex')
//Unzip the file to the directory "grader"
await mkdir('grader')
await exec('tar', [
'xzf',
'grader.tgz',
'-C',
'grader',
'--strip-components',
'1'
])
const workDir = process.env.GITHUB_WORKSPACE
//Run the autograder
const assignmentDir = `${workDir}/submission`
const graderDir = `${workDir}/grader`
const start = Date.now()
try {
const results = await grade(assignmentDir, graderDir)
const gradeResponse = await submitFeedback({
body: {
ret_code: 0,
output: '',
execution_time: Date.now() - start,
feedback: results,
grader_sha: graderSha
},
headers: {
Authorization: token
}
})
const score =
results.score ||
results.tests.reduce((acc, test) => acc + (test.score || 0), 0)
const max_score =
results.score ||
results.tests.reduce((acc, test) => acc + (test.max_score || 0), 0)
// Set job summary with test results
core.summary.addHeading('Autograder Results')
core.summary.addRaw(`Score: ${score}/${max_score}`, true)
core.summary.addLink(
'View the complete results with all details and logs in Pawtograder',
gradeResponse.details_url
)
if (results.output.visible?.output) {
core.summary.addDetails('Grader Output', results.output.visible.output)
}
core.summary.addHeading('Lint Results', 2)
core.summary.addRaw(
`Status: ${results.lint.status === 'pass' ? '✅' : '❌'}`
)
if (results.tests.length > 0) {
core.summary.addHeading('Test Results', 2)
core.summary.addHeading('Summary', 3)
const rows: SummaryTableRow[] = []
rows.push([
{ data: 'Status', header: true },
{ data: 'Name', header: true },
{ data: 'Score', header: true }
])
let lastPart = undefined
for (const test of results.tests) {
const icon = test.score === test.max_score ? '✅' : '❌'
if (test.part !== lastPart && test.part) {
lastPart = test.part
rows.push([{ data: test.part, colspan: '3' }])
}
rows.push([icon, test.name, `${test.score}/${test.max_score}`])
}
core.summary.addTable(rows)
}
await core.summary.write()
if (score == 0) {
core.error('Score: 0')
} else if (score != max_score) {
core.warning(`Score: ${score}/${max_score}`)
} else {
core.notice(`🚀 Score: ${score}/${max_score} `)
}
} catch (error) {
if (error instanceof Error) {
await submitFeedback({
body: {
ret_code: 1,
output: `${error.message}`,
execution_time: Date.now() - start,
grader_sha: graderSha,
feedback: {
output: {},
tests: [],
lint: {
output: 'Unknown error',
status: 'fail'
}
}
},
headers: {
Authorization: token
}
})
core.setFailed(error.message)
console.error(error)
}
}
} catch (error) {
// Fail the workflow run if an error occurs
console.trace(error)
core.setFailed(`An error occurred: ${error}`)
}
}