forked from repository-settings/app
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes repository-settings#607
- Loading branch information
Showing
6 changed files
with
508 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
const Diffable = require('./diffable') | ||
|
||
const environmentRepoEndpoint = '/repos/:org/:repo/environments/:environment_name' | ||
|
||
module.exports = class Environments extends Diffable { | ||
constructor (...args) { | ||
super(...args) | ||
|
||
if (this.entries) { | ||
// Force all names to lowercase to avoid comparison issues. | ||
this.entries.forEach(environment => { | ||
environment.name = environment.name.toLowerCase() | ||
}) | ||
} | ||
} | ||
|
||
async find () { | ||
const { | ||
data: { environments } | ||
} = await this.github.request('GET /repos/:org/:repo/environments', { | ||
org: this.repo.owner, | ||
repo: this.repo.repo | ||
}) | ||
return environments.map(environment => { | ||
return { | ||
...environment, | ||
// Force all names to lowercase to avoid comparison issues. | ||
name: environment.name.toLowerCase() | ||
} | ||
}) | ||
} | ||
|
||
comparator (existing, attrs) { | ||
return existing.name === attrs.name | ||
} | ||
|
||
changed (existing, attrs) { | ||
if (!attrs.wait_timer) attrs.wait_timer = 0 | ||
return ( | ||
(existing.wait_timer || 0) !== attrs.wait_timer || | ||
this.reviewersToString(existing.reviewers) !== this.reviewersToString(attrs.reviewers) || | ||
this.deploymentBranchPolicyToString(existing.deployment_branch_policy) !== | ||
this.deploymentBranchPolicyToString(attrs.deployment_branch_policy) | ||
) | ||
} | ||
|
||
update (existing, attrs) { | ||
return this.add(attrs) | ||
} | ||
|
||
add (attrs) { | ||
return this.github.request(`PUT ${environmentRepoEndpoint}`, this.toParams({ name: attrs.name }, attrs)) | ||
} | ||
|
||
remove (existing) { | ||
return this.github.request(`DELETE ${environmentRepoEndpoint}`, { | ||
environment_name: existing.name, | ||
repo: this.repo.repo, | ||
org: this.repo.owner | ||
}) | ||
} | ||
|
||
reviewersToString (attrs) { | ||
if (attrs === null || attrs === undefined) { | ||
return '' | ||
} else { | ||
attrs.sort((a, b) => { | ||
if (a.id < b.id) return -1 | ||
if (a.id > b.id) return 1 | ||
if (a.type < b.type) return -1 | ||
if (a.type > b.type) return 1 | ||
return 0 | ||
}) | ||
return JSON.stringify( | ||
attrs.map(reviewer => { | ||
return { | ||
id: reviewer.id, | ||
type: reviewer.type | ||
} | ||
}) | ||
) | ||
} | ||
} | ||
|
||
deploymentBranchPolicyToString (attrs) { | ||
if (attrs === null || attrs === undefined) { | ||
return '' | ||
} else { | ||
return JSON.stringify({ | ||
custom_branch_policies: attrs.custom_branch_policies, | ||
protected_branches: attrs.protected_branches | ||
}) | ||
} | ||
} | ||
|
||
toParams (existing, attrs) { | ||
return { | ||
environment_name: existing.name, | ||
repo: this.repo.repo, | ||
org: this.repo.owner, | ||
wait_timer: attrs.wait_timer, | ||
reviewers: attrs.reviewers, | ||
deployment_branch_policy: attrs.deployment_branch_policy | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
environments: | ||
- name: changed-wait-timer | ||
wait_timer: 10 | ||
|
||
- name: changed-reviewers-type | ||
reviewers: | ||
- id: 1 | ||
type: User | ||
- id: 2 | ||
type: User | ||
|
||
- name: changed-reviewers-id | ||
reviewers: | ||
- id: 1 | ||
type: Team | ||
- id: 3 | ||
type: User | ||
|
||
- name: changed-reviewers-remove | ||
reviewers: | ||
- id: 1 | ||
type: Team | ||
|
||
- name: changed-reviewers-add | ||
reviewers: | ||
- id: 1 | ||
type: Team | ||
- id: 2 | ||
type: User | ||
- id: 3 | ||
type: User | ||
|
||
- name: changed-deployment-branch-policy | ||
deployment_branch_policy: | ||
protected_branches: false | ||
custom_branch_policies: true | ||
|
||
- name: changed-all | ||
wait_timer: 10 | ||
reviewers: | ||
- id: 2 | ||
type: User | ||
deployment_branch_policy: | ||
protected_branches: false | ||
custom_branch_policies: true | ||
|
||
- name: new-environment | ||
wait_timer: 1 | ||
reviewers: | ||
- id: 1 | ||
type: Team | ||
- id: 2 | ||
type: User | ||
deployment_branch_policy: | ||
protected_branches: false | ||
custom_branch_policies: true | ||
|
||
- name: unchanged-default-wait-timer | ||
|
||
- name: unchanged-wait-timer | ||
wait_timer: 1 | ||
|
||
- name: unchanged-reviewers-unsorted | ||
reviewers: | ||
- id: 2 | ||
type: User | ||
- id: 1 | ||
type: Team | ||
|
||
- name: unchanged-reviewers-sorted | ||
reviewers: | ||
- id: 1 | ||
type: Team | ||
- id: 2 | ||
type: User | ||
|
||
- name: unchanged-deployment-branch-policy | ||
deployment_branch_policy: | ||
protected_branches: false | ||
custom_branch_policies: true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,166 @@ | ||
const path = require('path') | ||
const fs = require('fs') | ||
const { CREATED, NO_CONTENT, OK } = require('http-status-codes') | ||
const settings = require('../../../lib/settings') | ||
const { buildTriggerEvent, initializeNock, loadInstance, repository, teardownNock } = require('../common') | ||
|
||
describe('environments plugin', function () { | ||
let probot, githubScope | ||
|
||
beforeEach(async () => { | ||
githubScope = initializeNock() | ||
probot = await loadInstance() | ||
}) | ||
|
||
afterEach(() => { | ||
teardownNock(githubScope) | ||
}) | ||
|
||
it('syncs environments', async () => { | ||
const pathToConfig = path.resolve(__dirname, '..', '..', 'fixtures', 'environments-config.yml') | ||
const configFile = Buffer.from(fs.readFileSync(pathToConfig, 'utf8')) | ||
const config = configFile.toString() | ||
githubScope | ||
.get(`/repos/${repository.owner.name}/${repository.name}/contents/${encodeURIComponent(settings.FILE_NAME)}`) | ||
.reply(OK, config) | ||
githubScope.get(`/repos/${repository.owner.name}/${repository.name}/environments`).reply(OK, { | ||
environments: [ | ||
{ name: 'changed-wait-timer', wait_timer: 1 }, | ||
{ | ||
name: 'changed-reviewers-type', | ||
reviewers: [ | ||
{ id: 1, type: 'Team' }, | ||
{ id: 2, type: 'User' } | ||
] | ||
}, | ||
{ | ||
name: 'changed-reviewers-id', | ||
reviewers: [ | ||
{ id: 1, type: 'Team' }, | ||
{ id: 2, type: 'User' } | ||
] | ||
}, | ||
{ | ||
name: 'changed-reviewers-remove', | ||
reviewers: [ | ||
{ id: 1, type: 'Team' }, | ||
{ id: 2, type: 'User' } | ||
] | ||
}, | ||
{ | ||
name: 'changed-reviewers-add', | ||
reviewers: [ | ||
{ id: 1, type: 'Team' }, | ||
{ id: 2, type: 'User' } | ||
] | ||
}, | ||
{ | ||
name: 'changed-deployment-branch-policy', | ||
deployment_branch_policy: { protected_branches: true, custom_branch_policies: false } | ||
}, | ||
{ name: 'changed-all', wait_timer: 0 }, | ||
{ name: 'unchanged-default-wait-timer', wait_timer: 0 }, | ||
{ name: 'unchanged-wait-timer', wait_timer: 1 }, | ||
{ | ||
name: 'unchanged-reviewers-unsorted', | ||
reviewers: [ | ||
{ id: 1, type: 'Team' }, | ||
{ id: 2, type: 'User' } | ||
] | ||
}, | ||
{ | ||
name: 'unchanged-reviewers-sorted', | ||
reviewers: [ | ||
{ id: 1, type: 'Team' }, | ||
{ id: 2, type: 'User' } | ||
] | ||
}, | ||
{ | ||
name: 'unchanged-deployment-branch-policy', | ||
deployment_branch_policy: { protected_branches: false, custom_branch_policies: true } | ||
}, | ||
{ name: 'deleted', wait_timer: 0 } | ||
] | ||
}) | ||
githubScope | ||
.put(`/repos/${repository.owner.name}/${repository.name}/environments/changed-wait-timer`, body => { | ||
expect(body).toMatchObject({ wait_timer: 10 }) | ||
return true | ||
}) | ||
.reply(CREATED) | ||
githubScope | ||
.put(`/repos/${repository.owner.name}/${repository.name}/environments/changed-reviewers-type`, body => { | ||
expect(body).toMatchObject({ | ||
reviewers: [ | ||
{ id: 1, type: 'User' }, | ||
{ id: 2, type: 'User' } | ||
] | ||
}) | ||
return true | ||
}) | ||
.reply(CREATED) | ||
githubScope | ||
.put(`/repos/${repository.owner.name}/${repository.name}/environments/changed-reviewers-id`, body => { | ||
expect(body).toMatchObject({ | ||
reviewers: [ | ||
{ id: 1, type: 'Team' }, | ||
{ id: 3, type: 'User' } | ||
] | ||
}) | ||
return true | ||
}) | ||
.reply(CREATED) | ||
githubScope | ||
.put(`/repos/${repository.owner.name}/${repository.name}/environments/changed-reviewers-remove`, body => { | ||
expect(body).toMatchObject({ reviewers: [{ id: 1, type: 'Team' }] }) | ||
return true | ||
}) | ||
.reply(CREATED) | ||
githubScope | ||
.put(`/repos/${repository.owner.name}/${repository.name}/environments/changed-reviewers-add`, body => { | ||
expect(body).toMatchObject({ | ||
reviewers: [ | ||
{ id: 1, type: 'Team' }, | ||
{ id: 2, type: 'User' }, | ||
{ id: 3, type: 'User' } | ||
] | ||
}) | ||
return true | ||
}) | ||
.reply(CREATED) | ||
githubScope | ||
.put(`/repos/${repository.owner.name}/${repository.name}/environments/changed-deployment-branch-policy`, body => { | ||
expect(body).toMatchObject({ | ||
deployment_branch_policy: { protected_branches: false, custom_branch_policies: true } | ||
}) | ||
return true | ||
}) | ||
.reply(CREATED) | ||
githubScope | ||
.put(`/repos/${repository.owner.name}/${repository.name}/environments/changed-all`, body => { | ||
expect(body).toMatchObject({ | ||
wait_timer: 10, | ||
reviewers: [{ id: 2, type: 'User' }], | ||
deployment_branch_policy: { protected_branches: false, custom_branch_policies: true } | ||
}) | ||
return true | ||
}) | ||
.reply(CREATED) | ||
githubScope | ||
.put(`/repos/${repository.owner.name}/${repository.name}/environments/new-environment`, body => { | ||
expect(body).toMatchObject({ | ||
wait_timer: 1, | ||
reviewers: [ | ||
{ id: 1, type: 'Team' }, | ||
{ id: 2, type: 'User' } | ||
], | ||
deployment_branch_policy: { protected_branches: false, custom_branch_policies: true } | ||
}) | ||
return true | ||
}) | ||
.reply(CREATED) | ||
githubScope.delete(`/repos/${repository.owner.name}/${repository.name}/environments/deleted`).reply(NO_CONTENT) | ||
|
||
await probot.receive(buildTriggerEvent()) | ||
}) | ||
}) |
Oops, something went wrong.