Skip to content

Commit

Permalink
fix(action): Ensure working directory always starts out at repo root
Browse files Browse the repository at this point in the history
  • Loading branch information
andreiborza committed Feb 7, 2025
1 parent 427e14a commit 152762a
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 7 deletions.
21 changes: 21 additions & 0 deletions __tests__/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
getSetCommitsOption,
getProjects,
getUrlPrefixOption,
getWorkingDirectory,
} from '../src/options';

describe('options', () => {
Expand Down Expand Up @@ -209,6 +210,26 @@ describe('options', () => {
expect(getUrlPrefixOption()).toEqual('build');
});
});

describe('getWorkingDirectory', () => {
afterEach(() => {
delete process.env['GITHUB_WORKSPACE'];
delete process.env['INPUT_WORKING_DIRECTORY'];
});

it('gets the working directory url and prefixes it with the `GITHUB_WORKSPACE`', () => {
process.env['GITHUB_WORKSPACE'] = '/repo/root';
process.env['INPUT_WORKING_DIRECTORY'] = '/some/working/directory';
expect(getWorkingDirectory()).toEqual(
'/repo/root/some/working/directory'
);
});

it('should default to `GITHUB_WORKSPACE` even if no direcotry is passed', () => {
process.env['GITHUB_WORKSPACE'] = '/repo/root';
expect(getWorkingDirectory()).toEqual('/repo/root');
});
});
});

// shows how the runner will run a javascript action with env / stdout protocol
Expand Down
9 changes: 6 additions & 3 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -122852,9 +122852,13 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.getWorkingDirectory = exports.getUrlPrefixOption = exports.getProjects = exports.checkEnvironmentVariables = exports.getSetCommitsOption = exports.getBooleanOption = exports.getDist = exports.getSourcemaps = exports.getStartedAt = exports.getEnvironment = exports.getVersion = void 0;
const core = __importStar(__nccwpck_require__(42186));
const path_1 = __importDefault(__nccwpck_require__(71017));
const cli_1 = __nccwpck_require__(56733);
/**
* Get the release version string from parameter or propose one.
Expand Down Expand Up @@ -123029,9 +123033,8 @@ exports.getUrlPrefixOption = getUrlPrefixOption;
const getWorkingDirectory = () => {
// The action runs inside `github.action_path` and as such
// doesn't automatically have access to the user's git
// In case users don't provide their own `working_directory`
// we use `GITHUB_WORKSPACE` which is at the top of the repo.
return (core.getInput('working_directory') || process.env.GITHUB_WORKSPACE || '');
// We prefix all paths with `GITHUB_WORKSPACE` which is at the top of the repo.
return path_1.default.join(process.env.GITHUB_WORKSPACE || '', core.getInput('working_directory'));
};
exports.getWorkingDirectory = getWorkingDirectory;

Expand Down
9 changes: 5 additions & 4 deletions src/options.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as core from '@actions/core';
import path from 'path';
import {getCLI} from './cli';

/**
Expand Down Expand Up @@ -195,9 +196,9 @@ export const getUrlPrefixOption = (): string => {
export const getWorkingDirectory = (): string => {
// The action runs inside `github.action_path` and as such
// doesn't automatically have access to the user's git
// In case users don't provide their own `working_directory`
// we use `GITHUB_WORKSPACE` which is at the top of the repo.
return (
core.getInput('working_directory') || process.env.GITHUB_WORKSPACE || ''
// We prefix all paths with `GITHUB_WORKSPACE` which is at the top of the repo.
return path.join(
process.env.GITHUB_WORKSPACE || '',
core.getInput('working_directory')
);
};

0 comments on commit 152762a

Please # to comment.