Skip to content

Commit

Permalink
removed infinite loop
Browse files Browse the repository at this point in the history
  • Loading branch information
afujiwara-roblox committed Sep 26, 2023
1 parent 689c538 commit 55eca19
Showing 1 changed file with 62 additions and 64 deletions.
126 changes: 62 additions & 64 deletions src/configFile.ts
Original file line number Diff line number Diff line change
@@ -1,89 +1,87 @@
import {join, dirname} from "path";
import {parse} from "toml";
import {readFile, existsSync} from "fs";
import { join, dirname } from "path";
import { parse } from "toml";
import { readFile, existsSync } from "fs";
import { dir } from "console";

Check failure on line 4 in src/configFile.ts

View workflow job for this annotation

GitHub Actions / Test setup-foreman action (ubuntu-latest)

'dir' is defined but never used

Check failure on line 4 in src/configFile.ts

View workflow job for this annotation

GitHub Actions / Test setup-foreman action with working-directory (ubuntu-latest)

'dir' is defined but never used
interface foremanConfig {
tools: foremanTool[];
tools: foremanTool[];
}

interface foremanTool {
source?: string;
github?: string;
gitlab?: string;
version: string;
source?: string;
github?: string;
gitlab?: string;
version: string;
}

const MANIFEST = "foreman.toml";

function findManifestPath(): string | null {
let directory = __dirname;
while (true) {
if (!directory) {
break;
let directory = __dirname;
while (directory != "/") {
const configFilePath = join(directory, MANIFEST);
if (existsSync(configFilePath)) {
return configFilePath;
} else {
directory = dirname(directory);
}
}
const configFilePath = join(directory, MANIFEST);
if (existsSync(configFilePath)) {
return configFilePath;
} else {
directory = dirname(directory);
}
}
return null;
return null;
}

function checkSameOrgToolSpecs(manifestContent: foremanConfig): boolean {
const tools = manifestContent.tools;
if (tools == null) {
throw new Error("Tools section in Foreman config not found");
}

const orgs: string[] = [];
for (const tool_name in tools) {
const tool_spec = tools[tool_name];
let source = tool_spec["source"];
if (source == null) {
source = tool_spec["github"];
}
if (source == null) {
continue;
const tools = manifestContent.tools;
if (tools == null) {
throw new Error("Tools section in Foreman config not found");
}

const source_array = source.split("/");
const org = source_array[0];
const orgs: string[] = [];
for (const tool_name in tools) {
const tool_spec = tools[tool_name];
let source = tool_spec["source"];
if (source == null) {
source = tool_spec["github"];
}
if (source == null) {
continue;
}

const source_array = source.split("/");
const org = source_array[0];

if (org == null) {
throw new Error(
`Org not found in tool spec definition for: ${tool_name}`
);
if (org == null) {
throw new Error(
`Org not found in tool spec definition for: ${tool_name}`
);
}
orgs.push(org.toLowerCase());
}
orgs.push(org.toLowerCase());
}
if (orgs.length == 0) {
return true;
}
return orgs.every(val => val === orgs[0]);
if (orgs.length == 0) {
return true;
}
return orgs.every(val => val === orgs[0]);
}

async function checkSameOrgInConfig(): Promise<void> {
const manifestPath = findManifestPath();
if (manifestPath == null) {
throw new Error("Foreman config file could not be found");
}

await readFile(manifestPath, "utf8", (err, data) => {
if (err) {
throw new Error("Could not read Foreman config file");
const manifestPath = findManifestPath();
if (manifestPath == null) {
throw new Error("Foreman config file could not be found");
}
{
const manifestContent = parse(data);
const sameGithubOrgSource = checkSameOrgToolSpecs(manifestContent);
if (sameGithubOrgSource == false) {
throw new Error("Not all GitHub orgs are the same");
}
}
});

await readFile(manifestPath, "utf8", (err, data) => {
if (err) {
throw new Error("Could not read Foreman config file");
}
{
const manifestContent = parse(data);
const sameGithubOrgSource = checkSameOrgToolSpecs(manifestContent);
if (sameGithubOrgSource == false) {
throw new Error("Not all GitHub orgs are the same");
}
}
});
}

export default {
checkSameOrgInConfig,
checkSameOrgToolSpecs
checkSameOrgInConfig,
checkSameOrgToolSpecs
};

0 comments on commit 55eca19

Please sign in to comment.