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: pipeline variables's value should be nullable #1096

Merged
merged 1 commit into from
Feb 20, 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
4 changes: 3 additions & 1 deletion src/data-expander.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,9 @@ export function defaults (gitlabData: any) {

export function globalVariables (gitlabData: any) {
for (const [key, value] of Object.entries<any>(gitlabData.variables ?? {})) {
if (Utils.isObject(value)) {
if (value === null) {
gitlabData.variables[key] = ""; // variable's values are nullable
} else if (Utils.isObject(value)) {
gitlabData.variables[key] = value["value"];
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,9 @@ export class Parser {

// Check job variables for invalid hash of key value pairs, and cast numbers to strings
Utils.forEachRealJob(gitlabData, (jobName, jobData) => {
for (const [key, value] of Object.entries(jobData.variables || {})) {
for (const [key, _value] of Object.entries(jobData.variables || {})) {
let value = _value;
if (value === null) value = ""; // variable's values are nullable
assert(
typeof value === "string" || typeof value === "number",
chalk`{blueBright ${jobName}} has invalid variables hash of key value pairs. ${key}=${value}`
Expand Down
6 changes: 0 additions & 6 deletions tests/test-cases/invalid-variables-null/.gitlab-ci.yml

This file was deleted.

This file was deleted.

9 changes: 9 additions & 0 deletions tests/test-cases/variables-null/.gitlab-ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
variables:
EMPTY_GLOBAL_VAR:
test-job:
variables:
EMPTY_JOB_VAR:
script:
- echo "EMPTY_GLOBAL_VAR:${EMPTY_GLOBAL_VAR}"
- echo "EMPTY_JOB_VAR:${EMPTY_JOB_VAR}"
26 changes: 26 additions & 0 deletions tests/test-cases/variables-null/integration.variables-null.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import {WriteStreamsMock} from "../../../src/write-streams";
import {handler} from "../../../src/handler";
import chalk from "chalk";
import {initSpawnSpy} from "../../mocks/utils.mock";
import {WhenStatics} from "../../mocks/when-statics";

beforeAll(() => {
initSpawnSpy(WhenStatics.all);
});

test("variable-null <test-job>", async () => {
const writeStreams = new WriteStreamsMock();
await handler({
cwd: "tests/test-cases/variables-null",
job: ["test-job"],
}, writeStreams);

const expected = [
chalk`{blueBright test-job} {green $ echo \"EMPTY_GLOBAL_VAR:\${EMPTY_GLOBAL_VAR\}\"}`,
chalk`{blueBright test-job} {greenBright >} EMPTY_GLOBAL_VAR:`,
chalk`{blueBright test-job} {green $ echo \"EMPTY_JOB_VAR:\${EMPTY_JOB_VAR\}\"}`,
chalk`{blueBright test-job} {greenBright >} EMPTY_JOB_VAR:`,
];

expect(writeStreams.stdoutLines.slice(1, -3)).toEqual(expected);
});