From d393de1b87af5fccd790cb32192f713e431846e8 Mon Sep 17 00:00:00 2001 From: randomcascade Date: Tue, 24 Sep 2024 08:44:00 -0500 Subject: [PATCH 1/3] feat: add nop to environments --- lib/plugins/environments.js | 193 ++++++++++++--------- test/unit/lib/plugins/environments.test.js | 38 ++++ 2 files changed, 146 insertions(+), 85 deletions(-) diff --git a/lib/plugins/environments.js b/lib/plugins/environments.js index 4ceaa9ff..855d6432 100644 --- a/lib/plugins/environments.js +++ b/lib/plugins/environments.js @@ -19,6 +19,16 @@ module.exports = class Environments extends Diffable { } } + async nopifyRequest(url, options, description) { + if (!this.nop) { + await this.github.request(url, options); + } else { + return Promise.resolve([ + new NopCommand(this.constructor.name, this.repo, url, description) + ]); + } + } + async find() { const { data: { environments } } = await this.github.request('GET /repos/:org/:repo/environments', { org: this.repo.owner, @@ -117,11 +127,15 @@ module.exports = class Environments extends Diffable { async update(existing, attrs) { const {wait_timer, prevent_self_review, reviewers, deployment_branch_policy, variables, deployment_protection_rules} = this.getChanged(existing, attrs); - if(wait_timer || prevent_self_review || reviewers || deployment_branch_policy) { - await this.github.request(`PUT /repos/:org/:repo/environments/:environment_name`, { - org: this.repo.owner, - repo: this.repo.repo, - environment_name: attrs.name, + const baseRequestOptions = { + org: this.repo.owner, + repo: this.repo.repo, + environment_name: attrs.name + } + + if (wait_timer || prevent_self_review || reviewers || deployment_branch_policy) { + const options = { + ...baseRequestOptions, wait_timer: attrs.wait_timer, prevent_self_review: attrs.prevent_self_review, reviewers: attrs.reviewers, @@ -129,32 +143,27 @@ module.exports = class Environments extends Diffable { protected_branches: attrs.deployment_branch_policy.protected_branches, custom_branch_policies: !!attrs.deployment_branch_policy.custom_branch_policies } - }) + }; + + await this.nopifyRequest(`PUT /repos/:org/:repo/environments/:environment_name`, options, 'Update environment settings'); } if(deployment_branch_policy && attrs.deployment_branch_policy && attrs.deployment_branch_policy.custom_branch_policies) { - const existingPolicies = (await this.github.request('GET /repos/:org/:repo/environments/:environment_name/deployment-branch-policies', { - org: this.repo.owner, - repo: this.repo.repo, - environment_name: attrs.name - })).data.branch_policies; + const existingPolicies = (await this.github.request('GET /repos/:org/:repo/environments/:environment_name/deployment-branch-policies', baseRequestOptions)).data.branch_policies; for(let policy of existingPolicies) { - await this.github.request('DELETE /repos/:org/:repo/environments/:environment_name/deployment-branch-policies/:branch_policy_id', { - org: this.repo.owner, - repo: this.repo.repo, - environment_name: attrs.name, + await this.nopifyRequest('DELETE /repos/:org/:repo/environments/:environment_name/deployment-branch-policies/:branch_policy_id', { + ...baseRequestOptions, branch_policy_id: policy.id - }); + }, 'Delete deployment branch policy'); } for(let policy of attrs.deployment_branch_policy.custom_branch_policies) { - await this.github.request('POST /repos/:org/:repo/environments/:environment_name/deployment-branch-policies', { - org: this.repo.owner, - repo: this.repo.repo, - environment_name: attrs.name, - name: policy - }); + await this.nopifyRequest( + 'POST /repos/:org/:repo/environments/:environment_name/deployment-branch-policies',{ + ...baseRequestOptions, + name: policy + }, 'Create deployment branch policy'); } } @@ -166,33 +175,33 @@ module.exports = class Environments extends Diffable { if(existingVariable) { existingVariables = existingVariables.filter(_var => _var.name !== variable.name); if(existingVariable.value !== variable.value) { - await this.github.request(`PATCH /repos/:org/:repo/environments/:environment_name/variables/:variable_name`, { - org: this.repo.owner, - repo: this.repo.repo, - environment_name: attrs.name, - variable_name: variable.name, - value: variable.value - }); + await this.nopifyRequest( + 'PATCH /repos/:org/:repo/environments/:environment_name/variables/:variable_name', { + ...baseRequestOptions, + variable_name: variable.name, + value: variable.value + }, 'Update environment variable' + ); } } else { - await this.github.request(`POST /repos/:org/:repo/environments/:environment_name/variables`, { - org: this.repo.owner, - repo: this.repo.repo, - environment_name: attrs.name, - name: variable.name, - value: variable.value - }); + await this.nopifyRequest( + 'POST /repos/:org/:repo/environments/:environment_name/variables', { + ...baseRequestOptions, + name: variable.name, + value: variable.value + }, 'Create environment variable' + ); } } for(let variable of existingVariables) { - await this.github.request('DELETE /repos/:org/:repo/environments/:environment_name/variables/:variable_name', { - org: this.repo.owner, - repo: this.repo.repo, - environment_name: attrs.name, - variable_name: variable.name - }); + await this.nopifyRequest( + 'DELETE /repos/:org/:repo/environments/:environment_name/variables/:variable_name', { + ...baseRequestOptions, + variable_name: variable.name + }, 'Delete environment variable' + ); } } @@ -203,49 +212,56 @@ module.exports = class Environments extends Diffable { const existingRule = existingRules.find((_rule) => _rule.id === rule.id); if(!existingRule) { - await this.github.request(`POST /repos/:org/:repo/environments/:environment_name/deployment_protection_rules`, { - org: this.repo.owner, - repo: this.repo.repo, - environment_name: attrs.name, - integration_id: rule.app_id - }); + await this.nopifyRequest( + 'POST /repos/:org/:repo/environments/:environment_name/deployment_protection_rules', { + ...baseRequestOptions, + integration_id: rule.app_id + }, 'Create deployment protection rule' + ); } } for(let rule of existingRules) { - await this.github.request('DELETE /repos/:org/:repo/environments/:environment_name/deployment_protection_rules/:rule_id', { - org: this.repo.owner, - repo: this.repo.repo, - environment_name: attrs.name, - rule_id: rule.id - }); + await this.nopifyRequest( + 'DELETE /repos/:org/:repo/environments/:environment_name/deployment_protection_rules/:rule_id', { + ...baseRequestOptions, + rule_id: rule.id + }, 'Delete deployment protection rule' + ); } } } async add(attrs) { - await this.github.request(`PUT /repos/:org/:repo/environments/:environment_name`, { + + const baseRequestOptions = { org: this.repo.owner, repo: this.repo.repo, - environment_name: attrs.name, - wait_timer: attrs.wait_timer, - prevent_self_review: attrs.prevent_self_review, - reviewers: attrs.reviewers, - deployment_branch_policy: attrs.deployment_branch_policy == null ? null : { - protected_branches: !!attrs.deployment_branch_policy.protected_branches, - custom_branch_policies: !!attrs.deployment_branch_policy.custom_branch_policies - } - }); + environment_name: attrs.name + } + + await this.nopifyRequest( + 'PUT /repos/:org/:repo/environments/:environment_name', { + ...baseRequestOptions, + wait_timer: attrs.wait_timer, + prevent_self_review: attrs.prevent_self_review, + reviewers: attrs.reviewers, + deployment_branch_policy: attrs.deployment_branch_policy == null ? null : { + protected_branches: !!attrs.deployment_branch_policy.protected_branches, + custom_branch_policies: !!attrs.deployment_branch_policy.custom_branch_policies + } + }, 'Update environment settings' + ); if(attrs.deployment_branch_policy && attrs.deployment_branch_policy.custom_branch_policies) { for(let policy of attrs.deployment_branch_policy.custom_branch_policies) { - await this.github.request('POST /repos/:org/:repo/environments/:environment_name/deployment-branch-policies', { - org: this.repo.owner, - repo: this.repo.repo, - environment_name: attrs.name, - name: policy.name - }); + await this.nopifyRequest( + 'POST /repos/:org/:repo/environments/:environment_name/deployment-branch-policies', { + ...baseRequestOptions, + name: policy.name + }, 'Create deployment branch policy' + ); } } @@ -253,13 +269,13 @@ module.exports = class Environments extends Diffable { if(attrs.variables) { for(let variable of attrs.variables) { - await this.github.request(`POST /repos/:org/:repo/environments/:environment_name/variables`, { - org: this.repo.owner, - repo: this.repo.repo, - environment_name: attrs.name, - name: variable.name, - value: variable.value - }); + await this.nopifyRequest( + 'POST /repos/:org/:repo/environments/:environment_name/variables', { + ...baseRequestOptions, + name: variable.name, + value: variable.value + }, 'Create environment variable' + ); } } @@ -267,23 +283,30 @@ module.exports = class Environments extends Diffable { if(attrs.deployment_protection_rules) { for(let rule of attrs.deployment_protection_rules) { - await this.github.request(`POST /repos/:org/:repo/environments/:environment_name/deployment_protection_rules`, { - org: this.repo.owner, - repo: this.repo.repo, - environment_name: attrs.name, - integration_id: rule.app_id - }); + await this.nopifyRequest( + 'POST /repos/:org/:repo/environments/:environment_name/deployment_protection_rules', { + ...baseRequestOptions, + integration_id: rule.app_id + }, 'Create deployment protection rule' + ); } } } async remove(existing) { - await this.github.request(`DELETE /repos/:org/:repo/environments/:environment_name`, { + + const baseRequestOptions = { org: this.repo.owner, repo: this.repo.repo, environment_name: existing.name - }); + }; + + await this.nopifyRequest( + 'DELETE /repos/:org/:repo/environments/:environment_name', { + ...baseRequestOptions + }, 'Delete environment' + ); } sync () { diff --git a/test/unit/lib/plugins/environments.test.js b/test/unit/lib/plugins/environments.test.js index 276a82cd..854049af 100644 --- a/test/unit/lib/plugins/environments.test.js +++ b/test/unit/lib/plugins/environments.test.js @@ -1,5 +1,6 @@ const { when } = require('jest-when') const Environments = require('../../../../lib/plugins/environments') +const NopCommand = require('../../../../lib/nopcommand'); describe('Environments Plugin test suite', () => { let github @@ -1063,4 +1064,41 @@ describe('Environments Plugin test suite', () => { }) }) + }) + +describe('nopifyRequest', () => { + let github; + let plugin; + const org = 'bkeepers'; + const repo = 'test'; + const environment_name = 'test-environment'; + const url = 'PUT /repos/:org/:repo/environments/:environment_name'; + const options = { org, repo, environment_name, wait_timer: 1 }; + const description = 'Update environment wait timer'; + + beforeEach(() => { + github = { + request: jest.fn(() => Promise.resolve(true)) + }; + plugin = new Environments(undefined, github, { owner: org, repo }, [], { debug: jest.fn(), error: console.error }, []); + }); + + it('should make a request when nop is false', async () => { + plugin.nop = false; + + await plugin.nopifyRequest(url, options, description); + + expect(github.request).toHaveBeenCalledWith(url, options); + }); + + it('should return NopCommand when nop is true', async () => { + plugin.nop = true; + + const result = await plugin.nopifyRequest(url, options, description); + + expect(result).toEqual([ + new NopCommand('Environments', { owner: org, repo }, url, description) + ]); + }); +}); From d00fac6bc4006111bb2db4ed722ea2ca074ab476 Mon Sep 17 00:00:00 2001 From: randomcascade Date: Tue, 24 Sep 2024 12:08:57 -0500 Subject: [PATCH 2/3] fix: corrected endpoint parameter in nopifyRequest --- lib/plugins/environments.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/plugins/environments.js b/lib/plugins/environments.js index 855d6432..e334de96 100644 --- a/lib/plugins/environments.js +++ b/lib/plugins/environments.js @@ -23,8 +23,9 @@ module.exports = class Environments extends Diffable { if (!this.nop) { await this.github.request(url, options); } else { + const endpoint = {url, body: options}; return Promise.resolve([ - new NopCommand(this.constructor.name, this.repo, url, description) + new NopCommand(this.constructor.name, this.repo, endpoint, description) ]); } } From 57b9777d985981f5ccb2072d40b28f38052b48ab Mon Sep 17 00:00:00 2001 From: randomcascade Date: Tue, 24 Sep 2024 12:15:15 -0500 Subject: [PATCH 3/3] fix: include updated test --- test/unit/lib/plugins/environments.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/unit/lib/plugins/environments.test.js b/test/unit/lib/plugins/environments.test.js index 854049af..1b174722 100644 --- a/test/unit/lib/plugins/environments.test.js +++ b/test/unit/lib/plugins/environments.test.js @@ -1098,7 +1098,7 @@ describe('nopifyRequest', () => { const result = await plugin.nopifyRequest(url, options, description); expect(result).toEqual([ - new NopCommand('Environments', { owner: org, repo }, url, description) + new NopCommand('Environments', { owner: org, repo }, {url, body: options}, description) ]); }); });