Skip to content

Commit 13073c0

Browse files
committed
Fix new remote address being handled as local path
If it does not contain a `@` character.
1 parent 3b74930 commit 13073c0

3 files changed

Lines changed: 124 additions & 34 deletions

File tree

‎lib/entry-points.js‎

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

‎src/config-utils.test.ts‎

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import {
4141
initAllState,
4242
callee,
4343
SAMPLE_DOTCOM_API_DETAILS,
44+
setupBaseActionsVars,
4445
} from "./testing-utils";
4546
import {
4647
GitHubVariant,
@@ -443,6 +444,7 @@ test.serial("load non-existent input", async (t) => {
443444

444445
try {
445446
const state = initAllState();
447+
setupBaseActionsVars({}, state.env);
446448
await configUtils.initConfig(
447449
state,
448450
createTestInitConfigInputs({
@@ -2604,16 +2606,51 @@ test.serial(
26042606
async (t) => {
26052607
await withTmpDir(async (tmpDir) => {
26062608
const getRemoteConfig = sinon.stub(file, "getRemoteConfig").resolves({});
2609+
const target = callee(configUtils.loadUserConfig)
2610+
.withDefaultActionsEnv()
2611+
.withFeatures([Feature.NewRemoteFileAddresses]);
2612+
2613+
const testTargetWith = async (remoteAddress: string) => {
2614+
getRemoteConfig.resetHistory();
2615+
2616+
await target
2617+
.withArgs(remoteAddress, tmpDir, SAMPLE_DOTCOM_API_DETAILS, tmpDir)
2618+
.passes(t.deepEqual, {});
2619+
2620+
t.true(
2621+
getRemoteConfig.calledOnceWithExactly(
2622+
sinon.match.any,
2623+
remoteAddress,
2624+
SAMPLE_DOTCOM_API_DETAILS,
2625+
),
2626+
);
2627+
};
26072628

2608-
const remoteAddress = "repo:file";
2609-
await callee(configUtils.loadUserConfig)
2610-
.withArgs(remoteAddress, tmpDir, SAMPLE_DOTCOM_API_DETAILS, tmpDir)
2611-
.passes(t.deepEqual, {});
2629+
// When the new format FF is enabled, all inputs that don't explicitly refer
2630+
// to a local file that can be found will be tried as remote addresses.
2631+
await testTargetWith("repo:file");
2632+
await testTargetWith("input");
2633+
await testTargetWith("../input");
2634+
await testTargetWith("repo@main");
26122635

2613-
t.true(
2636+
// An explicitly local path can still override this.
2637+
const explicitlyLocalAddress = "./repo@main";
2638+
2639+
getRemoteConfig.resetHistory();
2640+
2641+
await target
2642+
.withArgs(
2643+
explicitlyLocalAddress,
2644+
tmpDir,
2645+
SAMPLE_DOTCOM_API_DETAILS,
2646+
tmpDir,
2647+
)
2648+
.throws(t);
2649+
2650+
t.false(
26142651
getRemoteConfig.calledOnceWithExactly(
26152652
sinon.match.any,
2616-
remoteAddress,
2653+
explicitlyLocalAddress,
26172654
SAMPLE_DOTCOM_API_DETAILS,
26182655
),
26192656
);

‎src/config-utils.ts‎

Lines changed: 52 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -485,17 +485,60 @@ export async function loadUserConfig(
485485
apiDetails: api.GitHubApiCombinedDetails,
486486
tempDir: string,
487487
): Promise<UserConfig> {
488-
if (isLocal(configFile)) {
489-
if (configFile !== userConfigFromActionPath(tempDir)) {
490-
// If the config file is not generated by the Action, it should be relative to the workspace.
491-
configFile = path.resolve(workspacePath, configFile);
492-
// Error if the config file is now outside of the workspace
493-
if (!(configFile + path.sep).startsWith(workspacePath + path.sep)) {
494-
throw new ConfigurationError(
495-
errorMessages.getConfigFileOutsideWorkspaceErrorMessage(configFile),
496-
);
488+
// Compute the path of the generated configuration file.
489+
const generatedConfigFile = userConfigFromActionPath(tempDir);
490+
491+
// A helper function to decide if the given path is relative to the workspacePath.
492+
const isWorkspaceRelative = (fp: string) =>
493+
(fp + path.sep).startsWith(workspacePath + path.sep);
494+
495+
// We must decide if `configFile` refers to a local or remote file. Using the old remote file
496+
// address format, an address referred to a remote file if it contained '@' somewhere. The
497+
// new format makes all components, except for the repo name optional, and (in theory) a
498+
// repo name could be something like `foo.yml`. That creates ambiguity over whether a
499+
// file is local or not.
500+
let localFile = false;
501+
502+
if (configFile === generatedConfigFile) {
503+
// The generated file is always local.
504+
localFile = true;
505+
} else if (isLocal(configFile)) {
506+
// If the config file is not generated by the Action, it should be relative to the workspace.
507+
const localFilePath = path.resolve(workspacePath, configFile);
508+
509+
// Determine whether the resulting path is relative to the workspace.
510+
const workspaceRelative = isWorkspaceRelative(localFilePath);
511+
512+
// If the path is relative to the workspace and the file exists, then we use it.
513+
if (workspaceRelative && fs.existsSync(localFilePath)) {
514+
configFile = localFilePath;
515+
localFile = true;
516+
} else {
517+
const allowNewFormat = await actionState.features.getValue(
518+
Feature.NewRemoteFileAddresses,
519+
);
520+
521+
// If the FF for the new format is not enabled or the input path is explicitly local,
522+
// throw the old errors depending on whether the file is outside of the workspace or not.
523+
// Otherwise, we assume the path refers to a remote file.
524+
if (isRelativePath(configFile) || !allowNewFormat) {
525+
if (workspaceRelative) {
526+
throw new ConfigurationError(
527+
errorMessages.getConfigFileDoesNotExistErrorMessage(localFilePath),
528+
);
529+
} else {
530+
throw new ConfigurationError(
531+
errorMessages.getConfigFileOutsideWorkspaceErrorMessage(
532+
localFilePath,
533+
),
534+
);
535+
}
497536
}
498537
}
538+
}
539+
540+
// Try to load the file according to whether it is local or not.
541+
if (localFile) {
499542
const validateConfig = await actionState.features.getValue(
500543
Feature.ValidateDbConfig,
501544
);
@@ -1321,13 +1364,6 @@ export function getLocalConfig(
13211364
configFile: string,
13221365
validateConfig: boolean,
13231366
): UserConfig {
1324-
// Error if the file does not exist
1325-
if (!fs.existsSync(configFile)) {
1326-
throw new ConfigurationError(
1327-
errorMessages.getConfigFileDoesNotExistErrorMessage(configFile),
1328-
);
1329-
}
1330-
13311367
return parseUserConfig(
13321368
logger,
13331369
configFile,

0 commit comments

Comments
 (0)