|
| 1 | +const Diffable = require('./diffable') |
| 2 | + |
| 3 | +const environmentRepoEndpoint = '/repos/:org/:repo/environments/:environment_name' |
| 4 | + |
| 5 | +module.exports = class Environments extends Diffable { |
| 6 | + constructor (...args) { |
| 7 | + super(...args) |
| 8 | + |
| 9 | + if (this.entries) { |
| 10 | + // Force all names to lowercase to avoid comparison issues. |
| 11 | + this.entries.forEach(environment => { |
| 12 | + environment.name = environment.name.toLowerCase() |
| 13 | + }) |
| 14 | + } |
| 15 | + } |
| 16 | + |
| 17 | + async find () { |
| 18 | + const { |
| 19 | + data: { environments } |
| 20 | + } = await this.github.request('GET /repos/:org/:repo/environments', { |
| 21 | + org: this.repo.owner, |
| 22 | + repo: this.repo.repo |
| 23 | + }) |
| 24 | + return Promise.all( |
| 25 | + environments.map(async environment => { |
| 26 | + if (environment.deployment_branch_policy) { |
| 27 | + if (environment.deployment_branch_policy.custom_branch_policies) { |
| 28 | + const branchPolicies = await this.getDeploymentBranchPolicies( |
| 29 | + this.repo.owner, |
| 30 | + this.repo.repo, |
| 31 | + environment.name |
| 32 | + ) |
| 33 | + environment.deployment_branch_policy = { |
| 34 | + custom_branches: branchPolicies.map(_ => _.name) |
| 35 | + } |
| 36 | + } else { |
| 37 | + environment.deployment_branch_policy = { |
| 38 | + protected_branches: true |
| 39 | + } |
| 40 | + } |
| 41 | + } |
| 42 | + return { |
| 43 | + ...environment, |
| 44 | + // Force all names to lowercase to avoid comparison issues. |
| 45 | + name: environment.name.toLowerCase() |
| 46 | + } |
| 47 | + }) |
| 48 | + ) |
| 49 | + } |
| 50 | + |
| 51 | + comparator (existing, attrs) { |
| 52 | + return existing.name === attrs.name |
| 53 | + } |
| 54 | + |
| 55 | + changed (existing, attrs) { |
| 56 | + if (!attrs.wait_timer) attrs.wait_timer = 0 |
| 57 | + return ( |
| 58 | + (existing.wait_timer || 0) !== attrs.wait_timer || |
| 59 | + this.reviewersToString(existing.reviewers) !== this.reviewersToString(attrs.reviewers) || |
| 60 | + this.deploymentBranchPolicyToString(existing.deployment_branch_policy) !== |
| 61 | + this.deploymentBranchPolicyToString(attrs.deployment_branch_policy) |
| 62 | + ) |
| 63 | + } |
| 64 | + |
| 65 | + async update (existing, attrs) { |
| 66 | + if (existing.deployment_branch_policy && existing.deployment_branch_policy.custom_branches) { |
| 67 | + const branchPolicies = await this.getDeploymentBranchPolicies(this.repo.owner, this.repo.repo, existing.name) |
| 68 | + await Promise.all( |
| 69 | + branchPolicies.map(branchPolicy => |
| 70 | + this.github.request( |
| 71 | + 'DELETE /repos/:org/:repo/environments/:environment_name/deployment-branch-policies/:id', |
| 72 | + { |
| 73 | + org: this.repo.owner, |
| 74 | + repo: this.repo.repo, |
| 75 | + environment_name: existing.name, |
| 76 | + id: branchPolicy.id |
| 77 | + } |
| 78 | + ) |
| 79 | + ) |
| 80 | + ) |
| 81 | + } |
| 82 | + return this.add(attrs) |
| 83 | + } |
| 84 | + |
| 85 | + async add (attrs) { |
| 86 | + await this.github.request(`PUT ${environmentRepoEndpoint}`, this.toParams({ name: attrs.name }, attrs)) |
| 87 | + if (attrs.deployment_branch_policy && attrs.deployment_branch_policy.custom_branches) { |
| 88 | + await Promise.all( |
| 89 | + attrs.deployment_branch_policy.custom_branches.map(name => |
| 90 | + this.github.request(`POST /repos/:org/:repo/environments/:environment_name/deployment-branch-policies`, { |
| 91 | + org: this.repo.owner, |
| 92 | + repo: this.repo.repo, |
| 93 | + environment_name: attrs.name, |
| 94 | + name |
| 95 | + }) |
| 96 | + ) |
| 97 | + ) |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + remove (existing) { |
| 102 | + return this.github.request(`DELETE ${environmentRepoEndpoint}`, { |
| 103 | + environment_name: existing.name, |
| 104 | + repo: this.repo.repo, |
| 105 | + org: this.repo.owner |
| 106 | + }) |
| 107 | + } |
| 108 | + |
| 109 | + reviewersToString (attrs) { |
| 110 | + if (attrs === null || attrs === undefined) { |
| 111 | + return '' |
| 112 | + } else { |
| 113 | + attrs.sort((a, b) => { |
| 114 | + if (a.id < b.id) return -1 |
| 115 | + if (a.id > b.id) return 1 |
| 116 | + if (a.type < b.type) return -1 |
| 117 | + if (a.type > b.type) return 1 |
| 118 | + return 0 |
| 119 | + }) |
| 120 | + return JSON.stringify( |
| 121 | + attrs.map(reviewer => { |
| 122 | + return { |
| 123 | + id: reviewer.id, |
| 124 | + type: reviewer.type |
| 125 | + } |
| 126 | + }) |
| 127 | + ) |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + deploymentBranchPolicyToString (attrs) { |
| 132 | + if (attrs === null || attrs === undefined) { |
| 133 | + return '' |
| 134 | + } else { |
| 135 | + return JSON.stringify( |
| 136 | + this.shouldUseProtectedBranches(attrs.protected_branches, attrs.custom_branches) |
| 137 | + ? { protected_branches: true } |
| 138 | + : { custom_branches: attrs.custom_branches.sort() } |
| 139 | + ) |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + async getDeploymentBranchPolicies (owner, repo, environmentName) { |
| 144 | + const { |
| 145 | + data: { branch_policies: branchPolicies } |
| 146 | + } = await this.github.request('GET /repos/:org/:repo/environments/:environment_name/deployment-branch-policies', { |
| 147 | + org: owner, |
| 148 | + repo, |
| 149 | + environment_name: environmentName |
| 150 | + }) |
| 151 | + return branchPolicies |
| 152 | + } |
| 153 | + |
| 154 | + toParams (existing, attrs) { |
| 155 | + const deploymentBranchPolicy = attrs.deployment_branch_policy |
| 156 | + ? this.shouldUseProtectedBranches( |
| 157 | + attrs.deployment_branch_policy.protected_branches, |
| 158 | + attrs.deployment_branch_policy.custom_branches |
| 159 | + ) |
| 160 | + ? { protected_branches: true, custom_branch_policies: false } |
| 161 | + : { protected_branches: false, custom_branch_policies: true } |
| 162 | + : null |
| 163 | + return { |
| 164 | + environment_name: existing.name, |
| 165 | + repo: this.repo.repo, |
| 166 | + org: this.repo.owner, |
| 167 | + wait_timer: attrs.wait_timer, |
| 168 | + reviewers: attrs.reviewers, |
| 169 | + deployment_branch_policy: deploymentBranchPolicy |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + shouldUseProtectedBranches (protectedBranches, customBranchPolicies) { |
| 174 | + if (protectedBranches || customBranchPolicies === undefined || customBranchPolicies === null) { |
| 175 | + return true // Returning booleans like this to avoid unexpected datatypes that result in truthy values |
| 176 | + } else { |
| 177 | + return false |
| 178 | + } |
| 179 | + } |
| 180 | +} |
0 commit comments