Skip to content

Commit a5bb1a3

Browse files
authored
fix: support including by commit sha over http (#1910)
1 parent 349e7cc commit a5bb1a3

3 files changed

Lines changed: 32 additions & 5 deletions

File tree

src/parser-includes.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ export class ParserIncludes {
301301
if (this._cache.sha === undefined) {
302302
if (this.isLocal) {
303303
this._cache.sha = this.gitData.commit.SHA;
304-
} else if (/^[0-9a-f]{40}$/.test(this.effectiveRef)) {
304+
} else if (/^[0-9a-f]{40}$/i.test(this.effectiveRef)) {
305305
// effectiveRef may already be a sha, if so return it directly
306306
this._cache.sha = this.effectiveRef;
307307
} else {
@@ -377,13 +377,15 @@ export class ParserIncludes {
377377
await fs.mkdirp(path.dirname(`${cwd}/${target}/${normalizedFile}`));
378378
tmpDir = `${cwd}/${target}.${ext}`;
379379

380-
const gitCloneBranch = (ref === "HEAD") ? "" : `--branch ${ref}`;
380+
const isCommitSha = /^[0-9a-f]{40}$/i.test(ref);
381+
const gitCloneBranch = (ref === "HEAD" || isCommitSha) ? "" : `--branch ${ref}`;
381382
await Utils.bashMulti([
382383
`cd ${cwd}/${stateDir}`,
383384
`git clone ${gitCloneBranch} -n --depth=1 --filter=tree:0 ${remote.schema}://${remote.host}:${remote.port}/${project}.git ${tmpDir}`,
384385
`cd ${tmpDir}`,
386+
...isCommitSha ? [`git fetch --depth=1 --filter=tree:0 origin ${ref}`] : [],
385387
`git sparse-checkout set --no-cone ${normalizedFile}`,
386-
"git checkout",
388+
isCommitSha ? "git checkout FETCH_HEAD" : "git checkout",
387389
`cd ${cwd}/${stateDir}`,
388390
`cp ${tmpDir}/${normalizedFile} ${cwd}/${target}/${normalizedFile}`,
389391
], cwd);
@@ -418,13 +420,15 @@ export class ParserIncludes {
418420
await fs.mkdirp(path.dirname(`${cwd}/${target}/templates`));
419421
tmpDir = `${cwd}/${target}.${ext}`;
420422

421-
const gitCloneBranch = (ref === "HEAD") ? "" : `--branch ${ref}`;
423+
const isCommitSha = /^[0-9a-f]{40}$/i.test(ref);
424+
const gitCloneBranch = (ref === "HEAD" || isCommitSha) ? "" : `--branch ${ref}`;
422425
await Utils.bashMulti([
423426
`cd ${cwd}/${stateDir}`,
424427
`git clone ${gitCloneBranch} -n --depth=1 --filter=tree:0 ${remote.schema}://${remote.host}:${remote.port}/${project}.git ${tmpDir}`,
425428
`cd ${tmpDir}`,
429+
...isCommitSha ? [`git fetch --depth=1 --filter=tree:0 origin ${ref}`] : [],
426430
`git sparse-checkout set --no-cone ${files[0]} ${files[1]}`,
427-
"git checkout",
431+
isCommitSha ? "git checkout FETCH_HEAD" : "git checkout",
428432
`cd ${cwd}/${stateDir}`,
429433
`mkdir -p ${tmpDir}/templates`, // create templates subdir (if it doesn't exist), as the check out may not create it
430434
`cp -r ${tmpDir}/templates ${cwd}/${target}`,
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
include:
3+
# https://gitlab.com/ANGkeith/gitlab-ci-local-test/-/tree/dev?ref_type=heads
4+
- project: angkeith/gitlab-ci-local-test
5+
ref: 126db0b30c40344e135a0335fdfd3d5f26af3b6d
6+
file:
7+
- .gitlab-ci.yml

tests/test-cases/include-project-file/integration.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,19 @@ test.concurrent("include:project should respect rules specified in included proj
6666

6767
expect(writeStreams.stdoutLines.join("\n")).toEqual(expected.join("\n"));
6868
});
69+
70+
test.concurrent("include:project be able to target a commit sha via ref", async () => {
71+
const writeStreams = new WriteStreamsMock();
72+
73+
await handler({
74+
file: ".gitlab-ci-4.yml",
75+
cwd: "tests/test-cases/include-project-file",
76+
noColor: true,
77+
stateDir: ".gitlab-ci-local-include-project-file-sha",
78+
}, writeStreams);
79+
80+
const expected = "job > hello world from dev branch";
81+
82+
const filteredStdout = writeStreams.stdoutLines.filter(f => f.startsWith("job >")).join("\n");
83+
expect(filteredStdout).toEqual(expected);
84+
});

0 commit comments

Comments
 (0)