diff --git a/README.md b/README.md index 24f7d2f..c6bbcc1 100644 --- a/README.md +++ b/README.md @@ -72,12 +72,22 @@ jobs: ```yaml - uses: nrwl/nx-set-shas@v4 with: + # The GitHub token used to perform git operations + # + # Default: ${ github.token } + gh-token: '' + # The "main" branch of your repository (the base branch which you target with PRs). # Common names for this branch include main and master. # - # Default: main + # Default: "main" main-branch-name: '' + # The name of the remote to fetch from + # + # Default: "origin" + remote: '' + # Applies the derived SHAs for base and head as NX_BASE and NX_HEAD environment variables within the current Job. # # Default: true @@ -89,18 +99,16 @@ jobs: error-on-no-successful-workflow: '' # Fallback SHA to use if no successful workflow run is found. This can be useful in scenarios where you need a specific commit as a reference for comparison, especially in newly set up repositories or those with sparse workflow runs. - # - # Default: "" fallback-sha: '' # The type of event to check for the last successful commit corresponding to that workflow-id, e.g. push, pull_request, release etc. # - # Default: push + # Default: "push" last-successful-event: '' # The path where your repository is. This is only required for cases where the repository code is checked out or moved to a specific path. # - # Default: . + # Default: "." working-directory: '' # The ID of the github action workflow to check for successful run or the name of the file name containing the workflow. diff --git a/action.yml b/action.yml index 98b1236..de1a6df 100644 --- a/action.yml +++ b/action.yml @@ -8,6 +8,9 @@ inputs: main-branch-name: description: 'The name of the main branch in your repo, used as the target of PRs. E.g. main, master etc' default: 'main' + remote: + description: 'The name of the remote to fetch from' + default: 'origin' set-environment-variables-for-job: description: 'Applies the derived SHAs for base and head as NX_BASE and NX_HEAD environment variables within the current Job' default: 'true' diff --git a/dist/index.js b/dist/index.js index 03434eb..2e9cc7f 100644 --- a/dist/index.js +++ b/dist/index.js @@ -29941,6 +29941,7 @@ const lastSuccessfulEvent = core.getInput('last-successful-event'); const workingDirectory = core.getInput('working-directory'); const workflowId = core.getInput('workflow-id'); const fallbackSHA = core.getInput('fallback-sha'); +const remote = core.getInput('remote'); const defaultWorkingDirectory = '.'; let BASE_SHA; (() => __awaiter(void 0, void 0, void 0, function* () { @@ -29962,7 +29963,7 @@ let BASE_SHA; !github.context.payload.pull_request.merged) { const baseResult = (0, child_process_1.spawnSync)('git', [ 'merge-base', - `origin/${github.context.payload[eventName].base.ref}`, + `${remote}/${github.context.payload[eventName].base.ref}`, 'HEAD', ], { encoding: 'utf-8' }); BASE_SHA = baseResult.stdout; @@ -29988,14 +29989,14 @@ let BASE_SHA; } else { process.stdout.write('\n'); - process.stdout.write(`WARNING: Unable to find a successful workflow run on 'origin/${mainBranchName}', or the latest successful workflow was connected to a commit which no longer exists on that branch (e.g. if that branch was rebased)\n`); + process.stdout.write(`WARNING: Unable to find a successful workflow run on '${remote}/${mainBranchName}', or the latest successful workflow was connected to a commit which no longer exists on that branch (e.g. if that branch was rebased)\n`); if (fallbackSHA) { BASE_SHA = fallbackSHA; process.stdout.write(`Using provided fallback SHA: ${fallbackSHA}\n`); } else { // Check if HEAD~1 exists, and if not, set BASE_SHA to the empty tree hash - const LAST_COMMIT_CMD = `origin/${mainBranchName}~1`; + const LAST_COMMIT_CMD = `${remote}/${mainBranchName}~1`; const baseRes = (0, child_process_1.spawnSync)('git', ['rev-parse', LAST_COMMIT_CMD], { encoding: 'utf-8', }); @@ -30009,7 +30010,7 @@ let BASE_SHA; process.stdout.write(`HEAD~1 does not exist. We are therefore defaulting to use the empty git tree hash as BASE.\n`); } else { - process.stdout.write(`We are therefore defaulting to use HEAD~1 on 'origin/${mainBranchName}'\n`); + process.stdout.write(`We are therefore defaulting to use HEAD~1 on '${remote}/${mainBranchName}'\n`); BASE_SHA = baseRes.stdout; } process.stdout.write('\n'); @@ -30021,7 +30022,7 @@ let BASE_SHA; } else { process.stdout.write('\n'); - process.stdout.write(`Found the last successful workflow run on 'origin/${mainBranchName}'\n`); + process.stdout.write(`Found the last successful workflow run on '${remote}/${mainBranchName}'\n`); process.stdout.write(`Commit: ${BASE_SHA}\n`); } } @@ -30046,10 +30047,10 @@ let BASE_SHA; }))(); function reportFailure(branchName) { core.setFailed(` - Unable to find a successful workflow run on 'origin/${branchName}' + Unable to find a successful workflow run on '${remote}/${branchName}' NOTE: You have set 'error-on-no-successful-workflow' on the action so this is a hard error. - Is it possible that you have no runs currently on 'origin/${branchName}'? + Is it possible that you have no runs currently on '${remote}/${branchName}'? - If yes, then you should run the workflow without this flag first. - If no, then you might have changed your git history and those commits no longer exist.`); } diff --git a/find-successful-workflow.ts b/find-successful-workflow.ts index 1be537b..3a09e33 100644 --- a/find-successful-workflow.ts +++ b/find-successful-workflow.ts @@ -18,6 +18,7 @@ const lastSuccessfulEvent = core.getInput('last-successful-event'); const workingDirectory = core.getInput('working-directory'); const workflowId = core.getInput('workflow-id'); const fallbackSHA = core.getInput('fallback-sha'); +const remote = core.getInput('remote'); const defaultWorkingDirectory = '.'; let BASE_SHA: string; @@ -46,7 +47,7 @@ let BASE_SHA: string; 'git', [ 'merge-base', - `origin/${github.context.payload[eventName].base.ref}`, + `${remote}/${github.context.payload[eventName].base.ref}`, 'HEAD', ], { encoding: 'utf-8' }, @@ -79,14 +80,14 @@ let BASE_SHA: string; } else { process.stdout.write('\n'); process.stdout.write( - `WARNING: Unable to find a successful workflow run on 'origin/${mainBranchName}', or the latest successful workflow was connected to a commit which no longer exists on that branch (e.g. if that branch was rebased)\n`, + `WARNING: Unable to find a successful workflow run on '${remote}/${mainBranchName}', or the latest successful workflow was connected to a commit which no longer exists on that branch (e.g. if that branch was rebased)\n`, ); if (fallbackSHA) { BASE_SHA = fallbackSHA; process.stdout.write(`Using provided fallback SHA: ${fallbackSHA}\n`); } else { // Check if HEAD~1 exists, and if not, set BASE_SHA to the empty tree hash - const LAST_COMMIT_CMD = `origin/${mainBranchName}~1`; + const LAST_COMMIT_CMD = `${remote}/${mainBranchName}~1`; const baseRes = spawnSync('git', ['rev-parse', LAST_COMMIT_CMD], { encoding: 'utf-8', @@ -108,7 +109,7 @@ let BASE_SHA: string; ); } else { process.stdout.write( - `We are therefore defaulting to use HEAD~1 on 'origin/${mainBranchName}'\n`, + `We are therefore defaulting to use HEAD~1 on '${remote}/${mainBranchName}'\n`, ); BASE_SHA = baseRes.stdout; @@ -125,7 +126,7 @@ let BASE_SHA: string; } else { process.stdout.write('\n'); process.stdout.write( - `Found the last successful workflow run on 'origin/${mainBranchName}'\n`, + `Found the last successful workflow run on '${remote}/${mainBranchName}'\n`, ); process.stdout.write(`Commit: ${BASE_SHA}\n`); } @@ -158,10 +159,10 @@ let BASE_SHA: string; function reportFailure(branchName: string): void { core.setFailed(` - Unable to find a successful workflow run on 'origin/${branchName}' + Unable to find a successful workflow run on '${remote}/${branchName}' NOTE: You have set 'error-on-no-successful-workflow' on the action so this is a hard error. - Is it possible that you have no runs currently on 'origin/${branchName}'? + Is it possible that you have no runs currently on '${remote}/${branchName}'? - If yes, then you should run the workflow without this flag first. - If no, then you might have changed your git history and those commits no longer exist.`); }