Skip to content

Commit

Permalink
fix: made envs application actually NOP (#691)
Browse files Browse the repository at this point in the history
* feat: add nop to environments

* fix: corrected endpoint parameter in nopifyRequest

* fix: include updated test

* Update environments.test.js
  • Loading branch information
randomcascade authored Jan 19, 2025
1 parent 1fff61c commit 3b7eb05
Show file tree
Hide file tree
Showing 2 changed files with 138 additions and 86 deletions.
187 changes: 101 additions & 86 deletions lib/plugins/environments.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -63,7 +73,6 @@ module.exports = class Environments extends Diffable {
environmentsMapped.push(mapped)
// console.log(mapped);
}

return environmentsMapped
}

Expand Down Expand Up @@ -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,
Expand All @@ -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')
}
}

Expand All @@ -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'
)
}
}

Expand All @@ -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 = []
Expand Down
37 changes: 37 additions & 0 deletions test/unit/lib/plugins/environments.test.js
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)
]);
});
});

0 comments on commit 3b7eb05

Please sign in to comment.