diff --git a/lib/plugins/environments.js b/lib/plugins/environments.js index e146bf666..526fef96a 100644 --- a/lib/plugins/environments.js +++ b/lib/plugins/environments.js @@ -6,18 +6,6 @@ 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() - if (environment.variables) { - environment.variables.forEach(variable => { - variable.name = variable.name.toLowerCase() - }) - } - }) - } - // Remove 'name' from filtering list so Environments with only a name defined are processed. MergeDeep.NAME_FIELDS.splice(MergeDeep.NAME_FIELDS.indexOf('name'), 1) } @@ -53,7 +41,7 @@ module.exports = class Environments extends Diffable { org: this.repo.owner, repo: this.repo.repo, environment_name: environment.name - })).data.variables.map(variable => ({ name: variable.name.toLowerCase(), value: variable.value })), + })).data.variables.map(variable => ({ name: variable.name, value: variable.value })), deployment_protection_rules: (await this.github.request('GET /repos/:org/:repo/environments/:environment_name/deployment_protection_rules', { org: this.repo.owner, repo: this.repo.repo, @@ -106,7 +94,7 @@ module.exports = class Environments extends Diffable { ) } - const variables = JSON.stringify(existing.variables.sort((x1, x2) => x1.name - x2.name)) !== JSON.stringify(attrs.variables.sort((x1, x2) => x1.name - x2.name)) + const variables = JSON.stringify(existing.variables.sort((x1, x2) => x1.name.toLowerCase() - x2.name.toLowerCase())) !== JSON.stringify(attrs.variables.sort((x1, x2) => x1.name.toLowerCase() - x2.name.toLowerCase())) const deployment_protection_rules = JSON.stringify(existing.deployment_protection_rules.map(x => ({ app_id: x.app_id })).sort((x1, x2) => x1.app_id - x2.app_id)) !== JSON.stringify(attrs.deployment_protection_rules.map(x => ({ app_id: x.app_id })).sort((x1, x2) => x1.app_id - x2.app_id)) return { wait_timer, prevent_self_review, reviewers, deployment_branch_policy, variables, deployment_protection_rules } @@ -168,9 +156,9 @@ module.exports = class Environments extends Diffable { let existingVariables = [...existing.variables] for (const variable of attrs.variables) { - const existingVariable = existingVariables.find((_var) => _var.name === variable.name) + const existingVariable = existingVariables.find((_var) => _var.name.toLowerCase() === variable.name.toLowerCase()) if (existingVariable) { - existingVariables = existingVariables.filter(_var => _var.name !== variable.name) + existingVariables = existingVariables.filter(_var => _var.name.toLowerCase() !== variable.name.toLowerCase()) if (existingVariable.value !== variable.value) { await this.github.request('PATCH /repos/:org/:repo/environments/:environment_name/variables/:variable_name', { org: this.repo.owner, diff --git a/test/unit/lib/plugins/environments.test.js b/test/unit/lib/plugins/environments.test.js index efa97e039..3fba8bf9c 100644 --- a/test/unit/lib/plugins/environments.test.js +++ b/test/unit/lib/plugins/environments.test.js @@ -365,7 +365,7 @@ describe('Environments Plugin test suite', () => { name: environment_name, variables: [ { - name: 'test', + name: 'TEST', value: 'test' } ] @@ -396,7 +396,7 @@ describe('Environments Plugin test suite', () => { org, repo, environment_name: environment_name, - name: 'test', + name: 'TEST', value: 'test' })); }) @@ -414,7 +414,73 @@ describe('Environments Plugin test suite', () => { name: environment_name, variables: [ { - name: 'test', + name: 'TEST', + value: 'test' + } + ] + } + ], log, errors); + + //model an existing environment with no reviewers + when(github.request) + .calledWith('GET /repos/:org/:repo/environments', { org, repo }) + .mockResolvedValue({ + data: { + environments: [ + fillEnvironment({ + name: environment_name, + variables: [] + }) + ] + } + }); + + when(github.request) + .calledWith('GET /repos/:org/:repo/environments/:environment_name/variables', { org, repo, environment_name }) + .mockResolvedValue({ + data: { + variables: [ + { + name: 'TEST', + value: 'old' + }, + { + name: 'TEST2', + value: 'test2' + } + ] + } + }) + + //act - run sync() in environments.js + await plugin.sync().then(() => { + //assert - update the variables + expect(github.request).toHaveBeenCalledWith('GET /repos/:org/:repo/environments', { org, repo }); + expect(github.request).toHaveBeenCalledWith('GET /repos/:org/:repo/environments/:environment_name/variables', { org, repo, environment_name }); + expect(github.request).toHaveBeenCalledWith('GET /repos/:org/:repo/environments/:environment_name/deployment_protection_rules', { org, repo, environment_name }); + expect(github.request).toHaveBeenCalledWith('PATCH /repos/:org/:repo/environments/:environment_name/variables/:variable_name', expect.objectContaining({ + org, + repo, + environment_name: environment_name, + variable_name: 'TEST', + value: 'test' + })); + }) + }) + }) + + // patch variable + describe('When there are existing lowercase variables and one requires an update', () => { + it('detect divergence and add the variable', async () => { + //arrange + environment_name = 'variables_environment' + // represent config with a reviewers being a user and a team + const plugin = new Environments(undefined, github, { owner: org, repo }, [ + { + name: environment_name, + variables: [ + { + name: 'TEST', value: 'test' } ] @@ -462,7 +528,7 @@ describe('Environments Plugin test suite', () => { org, repo, environment_name: environment_name, - variable_name: 'test', + variable_name: 'TEST', value: 'test' })); }) @@ -702,7 +768,7 @@ describe('Environments Plugin test suite', () => { name: 'variables_environment', variables: [ { - name: 'test', + name: 'TEST', value: 'test' } ] @@ -844,7 +910,7 @@ describe('Environments Plugin test suite', () => { org, repo, environment_name: 'variables_environment', - name: 'test', + name: 'TEST', value: 'test' })); @@ -906,7 +972,7 @@ describe('Environments Plugin test suite', () => { name: 'variables_environment', variables: [ { - name: 'test', + name: 'TEST', value: 'test' } ] @@ -961,7 +1027,7 @@ describe('Environments Plugin test suite', () => { name: 'new-variables', variables: [ { - name: 'test', + name: 'TEST', value: 'test' } ] @@ -1103,7 +1169,7 @@ describe('Environments Plugin test suite', () => { org, repo, environment_name: 'variables_environment', - name: 'test', + name: 'TEST', value: 'test' }));