Skip to content

Commit

Permalink
Add report loading and validating
Browse files Browse the repository at this point in the history
  • Loading branch information
devpow112 committed Dec 13, 2023
1 parent 871bf3f commit a24da53
Show file tree
Hide file tree
Showing 6 changed files with 371 additions and 21 deletions.
14 changes: 11 additions & 3 deletions dist/index.js

Large diffs are not rendered by default.

31 changes: 17 additions & 14 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"@actions/core": "^1",
"@actions/github": "^6",
"ajv": "^8",
"ajv-formats": "^2",
"chalk": "^4",
"uuid": "^9"
},
Expand Down
11 changes: 7 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import { getContext, getInputs, makeLogger, setFailed } from './github.js';
import { getReport } from './report.js';

(async() => {
const logger = makeLogger();

try {
getContext(logger);
await getInputs(logger);
} catch (err) {
logger.endGroup();
const context = getContext(logger);
const inputs = await getInputs(logger);

await getReport(logger, context, inputs);
} catch (err) {
setFailed(err.message);

logger.endGroup();
}
})();
131 changes: 131 additions & 0 deletions src/report.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
import Ajv from 'ajv';
import addFormats from 'ajv-formats';
import fs from 'fs/promises';

const ajv = new Ajv({ verbose: true });

addFormats(ajv, ['uuid', 'uri']);

const schema = {
type: 'object',
properties: {
reportId: { type: 'string', format: 'uuid' },
reportVersion: { type: 'integer', const: 1 },
summary: {
type: 'object',
properties: {
githubOrganization: { type: 'string', pattern: '[A-Za-z0-9_.-]+' },
githubRepository: { type: 'string', pattern: '[A-Za-z0-9_.-]+' },
githubWorkflow: { type: 'string', pattern: '^[^\\s].+[^\\s]$' },
githubRunId: { type: 'integer', minimum: 0 },
githubRunAttempt: { type: 'integer', minimum: 1 },
gitBranch: { type: 'string', pattern: '^[^\\s].+[^\\s]$' },
gitSha: { type: 'string', pattern: '([A-Fa-f0-9]{40})' },
lmsBuild: { type: 'string', pattern: '([0-9]{2}\\.){3}[0-9]{5}' },
lmsInstance: { type: 'string', format: 'uri' },
operatingSystem: { type: 'string', enum: ['windows', 'linux', 'mac'] },
framework: { type: 'string', pattern: '^[^\\s].+[^\\s]$' },
started: { type: 'integer', minimum: 0 },
totalDuration: { type: 'integer', minimum: 0 },
status: { type: 'string', enum: ['passed', 'failed'] },
countPassed: { type: 'integer', minimum: 0 },
countFailed: { type: 'integer', minimum: 0 },
countSkipped: { type: 'integer', minimum: 0 },
countFlaky: { type: 'integer', minimum: 0 },
},
required: [
'githubOrganization',
'githubRepository',
'githubWorkflow',
'githubRunId',
'githubRunAttempt',
'gitBranch',
'gitSha',
'operatingSystem',
'framework',
'started',
'totalDuration',
'status',
'countPassed',
'countFailed',
'countSkipped',
'countFlaky'
],
additionalProperties: false
},
details: {
type: 'array',
items: {
type: 'object',
properties: {
name: { type: 'string', pattern: '^[^\\s].+[^\\s]$' },
location: { type: 'string', pattern: '^[^\\s].+[^\\s]$' },
started: { type: 'integer', minimum: 0 },
duration: { type: 'integer', minimum: 0 },
totalDuration: { type: 'integer', minimum: 0 },
status: { type: 'string', enum: ['passed', 'failed', 'skipped'] },
tool: { type: 'string', pattern: '^[^\\s].+[^\\s]$' },
experience: { type: 'string', pattern: '^[^\\s].+[^\\s]$' },
type: { type: 'string', pattern: '^[^\\s].+[^\\s]$' },
browser: { type: 'string', enum: ['chromium', 'firefox ', 'webkit'] },
retries: { type: 'integer', minimum: 0 }
},
required: [
'name',
'location',
'started',
'duration',
'totalDuration',
'status',
'retries'
],
additionalProperties: false
}
}
},
required: [
'reportId',
'reportVersion',
'summary',
'details'
],
additionalProperties: false
};

const validate = ajv.compile(schema);

const getReport = async(logger, context, inputs) => {
logger.startGroup('Gathering test report');

const { reportPath } = inputs;
let report;

try {
const reportRaw = await fs.readFile(reportPath, 'utf8');

report = JSON.parse(reportRaw);
} catch {
throw new Error('Report is not valid');
}

logger.info('Injecting GitHub context');

report.summary = {
...context,
...report.summary
};

logger.info('Validating test report');

if (!validate(report)) {
const message = ajv.errorsText(validate.errors, { dataVar: 'report' });

throw new Error(`Report does not conform to needed schema: ${message}`);
}

logger.endGroup();

return report;
};

export { getReport };
Loading

0 comments on commit a24da53

Please sign in to comment.