From 0fd0fa4fd498a82fc21a38a4b9520e9b8c708339 Mon Sep 17 00:00:00 2001
From: Marco Accorinti <accosk8@gmail.com>
Date: Sun, 17 Sep 2023 20:45:12 +0200
Subject: [PATCH] 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<void> {
     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<IGitSourceSettings> {
   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<IGitSourceSettings> {
   // 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}'`
     )
   }