Skip to content

Commit

Permalink
check tool sources come from same org setup-foreman runs in
Browse files Browse the repository at this point in the history
  • Loading branch information
afujiwara-roblox committed Sep 29, 2023
1 parent 3d08eab commit ac51b7c
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 30 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ jobs:
with:
version: "*"
token: ${{ secrets.GITHUB_TOKEN }}
allow-external-github-orgs: true
# allow-external-github-orgs: true
- run: foreman --version
- run: rojo --version

Expand All @@ -55,6 +55,6 @@ jobs:
version: "*"
token: ${{ secrets.GITHUB_TOKEN }}
working-directory: tests
allow-external-github-orgs: true
# allow-external-github-orgs: true
- run: foreman --version
- run: selene --version
7 changes: 3 additions & 4 deletions __tests__/hello.test.ts → __tests__/configFile.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// who needs tests?
import configFile from "../src/configFile";
import {parse} from "toml";
import { parse } from "toml";

test("get off my back, Jest", () => {
expect(5).toEqual(5);
Expand All @@ -14,7 +13,7 @@ test("checkSameOrgToolSpec same org", () => {
tool3 = { source = "org1/tool3", version = "1.0.0" }\n
`;
let manifestContent = parse(config);
expect(configFile.checkSameOrgToolSpecs(manifestContent)).toEqual(true);
expect(configFile.checkSameOrgToolSpecs(manifestContent, "org1")).toEqual(true);
});

test("checkSameOrgToolSpec different org", () => {
Expand All @@ -25,5 +24,5 @@ test("checkSameOrgToolSpec different org", () => {
tool3 = { source = "org1/tool3", version = "1.0.0" }\n
`;
let manifestContent = parse(config);
expect(configFile.checkSameOrgToolSpecs(manifestContent)).toEqual(false);
expect(configFile.checkSameOrgToolSpecs(manifestContent, "org1")).toEqual(false);
});
27 changes: 13 additions & 14 deletions src/configFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,12 @@ interface foremanTool {

const MANIFEST = "foreman.toml";

function checkSameOrgToolSpecs(manifestContent: foremanConfig): boolean {
function checkSameOrgToolSpecs(manifestContent: foremanConfig, org: string): 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"];
Expand All @@ -34,35 +33,35 @@ function checkSameOrgToolSpecs(manifestContent: foremanConfig): boolean {
}

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

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

if (tool_org != org) {
return false
}
}
return orgs.every(val => val === orgs[0]);
return true
}

async function checkSameOrgInConfig(): Promise<void> {
async function checkSameOrgInConfig(org: string): Promise<void> {
const manifestPath = await findUp(MANIFEST);
if (manifestPath == null) {
throw new Error("Foreman config file could not be found");
throw new Error("setup-foreman could not find Foreman config file");
}

await readFile(manifestPath, "utf8", (err, data) => {
if (err) {
throw new Error("Could not read Foreman config file");
throw new Error("setup-foreman Could not read Foreman config file");
}
const manifestContent = parse(data);
const sameGithubOrgSource = checkSameOrgToolSpecs(manifestContent);
const sameGithubOrgSource = checkSameOrgToolSpecs(manifestContent, org);
if (sameGithubOrgSource == false) {
throw new Error("Not all GitHub orgs are the same");
throw new Error(`All GitHub orgs in Foreman config must match the org setup-foreman runs in: ${org}. To disable this check, set the \"allow-external-github-orgs\" option to true.`);
}
});
}
Expand Down
6 changes: 3 additions & 3 deletions src/foreman.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {addPath} from "@actions/core";
import {exec} from "@actions/exec";
import {GitHub} from "@actions/github";
import { addPath } from "@actions/core";
import { exec } from "@actions/exec";
import { GitHub } from "@actions/github";
import semver from "semver";
import os from "os";

Expand Down
25 changes: 18 additions & 7 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import {getInput, debug, addPath, setFailed} from "@actions/core";
import {downloadTool, extractZip} from "@actions/tool-cache";
import {GitHub} from "@actions/github";
import {resolve} from "path";
import {exec} from "@actions/exec";
import { getInput, debug, addPath, setFailed } from "@actions/core";
import { downloadTool, extractZip } from "@actions/tool-cache";
import { GitHub, context } from "@actions/github";
import { resolve } from "path";
import { exec } from "@actions/exec";
import configFile from "./configFile";
import foreman from "./foreman";

Expand All @@ -16,12 +16,23 @@ async function run(): Promise<void> {
).toLowerCase();

if (allowExternalGithubOrgs != "true") {
configFile.checkSameOrgInConfig();
let repo = context.payload.repository;

Check failure on line 19 in src/main.ts

View workflow job for this annotation

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

'repo' is never reassigned. Use 'const' instead

Check failure on line 19 in src/main.ts

View workflow job for this annotation

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

'repo' is never reassigned. Use 'const' instead
if (repo == null) {
throw new Error(
`Could not find repository`
)

Check failure on line 23 in src/main.ts

View workflow job for this annotation

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

'org' is never reassigned. Use 'const' instead

Check failure on line 23 in src/main.ts

View workflow job for this annotation

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

'org' is never reassigned. Use 'const' instead
}
let org = repo.owner.name;
if (org == null) {
throw new Error(
`Could not find owner of the repository`
)
}
configFile.checkSameOrgInConfig(org);
}

const octokit = new GitHub(githubToken);
const releases = await foreman.getReleases(octokit);

debug("Choosing release from GitHub API");

const release = foreman.chooseRelease(versionReq, releases);
Expand Down

0 comments on commit ac51b7c

Please sign in to comment.