@defra/grants-config-utils 0.0.4 → 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.
@@ -4,7 +4,7 @@
4
4
  "@changesets/changelog-github",
5
5
  { "repo": "DEFRA/grants-config-utils" }
6
6
  ],
7
- "commit": true,
7
+ "commit": false,
8
8
  "fixed": [],
9
9
  "linked": [],
10
10
  "access": "public",
@@ -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=AZ5ygy4oY6JbeDuiB89v
7
- ceTaskUrl=https://sonarcloud.io/api/ce/task?id=AZ5ygy4oY6JbeDuiB89v
6
+ ceTaskId=AZ5ysLDR_P-CqfyNE6lc
7
+ ceTaskUrl=https://sonarcloud.io/api/ce/task?id=AZ5ysLDR_P-CqfyNE6lc
package/CHANGELOG.md CHANGED
@@ -1,5 +1,17 @@
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
+
9
+ ## 0.0.5
10
+
11
+ ### Patch Changes
12
+
13
+ - [#7](https://github.com/DEFRA/grants-config-utils/pull/7) [`c9dd9db`](https://github.com/DEFRA/grants-config-utils/commit/c9dd9db4bb2e1f08cdcf042cb0c35a4f08224454) Thanks [@aaroncarroll82](https://github.com/aaroncarroll82)! - docs: Update documentation
14
+
3
15
  ## 0.0.4
4
16
 
5
17
  ### Patch Changes
package/CONTRIBUTING.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Contributing
2
2
 
3
- ## Coding Standard
3
+ ## Coding Standards
4
4
 
5
5
  - The code needs to adhere to the [Defra JavaScript Standards](https://defra.github.io/software-development-standards/standards/javascript_standards/)
6
6
  - Use [neostandard](https://github.com/neostandard/neostandard) to lint your code
@@ -9,18 +9,14 @@
9
9
 
10
10
  Use [conventional commits](https://www.conventionalcommits.org/en/v1.0.0/).
11
11
 
12
- By default, a commit into the trunk will cause a `minor` version increment. If you need either a `major` or `patch` version, append `#major` or `#patch` to the commit message.
13
-
14
12
  ## Making changes
15
13
 
16
- Install [pre-commit](https://pre-commit.com/), as it is used to scan commits for secrets using [Gitleaks](https://github.com/gitleaks/gitleaks).
17
-
18
14
  To make changes to this library, please follow the steps below:
19
- 1. Make the changes as usual to the codebase. Ensure your changes are exported via the index.js file!
20
- 2. Run `npm run version:create` OR `npx @changesets/cli` to create a changeset file. This will guide you through the process of documenting your changes.`
21
- 3. Ensure the changeset .md file is added to the commit (should be done automatically now).
15
+
16
+ 1. Make the changes as usual to the codebase. Ensure your changes are exported in package.json!
17
+ 2. Run `npm run version:create`. This will guide you through the process of documenting your changes.
18
+ 3. Ensure the changeset .md file is added to the commit.
22
19
  4. Push to branch, and create a pull request.
23
20
  5. Github actions will verify the build
24
21
  6. After review, merge the pull request.
25
- 7. Github action will automatically increment the version and publish to NPM.
26
-
22
+ 7. Github actions will automatically increment the version and publish to NPM.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@defra/grants-config-utils",
3
- "version": "0.0.4",
3
+ "version": "0.0.6",
4
4
  "description": "NPM module for providing utils for publishing grants config",
5
5
  "engines": {
6
6
  "node": ">=24",
@@ -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 mockLogger = {
10
- info: vi.fn(),
11
- warn: vi.fn(),
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
- const mockPutObjectResponse = { ETag: '"mock-etag"' };
23
- const mockS3Client = {
24
- send: vi.fn(() => Promise.resolve(mockPutObjectResponse)),
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 configNotAlreadyStored(configsAtServiceVersion, logger)) {
16
- await storeConfigAtServiceVersion(configsAtServiceVersion, logger);
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 configNotAlreadyStored = async (_configsAtServiceVersion, _logger) => {
70
- // Check if any configs have: $config/x.x.x/metadata.json
71
- return true;
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
  });