diff --git a/lib/plugins/environments.js b/lib/plugins/environments.js index 9e2f59eaf8..722bd4c6b8 100644 --- a/lib/plugins/environments.js +++ b/lib/plugins/environments.js @@ -96,6 +96,21 @@ module.exports = class Environments extends Diffable { ) ) } + if (attrs.variables) { + // perhaps this should be retrieved on init so that repo context is more broadly hydrated and not just for this one call + const repository = await this.getRepository(this.repo.owner, this.repo.repo) + if (repository && repository.id) { + await Promise.all( + Object.entries(attrs.variables).map(([name, value]) => + this.github.request('POST /repositories/:repository_id/environments/:environment_name/variables', { + repository_id: repository.id, + environment_name: attrs.name, + name, + value + }) + )) + } + } } remove (existing) { @@ -151,6 +166,16 @@ module.exports = class Environments extends Diffable { return branchPolicies } + async getRepository (org, repo) { + const { + data + } = await this.github.request('GET /repos/:org/:repo', { + org, + repo + }) + return data + } + toParams (existing, attrs) { const deploymentBranchPolicy = attrs.deployment_branch_policy ? this.shouldUseProtectedBranches( diff --git a/package-lock.json b/package-lock.json index fd0e396362..21ad06b339 100644 --- a/package-lock.json +++ b/package-lock.json @@ -5603,6 +5603,7 @@ "version": "0.1.13", "resolved": "https://registry.npmjs.org/encoding/-/encoding-0.1.13.tgz", "integrity": "sha512-ETBauow1T35Y/WZMkio9jiM0Z5xjHHmJ4XmjZOq1l/dXz3lr2sRn87nJy20RupqSh1F2m3HHPSp8ShIPQJrJ3A==", + "dev": true, "optional": true, "dependencies": { "iconv-lite": "^0.6.2" @@ -8333,6 +8334,7 @@ "version": "0.6.3", "resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.6.3.tgz", "integrity": "sha512-4fCk79wshMdzMp2rH06qWrJE4iolqLhCUH+OiuIgU++RB0+94NlDL81atO7GX55uUKueo0txHNtvEyI6D7WdMw==", + "dev": true, "optional": true, "dependencies": { "safer-buffer": ">= 2.1.2 < 3.0.0" @@ -22768,20 +22770,6 @@ "integrity": "sha512-/aCDEGatGvZ2BIk+HmLf4ifCJFwvKFNb9/JeZPMulfgFracn9QFcAf5GO8B/mweUjSoblS5In0cWhqpfs/5PQA==", "dev": true }, - "node_modules/typescript": { - "version": "5.0.4", - "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.0.4.tgz", - "integrity": "sha512-cW9T5W9xY37cc+jfEnaUvX91foxtHkza3Nw3wkoF4sSlKn0MONdkdEndig/qPBWXNkmplh3NzayQzCiHM4/hqw==", - "dev": true, - "peer": true, - "bin": { - "tsc": "bin/tsc", - "tsserver": "bin/tsserver" - }, - "engines": { - "node": ">=12.20" - } - }, "node_modules/uglify-js": { "version": "3.17.4", "resolved": "https://registry.npmjs.org/uglify-js/-/uglify-js-3.17.4.tgz", @@ -27369,8 +27357,7 @@ "version": "5.3.2", "resolved": "https://registry.npmjs.org/acorn-jsx/-/acorn-jsx-5.3.2.tgz", "integrity": "sha512-rq9s+JNhf0IChjtDXxllJ7g41oZk5SlXtp0LHwyA5cejwn7vKmKp4pPri6YEePv2PU65sAsegbXtIinmDFDXgQ==", - "dev": true, - "requires": {} + "dev": true }, "agent-base": { "version": "6.0.2", @@ -28876,6 +28863,7 @@ "version": "0.1.13", "resolved": "https://registry.npmjs.org/encoding/-/encoding-0.1.13.tgz", "integrity": "sha512-ETBauow1T35Y/WZMkio9jiM0Z5xjHHmJ4XmjZOq1l/dXz3lr2sRn87nJy20RupqSh1F2m3HHPSp8ShIPQJrJ3A==", + "dev": true, "optional": true, "requires": { "iconv-lite": "^0.6.2" @@ -31018,6 +31006,7 @@ "version": "0.6.3", "resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.6.3.tgz", "integrity": "sha512-4fCk79wshMdzMp2rH06qWrJE4iolqLhCUH+OiuIgU++RB0+94NlDL81atO7GX55uUKueo0txHNtvEyI6D7WdMw==", + "dev": true, "optional": true, "requires": { "safer-buffer": ">= 2.1.2 < 3.0.0" @@ -31737,8 +31726,7 @@ "version": "1.5.1", "resolved": "https://registry.npmjs.org/dedent/-/dedent-1.5.1.tgz", "integrity": "sha512-+LxW+KLWxu3HW3M2w2ympwtqPrqYRzU8fqi6Fhd18fBALe15blJPI/I4+UHveMVG6lJqB4JNd4UG0S5cnVHwIg==", - "dev": true, - "requires": {} + "dev": true } } }, @@ -31919,8 +31907,7 @@ "version": "1.2.3", "resolved": "https://registry.npmjs.org/jest-pnp-resolver/-/jest-pnp-resolver-1.2.3.tgz", "integrity": "sha512-+3NpwQEnRoIBtx4fyhblQDPgJI0H1IEIkX7ShLUjPGA7TtUTvI1oiKi3SR4oBR0hQhQR80l4WAe5RrXBwWMA8w==", - "dev": true, - "requires": {} + "dev": true }, "jest-regex-util": { "version": "29.6.3", @@ -32124,8 +32111,7 @@ "version": "3.6.0", "resolved": "https://registry.npmjs.org/jest-when/-/jest-when-3.6.0.tgz", "integrity": "sha512-+cZWTy0ekAJo7M9Om0Scdor1jm3wDiYJWmXE8U22UVnkH54YCXAuaqz3P+up/FdtOg8g4wHOxV7Thd7nKhT6Dg==", - "dev": true, - "requires": {} + "dev": true }, "jest-worker": { "version": "29.7.0", @@ -40785,15 +40771,13 @@ "version": "17.1.0", "resolved": "https://registry.npmjs.org/eslint-config-standard/-/eslint-config-standard-17.1.0.tgz", "integrity": "sha512-IwHwmaBNtDK4zDHQukFDW5u/aTb8+meQWZvNFWkiGmbWjD6bqyuSSBxxXKkCftCUzc1zwCH2m/baCNDLGmuO5Q==", - "dev": true, - "requires": {} + "dev": true }, "eslint-config-standard-jsx": { "version": "11.0.0", "resolved": "https://registry.npmjs.org/eslint-config-standard-jsx/-/eslint-config-standard-jsx-11.0.0.tgz", "integrity": "sha512-+1EV/R0JxEK1L0NGolAr8Iktm3Rgotx3BKwgaX+eAuSX8D952LULKtjgZD3F+e6SvibONnhLwoTi9DPxN5LvvQ==", - "dev": true, - "requires": {} + "dev": true }, "eslint-plugin-n": { "version": "15.7.0", @@ -40815,8 +40799,7 @@ "version": "6.1.1", "resolved": "https://registry.npmjs.org/eslint-plugin-promise/-/eslint-plugin-promise-6.1.1.tgz", "integrity": "sha512-tjqWDwVZQo7UIPMeDReOpUgHCmCiH+ePnVT+5zVapL0uuHnegBUs2smM13CzOs2Xb5+MHMRFTs9v24yjba4Oig==", - "dev": true, - "requires": {} + "dev": true }, "eslint-scope": { "version": "7.2.0", @@ -41628,13 +41611,6 @@ "integrity": "sha512-/aCDEGatGvZ2BIk+HmLf4ifCJFwvKFNb9/JeZPMulfgFracn9QFcAf5GO8B/mweUjSoblS5In0cWhqpfs/5PQA==", "dev": true }, - "typescript": { - "version": "5.0.4", - "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.0.4.tgz", - "integrity": "sha512-cW9T5W9xY37cc+jfEnaUvX91foxtHkza3Nw3wkoF4sSlKn0MONdkdEndig/qPBWXNkmplh3NzayQzCiHM4/hqw==", - "dev": true, - "peer": true - }, "uglify-js": { "version": "3.17.4", "resolved": "https://registry.npmjs.org/uglify-js/-/uglify-js-3.17.4.tgz", @@ -42173,8 +42149,7 @@ "update-dotenv": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/update-dotenv/-/update-dotenv-1.1.1.tgz", - "integrity": "sha512-3cIC18In/t0X/yH793c00qqxcKD8jVCgNOPif/fGQkFpYMGecM9YAc+kaAKXuZsM2dE9I9wFI7KvAuNX22SGMQ==", - "requires": {} + "integrity": "sha512-3cIC18In/t0X/yH793c00qqxcKD8jVCgNOPif/fGQkFpYMGecM9YAc+kaAKXuZsM2dE9I9wFI7KvAuNX22SGMQ==" }, "uri-js": { "version": "4.4.1", diff --git a/test/unit/lib/plugins/environments.test.js b/test/unit/lib/plugins/environments.test.js index bf399e9b49..3823c09de5 100644 --- a/test/unit/lib/plugins/environments.test.js +++ b/test/unit/lib/plugins/environments.test.js @@ -5,6 +5,7 @@ describe('Environments', () => { let github const org = 'bkeepers' const repo = 'test' + const repoId = 12345 function configure (config) { return new Environments(github, { owner: org, repo }, config) @@ -42,6 +43,10 @@ describe('Environments', () => { ], deployment_branch_policy: { custom_branches: ['dev/*', 'dev-*'] + }, + variables: { + FOO: 'bar', + FIZZ: 'buzz' } }, { @@ -66,6 +71,23 @@ describe('Environments', () => { { name: 'Different-case', wait_timer: 0 } ]) + when(github.request) + .calledWith('GET /repos/:org/:repo', { + org, + repo + }).mockResolvedValue({ + data: { + id: repoId, + node_id: 'A_bcdefghijk', + name: repo, + full_name: `${org}/${repo}`, + private: true, + owner: { + login: org + } + } + }) + when(github.request) .calledWith('GET /repos/:org/:repo/environments', { org, repo }) .mockResolvedValue({ @@ -215,6 +237,26 @@ describe('Environments', () => { } }) + expect(github.request).toHaveBeenCalledWith( + 'POST /repositories/:repository_id/environments/:environment_name/variables', + { + repoId, + environment_name: 'new-environment', + name: 'FOO', + value: 'bar' + } + ) + + expect(github.request).toHaveBeenCalledWith( + 'POST /repositories/:repository_id/environments/:environment_name/variables', + { + repoId, + environment_name: 'new-environment', + name: 'FIZZ', + value: 'buzz' + } + ) + expect(github.request).toHaveBeenCalledWith('DELETE /repos/:org/:repo/environments/:environment_name', { org, repo,