From 0fd0fa4fd498a82fc21a38a4b9520e9b8c708339 Mon Sep 17 00:00:00 2001 From: Marco Accorinti Date: Sun, 17 Sep 2023 20:45:12 +0200 Subject: [PATCH 1/7] Add workingDirectory option Let user override $GITHUB_WORKSPACE as default working directory Defaults to undefined, the original behaviour is maintained --- __test__/git-auth-helper.test.ts | 3 ++- dist/index.js | 20 ++++++++++---------- src/git-source-settings.ts | 5 +++++ src/input-helper.ts | 20 ++++++++++---------- 4 files changed, 27 insertions(+), 21 deletions(-) diff --git a/__test__/git-auth-helper.test.ts b/__test__/git-auth-helper.test.ts index 7633704..aae9e49 100644 --- a/__test__/git-auth-helper.test.ts +++ b/__test__/git-auth-helper.test.ts @@ -824,7 +824,8 @@ async function setup(testName: string): Promise { sshUser: '', workflowOrganizationId: 123456, setSafeDirectory: true, - githubServerUrl: githubServerUrl + githubServerUrl: githubServerUrl, + workingDirectory: undefined } } diff --git a/dist/index.js b/dist/index.js index c39596b..2bb3eaa 100644 --- a/dist/index.js +++ b/dist/index.js @@ -1716,14 +1716,14 @@ const workflowContextHelper = __importStar(__nccwpck_require__(9568)); function getInputs() { return __awaiter(this, void 0, void 0, function* () { const result = {}; - // GitHub workspace - let githubWorkspacePath = process.env['GITHUB_WORKSPACE']; - if (!githubWorkspacePath) { - throw new Error('GITHUB_WORKSPACE not defined'); + // Working directory + let workingDirectory = core.getInput('workingDirectory') || process.env['GITHUB_WORKSPACE']; + if (!workingDirectory) { + throw new Error('working dir not defined'); } - githubWorkspacePath = path.resolve(githubWorkspacePath); - core.debug(`GITHUB_WORKSPACE = '${githubWorkspacePath}'`); - fsHelper.directoryExistsSync(githubWorkspacePath, true); + workingDirectory = path.resolve(workingDirectory); + core.debug(`working directory = '${workingDirectory}'`); + fsHelper.directoryExistsSync(workingDirectory, true); // Qualified repository const qualifiedRepository = core.getInput('repository') || `${github.context.repo.owner}/${github.context.repo.repo}`; @@ -1738,9 +1738,9 @@ function getInputs() { result.repositoryName = splitRepository[1]; // Repository path result.repositoryPath = core.getInput('path') || '.'; - result.repositoryPath = path.resolve(githubWorkspacePath, result.repositoryPath); - if (!(result.repositoryPath + path.sep).startsWith(githubWorkspacePath + path.sep)) { - throw new Error(`Repository path '${result.repositoryPath}' is not under '${githubWorkspacePath}'`); + result.repositoryPath = path.resolve(workingDirectory, result.repositoryPath); + if (!(result.repositoryPath + path.sep).startsWith(workingDirectory + path.sep)) { + throw new Error(`Repository path '${result.repositoryPath}' is not under '${workingDirectory}'`); } // Workflow repository? const isWorkflowRepository = qualifiedRepository.toUpperCase() === diff --git a/src/git-source-settings.ts b/src/git-source-settings.ts index 4e41ac3..580ab9e 100644 --- a/src/git-source-settings.ts +++ b/src/git-source-settings.ts @@ -118,4 +118,9 @@ export interface IGitSourceSettings { * User override on the GitHub Server/Host URL that hosts the repository to be cloned */ githubServerUrl: string | undefined + + /** + * User override of the working directory (default is $GITHUB_WORKSPACE) + */ + workingDirectory: string | undefined } diff --git a/src/input-helper.ts b/src/input-helper.ts index 059232f..f9eabc1 100644 --- a/src/input-helper.ts +++ b/src/input-helper.ts @@ -8,14 +8,14 @@ import {IGitSourceSettings} from './git-source-settings' export async function getInputs(): Promise { const result = {} as unknown as IGitSourceSettings - // GitHub workspace - let githubWorkspacePath = process.env['GITHUB_WORKSPACE'] - if (!githubWorkspacePath) { - throw new Error('GITHUB_WORKSPACE not defined') + // Working directory + let workingDirectory = core.getInput('workingDirectory') || process.env['GITHUB_WORKSPACE'] + if (!workingDirectory) { + throw new Error('working dir not defined') } - githubWorkspacePath = path.resolve(githubWorkspacePath) - core.debug(`GITHUB_WORKSPACE = '${githubWorkspacePath}'`) - fsHelper.directoryExistsSync(githubWorkspacePath, true) + workingDirectory = path.resolve(workingDirectory) + core.debug(`working directory = '${workingDirectory}'`) + fsHelper.directoryExistsSync(workingDirectory, true) // Qualified repository const qualifiedRepository = @@ -38,16 +38,16 @@ export async function getInputs(): Promise { // Repository path result.repositoryPath = core.getInput('path') || '.' result.repositoryPath = path.resolve( - githubWorkspacePath, + workingDirectory, result.repositoryPath ) if ( !(result.repositoryPath + path.sep).startsWith( - githubWorkspacePath + path.sep + workingDirectory + path.sep ) ) { throw new Error( - `Repository path '${result.repositoryPath}' is not under '${githubWorkspacePath}'` + `Repository path '${result.repositoryPath}' is not under '${workingDirectory}'` ) } From b0635286c51c3a87731f682cfaa8fac6ab2267e7 Mon Sep 17 00:00:00 2001 From: Marco Accorinti Date: Tue, 19 Sep 2023 18:30:16 +0200 Subject: [PATCH 2/7] update: action.yml Add working-directory input parameter --- action.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/action.yml b/action.yml index 75d5ae2..90e5ed4 100644 --- a/action.yml +++ b/action.yml @@ -98,6 +98,9 @@ inputs: github-server-url: description: The base URL for the GitHub instance that you are trying to clone from, will use environment defaults to fetch from the same instance that the workflow is running from unless specified. Example URLs are https://github.com or https://my-ghes-server.example.com required: false + working-directory: + description: Provide the working directory for the git commands to execute into, defaults to $GITHUB_WORKSPACE + required: false runs: using: node20 main: dist/index.js From b2811239ce6362da37c3e8ada5b36352701e53a0 Mon Sep 17 00:00:00 2001 From: Marco Accorinti Date: Tue, 19 Sep 2023 18:35:24 +0200 Subject: [PATCH 3/7] fix input name (workingDirectory -> working-directory) --- src/input-helper.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/input-helper.ts b/src/input-helper.ts index f9eabc1..7fa47c3 100644 --- a/src/input-helper.ts +++ b/src/input-helper.ts @@ -9,7 +9,7 @@ export async function getInputs(): Promise { const result = {} as unknown as IGitSourceSettings // Working directory - let workingDirectory = core.getInput('workingDirectory') || process.env['GITHUB_WORKSPACE'] + let workingDirectory = core.getInput('working-directory') || process.env['GITHUB_WORKSPACE'] if (!workingDirectory) { throw new Error('working dir not defined') } From 5b1833c6cff20933e444b6948352f76f8459eeec Mon Sep 17 00:00:00 2001 From: Marco Accorinti Date: Wed, 20 Sep 2023 20:59:18 +0200 Subject: [PATCH 4/7] fix(action.yml): path description Add working-directory --- action.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/action.yml b/action.yml index 90e5ed4..5639bf3 100644 --- a/action.yml +++ b/action.yml @@ -53,7 +53,7 @@ inputs: description: 'Whether to configure the token or SSH key with the local git config' default: true path: - description: 'Relative path under $GITHUB_WORKSPACE to place the repository' + description: 'Relative path under $GITHUB_WORKSPACE/working-directory to place the repository' clean: description: 'Whether to execute `git clean -ffdx && git reset --hard HEAD` before fetching' default: true From 178be7a9f3387b6adc354532006a464dd402021d Mon Sep 17 00:00:00 2001 From: Marco Accorinti Date: Wed, 20 Sep 2023 21:00:03 +0200 Subject: [PATCH 5/7] remove(git-source-settings.ts): workingDirectory option Not needed (working-directory option is used internally) --- src/git-source-settings.ts | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/git-source-settings.ts b/src/git-source-settings.ts index 580ab9e..4e41ac3 100644 --- a/src/git-source-settings.ts +++ b/src/git-source-settings.ts @@ -118,9 +118,4 @@ export interface IGitSourceSettings { * User override on the GitHub Server/Host URL that hosts the repository to be cloned */ githubServerUrl: string | undefined - - /** - * User override of the working directory (default is $GITHUB_WORKSPACE) - */ - workingDirectory: string | undefined } From 21ffcee746306682a98ea2f7d159e812d2140502 Mon Sep 17 00:00:00 2001 From: Marco Accorinti Date: Wed, 20 Sep 2023 21:00:50 +0200 Subject: [PATCH 6/7] update: README.md with new options, descriptions --- README.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index a7924cd..e73deab 100644 --- a/README.md +++ b/README.md @@ -71,7 +71,7 @@ Please refer to the [release page](https://github.com/actions/checkout/releases/ # Default: true persist-credentials: '' - # Relative path under $GITHUB_WORKSPACE to place the repository + # Relative path under $GITHUB_WORKSPACE/working-directory to place the repository path: '' # Whether to execute `git clean -ffdx && git reset --hard HEAD` before fetching @@ -126,6 +126,10 @@ Please refer to the [release page](https://github.com/actions/checkout/releases/ # running from unless specified. Example URLs are https://github.com or # https://my-ghes-server.example.com github-server-url: '' + + # Provide the working directory for the git commands to execute into, defaults to + # $GITHUB_WORKSPACE + working-directory: '' ``` From 8461fff5ebbd8df1ff4b43ab5de858024dd5acd7 Mon Sep 17 00:00:00 2001 From: Marco Accorinti Date: Wed, 20 Sep 2023 21:02:10 +0200 Subject: [PATCH 7/7] change: remove path.sep, add unit tests, dist Remove path.sep during comparison with repositoryPath (did not take in account root folders) Updated and added more unit tests Updated dist/index.js --- __test__/git-auth-helper.test.ts | 1 - __test__/input-helper.test.ts | 21 +++++++++++++++++++++ src/input-helper.ts | 4 ++-- 3 files changed, 23 insertions(+), 3 deletions(-) diff --git a/__test__/git-auth-helper.test.ts b/__test__/git-auth-helper.test.ts index aae9e49..c5a72d7 100644 --- a/__test__/git-auth-helper.test.ts +++ b/__test__/git-auth-helper.test.ts @@ -825,7 +825,6 @@ async function setup(testName: string): Promise { workflowOrganizationId: 123456, setSafeDirectory: true, githubServerUrl: githubServerUrl, - workingDirectory: undefined } } diff --git a/__test__/input-helper.test.ts b/__test__/input-helper.test.ts index 9514cb4..5c79f7a 100644 --- a/__test__/input-helper.test.ts +++ b/__test__/input-helper.test.ts @@ -144,4 +144,25 @@ describe('input-helper tests', () => { const settings: IGitSourceSettings = await inputHelper.getInputs() expect(settings.workflowOrganizationId).toBe(123456) }) + + it('sets a different working directory', async() => { + inputs['working-directory'] = '/home/user/test' + inputs['path'] = 'path/to/repo' + const settings: IGitSourceSettings = await inputHelper.getInputs() + expect(settings.repositoryPath).toBe(path.resolve('/home/user/test/path/to/repo')) + }) + + it('sets a working directory on root', async() => { + inputs['working-directory'] = '/' + inputs['path'] = 'path/to/repo' + const settings: IGitSourceSettings = await inputHelper.getInputs() + expect(settings.repositoryPath).toBe(path.resolve('/path/to/repo')) + }) + + it('sets a working directory on root and repository path is set to empty', async() => { + inputs['working-directory'] = '/' + inputs['path'] = '' + const settings: IGitSourceSettings = await inputHelper.getInputs() + expect(settings.repositoryPath).toBe(path.resolve('/')) + }) }) diff --git a/src/input-helper.ts b/src/input-helper.ts index 7fa47c3..2e1d2d4 100644 --- a/src/input-helper.ts +++ b/src/input-helper.ts @@ -43,11 +43,11 @@ export async function getInputs(): Promise { ) if ( !(result.repositoryPath + path.sep).startsWith( - workingDirectory + path.sep + workingDirectory ) ) { throw new Error( - `Repository path '${result.repositoryPath}' is not under '${workingDirectory}'` + `Repository path '${result.repositoryPath + path.sep}' is not under '${workingDirectory}'` ) }