Initial commit

This commit is contained in:
Alan Bridgeman 2025-02-17 16:19:35 -06:00
commit 9277a804bb
18 changed files with 851 additions and 0 deletions

View file

@ -0,0 +1,61 @@
import express, { Request, Response } from 'express';
import { v4 as newUUID } from 'uuid';
import { Controller, POST, BaseController } from '@BridgemanAccessible/ba-web-framework';
import { Chart } from '../entity/Chart';
type WebhookResponse = {
type: 'PUSH_ARTIFACT' | 'DELETE_ARTIFACT',
occur_at: number,
operator: string,
event_data: {
resources: {
digest: string,
tag: string,
resource_url: string
}[],
repository: {
date_created?: number,
name: string,
namespace: string,
repo_full_name: string,
repo_type: string
}
}
};
@Controller()
export class HarborRoutes extends BaseController {
@POST('/harbor', express.json())
async webhook(req: Request, res: Response) {
const response: WebhookResponse = req.body;
if(response.type === 'PUSH_ARTIFACT') {
console.log('Pushed a new Helm Chart');
// Create a new database entry for the Helm Chart that got pushed
await Chart.create({
id: newUUID(),
created: new Date(response.occur_at),
user: response.operator,
name: response.event_data.repository.name,
digest: response.event_data.resources[0].digest,
tag: response.event_data.resources[0].tag,
url: response.event_data.resources[0].resource_url
}).save();
}
else if(response.type === 'DELETE_ARTIFACT') {
console.log('Deleted a Helm Chart');
// Delete the Helm Chart from the database
Chart.delete({ digest: response.event_data.resources[0].digest });
}
console.log(`Happened At: ${response.occur_at}`);
console.log(`User (Operator): ${response.operator}`);
console.log(`Name: ${response.event_data.repository.name}`);
console.log(`Digest: ${response.event_data.resources[0].digest}`);
console.log(`Tag: ${response.event_data.resources[0].tag}`);
console.log(`URL: ${response.event_data.resources[0].resource_url}`);
res.status(200).send('ok');
}
}

63
src/routes/HelmRoutes.ts Normal file
View file

@ -0,0 +1,63 @@
import { Request, Response } from 'express';
import { Controller, GET, BaseController } from '@BridgemanAccessible/ba-web-framework';
import { dump as writeYAML } from 'js-yaml';
import { Chart } from '../entity/Chart';
type HelmRepoIndexEntry = {
apiVersion: 'v1',
appVersion: `${string}.${string}.${string}`,
dependencies: {
name: string,
repository: string,
version: `${string}.${string}.${string}`
}[],
description: string,
digest: string,
home: string,
name: string,
sources: string[],
urls: string[],
version: `${string}.${string}.${string}`
};
type HelmRepoIndex = {
apiVersion: 'v1',
entries: {
[key: string]: HelmRepoIndexEntry[]
}
};
@Controller()
export class HelmRoutes extends BaseController {
@GET('/index.yaml')
private async index(req: Request, res: Response) {
const index: HelmRepoIndex = {
apiVersion: 'v1',
entries: {}
};
// Get all Helm Charts from the database and add them to the index
const charts = await Chart.find();
charts.forEach(chart => {
index.entries[chart.name] = [
{
apiVersion: 'v1',
appVersion: '1.0.0',
dependencies: [],
description: chart.name,
digest: chart.digest,
home: `https://${process.env.HOSTNAME}/${chart.url.substring(chart.url.indexOf('/', chart.url.indexOf('/') + 1) + 1, chart.url.indexOf(':'))}`,
name: chart.name,
sources: [],
urls: [`oci://${chart.url}`],
version: chart.tag as `${string}.${string}.${string}`
}
];
})
res.status(200)
.setHeader('Content-Type', 'application/yaml') // Per IANA Media Types listing as noted in [RFC 9512](https://datatracker.ietf.org/doc/html/rfc9512)
.send(writeYAML(index));
}
}

9
src/routes/HomeRoutes.ts Normal file
View file

@ -0,0 +1,9 @@
import { Request, Response } from 'express';
import { Controller, GET, Page, BaseController } from '@BridgemanAccessible/ba-web-framework';
@Controller()
export class HomeRoutes extends BaseController {
@Page('Home', 'index.ejs')
@GET('/')
async home(req: Request, res: Response) {}
}