Skip to content

Commit d1edf2e

Browse files
committed
Improve replaces-base validation and add tests
1 parent b779832 commit d1edf2e

3 files changed

Lines changed: 78 additions & 4 deletions

File tree

lib/start-proxy-action.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/start-proxy.test.ts

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -639,6 +639,76 @@ test(
639639
},
640640
);
641641

642+
test("getCredentials validates 'replaces-base' correctly", async (t) => {
643+
// Valid cases.
644+
const credentialsInput = toEncodedJSON([
645+
{
646+
type: "maven_repository",
647+
host: "maven1.pkg.github.com",
648+
token: "abc",
649+
"replaces-base": false,
650+
},
651+
{
652+
type: "maven_repository",
653+
host: "maven2.pkg.github.com",
654+
token: "def",
655+
"replaces-base": true,
656+
},
657+
{
658+
type: "maven_repository",
659+
host: "maven3.pkg.github.com",
660+
token: "ghi",
661+
},
662+
]);
663+
664+
const credentials = startProxyExports.getCredentials(
665+
getRunnerLogger(true),
666+
undefined,
667+
credentialsInput,
668+
BuiltInLanguage.java,
669+
false,
670+
);
671+
672+
t.is(credentials.length, 3);
673+
t.true(credentials.some((c) => c["replaces-base"] === true));
674+
t.true(credentials.some((c) => c["replaces-base"] === false));
675+
t.true(credentials.some((c) => c["replaces-base"] === undefined));
676+
677+
// Invalid cases.
678+
const baseInvalid = {
679+
type: "maven_repository",
680+
host: "maven4.pkg.github.com",
681+
token: "jkl",
682+
};
683+
t.throws(() =>
684+
startProxyExports.getCredentials(
685+
getRunnerLogger(true),
686+
undefined,
687+
toEncodedJSON([{ ...baseInvalid, "replaces-base": null }]),
688+
BuiltInLanguage.actions,
689+
false,
690+
),
691+
);
692+
t.throws(() =>
693+
startProxyExports.getCredentials(
694+
getRunnerLogger(true),
695+
undefined,
696+
toEncodedJSON([{ ...baseInvalid, "replaces-base": 123 }]),
697+
BuiltInLanguage.actions,
698+
false,
699+
),
700+
);
701+
t.throws(() =>
702+
startProxyExports.getCredentials(
703+
getRunnerLogger(true),
704+
undefined,
705+
toEncodedJSON([{ ...baseInvalid, "replaces-base": "true" }]),
706+
BuiltInLanguage.actions,
707+
false,
708+
),
709+
);
710+
});
711+
642712
test("getCredentials returns all credentials for Actions when using LANGUAGE_TO_REGISTRY_TYPE", async (t) => {
643713
const credentialsInput = toEncodedJSON(mixedCredentials);
644714

src/start-proxy.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -350,8 +350,12 @@ export function getCredentials(
350350
// Construct the base credential object.
351351
const baseCredential: Omit<Registry, keyof Address> = { type: e.type };
352352

353-
if (isDefined(e["replaces-base"])) {
354-
if (typeof e["replaces-base"] === "boolean") {
353+
// If "replaces-base" is present, it must be a boolean.
354+
if ("replaces-base" in e) {
355+
if (
356+
isDefined(e["replaces-base"]) &&
357+
typeof e["replaces-base"] === "boolean"
358+
) {
355359
baseCredential["replaces-base"] = e["replaces-base"];
356360
} else {
357361
throw new ConfigurationError(

0 commit comments

Comments
 (0)