@defra/grants-config-utils 0.0.5 → 0.0.6
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/.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 +2 -2
- package/CHANGELOG.md +6 -0
- package/package.json +1 -1
- package/src/s3/s3-interactions.js +16 -1
- package/src/s3/s3-interactions.test.js +70 -13
- package/src/service/config-store-and-inform.js +17 -7
- package/src/service/config-store-and-inform.test.js +27 -9
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
@@ -3,5 +3,5 @@ projectKey=DEFRA_grants-config-utils
|
|
|
3
3
|
serverUrl=https://sonarcloud.io
|
|
4
4
|
serverVersion=8.0.0.88736
|
|
5
5
|
dashboardUrl=https://sonarcloud.io/dashboard?id=DEFRA_grants-config-utils&branch=main
|
|
6
|
-
ceTaskId=
|
|
7
|
-
ceTaskUrl=https://sonarcloud.io/api/ce/task?id=
|
|
6
|
+
ceTaskId=AZ5ysLDR_P-CqfyNE6lc
|
|
7
|
+
ceTaskUrl=https://sonarcloud.io/api/ce/task?id=AZ5ysLDR_P-CqfyNE6lc
|
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
# @defra/grants-config-utils
|
|
2
2
|
|
|
3
|
+
## 0.0.6
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- [#5](https://github.com/DEFRA/grants-config-utils/pull/5) [`8dce91a`](https://github.com/DEFRA/grants-config-utils/commit/8dce91af68ace1d17faefe01d0b315f19193c940) Thanks [@bendef-24](https://github.com/bendef-24)! - feat(GRAN-20): add config already published check
|
|
8
|
+
|
|
3
9
|
## 0.0.5
|
|
4
10
|
|
|
5
11
|
### Patch Changes
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { config } from "../config/config.js";
|
|
2
2
|
import { createS3Client } from "./s3-client.js";
|
|
3
|
-
import { PutObjectCommand } from "@aws-sdk/client-s3";
|
|
3
|
+
import { ListObjectsV2Command, PutObjectCommand } from "@aws-sdk/client-s3";
|
|
4
4
|
|
|
5
5
|
let s3client;
|
|
6
6
|
|
|
@@ -17,6 +17,21 @@ const initialiseClient = () => {
|
|
|
17
17
|
return s3client;
|
|
18
18
|
};
|
|
19
19
|
|
|
20
|
+
export const listFiles = async (logger, prefix) => {
|
|
21
|
+
const client = initialiseClient();
|
|
22
|
+
const params = {
|
|
23
|
+
Bucket: bucketName,
|
|
24
|
+
Prefix: prefix,
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
const result = await client.send(new ListObjectsV2Command(params));
|
|
28
|
+
logger.info(
|
|
29
|
+
`Found ${result.Contents?.length ?? 0} files using prefix ${prefix}`,
|
|
30
|
+
);
|
|
31
|
+
|
|
32
|
+
return result.Contents ?? [];
|
|
33
|
+
};
|
|
34
|
+
|
|
20
35
|
export const uploadBlob = async (logger, filename, contents) => {
|
|
21
36
|
const client = initialiseClient();
|
|
22
37
|
|
|
@@ -1,16 +1,15 @@
|
|
|
1
|
-
import { getBucketName, uploadBlob } from "./s3-interactions.js";
|
|
1
|
+
import { getBucketName, listFiles, uploadBlob } from "./s3-interactions.js";
|
|
2
2
|
import { createS3Client } from "./s3-client.js";
|
|
3
|
+
import { ListObjectsV2Command } from "@aws-sdk/client-s3";
|
|
3
4
|
|
|
4
5
|
vi.mock("./s3-client.js");
|
|
5
6
|
|
|
6
7
|
const bucketName = "configs-bucket";
|
|
7
8
|
|
|
8
9
|
describe("s3-interactions", () => {
|
|
9
|
-
const
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
error: vi.fn(),
|
|
13
|
-
};
|
|
10
|
+
const mockS3Client = { send: vi.fn() };
|
|
11
|
+
const mockLogger = { info: vi.fn(), warn: vi.fn(), error: vi.fn() };
|
|
12
|
+
|
|
14
13
|
beforeEach(() => {
|
|
15
14
|
createS3Client.mockReturnValueOnce(mockS3Client);
|
|
16
15
|
});
|
|
@@ -19,19 +18,76 @@ describe("s3-interactions", () => {
|
|
|
19
18
|
vi.clearAllMocks();
|
|
20
19
|
});
|
|
21
20
|
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
21
|
+
describe("listFiles", () => {
|
|
22
|
+
it("should list files with a given prefix", async () => {
|
|
23
|
+
const prefix = "test-prefix";
|
|
24
|
+
const mockListResponse = {
|
|
25
|
+
Contents: [
|
|
26
|
+
{ Key: "test-prefix/file1.txt" },
|
|
27
|
+
{ Key: "test-prefix/file2.txt" },
|
|
28
|
+
],
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
mockS3Client.send.mockResolvedValueOnce(mockListResponse);
|
|
32
|
+
|
|
33
|
+
const result = await listFiles(mockLogger, prefix);
|
|
34
|
+
|
|
35
|
+
expect(mockS3Client.send).toHaveBeenCalledWith(
|
|
36
|
+
expect.objectContaining({
|
|
37
|
+
input: {
|
|
38
|
+
Bucket: bucketName,
|
|
39
|
+
Prefix: prefix,
|
|
40
|
+
},
|
|
41
|
+
}),
|
|
42
|
+
);
|
|
43
|
+
|
|
44
|
+
expect(mockS3Client.send).toHaveBeenCalledWith(
|
|
45
|
+
expect.any(ListObjectsV2Command),
|
|
46
|
+
);
|
|
47
|
+
expect(result).toEqual(mockListResponse.Contents);
|
|
48
|
+
expect(mockLogger.info).toHaveBeenCalledWith(
|
|
49
|
+
`Found 2 files using prefix ${prefix}`,
|
|
50
|
+
);
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
it("should return an empty array if no files are found", async () => {
|
|
54
|
+
const prefix = "empty-prefix";
|
|
55
|
+
const mockListResponse = {
|
|
56
|
+
Contents: undefined,
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
mockS3Client.send.mockResolvedValueOnce(mockListResponse);
|
|
60
|
+
|
|
61
|
+
const result = await listFiles(mockLogger, prefix);
|
|
62
|
+
|
|
63
|
+
expect(result).toEqual([]);
|
|
64
|
+
expect(mockLogger.info).toHaveBeenCalledWith(
|
|
65
|
+
`Found 0 files using prefix ${prefix}`,
|
|
66
|
+
);
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
it("should throw an error if listing fails", async () => {
|
|
70
|
+
const prefix = "fail-prefix";
|
|
71
|
+
const mockError = new Error("List failed");
|
|
72
|
+
|
|
73
|
+
mockS3Client.send.mockRejectedValueOnce(mockError);
|
|
74
|
+
|
|
75
|
+
await expect(listFiles(mockLogger, prefix)).rejects.toThrow(
|
|
76
|
+
"List failed",
|
|
77
|
+
);
|
|
78
|
+
});
|
|
79
|
+
});
|
|
26
80
|
|
|
27
81
|
describe("uploadBlob", () => {
|
|
28
82
|
it("should upload a blob to the S3 bucket with specified key", async () => {
|
|
29
83
|
const key = "test-key";
|
|
30
84
|
const body = "test-data";
|
|
31
85
|
|
|
86
|
+
const mockPutObjectResponse = { ETag: '"mock-etag"' };
|
|
87
|
+
mockS3Client.send.mockResolvedValueOnce(mockPutObjectResponse);
|
|
88
|
+
|
|
32
89
|
const result = await uploadBlob(mockLogger, key, body);
|
|
33
90
|
|
|
34
|
-
expect(createS3Client).toHaveBeenCalledTimes(1);
|
|
35
91
|
expect(mockS3Client.send).toHaveBeenCalledWith(
|
|
36
92
|
expect.objectContaining({
|
|
37
93
|
input: {
|
|
@@ -50,12 +106,13 @@ describe("s3-interactions", () => {
|
|
|
50
106
|
it("should throw an error if the upload fails", async () => {
|
|
51
107
|
const key = "test-key";
|
|
52
108
|
const body = "test-data";
|
|
53
|
-
const mockError = new Error("Upload failed");
|
|
54
109
|
|
|
110
|
+
const mockError = new Error("Upload failed");
|
|
55
111
|
mockS3Client.send.mockRejectedValueOnce(mockError);
|
|
56
|
-
|
|
57
112
|
createS3Client.mockReturnValueOnce(mockS3Client);
|
|
58
113
|
|
|
114
|
+
mockS3Client.send.mockRejectedValueOnce(mockError);
|
|
115
|
+
|
|
59
116
|
await expect(uploadBlob(mockLogger, key, body)).rejects.toThrow(
|
|
60
117
|
"Upload failed",
|
|
61
118
|
);
|
|
@@ -1,21 +1,23 @@
|
|
|
1
1
|
import { readFileSync, existsSync, lstatSync, readdirSync } from "node:fs";
|
|
2
2
|
import { config } from "../config/config.js";
|
|
3
|
-
import { uploadBlob } from "../s3/s3-interactions.js";
|
|
3
|
+
import { listFiles, uploadBlob } from "../s3/s3-interactions.js";
|
|
4
4
|
import { createApiHeadersForConfigBroker } from "../broker/broker-auth-helper.js";
|
|
5
5
|
|
|
6
6
|
const configsDirectory = "configurations";
|
|
7
7
|
|
|
8
8
|
export const storeConfigVersionAndInformBroker = async (logger) => {
|
|
9
9
|
if (!configsDirectoryExists(logger)) {
|
|
10
|
-
return;
|
|
10
|
+
return; // exit early, configurations directory not found
|
|
11
11
|
}
|
|
12
12
|
|
|
13
13
|
const configsAtServiceVersion = constructConfigsAtServiceVersion();
|
|
14
14
|
|
|
15
|
-
if (await
|
|
16
|
-
|
|
15
|
+
if (await configAlreadyPublished(configsAtServiceVersion, logger)) {
|
|
16
|
+
return; // exit early, broker already published config
|
|
17
17
|
}
|
|
18
18
|
|
|
19
|
+
await storeConfigAtServiceVersion(configsAtServiceVersion, logger);
|
|
20
|
+
|
|
19
21
|
await notifyConfigBrokerServiceVersionAvailable(
|
|
20
22
|
configsAtServiceVersion,
|
|
21
23
|
logger,
|
|
@@ -66,9 +68,17 @@ const constructConfigsAtServiceVersion = () => {
|
|
|
66
68
|
});
|
|
67
69
|
};
|
|
68
70
|
|
|
69
|
-
const
|
|
70
|
-
|
|
71
|
-
|
|
71
|
+
const configAlreadyPublished = async (configsAtServiceVersion, logger) => {
|
|
72
|
+
for (const { grant, version } of configsAtServiceVersion) {
|
|
73
|
+
const files = await listFiles(logger, `${grant}/${version}/metadata.json`);
|
|
74
|
+
if (files.length > 0) {
|
|
75
|
+
logger.warn(
|
|
76
|
+
`grant config '${grant}' at version '${version}' already published, not safe to store`,
|
|
77
|
+
);
|
|
78
|
+
return true;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
return false;
|
|
72
82
|
};
|
|
73
83
|
|
|
74
84
|
const storeConfigAtServiceVersion = async (configsAtServiceVersion, logger) => {
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { storeConfigVersionAndInformBroker } from "./config-store-and-inform.js";
|
|
2
2
|
import { readFileSync, existsSync, lstatSync, readdirSync } from "node:fs";
|
|
3
|
-
import { uploadBlob } from "../s3/s3-interactions.js";
|
|
3
|
+
import { listFiles, uploadBlob } from "../s3/s3-interactions.js";
|
|
4
4
|
|
|
5
5
|
vi.mock("node:fs", () => ({
|
|
6
6
|
existsSync: vi.fn(),
|
|
@@ -23,6 +23,7 @@ vi.mock("../config/config.js", () => ({
|
|
|
23
23
|
}));
|
|
24
24
|
|
|
25
25
|
vi.mock("../s3/s3-interactions.js", () => ({
|
|
26
|
+
listFiles: vi.fn(() => []),
|
|
26
27
|
uploadBlob: vi.fn(),
|
|
27
28
|
}));
|
|
28
29
|
|
|
@@ -71,15 +72,12 @@ describe("storeConfigVersionAndInformBroker", () => {
|
|
|
71
72
|
expect(mockLogger.warn).toHaveBeenCalledWith(
|
|
72
73
|
"config folder 'configurations' not found, so performing the file upload",
|
|
73
74
|
);
|
|
75
|
+
expect(listFiles).not.toHaveBeenCalled();
|
|
74
76
|
expect(readFileSync).not.toHaveBeenCalled();
|
|
75
77
|
expect(uploadBlob).not.toHaveBeenCalled();
|
|
76
78
|
expect(fetch).not.toHaveBeenCalled();
|
|
77
79
|
});
|
|
78
80
|
|
|
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
81
|
it("should store config and inform config broker", async () => {
|
|
84
82
|
existsSync.mockReturnValue(true);
|
|
85
83
|
lstatSync.mockReturnValue({ isDirectory: () => true });
|
|
@@ -104,11 +102,34 @@ describe("storeConfigVersionAndInformBroker", () => {
|
|
|
104
102
|
expect(mockLogger.info).toHaveBeenCalledWith(
|
|
105
103
|
"successfully notified the config broker about 'grant-1' at version '1.2.3'",
|
|
106
104
|
);
|
|
105
|
+
expect(listFiles).toHaveBeenCalledTimes(1);
|
|
107
106
|
expect(readFileSync).toHaveBeenCalledTimes(1);
|
|
108
107
|
expect(uploadBlob).toHaveBeenCalledTimes(1);
|
|
109
108
|
expect(fetch).toHaveBeenCalledTimes(1);
|
|
110
109
|
});
|
|
111
110
|
|
|
111
|
+
it("should not store or inform when already published by config broker", async () => {
|
|
112
|
+
existsSync.mockReturnValue(true);
|
|
113
|
+
lstatSync.mockReturnValue({ isDirectory: () => true });
|
|
114
|
+
readdirSync
|
|
115
|
+
.mockReturnValueOnce([{ name: "grant-1", isDirectory: () => true }])
|
|
116
|
+
.mockReturnValueOnce([
|
|
117
|
+
{
|
|
118
|
+
name: "main.json",
|
|
119
|
+
isFile: () => true,
|
|
120
|
+
parentPath: "configurations/grant-1",
|
|
121
|
+
},
|
|
122
|
+
]);
|
|
123
|
+
listFiles.mockReturnValueOnce([{ Contents: ["fake-content-1"] }]);
|
|
124
|
+
|
|
125
|
+
await storeConfigVersionAndInformBroker(mockLogger);
|
|
126
|
+
|
|
127
|
+
expect(listFiles).toHaveBeenCalledTimes(1);
|
|
128
|
+
expect(readFileSync).not.toHaveBeenCalled();
|
|
129
|
+
expect(uploadBlob).not.toHaveBeenCalled();
|
|
130
|
+
expect(fetch).not.toHaveBeenCalled();
|
|
131
|
+
});
|
|
132
|
+
|
|
112
133
|
it("should call config broker for each config (subdirectory) found in configurations folder", async () => {
|
|
113
134
|
existsSync.mockReturnValue(true);
|
|
114
135
|
lstatSync.mockReturnValue({ isDirectory: () => true });
|
|
@@ -157,12 +178,9 @@ describe("storeConfigVersionAndInformBroker", () => {
|
|
|
157
178
|
expect(mockLogger.info).toHaveBeenCalledWith(
|
|
158
179
|
"successfully notified the config broker about 'grant-2' at version '1.2.3'",
|
|
159
180
|
);
|
|
181
|
+
expect(listFiles).toHaveBeenCalledTimes(2);
|
|
160
182
|
expect(readFileSync).toHaveBeenCalledTimes(3);
|
|
161
183
|
expect(uploadBlob).toHaveBeenCalledTimes(3);
|
|
162
184
|
expect(fetch).toHaveBeenCalledTimes(2);
|
|
163
185
|
});
|
|
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
186
|
});
|