Skip to content

Commit 9a79ceb

Browse files
authored
Merge pull request #53 from Nicell/allow-list
Add allow list option to setup-foreman
2 parents 5fd21a8 + f58b095 commit 9a79ceb

File tree

5 files changed

+40
-8
lines changed

5 files changed

+40
-8
lines changed

__tests__/configFile.test.ts

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ test("checkSameOrgToolSpec same org", () => {
1414
tool3 = { source = "org1/tool3", version = "1.0.0" }\n
1515
`;
1616
let manifestContent = parse(config);
17-
expect(configFile.checkSameOrgToolSpecs(manifestContent, "org1")).toEqual(
17+
expect(configFile.checkSameOrgToolSpecs(manifestContent, "org1", [])).toEqual(
1818
true
1919
);
2020
});
@@ -27,11 +27,36 @@ test("checkSameOrgToolSpec different org", () => {
2727
tool3 = { source = "org1/tool3", version = "1.0.0" }\n
2828
`;
2929
let manifestContent = parse(config);
30-
expect(configFile.checkSameOrgToolSpecs(manifestContent, "org1")).toEqual(
30+
expect(configFile.checkSameOrgToolSpecs(manifestContent, "org1", [])).toEqual(
3131
false
3232
);
3333
});
3434

35+
test("checkSameOrgToolSpec external org allowed with allowList", () => {
36+
let config = `
37+
[tools]\n
38+
tool1 = { source = "org1/tool1", version = "1.0.0" }\n
39+
tool2 = { source = "org2/tool2", version = "1.0.0" }\n
40+
tool3 = { source = "org1/tool3", version = "1.0.0" }\n
41+
`;
42+
let manifestContent = parse(config);
43+
expect(
44+
configFile.checkSameOrgToolSpecs(manifestContent, "org1", ["org2"])
45+
).toEqual(true);
46+
});
47+
48+
test("checkSameOrgToolSpec external org allowed case-insensitive", () => {
49+
let config = `
50+
[tools]\n
51+
tool1 = { source = "org1/tool1", version = "1.0.0" }\n
52+
tool2 = { source = "ORG2/tool2", version = "1.0.0" }\n
53+
`;
54+
let manifestContent = parse(config);
55+
expect(
56+
configFile.checkSameOrgToolSpecs(manifestContent, "org1", ["org2"])
57+
).toEqual(true);
58+
});
59+
3560
test("filter valid releases", () => {
3661
const releases: GitHubRelease[] = [
3762
{

action.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ inputs:
1717
allow-external-github-orgs:
1818
required: false
1919
description: 'Allow installing from external GitHub organizations'
20+
github-orgs-allow-list:
21+
required: false
22+
description: 'Comma separated list of orgs that are allowed even if external orgs are not allowed'
2023
artifactory-url:
2124
required: false
2225
description: 'Artifactory URL to use for downloading Foreman tools'

src/configFile.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ const MANIFEST = "foreman.toml";
1818

1919
function checkSameOrgToolSpecs(
2020
manifestContent: foremanConfig,
21-
org: string
21+
org: string,
22+
allowList: string[]
2223
): boolean {
2324
const tools = manifestContent.tools;
2425
if (tools == null) {
@@ -44,13 +45,13 @@ function checkSameOrgToolSpecs(
4445
);
4546
}
4647
if (tool_org.toLowerCase() != org) {
47-
return false;
48+
return allowList.includes(tool_org.toLowerCase())
4849
}
4950
}
5051
return true;
5152
}
5253

53-
async function checkSameOrgInConfig(org: string): Promise<void> {
54+
async function checkSameOrgInConfig(org: string, allowList: string[]): Promise<void> {
5455
const manifestPath = await findUp(MANIFEST);
5556
if (manifestPath == undefined) {
5657
throw new Error("setup-foreman could not find Foreman config file");
@@ -63,7 +64,7 @@ async function checkSameOrgInConfig(org: string): Promise<void> {
6364
);
6465
}
6566
const manifestContent = parse(data);
66-
const sameGithubOrgSource = checkSameOrgToolSpecs(manifestContent, org);
67+
const sameGithubOrgSource = checkSameOrgToolSpecs(manifestContent, org, allowList);
6768
if (!sameGithubOrgSource) {
6869
throw new Error(
6970
`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.`

src/main.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ async function run(): Promise<void> {
1919
const allowExternalGithubOrgs: string = getInput(
2020
"allow-external-github-orgs"
2121
).toLowerCase();
22+
const githubOrgsAllowList: string[] = getInput(
23+
"github-orgs-allow-list"
24+
).split(",").map((org: string) => org.trim().toLowerCase()).filter((org) => org !== "");
2225
const artifactoryUrl = getInput("artifactory-url");
2326
const artifactoryToken = getInput("artifactory-token");
2427

@@ -85,7 +88,7 @@ async function run(): Promise<void> {
8588
`Could not find repository owner setup-foreman is running in`
8689
);
8790
}
88-
configFile.checkSameOrgInConfig(owner.toLowerCase());
91+
configFile.checkSameOrgInConfig(owner.toLowerCase(), githubOrgsAllowList);
8992
}
9093

9194
await foreman.installTools();

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"compilerOptions": {
3-
"target": "es6",
3+
"target": "es2016",
44
"module": "commonjs",
55
"outDir": "./lib",
66
"rootDir": "./src",

0 commit comments

Comments
 (0)