Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: made envs application actually NOP #691

Open
wants to merge 3 commits into
base: main-enterprise
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
194 changes: 109 additions & 85 deletions lib/plugins/environments.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,17 @@ module.exports = class Environments extends Diffable {
}
}

async nopifyRequest(url, options, description) {
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, endpoint, description)
]);
}
}

async find() {
const { data: { environments } } = await this.github.request('GET /repos/:org/:repo/environments', {
org: this.repo.owner,
Expand Down Expand Up @@ -117,44 +128,43 @@ 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,
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
}
})
};

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');
}
}

Expand All @@ -166,33 +176,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'
);
}
}

Expand All @@ -203,87 +213,101 @@ 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'
);
}

}

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'
);
}

}

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 () {
Expand Down
38 changes: 38 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 @@ -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, body: options}, description)
]);
});
});