@defra/grants-config-utils 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.changeset/README.md +8 -0
- package/.changeset/config.json +14 -0
- package/.github/dependabot.yml +28 -0
- package/.github/workflows/check-pull-request.yml +44 -0
- package/.github/workflows/release.yml +60 -0
- package/.husky/pre-commit +1 -0
- package/.nvmrc +1 -0
- package/.prettierignore +4 -0
- package/.scannerwork/.sonar_lock +0 -0
- package/.scannerwork/architecture/js/src_broker_broker-auth-helper_js.udg +0 -0
- package/.scannerwork/architecture/js/src_broker_broker-auth-helper_test_js.udg +0 -0
- package/.scannerwork/architecture/js/src_config_config_js.udg +0 -0
- package/.scannerwork/architecture/js/src_s3_s3-client_js.udg +0 -0
- package/.scannerwork/architecture/js/src_s3_s3-client_test_js.udg +0 -0
- package/.scannerwork/architecture/js/src_s3_s3-interactions_js.udg +0 -0
- package/.scannerwork/architecture/js/src_s3_s3-interactions_test_js.udg +0 -0
- package/.scannerwork/architecture/js/src_service_config-store-and-inform_js.udg +0 -0
- package/.scannerwork/architecture/js/src_service_config-store-and-inform_test_js.udg +0 -0
- package/.scannerwork/report-task.txt +7 -0
- package/.vite/setup-files.js +15 -0
- package/CONTRIBUTING.md +26 -0
- package/LICENCE +8 -0
- package/README.md +32 -0
- package/babel.config.cjs +17 -0
- package/ci/release-it.sh +19 -0
- package/eslint.config.js +12 -0
- package/package.json +92 -0
- package/sonar-project.properties +15 -0
- package/src/broker/broker-auth-helper.js +74 -0
- package/src/broker/broker-auth-helper.test.js +262 -0
- package/src/config/config.js +146 -0
- package/src/s3/s3-client.js +20 -0
- package/src/s3/s3-client.test.js +56 -0
- package/src/s3/s3-interactions.js +35 -0
- package/src/s3/s3-interactions.test.js +82 -0
- package/src/service/config-store-and-inform.js +151 -0
- package/src/service/config-store-and-inform.test.js +168 -0
- package/vitest.config.js +18 -0
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
import { readFileSync, existsSync, lstatSync, readdirSync } from "node:fs";
|
|
2
|
+
import { config } from "../config/config.js";
|
|
3
|
+
import { uploadBlob } from "../s3/s3-interactions.js";
|
|
4
|
+
import { createApiHeadersForConfigBroker } from "../broker/broker-auth-helper.js";
|
|
5
|
+
|
|
6
|
+
const configsDirectory = "configurations";
|
|
7
|
+
|
|
8
|
+
export const storeConfigVersionAndInformBroker = async (logger) => {
|
|
9
|
+
if (!configsDirectoryExists(logger)) {
|
|
10
|
+
return;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
const configsAtServiceVersion = constructConfigsAtServiceVersion();
|
|
14
|
+
|
|
15
|
+
if (await configNotAlreadyStored(configsAtServiceVersion, logger)) {
|
|
16
|
+
await storeConfigAtServiceVersion(configsAtServiceVersion, logger);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
await notifyConfigBrokerServiceVersionAvailable(
|
|
20
|
+
configsAtServiceVersion,
|
|
21
|
+
logger,
|
|
22
|
+
);
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
const configsDirectoryExists = (logger) => {
|
|
26
|
+
const directoryExists =
|
|
27
|
+
existsSync(configsDirectory) && lstatSync(configsDirectory).isDirectory();
|
|
28
|
+
|
|
29
|
+
if (!directoryExists) {
|
|
30
|
+
logger.warn(
|
|
31
|
+
`config folder '${configsDirectory}' not found, so performing the file upload`,
|
|
32
|
+
);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
return directoryExists;
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
const constructConfigsAtServiceVersion = () => {
|
|
39
|
+
const version = config.get("serviceVersion");
|
|
40
|
+
|
|
41
|
+
// all top-level directories are considered separate grant configurations
|
|
42
|
+
const configDirs = readdirSync(configsDirectory, { withFileTypes: true })
|
|
43
|
+
.filter((dirent) => dirent.isDirectory())
|
|
44
|
+
.map((dirent) => dirent.name);
|
|
45
|
+
|
|
46
|
+
// iterate each grant configuration, collecting: grant, version and files
|
|
47
|
+
return configDirs.map((grant) => {
|
|
48
|
+
const files = readdirSync(`${configsDirectory}/${grant}`, {
|
|
49
|
+
withFileTypes: true,
|
|
50
|
+
recursive: true,
|
|
51
|
+
})
|
|
52
|
+
.filter((dirent) => dirent.isFile())
|
|
53
|
+
.map((dirent) => {
|
|
54
|
+
const configPath = `${configsDirectory}/${grant}`;
|
|
55
|
+
|
|
56
|
+
const direntWithoutConfigPath = dirent.parentPath
|
|
57
|
+
? `${dirent.parentPath.replace(configPath, "")}/${dirent.name}`
|
|
58
|
+
: `${dirent.name}`;
|
|
59
|
+
|
|
60
|
+
const localPath = `${configPath}${direntWithoutConfigPath}`;
|
|
61
|
+
const s3Path = `${grant}/${version}${direntWithoutConfigPath}`;
|
|
62
|
+
return [localPath, s3Path];
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
return { grant, version, files };
|
|
66
|
+
});
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
const configNotAlreadyStored = async (_configsAtServiceVersion, _logger) => {
|
|
70
|
+
// Check if any configs have: $config/x.x.x/metadata.json
|
|
71
|
+
return true;
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
const storeConfigAtServiceVersion = async (configsAtServiceVersion, logger) => {
|
|
75
|
+
// upload all files across all grant configurations at once
|
|
76
|
+
const allConfigFiles = configsAtServiceVersion.flatMap(
|
|
77
|
+
(grant) => grant.files,
|
|
78
|
+
);
|
|
79
|
+
for (const [localPath, s3Path] of allConfigFiles) {
|
|
80
|
+
logger.info(`uploading '${s3Path}' to S3`);
|
|
81
|
+
await uploadBlob(logger, s3Path, readFileSync(localPath, "utf8"));
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
logger.info(
|
|
85
|
+
`successfully uploaded '${allConfigFiles.length}' files across '${configsAtServiceVersion.length}' configs`,
|
|
86
|
+
);
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
const notifyConfigBrokerServiceVersionAvailable = async (
|
|
90
|
+
configsAtServiceVersion,
|
|
91
|
+
logger,
|
|
92
|
+
) => {
|
|
93
|
+
const configBrokerEndpoint = config.get("configBroker.apiEndpoint");
|
|
94
|
+
|
|
95
|
+
// iterate each grant configuration, notify config available at current service version
|
|
96
|
+
for (const configAtServiceVersion of configsAtServiceVersion) {
|
|
97
|
+
await callReleaseConfigEndpoint(
|
|
98
|
+
configBrokerEndpoint,
|
|
99
|
+
configAtServiceVersion,
|
|
100
|
+
logger,
|
|
101
|
+
);
|
|
102
|
+
}
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
const callReleaseConfigEndpoint = async (
|
|
106
|
+
configBrokerEndpoint,
|
|
107
|
+
configAtServiceVersion,
|
|
108
|
+
logger,
|
|
109
|
+
) => {
|
|
110
|
+
if (!configBrokerEndpoint?.length) {
|
|
111
|
+
logger.warn(
|
|
112
|
+
`config broker endpoint not set, so skipping release config call`,
|
|
113
|
+
);
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
const { grant, version, files } = configAtServiceVersion;
|
|
118
|
+
// files is an array of tuples, we only want the S3 paths here
|
|
119
|
+
const s3Paths = files.map(([_, s3Path]) => s3Path);
|
|
120
|
+
|
|
121
|
+
const payload = {
|
|
122
|
+
grant,
|
|
123
|
+
version,
|
|
124
|
+
files: s3Paths,
|
|
125
|
+
status: "draft", // hardcode status for now
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
const url = new URL(`/api/release-config`, configBrokerEndpoint);
|
|
129
|
+
try {
|
|
130
|
+
const response = await fetch(url.href, {
|
|
131
|
+
method: "POST",
|
|
132
|
+
headers: {
|
|
133
|
+
...createApiHeadersForConfigBroker(),
|
|
134
|
+
"Content-Type": "application/json",
|
|
135
|
+
},
|
|
136
|
+
body: JSON.stringify(payload),
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
if (response.ok) {
|
|
140
|
+
logger.info(
|
|
141
|
+
`successfully notified the config broker about '${grant}' at version '${version}'`,
|
|
142
|
+
);
|
|
143
|
+
} else {
|
|
144
|
+
logger.error(
|
|
145
|
+
`call to release config failed with status '${response.status}' and text '${response.statusText}'`,
|
|
146
|
+
);
|
|
147
|
+
}
|
|
148
|
+
} catch (err) {
|
|
149
|
+
logger.error("call to release config failed", err);
|
|
150
|
+
}
|
|
151
|
+
};
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
import { storeConfigVersionAndInformBroker } from "./config-store-and-inform.js";
|
|
2
|
+
import { readFileSync, existsSync, lstatSync, readdirSync } from "node:fs";
|
|
3
|
+
import { uploadBlob } from "../s3/s3-interactions.js";
|
|
4
|
+
|
|
5
|
+
vi.mock("node:fs", () => ({
|
|
6
|
+
existsSync: vi.fn(),
|
|
7
|
+
lstatSync: vi.fn(),
|
|
8
|
+
readdirSync: vi.fn(),
|
|
9
|
+
readFileSync: vi.fn(() => "file-content"),
|
|
10
|
+
}));
|
|
11
|
+
|
|
12
|
+
vi.mock("../config/config.js", () => ({
|
|
13
|
+
config: {
|
|
14
|
+
get: (key) => {
|
|
15
|
+
const values = {
|
|
16
|
+
serviceVersion: "1.2.3",
|
|
17
|
+
serviceName: "grants-config-playground",
|
|
18
|
+
"configBroker.apiEndpoint": "https://broker.unit.test",
|
|
19
|
+
};
|
|
20
|
+
return values[key];
|
|
21
|
+
},
|
|
22
|
+
},
|
|
23
|
+
}));
|
|
24
|
+
|
|
25
|
+
vi.mock("../s3/s3-interactions.js", () => ({
|
|
26
|
+
uploadBlob: vi.fn(),
|
|
27
|
+
}));
|
|
28
|
+
|
|
29
|
+
vi.mock("../broker/broker-auth-helper.js", () => ({
|
|
30
|
+
createApiHeadersForConfigBroker: vi.fn(() => ({
|
|
31
|
+
authorization: "Bearer token",
|
|
32
|
+
})),
|
|
33
|
+
}));
|
|
34
|
+
|
|
35
|
+
const mockLogger = {
|
|
36
|
+
info: vi.fn(),
|
|
37
|
+
warn: vi.fn(),
|
|
38
|
+
error: vi.fn(),
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
describe("storeConfigVersionAndInformBroker", () => {
|
|
42
|
+
beforeEach(async () => {
|
|
43
|
+
vi.resetModules();
|
|
44
|
+
vi.clearAllMocks();
|
|
45
|
+
|
|
46
|
+
vi.stubGlobal(
|
|
47
|
+
"fetch",
|
|
48
|
+
vi.fn(() =>
|
|
49
|
+
Promise.resolve({
|
|
50
|
+
ok: true,
|
|
51
|
+
status: 200,
|
|
52
|
+
statusText: "OK",
|
|
53
|
+
json: vi.fn().mockResolvedValue({}),
|
|
54
|
+
}),
|
|
55
|
+
),
|
|
56
|
+
);
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
afterEach(() => {
|
|
60
|
+
vi.restoreAllMocks();
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
it("should exit early when configurations directory does not exist or not a directory", async () => {
|
|
64
|
+
existsSync.mockReturnValue(true);
|
|
65
|
+
lstatSync.mockReturnValue({ isDirectory: () => false });
|
|
66
|
+
|
|
67
|
+
await expect(
|
|
68
|
+
storeConfigVersionAndInformBroker(mockLogger),
|
|
69
|
+
).resolves.not.toThrow();
|
|
70
|
+
|
|
71
|
+
expect(mockLogger.warn).toHaveBeenCalledWith(
|
|
72
|
+
"config folder 'configurations' not found, so performing the file upload",
|
|
73
|
+
);
|
|
74
|
+
expect(readFileSync).not.toHaveBeenCalled();
|
|
75
|
+
expect(uploadBlob).not.toHaveBeenCalled();
|
|
76
|
+
expect(fetch).not.toHaveBeenCalled();
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
it.skip("should still inform config broker but skip storing when already exists", async () => {
|
|
80
|
+
// TODO BH implement in next PR
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
it("should store config and inform config broker", async () => {
|
|
84
|
+
existsSync.mockReturnValue(true);
|
|
85
|
+
lstatSync.mockReturnValue({ isDirectory: () => true });
|
|
86
|
+
readdirSync
|
|
87
|
+
.mockReturnValueOnce([{ name: "grant-1", isDirectory: () => true }])
|
|
88
|
+
.mockReturnValueOnce([
|
|
89
|
+
{
|
|
90
|
+
name: "main.json",
|
|
91
|
+
isFile: () => true,
|
|
92
|
+
parentPath: "configurations/grant-1",
|
|
93
|
+
},
|
|
94
|
+
]);
|
|
95
|
+
|
|
96
|
+
await storeConfigVersionAndInformBroker(mockLogger);
|
|
97
|
+
|
|
98
|
+
expect(mockLogger.info).toHaveBeenCalledWith(
|
|
99
|
+
"uploading 'grant-1/1.2.3/main.json' to S3",
|
|
100
|
+
);
|
|
101
|
+
expect(mockLogger.info).toHaveBeenCalledWith(
|
|
102
|
+
"successfully uploaded '1' files across '1' configs",
|
|
103
|
+
);
|
|
104
|
+
expect(mockLogger.info).toHaveBeenCalledWith(
|
|
105
|
+
"successfully notified the config broker about 'grant-1' at version '1.2.3'",
|
|
106
|
+
);
|
|
107
|
+
expect(readFileSync).toHaveBeenCalledTimes(1);
|
|
108
|
+
expect(uploadBlob).toHaveBeenCalledTimes(1);
|
|
109
|
+
expect(fetch).toHaveBeenCalledTimes(1);
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
it("should call config broker for each config (subdirectory) found in configurations folder", async () => {
|
|
113
|
+
existsSync.mockReturnValue(true);
|
|
114
|
+
lstatSync.mockReturnValue({ isDirectory: () => true });
|
|
115
|
+
readdirSync
|
|
116
|
+
.mockReturnValueOnce([
|
|
117
|
+
{ name: "grant-1", isDirectory: () => true },
|
|
118
|
+
{ name: "grant-2", isDirectory: () => true },
|
|
119
|
+
])
|
|
120
|
+
.mockReturnValueOnce([
|
|
121
|
+
{
|
|
122
|
+
name: "main.json",
|
|
123
|
+
isFile: () => true,
|
|
124
|
+
parentPath: "configurations/grant-1",
|
|
125
|
+
},
|
|
126
|
+
{
|
|
127
|
+
name: "submain.json",
|
|
128
|
+
isFile: () => true,
|
|
129
|
+
parentPath: "configurations/grant-1",
|
|
130
|
+
},
|
|
131
|
+
])
|
|
132
|
+
.mockReturnValueOnce([
|
|
133
|
+
{
|
|
134
|
+
name: "main.json",
|
|
135
|
+
isFile: () => true,
|
|
136
|
+
parentPath: "configurations/grant-2",
|
|
137
|
+
},
|
|
138
|
+
]);
|
|
139
|
+
|
|
140
|
+
await storeConfigVersionAndInformBroker(mockLogger);
|
|
141
|
+
|
|
142
|
+
expect(mockLogger.info).toHaveBeenCalledWith(
|
|
143
|
+
"uploading 'grant-1/1.2.3/main.json' to S3",
|
|
144
|
+
);
|
|
145
|
+
expect(mockLogger.info).toHaveBeenCalledWith(
|
|
146
|
+
"uploading 'grant-1/1.2.3/submain.json' to S3",
|
|
147
|
+
);
|
|
148
|
+
expect(mockLogger.info).toHaveBeenCalledWith(
|
|
149
|
+
"uploading 'grant-2/1.2.3/main.json' to S3",
|
|
150
|
+
);
|
|
151
|
+
expect(mockLogger.info).toHaveBeenCalledWith(
|
|
152
|
+
"successfully uploaded '3' files across '2' configs",
|
|
153
|
+
);
|
|
154
|
+
expect(mockLogger.info).toHaveBeenCalledWith(
|
|
155
|
+
"successfully notified the config broker about 'grant-1' at version '1.2.3'",
|
|
156
|
+
);
|
|
157
|
+
expect(mockLogger.info).toHaveBeenCalledWith(
|
|
158
|
+
"successfully notified the config broker about 'grant-2' at version '1.2.3'",
|
|
159
|
+
);
|
|
160
|
+
expect(readFileSync).toHaveBeenCalledTimes(3);
|
|
161
|
+
expect(uploadBlob).toHaveBeenCalledTimes(3);
|
|
162
|
+
expect(fetch).toHaveBeenCalledTimes(2);
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
it.skip("should check storage for each config (subdirectory) found in configurations folder", async () => {
|
|
166
|
+
// TODO BH implement in next PR
|
|
167
|
+
});
|
|
168
|
+
});
|
package/vitest.config.js
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { defineConfig, configDefaults } from "vitest/config";
|
|
2
|
+
|
|
3
|
+
export default defineConfig({
|
|
4
|
+
test: {
|
|
5
|
+
globals: true,
|
|
6
|
+
environment: "node",
|
|
7
|
+
clearMocks: true,
|
|
8
|
+
fileParallelism: false,
|
|
9
|
+
coverage: {
|
|
10
|
+
provider: "v8",
|
|
11
|
+
reportsDirectory: "./coverage",
|
|
12
|
+
reporter: ["text", "lcov"],
|
|
13
|
+
include: ["src/**/*.js"],
|
|
14
|
+
exclude: [...configDefaults.exclude, "coverage"],
|
|
15
|
+
},
|
|
16
|
+
setupFiles: [".vite/setup-files.js"],
|
|
17
|
+
},
|
|
18
|
+
});
|