Skip to content

Commit 00c27ce

Browse files
authored
feat(workflow): Support defaults (#39)
* chore: Define Defaults type * chore: Implement defaultsJSON * feat(workflow): Support defaults
1 parent 0db25c2 commit 00c27ce

File tree

6 files changed

+88
-13
lines changed

6 files changed

+88
-13
lines changed

lib/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
export * from "ghats/../.ghats/action.js";
22
export * from "./package/concurrency";
3+
export * from "./package/defaults";
34
export * from "./package/expression";
45
export * from "./package/job";
56
export * from "./package/on";

lib/internal/defaults.spec.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { describe, expect, test } from "bun:test";
2+
import type { Defaults } from "../package/defaults";
3+
import { defaultsJSON } from "./defaults";
4+
5+
describe("defaultsJSON", () => {
6+
test.each<[Defaults, ReturnType<typeof defaultsJSON>]>([
7+
[{}, {}],
8+
[{ run: {} }, { run: {} }],
9+
[{ run: { shell: "bash" } }, { run: { shell: "bash" } }],
10+
[
11+
{ run: { workingDirectory: "src" } },
12+
{ run: { "working-directory": "src" } },
13+
],
14+
[
15+
{ run: { shell: "bash", workingDirectory: "src" } },
16+
{ run: { shell: "bash", "working-directory": "src" } },
17+
],
18+
])("defaultsJSON(%j) -> %j", (defaults, expected) => {
19+
expect(defaultsJSON(defaults)).toEqual(expected);
20+
});
21+
});

lib/internal/defaults.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import type { Defaults } from "../package/defaults";
2+
3+
export function defaultsJSON(defaults: Defaults): Record<string, unknown> {
4+
return {
5+
...(defaults.run != null && {
6+
run: {
7+
...(defaults.run.shell != null && {
8+
shell: defaults.run.shell,
9+
}),
10+
...(defaults.run.workingDirectory != null && {
11+
"working-directory": defaults.run.workingDirectory,
12+
}),
13+
},
14+
}),
15+
};
16+
}

lib/package/defaults.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import type { Shell } from "./shell";
2+
3+
export type Defaults = {
4+
run?: {
5+
shell?: Shell;
6+
workingDirectory?: string;
7+
};
8+
};

lib/package/workflow.spec.ts

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,33 @@ describe("Workflow", () => {
99
{ name: "simple", on: "push", jobs: {} },
1010
],
1111
[
12-
new Workflow("simple", { runName: "Simple Workflow", on: "push" }),
13-
{ name: "simple", "run-name": "Simple Workflow", on: "push", jobs: {} },
12+
new Workflow("with run-name", { runName: "Simple Workflow", on: "push" }),
13+
{
14+
name: "with run-name",
15+
"run-name": "Simple Workflow",
16+
on: "push",
17+
jobs: {},
18+
},
1419
],
1520
[
16-
new Workflow("simple", { on: "push", permissions: { contents: "read" } }),
21+
new Workflow("with permissions", {
22+
on: "push",
23+
permissions: { contents: "read" },
24+
}),
1725
{
18-
name: "simple",
26+
name: "with permissions",
1927
on: "push",
2028
permissions: { contents: "read" },
2129
jobs: {},
2230
},
2331
],
2432
[
25-
new Workflow("simple", { on: "push", env: { FOO: "foo", BAR: "bar" } }),
33+
new Workflow("with env", {
34+
on: "push",
35+
env: { FOO: "foo", BAR: "bar" },
36+
}),
2637
{
27-
name: "simple",
38+
name: "with env",
2839
on: "push",
2940
env: {
3041
FOO: "foo",
@@ -34,12 +45,12 @@ describe("Workflow", () => {
3445
},
3546
],
3647
[
37-
new Workflow("simple", {
48+
new Workflow("with concurrency", {
3849
on: "push",
3950
concurrency: { group: "group", cancelInProgress: true },
4051
}),
4152
{
42-
name: "simple",
53+
name: "with concurrency",
4354
on: "push",
4455
concurrency: {
4556
group: "group",
@@ -48,21 +59,33 @@ describe("Workflow", () => {
4859
jobs: {},
4960
},
5061
],
62+
[
63+
new Workflow("with defaults", {
64+
on: "push",
65+
defaults: { run: { shell: "bash" } },
66+
}),
67+
{
68+
name: "with defaults",
69+
on: "push",
70+
defaults: { run: { shell: "bash" } },
71+
jobs: {},
72+
},
73+
],
5174
[
5275
(() => {
53-
const workflow = new Workflow("simple", { on: "push" });
76+
const workflow = new Workflow("with job", { on: "push" });
5477
workflow.addJob(
55-
new Job("test", {
78+
new Job("simple-job", {
5679
runsOn: "ubuntu-latest",
5780
}).run("echo 'Hello, world!'"),
5881
);
5982
return workflow;
6083
})(),
6184
{
62-
name: "simple",
85+
name: "with job",
6386
on: "push",
6487
jobs: {
65-
test: {
88+
"simple-job": {
6689
"runs-on": "ubuntu-latest",
6790
steps: [{ run: "echo 'Hello, world!'" }],
6891
},

lib/package/workflow.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import { concurrencyJSON } from "../internal/concurrency";
2+
import { defaultsJSON } from "../internal/defaults";
23
import { envJSON } from "../internal/env";
34
import { onJSON } from "../internal/on";
45
import { permissionsJSON } from "../internal/permissions";
56
import { type Concurrency } from "./concurrency";
7+
import type { Defaults } from "./defaults";
68
import { type Env } from "./env";
79
import { Job } from "./job";
810
import { type On } from "./on";
@@ -14,7 +16,7 @@ export type WorkflowConfig = {
1416
on: On;
1517
permissions?: Permissions;
1618
env?: Env;
17-
// TODO: defaults
19+
defaults?: Defaults;
1820
};
1921

2022
export class Workflow {
@@ -50,6 +52,10 @@ export class Workflow {
5052
concurrency: concurrencyJSON(this._config.concurrency),
5153
}),
5254

55+
...(this._config.defaults && {
56+
defaults: defaultsJSON(this._config.defaults),
57+
}),
58+
5359
jobs: this._jobs.reduce<Record<string, unknown>>((acc, job) => {
5460
acc[job.id] = job.toJSON();
5561
return acc;

0 commit comments

Comments
 (0)