Skip to content

Commit 4d9e257

Browse files
authored
feat: allow options to be configured for hcl merge scaffolder actions (#73)
1 parent 93fdc95 commit 4d9e257

File tree

6 files changed

+623
-30
lines changed

6 files changed

+623
-30
lines changed

Diff for: .github/workflows/build.yml

+6-6
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ jobs:
1818
runs-on: ubuntu-latest
1919
strategy:
2020
matrix:
21-
node-version: ['>=20.8.0']
21+
node-version: [20.x]
2222
steps:
2323
- uses: actions/checkout@v3
2424
- name: Use Node.js ${{ matrix.node-version }}
@@ -34,7 +34,7 @@ jobs:
3434
runs-on: ubuntu-latest
3535
strategy:
3636
matrix:
37-
node-version: ['>=20.8.0']
37+
node-version: [20.x]
3838
steps:
3939
- uses: actions/checkout@v3
4040
- name: Use Node.js ${{ matrix.node-version }}
@@ -48,7 +48,7 @@ jobs:
4848
runs-on: ubuntu-latest
4949
strategy:
5050
matrix:
51-
node-version: ['>=20.8.0']
51+
node-version: [20.x]
5252
steps:
5353
- uses: actions/checkout@v3
5454
- name: Use Node.js ${{ matrix.node-version }}
@@ -65,7 +65,7 @@ jobs:
6565
- uses: actions/checkout@v3
6666
- uses: actions/setup-node@v3
6767
with:
68-
node-version: '>=20.8.0'
68+
node-version: '20.x'
6969
cache: 'yarn'
7070
# failures with peer dependencies will fail release
7171
- run: yarn install
@@ -81,10 +81,10 @@ jobs:
8181
- uses: actions/checkout@v3
8282
with:
8383
fetch-depth: 0
84-
- name: Use Node.js >=20.8.0
84+
- name: Use Node.js 20.x
8585
uses: actions/setup-node@v3
8686
with:
87-
node-version: '>=20.8.0'
87+
node-version: '20.x'
8888
cache: 'yarn'
8989
- run: yarn install
9090
- run: yarn tsc

Diff for: .github/workflows/release.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ jobs:
1818
- name: Set up Node.js
1919
uses: actions/setup-node@v3
2020
with:
21-
node-version: '>=20.8.0'
21+
node-version: '20.x'
2222
cache: 'yarn'
2323
- run: yarn install
2424
- run: yarn tsc

Diff for: plugins/scaffolder-backend-module-hcl/package.json

+2
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,10 @@
2929
"dependencies": {
3030
"@backstage/backend-common": "^0.24.1",
3131
"@backstage/backend-plugin-api": "^0.8.1",
32+
"@backstage/backend-test-utils": "^1.1.0",
3233
"@backstage/config": "^1.2.0",
3334
"@backstage/plugin-scaffolder-node": "^0.4.10",
35+
"@backstage/types": "^1.2.0",
3436
"@seatgeek/node-hcl": "^1.0.0",
3537
"fs-extra": "^11.2.0",
3638
"zod": "^3.23.8"

Diff for: plugins/scaffolder-backend-module-hcl/src/actions/hcl/hcl.test.ts

+77-5
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,22 @@
22
* Copyright SeatGeek
33
* Licensed under the terms of the Apache-2.0 license. See LICENSE file in project root for terms.
44
*/
5-
import { getVoidLogger } from '@backstage/backend-common';
5+
import { mockServices } from '@backstage/backend-test-utils';
66
import { randomBytes } from 'crypto';
77
import { writeFileSync } from 'fs-extra';
88
import { tmpdir } from 'os';
99
import { PassThrough } from 'stream';
1010
import { createHclMergeAction, createHclMergeFilesAction } from './hcl';
1111

12-
// Since we have to
1312
const TMP_DIR = tmpdir();
1413

14+
beforeEach(() => {
15+
jest.clearAllMocks();
16+
});
17+
1518
describe('createHclMergeAction', () => {
1619
const mockContext = {
17-
logger: getVoidLogger(),
20+
logger: mockServices.logger.mock(),
1821
logStream: new PassThrough(),
1922
output: jest.fn(),
2023
createTemporaryDirectory: jest.fn(),
@@ -29,18 +32,79 @@ describe('createHclMergeAction', () => {
2932
description = "Name to be used on all the resources as identifier"
3033
type = string
3134
default = ""
35+
36+
map = {
37+
foo = "bar"
38+
}
3239
}`;
3340

3441
const b = `
3542
variable "name" {
3643
type = string
3744
default = "my-name"
45+
46+
map = {
47+
bar = "baz"
48+
}
3849
}`;
3950

51+
// with mergeMapKeys set to false, the map will be overridden
4052
const expected = `variable "name" {
4153
default = "my-name"
4254
description = "Name to be used on all the resources as identifier"
43-
type = string
55+
map = {
56+
bar = "baz"
57+
}
58+
type = string
59+
}
60+
61+
`;
62+
63+
const mockCtx = {
64+
...mockContext,
65+
input: {
66+
aSourceContent: a,
67+
bSourceContent: b,
68+
options: {
69+
mergeMapKeys: false,
70+
},
71+
},
72+
};
73+
74+
// @ts-expect-error
75+
await createHclMergeAction().handler(mockCtx);
76+
77+
expect(mockCtx.output.mock.calls[0][0]).toEqual('hcl');
78+
expect(mockCtx.output.mock.calls[0][1]).toEqual(expected);
79+
});
80+
81+
it('should merge HCL files with mergeMapKeys set to true', async () => {
82+
const a = `
83+
variable "name" {
84+
type = string
85+
86+
map1 = {
87+
foo = "bar"
88+
}
89+
}`;
90+
91+
const b = `
92+
variable "name" {
93+
default = "my-name"
94+
95+
map1 = {
96+
bar = "baz"
97+
}
98+
}`;
99+
100+
// with mergeMapKeys set to true, the map will be merged
101+
const expected = `variable "name" {
102+
default = "my-name"
103+
map1 = {
104+
bar = "baz"
105+
foo = "bar"
106+
}
107+
type = string
44108
}
45109
46110
`;
@@ -50,9 +114,13 @@ describe('createHclMergeAction', () => {
50114
input: {
51115
aSourceContent: a,
52116
bSourceContent: b,
117+
options: {
118+
mergeMapKeys: true,
119+
},
53120
},
54121
};
55122

123+
// @ts-expect-error
56124
await createHclMergeAction().handler(mockCtx);
57125

58126
expect(mockCtx.output.mock.calls[0][0]).toEqual('hcl');
@@ -62,7 +130,7 @@ describe('createHclMergeAction', () => {
62130

63131
describe('createHclMergeFilesAction', () => {
64132
const mockContext = {
65-
logger: getVoidLogger(),
133+
logger: mockServices.logger.mock(),
66134
logStream: new PassThrough(),
67135
output: jest.fn(),
68136
createTemporaryDirectory: jest.fn(),
@@ -108,9 +176,13 @@ describe('createHclMergeFilesAction', () => {
108176
input: {
109177
aSourcePath: aPath,
110178
bSourcePath: bPath,
179+
options: {
180+
mergeMapKeys: false,
181+
},
111182
},
112183
};
113184

185+
// @ts-expect-error
114186
await createHclMergeFilesAction().handler(mockCtx);
115187

116188
expect(mockCtx.output.mock.calls[0][0]).toEqual('hcl');

0 commit comments

Comments
 (0)