Skip to content

Commit

Permalink
fix: pipeline variables's value should be nullable (#1096)
Browse files Browse the repository at this point in the history
  • Loading branch information
ANGkeith committed Feb 20, 2024
1 parent eec6cb1 commit 975650c
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 32 deletions.
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);
});

0 comments on commit 975650c

Please sign in to comment.