Skip to content

Commit

Permalink
ci: better (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
Alxandr authored Oct 9, 2024
1 parent 15520de commit ef80d98
Show file tree
Hide file tree
Showing 21 changed files with 546 additions and 203 deletions.
32 changes: 31 additions & 1 deletion .github/scripts/_meta.mts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,17 @@ import fs from "node:fs";

export type VerticalType = "app" | "lib" | "pkg";

export type ImageInfo = {
readonly name: string;
};

export type Vertical = {
readonly type: VerticalType;
readonly name: string;
readonly shortName: string;
readonly path: string;
readonly relPath: string;
readonly image?: ImageInfo;
readonly rawConfig: Readonly<Record<string, unknown>>;
};

Expand All @@ -17,6 +24,8 @@ const vertialDirs = {
pkg: "src/pkgs",
};

const last = (arr: string[]) => arr[arr.length - 1];

const readVertical = (type: VerticalType, dirPath: string): Vertical => {
const verticalPath = path.resolve(dirPath);
const dirName = path.basename(verticalPath);
Expand All @@ -33,7 +42,28 @@ const readVertical = (type: VerticalType, dirPath: string): Vertical => {
name = config.name;
}

return { type, name, path: verticalPath, rawConfig: config };
let shortName = last(name.split("."));
if (typeof config.shortName === "string" && config.shortName) {
shortName = config.shortName;
}

let image: ImageInfo | undefined;
if ("image" in config) {
// TODO: validate?
image = {
name: (config.image as any).name,
};
}

return {
type,
name,
shortName,
path: verticalPath,
relPath: dirPath,
image,
rawConfig: config,
};
};

const apps = await globby(`${vertialDirs.app}/*`, { onlyDirectories: true });
Expand Down
109 changes: 109 additions & 0 deletions .github/scripts/bootstrap-db.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import yargs from "yargs";
import { hideBin } from "yargs/helpers";

const argv = yargs(hideBin(process.argv))
.option("deploy-api", {
description: "Deploy API base path",
})
.option("subscription-id", {
type: "string",
required: true,
})
.option("resource-group", {
type: "string",
required: true,
})
.option("server-name", {
type: "string",
required: true,
})
.option("key-vault-name", {
type: "string",
required: true,
})
.option("user", {
type: "string",
required: true,
})
.option("database", {
type: "string",
required: true,
})
.option("role-prefix", {
type: "string",
required: true,
})
.option("schema", {
type: "array",
required: true,
})
.parse();

const request = {
resources: {
subscriptionId: argv.subscriptionId,
resourceGroup: argv.resourceGroup,
serverName: argv.serverName,
keyVaultName: argv.keyVaultName,
user: argv.user,
},
databaseName: argv.database,
userPrefix: argv.rolePrefix,
schemas: Object.fromEntries(argv.schema.map((n) => [n, {}])),
};

// console.log(request);
// process.exit(1);

const baseUrl = new URL(argv.deployApi);
if (baseUrl.protocol === "http:") {
baseUrl.protocol = "ws:";
} else if (baseUrl.protocol === "https:") {
baseUrl.protocol = "wss:";
} else {
throw new Error(`Unsupported protocol: ${baseUrl.protocol}`);
}

const ws = new WebSocket(
new URL("api/v1/database/bootstrap", baseUrl),
"altinn.task-pipeline"
);

let pending = Promise.resolve();

const write = (data: Blob) => {
const buffer = data.arrayBuffer();
pending = pending
.then(() => buffer)
.then((buffer) => {
var arr = new Uint8Array(buffer);
process.stderr.write(arr);
})
.catch((err) => {
console.error(err);
process.exit(1);
});
};

ws.addEventListener("open", (e) => {
ws.send(JSON.stringify(request));
});

ws.addEventListener("message", (ev) => {
write(ev.data);
});

ws.addEventListener("close", (ev) => {
console.log("closed", ev.code, ev.reason);
ws.close();

if (ev.code !== 4000) {
process.exit(1);
}
});

ws.addEventListener("error", (ev) => {
if (ev && "message" in ev) {
console.error("error", ev.message);
}
});
38 changes: 38 additions & 0 deletions .github/scripts/list-verticals.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { verticals } from "./_meta.mts";
import * as actions from "@actions/core";
import yargs from "yargs";
import { hideBin } from "yargs/helpers";

const argv = yargs(hideBin(process.argv))
.option("type", {
type: "array",
required: true,
})
.parse();

let output = verticals;
if (argv.type) {
output = verticals.filter((v) => argv.type.includes(v.type));
}

const paths = output.map((v) => v.relPath);
var matrix = {
shortName: output.map((v) => v.shortName),
include: output.map((v) => {
const ret: Record<string, string> = {
path: v.relPath,
name: v.name,
shortName: v.shortName,
type: v.type,
};

if (v.image && v.image.name) {
ret.imageName = v.image.name;
}

return ret;
}),
};

actions.setOutput("matrix", JSON.stringify(matrix));
actions.setOutput("verticals", JSON.stringify(paths));
2 changes: 2 additions & 0 deletions .github/scripts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@
"author": "",
"private": true,
"devDependencies": {
"@actions/core": "^1.11.1",
"@octokit/action": "^7.0.0",
"@types/node": "^20.11.30",
"chalk": "^5.3.0",
"globby": "^14.0.1",
"tsx": "^4.7.1",
"typescript": "^5.4.3",
"yargs": "^17.7.2",
"zx": "^8.0.0"
}
}
Loading

0 comments on commit ef80d98

Please sign in to comment.