Skip to content

Commit

Permalink
Merge pull request #4484 from cloud-gov/chore-admin-qol
Browse files Browse the repository at this point in the history
chore(admin): add extra information for testing scans
  • Loading branch information
drewbo authored Apr 17, 2024
2 parents 1b08851 + b0a9d87 commit 1cb0748
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 7 deletions.
1 change: 1 addition & 0 deletions admin-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"dependencies": {
"date-fns": "^2.30.0",
"page": "^1.11.6",
"pretty-bytes": "^6.1.1",
"svelecte": "^3.17.2",
"svelte": "^3.0.0",
"uswds": "^2.14.0"
Expand Down
15 changes: 14 additions & 1 deletion admin-client/src/components/TaskTable.svelte
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
<script>
import prettyBytes from 'pretty-bytes';
import { formatDateTime } from '../helpers/formatter';
import DataTable from './DataTable.svelte';
import JSONTreeView from './JSONTreeView.svelte';
export let showSite = false;
export let tasks = [];
export let borderless = false;
Expand All @@ -17,21 +20,31 @@
<th class="center">Status</th>
<th>Id</th>
<th>Build</th>
{#if showSite}<th>Site</th>{/if}
<th>Task Type</th>
<th>Artifact</th>
<th>Created</th>
<th>Updated</th>
<th>Message</th>
</tr>
<tr slot="item" let:item={task}>
<td class="center">
<span class="usa-tag radius-pill {stateColor(task.status)}">{task.status}</span>
</td>
<td>{task.id}</td>
<td><a href="/builds/{task.buildId}">{task.buildId}</a></td>
{#if showSite}<td><a href="/sites/{task.Build.site}">{task.Build.Site.owner}/{task.Build.Site.repository}</a></td>{/if}
<td>{task.BuildTaskType.name}</td>
<td>{task.artifact}</td>
<td>
{#if task.artifact}
<a href={task.artifact.url}>Artifact ({prettyBytes(task.artifact.size)})</a>
{:else}
null
{/if}
</td>
<td title={formatDateTime(task.createdAt)}>{formatDateTime(task.createdAt, true)}</td>
<td title={formatDateTime(task.updatedAt)}>{formatDateTime(task.updatedAt, true)}</td>
<td><JSONTreeView data={task.message}/></td>
</tr>
<p slot="empty">No build tasks found</p>
</DataTable>
Expand Down
4 changes: 2 additions & 2 deletions admin-client/src/pages/Tasks.svelte
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<script>
import { fetchTasks } from '../lib/api';
import { PaginatedQueryPage, TaskTable } from '../components';
const fields = {};
</script>

<PaginatedQueryPage path="tasks" query={fetchTasks} {fields} let:data>
<TaskTable tasks={data} />
<TaskTable tasks={data} showSite={true} />
</PaginatedQueryPage>
5 changes: 5 additions & 0 deletions admin-client/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1721,6 +1721,11 @@ prelude-ls@^1.2.1:
resolved "https://registry.yarnpkg.com/prelude-ls/-/prelude-ls-1.2.1.tgz#debc6489d7a6e6b0e7611888cec880337d316396"
integrity sha512-vkcDPrRZo1QZLbn5RLGPpg/WmIQ65qoWWhcGKf/b5eplkkarX0m9z8ppCat4mlOqUsWpyNuYgO3VRyrYHSzX5g==

pretty-bytes@^6.1.1:
version "6.1.1"
resolved "https://registry.yarnpkg.com/pretty-bytes/-/pretty-bytes-6.1.1.tgz#38cd6bb46f47afbf667c202cfc754bffd2016a3b"
integrity sha512-mQUvGU6aUFQ+rNvTIAcZuWGRT9a6f6Yrg9bHs4ImKF+HZCEK+plBvnAZYSIQztknZF2qnzNtr6F8s0+IuptdlQ==

punycode@^2.1.0:
version "2.1.1"
resolved "https://registry.yarnpkg.com/punycode/-/punycode-2.1.1.tgz#b58b010ac40c22c5657616c8d2c2c02c7bf479ec"
Expand Down
29 changes: 28 additions & 1 deletion api/admin/controllers/task.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@

const {
BuildTask,
Build,
BuildTaskType,
SiteBuildTask,
Site,
} = require('../../models');
const { paginate, wrapHandlers } = require('../../utils');
const { getSignedTaskUrl, getTaskArtifactSize } = require('../../services/S3BuildTask');

module.exports = wrapHandlers({
async list(req, res) {
Expand All @@ -20,7 +23,19 @@ module.exports = wrapHandlers({
scopes.push(BuildTask.siteScope(site));
} else {
// non-site scoped lists need this included explicitly
query = { include: [BuildTaskType] };
// TODO: avoid duplicating this logic here and in the model scope
query = {
include: [
BuildTaskType,
{
model: Build,
required: true,
include: [
{ model: Site, required: true },
],
},
],
};
}

const pagination = await paginate(
Expand All @@ -34,6 +49,18 @@ module.exports = wrapHandlers({
...pagination,
};

const updatedTasks = await Promise.all(json.data.map(async (task) => {
if (task.artifact) {
const size = await getTaskArtifactSize(task.Build.Site, task.artifact);
const url = await getSignedTaskUrl(task.Build.Site, task.artifact);
// eslint-disable-next-line no-param-reassign
task.artifact = { size, url };
}
return task;
}));

json.data = updatedTasks;

return res.json(json);
},

Expand Down
13 changes: 11 additions & 2 deletions api/models/build-task.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@ const Statuses = buildEnum([
'success',
]);

const associate = ({ BuildTask, Build, BuildTaskType }) => {
const associate = ({
BuildTask,
Build,
BuildTaskType,
Site,
}) => {
BuildTask.belongsTo(Build, {
foreignKey: 'buildId',
allowNull: false,
Expand All @@ -28,10 +33,14 @@ const associate = ({ BuildTask, Build, BuildTaskType }) => {
'$Build.site$': id,
},
include: [
BuildTaskType,
{
model: Build,
required: true,
include: [
{ model: Site, required: true },
],
},
BuildTaskType,
],
}));
BuildTask.addScope('byStartsWhen', startsWhen => ({
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,8 @@
"queued-builds-check": "node ./scripts/queued-builds-check.js",
"test:e2e": "yarn playwright test",
"create-test-users": "DOTENV_CONFIG_PATH=.env node -r dotenv/config ./scripts/create-test-users.js",
"remove-test-users": "DOTENV_CONFIG_PATH=.env node -r dotenv/config ./scripts/remove-test-users.js"
"remove-test-users": "DOTENV_CONFIG_PATH=.env node -r dotenv/config ./scripts/remove-test-users.js",
"run-scans-for-build": "node ./scripts/run-scans-for-build.js"
},
"main": "index.js",
"repository": {
Expand Down
43 changes: 43 additions & 0 deletions scripts/run-scans-for-build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/* eslint-disable no-console */
const { Build, BuildTask, SiteBuildTask } = require('../api/models');

const logHelper = '[run-scans-for-build]';

async function main() {
try {
const args = Array.prototype.slice.call(process.argv, 2);

if (args.length !== 1) {
throw `
Please make sure you provide 1 arguments. (buildId).\n
You provided the following: ${args}
`;
}

const [buildId] = args;

const build = await Build.findByPk(buildId);
let tasks = [];
try {
// try to create build tasks, this will fail if they already
// exist and throw a uniqueness violation
tasks = await SiteBuildTask.createBuildTasks({ build });
console.log(`${logHelper} Created ${tasks.length} new scan(s)`);
} catch {
// if there are existing build tasks, find these
tasks = await BuildTask.findAll({ where: { buildId: build.id } });
console.log(`${logHelper} Found ${tasks.length} existing scan(s)`);
}

await Promise.all(tasks.map(async task => task.enqueue()));

console.log(`${logHelper} Successfully queued ${tasks.length} scan(s)`);

process.exit(0);
} catch (error) {
console.error(error);
process.exit(1);
}
}

main();

0 comments on commit 1cb0748

Please sign in to comment.