Skip to content

Commit 5440f42

Browse files
committed
feat: implement an option to downcase TF_VAR_
1 parent c40bcbc commit 5440f42

File tree

5 files changed

+129
-9
lines changed

5 files changed

+129
-9
lines changed

README.md

+14-1
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,22 @@ Export GitHub Actions secrets as environment variables.
44

55
## Usage
66

7-
```yml
7+
Set the JSON of secrets to `secrets`.
8+
9+
```yaml
10+
steps:
11+
- uses: koyashiro/[email protected]
12+
with:
13+
secrets: ${{ toJSON(secrets) }}
14+
```
15+
16+
If you want to downcase secrets starting with `TF_VAR_`, you can use the `downcase-tf-var` option.
17+
For example, a secret like `TF_VAR_EXAMPLE` will be exported as `TF_VAR_example`.
18+
19+
```yaml
820
steps:
921
- uses: koyashiro/[email protected]
1022
with:
1123
secrets: ${{ toJSON(secrets) }}
24+
downcase-tf-var: true
1225
```

action.yml

+3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ inputs:
55
secrets:
66
description: JSON representation of secrets
77
required: true
8+
downcase-tf-var:
9+
description: Set this option if you want to downcase the secrets starting with `TF_VAR_`
10+
required: false
811
runs:
912
using: node20
1013
main: dist/index.js

dist/index.js

+4
Original file line numberDiff line numberDiff line change
@@ -30847,11 +30847,15 @@ var z = /*#__PURE__*/Object.freeze({
3084730847
const secretsSchema = z.record(z.string(), z.string());
3084830848
function exportSecrets(core) {
3084930849
const secretsJson = core.getInput("secrets");
30850+
const downcaseTfVar = core.getInput("downcase-tf-var") === "true";
3085030851
if (!secretsJson) {
3085130852
throw new Error("secrets is required");
3085230853
}
3085330854
const secrets = secretsSchema.parse(JSON.parse(secretsJson));
3085430855
for (const [key, value] of Object.entries(secrets)) {
30856+
if (downcaseTfVar && key.startsWith("TF_VAR_")) {
30857+
core.exportVariable(`TF_VAR_${key.replace(/^TF_VAR_/, "").toLowerCase()}`, value);
30858+
}
3085530859
core.exportVariable(key, value);
3085630860
}
3085730861
}

src/lib.test.ts

+101-8
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,16 @@ import { exportSecrets } from "./lib";
55
describe("exportSecrets()", () => {
66
describe("success", () => {
77
const coreMock = {
8-
getInput: vi
9-
.fn()
10-
.mockReturnValue(
11-
'{"KEY_A":"VALUE_A","KEY_B":"VALUE_B","KEY_C":"VALUE_C"}',
12-
),
8+
getInput: vi.fn().mockImplementation((s: string) => {
9+
switch (s) {
10+
case "secrets":
11+
return '{"KEY_A":"VALUE_A","KEY_B":"VALUE_B","KEY_C":"VALUE_C","TF_VAR_KEY_D":"VALUE_D"}';
12+
case "downcase-tf-var":
13+
return "";
14+
default:
15+
return "";
16+
}
17+
}),
1318
exportVariable: vi.fn(),
1419
};
1520

@@ -23,7 +28,64 @@ describe("exportSecrets()", () => {
2328
expect(coreMock.getInput).toHaveBeenCalledWith("secrets");
2429
});
2530

26-
it("calls exportVariable() 3 times", () => {
31+
it('calls getInput() with "downcase-tf-var"', () => {
32+
expect(coreMock.getInput).toHaveBeenCalledWith("downcase-tf-var");
33+
});
34+
35+
it("calls exportVariable() 4 times", () => {
36+
expect(coreMock.exportVariable).toHaveBeenNthCalledWith(
37+
1,
38+
"KEY_A",
39+
"VALUE_A",
40+
);
41+
expect(coreMock.exportVariable).toHaveBeenNthCalledWith(
42+
2,
43+
"KEY_B",
44+
"VALUE_B",
45+
);
46+
expect(coreMock.exportVariable).toHaveBeenNthCalledWith(
47+
3,
48+
"KEY_C",
49+
"VALUE_C",
50+
);
51+
expect(coreMock.exportVariable).toHaveBeenNthCalledWith(
52+
4,
53+
"TF_VAR_KEY_D",
54+
"VALUE_D",
55+
);
56+
});
57+
});
58+
59+
describe("success_downcase-tf-var", () => {
60+
const coreMock = {
61+
getInput: vi.fn().mockImplementation((s: string) => {
62+
switch (s) {
63+
case "secrets":
64+
return '{"KEY_A":"VALUE_A","KEY_B":"VALUE_B","KEY_C":"VALUE_C","TF_VAR_KEY_D":"VALUE_D"}';
65+
case "downcase-tf-var":
66+
return "true";
67+
default:
68+
return "";
69+
}
70+
}),
71+
exportVariable: vi.fn(),
72+
};
73+
74+
it("does not throws an error", () => {
75+
expect(() => {
76+
exportSecrets(coreMock as unknown as typeof core);
77+
}).not.toThrow();
78+
});
79+
80+
it('calls getInput() with "secrets"', () => {
81+
expect(coreMock.getInput).toHaveBeenCalledWith("secrets");
82+
});
83+
84+
it('calls getInput() with "downcase-tf-var"', () => {
85+
expect(coreMock.getInput).toHaveBeenCalledWith("downcase-tf-var");
86+
});
87+
88+
it("calls exportVariable() 4 times", () => {
2789
expect(coreMock.exportVariable).toHaveBeenNthCalledWith(
2890
1,
2991
"KEY_A",
@@ -39,12 +101,26 @@ describe("exportSecrets()", () => {
39101
"KEY_C",
40102
"VALUE_C",
41103
);
104+
expect(coreMock.exportVariable).toHaveBeenNthCalledWith(
105+
4,
106+
"TF_VAR_key_d",
107+
"VALUE_D",
108+
);
42109
});
43110
});
44111

45112
describe("secrets is empty", () => {
46113
const coreMock = {
47-
getInput: vi.fn().mockReturnValue(""),
114+
getInput: vi.fn().mockImplementation((s: string) => {
115+
switch (s) {
116+
case "secrets":
117+
return "{";
118+
case "downcase-tf-var":
119+
return "";
120+
default:
121+
return "";
122+
}
123+
}),
48124
exportVariable: vi.fn(),
49125
};
50126

@@ -58,14 +134,27 @@ describe("exportSecrets()", () => {
58134
expect(coreMock.getInput).toHaveBeenCalledWith("secrets");
59135
});
60136

137+
it('calls getInput() with "downcase-tf-var"', () => {
138+
expect(coreMock.getInput).toHaveBeenCalledWith("downcase-tf-var");
139+
});
140+
61141
it("does not calls exportVariable()", () => {
62142
expect(coreMock.exportVariable).not.toHaveBeenCalled();
63143
});
64144
});
65145

66146
describe("secret is invalid JSON", () => {
67147
const coreMock = {
68-
getInput: vi.fn().mockReturnValue("{"),
148+
getInput: vi.fn().mockImplementation((s: string) => {
149+
switch (s) {
150+
case "secrets":
151+
return "{";
152+
case "downcase-tf-var":
153+
return "";
154+
default:
155+
return "";
156+
}
157+
}),
69158
exportVariable: vi.fn(),
70159
};
71160

@@ -79,6 +168,10 @@ describe("exportSecrets()", () => {
79168
expect(coreMock.getInput).toHaveBeenCalledWith("secrets");
80169
});
81170

171+
it('calls getInput() with "downcase-tf-var"', () => {
172+
expect(coreMock.getInput).toHaveBeenCalledWith("downcase-tf-var");
173+
});
174+
82175
it("does not calls exportVariable()", () => {
83176
expect(coreMock.exportVariable).not.toHaveBeenCalled();
84177
});

src/lib.ts

+7
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const secretsSchema = z.record(z.string(), z.string());
77

88
export function exportSecrets(core: ActionsCore) {
99
const secretsJson = core.getInput("secrets");
10+
const downcaseTfVar = core.getInput("downcase-tf-var") === "true";
1011

1112
if (!secretsJson) {
1213
throw new Error("secrets is required");
@@ -15,6 +16,12 @@ export function exportSecrets(core: ActionsCore) {
1516
const secrets = secretsSchema.parse(JSON.parse(secretsJson));
1617

1718
for (const [key, value] of Object.entries(secrets)) {
19+
if (downcaseTfVar && key.startsWith("TF_VAR_")) {
20+
core.exportVariable(
21+
`TF_VAR_${key.replace(/^TF_VAR_/, "").toLowerCase()}`,
22+
value,
23+
);
24+
}
1825
core.exportVariable(key, value);
1926
}
2027
}

0 commit comments

Comments
 (0)