Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add option to set custom remote #183

Merged
merged 1 commit into from
Mar 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand Down
3 changes: 3 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
15 changes: 8 additions & 7 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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* () {
Expand All @@ -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;
Expand All @@ -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',
});
Expand All @@ -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');
Expand All @@ -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`);
}
}
Expand All @@ -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.`);
}
Expand Down
15 changes: 8 additions & 7 deletions find-successful-workflow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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' },
Expand Down Expand Up @@ -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',
Expand All @@ -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;
Expand All @@ -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`);
}
Expand Down Expand Up @@ -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.`);
}
Expand Down