Skip to content

Commit 3e5ded7

Browse files
authored
feat(job): Support defaults (#41)
1 parent 691f1eb commit 3e5ded7

File tree

2 files changed

+29
-10
lines changed

2 files changed

+29
-10
lines changed

lib/package/job.spec.ts

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,16 @@ import { Job } from "./job";
44
describe("Job", () => {
55
test.each<[Job, Record<string, unknown>]>([
66
[
7-
new Job("test", { runsOn: "ubuntu-latest" }).run("echo 'Hello, world!'"),
7+
new Job("simple-job", { runsOn: "ubuntu-latest" }).run(
8+
"echo 'Hello, world!'",
9+
),
810
{
911
"runs-on": "ubuntu-latest",
1012
steps: [{ run: "echo 'Hello, world!'" }],
1113
},
1214
],
1315
[
14-
new Job("test", {
16+
new Job("with-permissions", {
1517
runsOn: "ubuntu-latest",
1618
permissions: {
1719
contents: "read",
@@ -28,7 +30,7 @@ describe("Job", () => {
2830
},
2931
],
3032
[
31-
new Job("test", {
33+
new Job("with-timeout-minutes", {
3234
runsOn: "ubuntu-latest",
3335
timeoutMinutes: 10,
3436
}),
@@ -39,7 +41,7 @@ describe("Job", () => {
3941
},
4042
],
4143
[
42-
new Job("test", {
44+
new Job("with-timeout-minutes", {
4345
runsOn: "ubuntu-latest",
4446
timeoutMinutes: "${{ foo }}",
4547
}),
@@ -50,7 +52,7 @@ describe("Job", () => {
5052
},
5153
],
5254
[
53-
new Job("test", {
55+
new Job("with-outputs", {
5456
runsOn: "ubuntu-latest",
5557
outputs: {
5658
"output-name": "output-value",
@@ -63,7 +65,7 @@ describe("Job", () => {
6365
},
6466
],
6567
[
66-
new Job("test", {
68+
new Job("with-needs", {
6769
runsOn: "ubuntu-latest",
6870
needs: "job-name",
6971
}).run("echo 'Hello, world!'"),
@@ -74,7 +76,7 @@ describe("Job", () => {
7476
},
7577
],
7678
[
77-
new Job("test", {
79+
new Job("with-if", {
7880
runsOn: "ubuntu-latest",
7981
if: "always()",
8082
}).run("echo 'Hello, world!'"),
@@ -85,7 +87,7 @@ describe("Job", () => {
8587
},
8688
],
8789
[
88-
new Job("test", {
90+
new Job("with-concurrency", {
8991
runsOn: "ubuntu-latest",
9092
concurrency: "group-name",
9193
}).run("echo 'Hello, world!'"),
@@ -96,7 +98,7 @@ describe("Job", () => {
9698
},
9799
],
98100
[
99-
new Job("test", {
101+
new Job("with-concurrency", {
100102
runsOn: "ubuntu-latest",
101103
concurrency: { group: "group-name", cancelInProgress: true },
102104
}).run("echo 'Hello, world!'"),
@@ -106,6 +108,17 @@ describe("Job", () => {
106108
steps: [{ run: "echo 'Hello, world!'" }],
107109
},
108110
],
111+
[
112+
new Job("with-defaults", {
113+
runsOn: "ubuntu-latest",
114+
defaults: { run: { shell: "bash" } },
115+
}).run("echo 'Hello, world!'"),
116+
{
117+
"runs-on": "ubuntu-latest",
118+
defaults: { run: { shell: "bash" } },
119+
steps: [{ run: "echo 'Hello, world!'" }],
120+
},
121+
],
109122
])("job.toJSON(%j) -> %j", (job, expected) => {
110123
expect(job.toJSON()).toEqual(expected);
111124
});

lib/package/job.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import { concurrencyJSON } from "../internal/concurrency";
2+
import { defaultsJSON } from "../internal/defaults";
23
import { permissionsJSON } from "../internal/permissions";
34
import { stepJSON } from "../internal/step";
45
import { type Concurrency } from "./concurrency";
6+
import type { Defaults } from "./defaults";
57
import type { Expression } from "./expression";
68
import { type Permissions } from "./permissions";
79
import { type RunStep, type Step, type UsesStep } from "./step";
@@ -17,7 +19,7 @@ export type JobConfig = {
1719
concurrency?: Concurrency;
1820
// TODO: outputs
1921
// TODO: env
20-
// TODO: defaults
22+
defaults?: Defaults;
2123
// TODO: strategy
2224
// TODO: continue-on-error
2325
// TODO: container
@@ -87,6 +89,10 @@ export class Job {
8789
concurrency: concurrencyJSON(this._config.concurrency),
8890
}),
8991

92+
...(this._config.defaults != null && {
93+
defaults: defaultsJSON(this._config.defaults),
94+
}),
95+
9096
steps: this._steps.map((step) => stepJSON(step)),
9197
};
9298
}

0 commit comments

Comments
 (0)