From 3b7eb052630a92b885d0f2ff1077b174e837dafd Mon Sep 17 00:00:00 2001 From: Cameron Goode Date: Sun, 19 Jan 2025 15:31:00 -0600 Subject: [PATCH] fix: made envs application actually NOP (#691) * feat: add nop to environments * fix: corrected endpoint parameter in nopifyRequest * fix: include updated test * Update environments.test.js --- lib/plugins/environments.js | 187 +++++++++++---------- test/unit/lib/plugins/environments.test.js | 37 ++++ 2 files changed, 138 insertions(+), 86 deletions(-) diff --git a/lib/plugins/environments.js b/lib/plugins/environments.js index 172e53c2..bc44ae37 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, @@ -63,7 +73,6 @@ module.exports = class Environments extends Diffable { environmentsMapped.push(mapped) // console.log(mapped); } - return environmentsMapped } @@ -117,9 +126,14 @@ module.exports = class Environments extends Diffable { async update (existing, attrs) { const { waitTimer, preventSelfReview, reviewers, deploymentBranchPolicy, variables, deploymentProtectionRules } = this.getChanged(existing, attrs) + const baseRequestOptions = { + org: this.repo.owner, + repo: this.repo.repo, + environment_name: attrs.name + } if (waitTimer || preventSelfReview || reviewers || deploymentBranchPolicy) { - await this.github.request('PUT /repos/:org/:repo/environments/:environment_name', { + const options = { org: this.repo.owner, repo: this.repo.repo, environment_name: attrs.name, @@ -132,32 +146,26 @@ 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 (deploymentBranchPolicy && 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 (const 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 (const 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') } } @@ -169,32 +177,32 @@ 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 (const 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' + ) } } @@ -205,84 +213,91 @@ 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 (const 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, + 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 (const 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' + ) } } if (attrs.variables) { - for (const 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 - }) + for(const variable of attrs.variables) { + await this.nopifyRequest( + 'POST /repos/:org/:repo/environments/:environment_name/variables', { + ...baseRequestOptions, + name: variable.name, + value: variable.value + }, 'Create environment variable' + ) + } } - } if (attrs.deployment_protection_rules) { for (const 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 () { const resArray = [] diff --git a/test/unit/lib/plugins/environments.test.js b/test/unit/lib/plugins/environments.test.js index 87e1bf97..8b5467de 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 @@ -1060,3 +1061,39 @@ 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) + ]); + }); +});