Skip to content

Commit 00274d9

Browse files
committed
add missing optional prop
1 parent 2c0a5ba commit 00274d9

File tree

5 files changed

+212
-0
lines changed

5 files changed

+212
-0
lines changed

.changeset/tough-rings-laugh.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@noxify/gitlab-ci-builder": patch
3+
---
4+
5+
add missing `optional` prop for `needs:`

src/types/needs.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,20 @@ type NeedsDefinition =
99
* @see https://docs.gitlab.com/ee/ci/yaml/#artifact-downloads-with-needs
1010
*/
1111
artifacts?: boolean
12+
/**
13+
* @see https://docs.gitlab.com/ee/ci/yaml/#needsoptional
14+
*/
15+
optional?: boolean
1216
}[]
1317
| {
1418
/**
1519
* @see https://docs.gitlab.com/ee/ci/yaml/#complex-trigger-syntax-for-multi-project-pipelines
1620
*/
1721
pipeline: string
22+
/**
23+
* @see https://docs.gitlab.com/ee/ci/yaml/#needsoptional
24+
*/
25+
optional?: boolean
1826
}
1927

2028
export type { NeedsDefinition }

tests/export.test.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,5 +251,35 @@ describe("export", () => {
251251
"Write failed",
252252
)
253253
})
254+
255+
it("should export needs with optional property", () => {
256+
const config = new Config()
257+
config.job("generate_version", {
258+
script: ["echo version"],
259+
})
260+
config.job("unit_tests", {
261+
script: ["npm test"],
262+
})
263+
config.job("deploy", {
264+
script: ["echo deploying"],
265+
needs: [
266+
{
267+
job: "generate_version",
268+
optional: true,
269+
},
270+
{
271+
job: "unit_tests",
272+
},
273+
],
274+
})
275+
276+
const yaml = toYaml(config.getPlainObject())
277+
278+
expect(yaml).toContain("deploy:")
279+
expect(yaml).toContain("needs:")
280+
expect(yaml).toContain("- job: generate_version")
281+
expect(yaml).toContain("optional: true")
282+
expect(yaml).toContain("- job: unit_tests")
283+
})
254284
})
255285
})

tests/import.test.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,5 +398,33 @@ build:
398398
expect(ts).toContain('config.job("build",')
399399
expect(ts).toContain('extends: [".base1", ".base2"]')
400400
})
401+
402+
it("should handle needs with optional property", () => {
403+
const yaml = `
404+
generate_version:
405+
script:
406+
- echo version
407+
408+
unit_tests:
409+
script:
410+
- npm test
411+
412+
deploy:
413+
script:
414+
- echo deploying
415+
needs:
416+
- job: generate_version
417+
optional: true
418+
- job: unit_tests
419+
`
420+
421+
const ts = fromYaml(yaml)
422+
423+
expect(ts).toContain('config.job("deploy",')
424+
expect(ts).toContain("needs:")
425+
expect(ts).toContain('job: "generate_version"')
426+
expect(ts).toContain("optional: true")
427+
expect(ts).toContain('job: "unit_tests"')
428+
})
401429
})
402430
})

tests/needs.test.ts

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
import { describe, expect, it } from "vitest"
2+
3+
import { Config } from "../src"
4+
5+
describe("needs", () => {
6+
it("should support needs as simple string array", () => {
7+
const cfg = new Config()
8+
9+
cfg.job("build", {
10+
script: ["npm run build"],
11+
})
12+
13+
cfg.job("test", {
14+
script: ["npm test"],
15+
needs: ["build"],
16+
})
17+
18+
const plain = cfg.getPlainObject()
19+
20+
expect(plain.jobs?.test?.needs).toEqual(["build"])
21+
})
22+
23+
it("should support needs with job objects", () => {
24+
const cfg = new Config()
25+
26+
cfg.job("build", {
27+
script: ["npm run build"],
28+
})
29+
30+
cfg.job("test", {
31+
script: ["npm test"],
32+
needs: [
33+
{
34+
job: "build",
35+
},
36+
],
37+
})
38+
39+
const plain = cfg.getPlainObject()
40+
41+
expect(plain.jobs?.test?.needs).toEqual([{ job: "build" }])
42+
})
43+
44+
it("should support needs with artifacts property", () => {
45+
const cfg = new Config()
46+
47+
cfg.job("build", {
48+
script: ["npm run build"],
49+
})
50+
51+
cfg.job("test", {
52+
script: ["npm test"],
53+
needs: [
54+
{
55+
job: "build",
56+
artifacts: false,
57+
},
58+
],
59+
})
60+
61+
const plain = cfg.getPlainObject()
62+
63+
expect(plain.jobs?.test?.needs).toEqual([{ job: "build", artifacts: false }])
64+
})
65+
66+
it("should support needs with optional property", () => {
67+
const cfg = new Config()
68+
69+
cfg.job("generate_version", {
70+
script: ["echo version"],
71+
})
72+
73+
cfg.job("unit_tests", {
74+
script: ["npm test"],
75+
})
76+
77+
cfg.job("deploy", {
78+
script: ["echo deploying"],
79+
needs: [
80+
{
81+
job: "generate_version",
82+
optional: true,
83+
},
84+
{
85+
job: "unit_tests",
86+
},
87+
],
88+
})
89+
90+
const plain = cfg.getPlainObject()
91+
92+
expect(plain.jobs?.deploy?.needs).toEqual([
93+
{ job: "generate_version", optional: true },
94+
{ job: "unit_tests" },
95+
])
96+
})
97+
98+
it("should support needs with both optional and artifacts", () => {
99+
const cfg = new Config()
100+
101+
cfg.job("build", {
102+
script: ["npm run build"],
103+
})
104+
105+
cfg.job("deploy", {
106+
script: ["echo deploying"],
107+
needs: [
108+
{
109+
job: "build",
110+
artifacts: true,
111+
optional: true,
112+
},
113+
],
114+
})
115+
116+
const plain = cfg.getPlainObject()
117+
118+
expect(plain.jobs?.deploy?.needs).toEqual([
119+
{ job: "build", artifacts: true, optional: true },
120+
])
121+
})
122+
123+
it("should support pipeline needs with optional", () => {
124+
const cfg = new Config()
125+
126+
cfg.job("deploy", {
127+
script: ["echo deploying"],
128+
needs: {
129+
pipeline: "other-project/pipeline",
130+
optional: true,
131+
},
132+
})
133+
134+
const plain = cfg.getPlainObject()
135+
136+
expect(plain.jobs?.deploy?.needs).toEqual({
137+
pipeline: "other-project/pipeline",
138+
optional: true,
139+
})
140+
})
141+
})

0 commit comments

Comments
 (0)