diff --git a/.github/actions/checkout/README.md b/.github/actions/checkout/README.md index c5a17a87c3..534b514032 100644 --- a/.github/actions/checkout/README.md +++ b/.github/actions/checkout/README.md @@ -97,6 +97,11 @@ Please refer to the [release page](https://github.com/actions/checkout/releases/ # Default: false single-branch: '' + # Additional refs to fetch. Each ref should be separated with new lines. For + # example, to fetch all tags: `refs/tags/*:refs/tags/*` + # Default: refs/heads/main + additional-fetch-refs: '' + # Personal access token (PAT) used to fetch the repository. The PAT is configured # with the local git config, which enables your scripts to run authenticated git # commands. The post-job step removes the PAT. diff --git a/.github/actions/checkout/__test__/git-auth-helper.test.ts b/.github/actions/checkout/__test__/git-auth-helper.test.ts index 8753b76ebf..4ae739560f 100644 --- a/.github/actions/checkout/__test__/git-auth-helper.test.ts +++ b/.github/actions/checkout/__test__/git-auth-helper.test.ts @@ -1166,6 +1166,7 @@ async function setup(testName: string): Promise { submodulesFilter: undefined, persistCredentials: true, ref: 'refs/heads/main', + additionalFetchRefs: [], repositoryName: 'my-repo', repositoryOwner: 'my-org', repositoryPath: '', diff --git a/.github/actions/checkout/__test__/ref-helper.test.ts b/.github/actions/checkout/__test__/ref-helper.test.ts index 5c8d76b87e..f75cf26799 100644 --- a/.github/actions/checkout/__test__/ref-helper.test.ts +++ b/.github/actions/checkout/__test__/ref-helper.test.ts @@ -132,44 +132,44 @@ describe('ref-helper tests', () => { it('getRefSpec requires ref or commit', async () => { assert.throws( - () => refHelper.getRefSpec('', ''), + () => refHelper.getRefSpec('', '', []), /Args ref and commit cannot both be empty/ ) }) it('getRefSpec sha + refs/heads/', async () => { - const refSpec = refHelper.getRefSpec('refs/heads/my/branch', commit) + const refSpec = refHelper.getRefSpec('refs/heads/my/branch', commit, []) expect(refSpec.length).toBe(1) expect(refSpec[0]).toBe(`+${commit}:refs/remotes/origin/my/branch`) }) it('getRefSpec sha + refs/pull/', async () => { - const refSpec = refHelper.getRefSpec('refs/pull/123/merge', commit) + const refSpec = refHelper.getRefSpec('refs/pull/123/merge', commit, []) expect(refSpec.length).toBe(1) expect(refSpec[0]).toBe(`+${commit}:refs/remotes/pull/123/merge`) }) it('getRefSpec sha + refs/tags/', async () => { - const refSpec = refHelper.getRefSpec('refs/tags/my-tag', commit) + const refSpec = refHelper.getRefSpec('refs/tags/my-tag', commit, []) expect(refSpec.length).toBe(1) expect(refSpec[0]).toBe(`+${commit}:refs/tags/my-tag`) }) it('getRefSpec sha only', async () => { - const refSpec = refHelper.getRefSpec('', commit) + const refSpec = refHelper.getRefSpec('', commit, []) expect(refSpec.length).toBe(1) expect(refSpec[0]).toBe(commit) }) it('getRefSpec unqualified ref only', async () => { - const refSpec = refHelper.getRefSpec('my-ref', '') + const refSpec = refHelper.getRefSpec('my-ref', '', []) expect(refSpec.length).toBe(2) expect(refSpec[0]).toBe('+refs/heads/my-ref*:refs/remotes/origin/my-ref*') expect(refSpec[1]).toBe('+refs/tags/my-ref*:refs/tags/my-ref*') }) it('getRefSpec refs/heads/ only', async () => { - const refSpec = refHelper.getRefSpec('refs/heads/my/branch', '') + const refSpec = refHelper.getRefSpec('refs/heads/my/branch', '', []) expect(refSpec.length).toBe(1) expect(refSpec[0]).toBe( '+refs/heads/my/branch:refs/remotes/origin/my/branch' @@ -177,14 +177,48 @@ describe('ref-helper tests', () => { }) it('getRefSpec refs/pull/ only', async () => { - const refSpec = refHelper.getRefSpec('refs/pull/123/merge', '') + const refSpec = refHelper.getRefSpec('refs/pull/123/merge', '', []) expect(refSpec.length).toBe(1) expect(refSpec[0]).toBe('+refs/pull/123/merge:refs/remotes/pull/123/merge') }) it('getRefSpec refs/tags/ only', async () => { - const refSpec = refHelper.getRefSpec('refs/tags/my-tag', '') + const refSpec = refHelper.getRefSpec('refs/tags/my-tag', '', []) expect(refSpec.length).toBe(1) expect(refSpec[0]).toBe('+refs/tags/my-tag:refs/tags/my-tag') }) + + it('getRefSpec additional fetch refs unqualified', async () => { + const refSpec = refHelper.getRefSpec('refs/heads/main', '', [ + 'viable/strict' + ]) + expect(refSpec.length).toBe(3) + expect(refSpec[0]).toBe('+refs/heads/main:refs/remotes/origin/main') + expect(refSpec[1]).toBe( + '+refs/heads/viable/strict*:refs/remotes/origin/viable/strict*' + ) + expect(refSpec[2]).toBe( + '+refs/tags/viable/strict*:refs/tags/viable/strict*' + ) + }) + + it('getRefSpec additional fetch refs heads only', async () => { + const refSpec = refHelper.getRefSpec('refs/heads/main', '', [ + 'refs/heads/viable/strict' + ]) + expect(refSpec.length).toBe(2) + expect(refSpec[0]).toBe('+refs/heads/main:refs/remotes/origin/main') + expect(refSpec[1]).toBe( + '+refs/heads/viable/strict:refs/remotes/origin/viable/strict' + ) + }) + + it('getRefSpec additional fetch refs tags only', async () => { + const refSpec = refHelper.getRefSpec('refs/heads/main', '', [ + 'refs/tags/my-tag' + ]) + expect(refSpec.length).toBe(2) + expect(refSpec[0]).toBe('+refs/heads/main:refs/remotes/origin/main') + expect(refSpec[1]).toBe('+refs/tags/my-tag:refs/tags/my-tag') + }) }) diff --git a/.github/actions/checkout/action.yml b/.github/actions/checkout/action.yml index 95508dc083..e7e23dd44b 100644 --- a/.github/actions/checkout/action.yml +++ b/.github/actions/checkout/action.yml @@ -15,6 +15,11 @@ inputs: history leading to the specified ref is fetched. When false, the full history is fetched. default: "false" + additional-fetch-refs: + description: > + Additional refs to fetch. Each ref should be separated with new lines. + For example, to fetch all tags: `refs/tags/*:refs/tags/*` + default: refs/heads/main token: description: > Personal access token (PAT) used to fetch the repository. The PAT is configured diff --git a/.github/actions/checkout/dist/index.js b/.github/actions/checkout/dist/index.js index 2ee26cd361..a3242ad281 100644 --- a/.github/actions/checkout/dist/index.js +++ b/.github/actions/checkout/dist/index.js @@ -1525,20 +1525,20 @@ function getSource(settings) { } if (settings.fetchDepth <= 0) { let refSpec = settings.singleBranch - ? refHelper.getRefSpec(settings.ref, settings.commit) + ? refHelper.getRefSpec(settings.ref, settings.commit, settings.additionalFetchRefs) : refHelper.getRefSpecForAllHistory(settings.ref, settings.commit); yield git.fetch(refSpec, fetchOptions); // When all history is fetched, the ref we're interested in may have moved to a different // commit (push or force push). If so, fetch again with a targeted refspec. if (!(yield refHelper.testRef(git, settings.ref, settings.commit))) { - refSpec = refHelper.getRefSpec(settings.ref, settings.commit); + refSpec = refHelper.getRefSpec(settings.ref, settings.commit, settings.additionalFetchRefs); yield git.fetch(refSpec, fetchOptions); } } else { fetchOptions.fetchDepth = settings.fetchDepth; fetchOptions.fetchTags = settings.fetchTags; - const refSpec = refHelper.getRefSpec(settings.ref, settings.commit); + const refSpec = refHelper.getRefSpec(settings.ref, settings.commit, settings.additionalFetchRefs); yield git.fetch(refSpec, fetchOptions); } core.endGroup(); @@ -2013,6 +2013,10 @@ function getInputs() { result.singleBranch = (core.getInput('single-branch') || 'false').toUpperCase() === 'TRUE'; core.debug(`single branch = ${result.singleBranch}`); + // Additional fetch refs + result.additionalFetchRefs = + core.getMultilineInput('additional-fetch-refs') || []; + core.debug(`additional fetch refs = ${JSON.stringify(result.additionalFetchRefs)}`); // Clean result.clean = (core.getInput('clean') || 'true').toUpperCase() === 'TRUE'; core.debug(`clean = ${result.clean}`); @@ -2223,6 +2227,7 @@ Object.defineProperty(exports, "__esModule", ({ value: true })); exports.tagsRefSpec = void 0; exports.getCheckoutInfo = getCheckoutInfo; exports.getRefSpecForAllHistory = getRefSpecForAllHistory; +exports.getSingleRefSpec = getSingleRefSpec; exports.getRefSpec = getRefSpec; exports.testRef = testRef; exports.checkCommitInfo = checkCommitInfo; @@ -2287,7 +2292,7 @@ function getRefSpecForAllHistory(ref, commit) { } return result; } -function getRefSpec(ref, commit) { +function getSingleRefSpec(ref, commit) { if (!ref && !commit) { throw new Error('Args ref and commit cannot both be empty'); } @@ -2335,6 +2340,16 @@ function getRefSpec(ref, commit) { return [`+${ref}:${ref}`]; } } +function getRefSpec(ref, commit, additionalFetchRefs) { + const result = []; + const singleRefSpec = getSingleRefSpec(ref, commit); + result.push(...singleRefSpec); + for (const additionalRef of additionalFetchRefs) { + const additionalRefSpec = getSingleRefSpec(additionalRef, ''); + result.push(...additionalRefSpec); + } + return result; +} /** * Tests whether the initial fetch created the ref at the expected commit */ diff --git a/.github/actions/checkout/src/git-source-provider.ts b/.github/actions/checkout/src/git-source-provider.ts index e17bc2c4a1..6e56bfe68f 100644 --- a/.github/actions/checkout/src/git-source-provider.ts +++ b/.github/actions/checkout/src/git-source-provider.ts @@ -171,20 +171,32 @@ export async function getSource(settings: IGitSourceSettings): Promise { if (settings.fetchDepth <= 0) { let refSpec: string[] = settings.singleBranch - ? refHelper.getRefSpec(settings.ref, settings.commit) + ? refHelper.getRefSpec( + settings.ref, + settings.commit, + settings.additionalFetchRefs + ) : refHelper.getRefSpecForAllHistory(settings.ref, settings.commit) await git.fetch(refSpec, fetchOptions) // When all history is fetched, the ref we're interested in may have moved to a different // commit (push or force push). If so, fetch again with a targeted refspec. if (!(await refHelper.testRef(git, settings.ref, settings.commit))) { - refSpec = refHelper.getRefSpec(settings.ref, settings.commit) + refSpec = refHelper.getRefSpec( + settings.ref, + settings.commit, + settings.additionalFetchRefs + ) await git.fetch(refSpec, fetchOptions) } } else { fetchOptions.fetchDepth = settings.fetchDepth fetchOptions.fetchTags = settings.fetchTags - const refSpec = refHelper.getRefSpec(settings.ref, settings.commit) + const refSpec = refHelper.getRefSpec( + settings.ref, + settings.commit, + settings.additionalFetchRefs + ) await git.fetch(refSpec, fetchOptions) } core.endGroup() diff --git a/.github/actions/checkout/src/git-source-settings.ts b/.github/actions/checkout/src/git-source-settings.ts index 056e54e163..3e51a17e47 100644 --- a/.github/actions/checkout/src/git-source-settings.ts +++ b/.github/actions/checkout/src/git-source-settings.ts @@ -29,6 +29,11 @@ export interface IGitSourceSettings { */ singleBranch: boolean + /** + * Additional fetch refs + */ + additionalFetchRefs: string[] + /** * Indicates whether to clean the repository */ diff --git a/.github/actions/checkout/src/input-helper.ts b/.github/actions/checkout/src/input-helper.ts index e545710b86..3896bd6275 100644 --- a/.github/actions/checkout/src/input-helper.ts +++ b/.github/actions/checkout/src/input-helper.ts @@ -83,6 +83,13 @@ export async function getInputs(): Promise { (core.getInput('single-branch') || 'false').toUpperCase() === 'TRUE' core.debug(`single branch = ${result.singleBranch}`) + // Additional fetch refs + result.additionalFetchRefs = + core.getMultilineInput('additional-fetch-refs') || [] + core.debug( + `additional fetch refs = ${JSON.stringify(result.additionalFetchRefs)}` + ) + // Clean result.clean = (core.getInput('clean') || 'true').toUpperCase() === 'TRUE' core.debug(`clean = ${result.clean}`) diff --git a/.github/actions/checkout/src/ref-helper.ts b/.github/actions/checkout/src/ref-helper.ts index 58f929098b..f3a97071ee 100644 --- a/.github/actions/checkout/src/ref-helper.ts +++ b/.github/actions/checkout/src/ref-helper.ts @@ -76,7 +76,7 @@ export function getRefSpecForAllHistory(ref: string, commit: string): string[] { return result } -export function getRefSpec(ref: string, commit: string): string[] { +export function getSingleRefSpec(ref: string, commit: string): string[] { if (!ref && !commit) { throw new Error('Args ref and commit cannot both be empty') } @@ -127,6 +127,21 @@ export function getRefSpec(ref: string, commit: string): string[] { } } +export function getRefSpec( + ref: string, + commit: string, + additionalFetchRefs: string[] +): string[] { + const result: string[] = [] + const singleRefSpec: string[] = getSingleRefSpec(ref, commit) + result.push(...singleRefSpec) + for (const additionalRef of additionalFetchRefs) { + const additionalRefSpec = getSingleRefSpec(additionalRef, '') + result.push(...additionalRefSpec) + } + return result +} + /** * Tests whether the initial fetch created the ref at the expected commit */ diff --git a/.github/workflows/checkout-test.yml b/.github/workflows/checkout-test.yml index b756b7a114..bbb283a073 100644 --- a/.github/workflows/checkout-test.yml +++ b/.github/workflows/checkout-test.yml @@ -106,6 +106,14 @@ jobs: - name: Verify fetch filter run: __test__/verify-fetch-filter.sh + # Additional fetch refs + - name: Additional fetch refs + uses: ./.github/actions/checkout/ + with: + path: .github/actions/checkout/additional-fetch-refs + additional-fetch-refs: | + refs/heads/main + # Sparse checkout - name: Sparse checkout uses: ./.github/actions/checkout/