Files

189 lines
5.2 KiB
TypeScript
Raw Permalink Normal View History

#!/usr/bin/env npx ts-node
import { promises as fs } from "fs";
import { safeLoad } from "js-yaml";
import { basename, extname, join } from "path";
import { exec } from "./exec";
interface WorkflowDesc {
folder: string;
id: string;
2020-05-20 10:54:19 -07:00
iconName?: string;
iconType?: "svg" | "octicon";
}
interface WorkflowProperties {
name: string;
description: string;
iconName?: string;
categories: string[] | null;
2021-08-23 11:00:59 +05:30
creator?: string;
}
interface WorkflowsCheckResult {
compatibleWorkflows: WorkflowDesc[];
incompatibleWorkflows: WorkflowDesc[];
}
async function checkWorkflows(
folders: string[],
2021-08-23 11:00:59 +05:30
enabledActions: string[],
partners: string[]
): Promise<WorkflowsCheckResult> {
const result: WorkflowsCheckResult = {
compatibleWorkflows: [],
incompatibleWorkflows: [],
};
2021-08-23 11:00:59 +05:30
const partnersSet = new Set(partners.map((x) => x.toLowerCase()));
for (const folder of folders) {
const dir = await fs.readdir(folder, {
withFileTypes: true,
});
for (const e of dir) {
2021-09-08 10:08:06 +01:00
if (e.isFile() && extname(e.name) === ".yml") {
const workflowFilePath = join(folder, e.name);
2020-05-20 10:40:26 -07:00
const workflowId = basename(e.name, extname(e.name));
const workflowProperties: WorkflowProperties = require(join(
2020-05-20 10:40:26 -07:00
folder,
"properties",
`${workflowId}.properties.json`
));
2020-05-20 10:54:19 -07:00
const iconName: string | undefined = workflowProperties["iconName"];
2020-05-21 14:34:40 -07:00
2021-08-24 10:28:09 +05:30
const isPartnerWorkflow = workflowProperties.creator ? partnersSet.has(workflowProperties.creator.toLowerCase()) : false;
const enabled =
2021-09-08 10:28:14 +01:00
!isPartnerWorkflow &&
(await checkWorkflow(workflowFilePath, enabledActions));
2020-05-20 10:40:26 -07:00
const workflowDesc: WorkflowDesc = {
folder,
2020-05-20 10:40:26 -07:00
id: workflowId,
iconName,
2020-05-20 10:54:19 -07:00
iconType:
iconName && iconName.startsWith("octicon") ? "octicon" : "svg",
};
if (!enabled) {
result.incompatibleWorkflows.push(workflowDesc);
} else {
result.compatibleWorkflows.push(workflowDesc);
}
}
}
}
return result;
}
/**
2020-05-20 10:00:39 -07:00
* Check if a workflow uses only the given set of actions.
*
* @param workflowPath Path to workflow yaml file
* @param enabledActions List of enabled actions
*/
async function checkWorkflow(
workflowPath: string,
enabledActions: string[]
): Promise<boolean> {
// Create set with lowercase action names for easier, case-insensitive lookup
const enabledActionsSet = new Set(enabledActions.map((x) => x.toLowerCase()));
try {
const workflowFileContent = await fs.readFile(workflowPath, "utf8");
const workflow = safeLoad(workflowFileContent);
for (const job of Object.keys(workflow.jobs || {}).map(
(k) => workflow.jobs[k]
)) {
for (const step of job.steps || []) {
if (!!step.uses) {
// Check if allowed action
const [actionName, _] = step.uses.split("@");
2021-09-08 10:28:14 +01:00
const actionNwo = actionName.split("/").slice(0, 2).join("/");
if (!enabledActionsSet.has(actionNwo.toLowerCase())) {
2020-05-20 10:00:39 -07:00
console.info(
`Workflow ${workflowPath} uses '${actionName}' which is not supported for GHES.`
);
return false;
}
}
}
}
// All used actions are enabled 🎉
return true;
} catch (e) {
console.error("Error while checking workflow", e);
throw e;
}
}
(async function main() {
try {
const settings = require("./settings.json");
const result = await checkWorkflows(
settings.folders,
2021-08-23 11:00:59 +05:30
settings.enabledActions,
settings.partners
);
console.group(
`Found ${result.compatibleWorkflows.length} starter workflows compatible with GHES:`
);
console.log(
result.compatibleWorkflows.map((x) => `${x.folder}/${x.id}`).join("\n")
);
console.groupEnd();
console.group(
`Ignored ${result.incompatibleWorkflows.length} starter-workflows incompatible with GHES:`
);
console.log(
result.incompatibleWorkflows.map((x) => `${x.folder}/${x.id}`).join("\n")
);
console.groupEnd();
console.log("Switch to GHES branch");
await exec("git", ["checkout", "ghes"]);
2020-07-15 16:21:25 -07:00
// In order to sync from main, we might need to remove some workflows, add some
// and modify others. The lazy approach is to delete all workflows first, and then
2020-07-15 16:21:25 -07:00
// just bring the compatible ones over from the main branch. We let git figure out
// whether it's a deletion, add, or modify and commit the new state.
console.log("Remove all workflows");
await exec("rm", ["-fr", ...settings.folders]);
2020-05-20 10:40:26 -07:00
await exec("rm", ["-fr", "../../icons"]);
2020-07-15 16:21:25 -07:00
console.log("Sync changes from main for compatible workflows");
await exec("git", [
"checkout",
2020-07-15 16:21:25 -07:00
"main",
"--",
...Array.prototype.concat.apply(
[],
2020-05-20 10:54:19 -07:00
result.compatibleWorkflows.map((x) => {
const r = [
join(x.folder, `${x.id}.yml`),
join(x.folder, "properties", `${x.id}.properties.json`),
];
if (x.iconType === "svg") {
r.push(join("../../icons", `${x.iconName}.svg`));
}
return r;
})
),
]);
} catch (e) {
console.error("Unhandled error while syncing workflows", e);
process.exitCode = 1;
}
})();