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

fix: masked exit code in bash #1192

Merged
merged 3 commits into from
Apr 21, 2024
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
23 changes: 12 additions & 11 deletions src/parser-includes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import chalk from "chalk";
import {Parser} from "./parser";
import axios from "axios";
import globby from "globby";
import path from "path";

type ParserIncludesInitOptions = {
cwd: string;
Expand Down Expand Up @@ -184,22 +185,22 @@ export class ParserIncludes {

if (remote.schema.startsWith("http")) {
const ext = "tmp-" + Math.random();
await fs.ensureFile(`${cwd}/${target}/${normalizedFile}`);
await fs.mkdirp(path.dirname(`${cwd}/${target}/${normalizedFile}`));
await Utils.bash(`
cd ${cwd}/${stateDir} \
&& git clone -n --depth=1 --filter=tree:0 \
${remote.schema}://${remote.host}/${project}.git \
${cwd}/${target}.${ext} \
&& cd ${cwd}/${target}.${ext} \
&& git sparse-checkout set --no-cone ${normalizedFile} \
&& git checkout \
&& cd ${cwd}/${stateDir} \
&& cp ${cwd}/${target}.${ext}/${normalizedFile}\
cd ${cwd}/${stateDir} \\
&& git clone -n --depth=1 --filter=tree:0 \\
${remote.schema}://${remote.host}/${project}.git \\
${cwd}/${target}.${ext} \\
&& cd ${cwd}/${target}.${ext} \\
&& git sparse-checkout set --no-cone ${normalizedFile} \\
&& git checkout \\
&& cd ${cwd}/${stateDir} \\
&& cp ${cwd}/${target}.${ext}/${normalizedFile} \\
${cwd}/${target}/${normalizedFile}
`, cwd);
} else {
await fs.mkdirp(`${cwd}/${target}`);
await Utils.bash(`git archive --remote=ssh://git@${remote.host}:${remote.port}/${project}.git ${ref} ${normalizedFile} | tar -f - -xC ${target}/`, cwd);
await Utils.bash(`set -eou pipefail; git archive --remote=ssh://git@${remote.host}:${remote.port}/${project}.git ${ref} ${normalizedFile} | tar -f - -xC ${target}/`, cwd);
}
} catch (e) {
throw new AssertionError({message: `Project include could not be fetched { project: ${project}, ref: ${ref}, file: ${normalizedFile} }\n${e}`});
Expand Down
2 changes: 1 addition & 1 deletion src/variables-from-files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export class VariablesFromFiles {
const url = match.groups?.url;
const file = match.groups?.file;
const ref = match.groups?.ref;
const res = await Utils.bash(`git archive --remote=${url} ${ref} ${file} | tar -xO ${file}`, cwd);
const res = await Utils.bash(`set -eou pipefail; git archive --remote=${url} ${ref} ${file} | tar -xO ${file}`, cwd);
remoteFileData = yaml.load(`${res.stdout}`);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
import {WriteStreamsMock} from "../../../src/write-streams";
import {handler} from "../../../src/handler";
import assert, {AssertionError} from "assert";
import {initBashSpyReject, initSpawnSpy} from "../../mocks/utils.mock";
import {initSpawnSpy} from "../../mocks/utils.mock";
import {WhenStatics} from "../../mocks/when-statics";

test("include-invalid-project-file-ref", async () => {
try {

const target = ".gitlab-ci-local/includes/gitlab.com/firecow/gitlab-ci-local-includes/HEAD/";
const spyGitArchive = {
cmd: `git archive --remote=ssh://git@gitlab.com:22/firecow/gitlab-ci-local-includes.git HEAD .gitlab-modue.yml | tar -f - -xC ${target}`,
rejection: "git archive | tar -f - -xC failed horribly\n this is a core error\n read it carefully",
};
initBashSpyReject([spyGitArchive]);
const spyGitRemote = {
cmdArgs: ["git", "remote", "-v"],
returnValue: {stdout: "origin\tgit@gitlab.com:gcl/test-hest.git (fetch)\norigin\tgit@gitlab.com:gcl/test-hest.git (push)\n"},
Expand All @@ -29,10 +22,7 @@ test("include-invalid-project-file-ref", async () => {

const msg = [
"Project include could not be fetched { project: firecow/gitlab-ci-local-includes, ref: HEAD, file: .gitlab-modue.yml }",
"Error: git archive | tar -f - -xC failed horribly",
" this is a core error",
" read it carefully",
];
expect(e.message).toBe(msg.join("\n"));
expect(e.message).toContain(msg.join("\n"));
}
});
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {WhenStatics} from "../../mocks/when-statics";

beforeAll(() => {
const spyGitArchive = {
cmd: "git archive --remote=git@gitlab.com:example/firecow.git main variables.yml | tar -xO variables.yml",
cmd: "set -eou pipefail; git archive --remote=git@gitlab.com:example/firecow.git main variables.yml | tar -xO variables.yml",
returnValue: {stdout: "---\nglobal:\n SOMEVARIABLE: very-special-value"},
};
initSpawnSpy([...WhenStatics.all]);
Expand Down