|
| 1 | +/* eslint-disable no-undef */ |
| 2 | + |
| 3 | +const { when } = require('jest-when') |
| 4 | +const Rulesets = require('../../../../lib/plugins/rulesets') |
| 5 | +const version = { |
| 6 | + 'X-GitHub-Api-Version': '2022-11-28' |
| 7 | +} |
| 8 | + |
| 9 | +function generateRequestRuleset(id, name, checks) { |
| 10 | + return { |
| 11 | + id: id, |
| 12 | + name: name, |
| 13 | + source_type: 'Repository', |
| 14 | + target: 'branch', |
| 15 | + enforcement: 'active', |
| 16 | + conditions: { |
| 17 | + ref_name: { |
| 18 | + include: ['~ALL'], |
| 19 | + exclude: [] |
| 20 | + } |
| 21 | + }, |
| 22 | + rules: [ |
| 23 | + { |
| 24 | + type: 'required_status_checks', |
| 25 | + parameters: { |
| 26 | + strict_required_status_checks_policy: true, |
| 27 | + required_status_checks: checks |
| 28 | + } |
| 29 | + } |
| 30 | + ] |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +function generateResponseRuleset(id, name, checks) { |
| 35 | + return { |
| 36 | + id: id, |
| 37 | + name: name, |
| 38 | + source_type: 'Repository', |
| 39 | + target: 'branch', |
| 40 | + enforcement: 'active', |
| 41 | + conditions: { |
| 42 | + ref_name: { |
| 43 | + include: ['~ALL'], |
| 44 | + exclude: [] |
| 45 | + } |
| 46 | + }, |
| 47 | + owner: 'jitran', |
| 48 | + repo: 'test', |
| 49 | + rules: [ |
| 50 | + { |
| 51 | + type: 'required_status_checks', |
| 52 | + parameters: { |
| 53 | + strict_required_status_checks_policy: true, |
| 54 | + required_status_checks: checks |
| 55 | + } |
| 56 | + } |
| 57 | + ], |
| 58 | + headers: version, |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +describe('Rulesets', () => { |
| 63 | + let github |
| 64 | + const log = jest.fn() |
| 65 | + log.debug = jest.fn() |
| 66 | + log.error = jest.fn() |
| 67 | + |
| 68 | + function configure (config) { |
| 69 | + const noop = false |
| 70 | + const errors = [] |
| 71 | + return new Rulesets(noop, github, { owner: 'jitran', repo: 'test' }, config, log, errors) |
| 72 | + } |
| 73 | + |
| 74 | + beforeEach(() => { |
| 75 | + github = { |
| 76 | + repos: { |
| 77 | + get: jest.fn().mockResolvedValue({ |
| 78 | + data: { |
| 79 | + default_branch: 'main' |
| 80 | + } |
| 81 | + }) |
| 82 | + }, |
| 83 | + request: jest.fn().mockImplementation(() => Promise.resolve('request')), |
| 84 | + } |
| 85 | + |
| 86 | + github.request.endpoint = { |
| 87 | + merge: jest.fn().mockReturnValue({ |
| 88 | + method: 'GET', |
| 89 | + url: '/repos/jitran/test/rulesets', |
| 90 | + headers: version |
| 91 | + }) |
| 92 | + } |
| 93 | + }) |
| 94 | + |
| 95 | + describe('sync', () => { |
| 96 | + it('syncs ruleset settings', () => { |
| 97 | + // Mock the GitHub API response |
| 98 | + github.paginate = jest.fn().mockResolvedValue([]) |
| 99 | + |
| 100 | + // Initialise safe-settings |
| 101 | + const plugin = configure( |
| 102 | + [ |
| 103 | + generateRequestRuleset( |
| 104 | + 1, |
| 105 | + 'All branches', |
| 106 | + [ |
| 107 | + { context: 'Status Check 1' }, |
| 108 | + { context: 'Status Check 2' } |
| 109 | + ] |
| 110 | + ) |
| 111 | + ] |
| 112 | + ) |
| 113 | + |
| 114 | + return plugin.sync().then(() => { |
| 115 | + expect(github.request).toHaveBeenLastCalledWith( |
| 116 | + 'POST /repos/{owner}/{repo}/rulesets', |
| 117 | + generateResponseRuleset( |
| 118 | + 1, |
| 119 | + 'All branches', |
| 120 | + [ |
| 121 | + { context: 'Status Check 1' }, |
| 122 | + { context: 'Status Check 2' } |
| 123 | + ] |
| 124 | + ) |
| 125 | + ) |
| 126 | + }) |
| 127 | + }) |
| 128 | + }) |
| 129 | + |
| 130 | + describe('when {{EXTERNALLY_DEFINED}} is present in "required_status_checks" and no status checks exists in GitHub', () => { |
| 131 | + it('it initialises the status checks with an empty list', () => { |
| 132 | + // Mock the GitHub API response |
| 133 | + github.paginate = jest.fn().mockResolvedValue([]) |
| 134 | + |
| 135 | + // Initialise safe-settings |
| 136 | + const plugin = configure( |
| 137 | + [ |
| 138 | + generateRequestRuleset( |
| 139 | + 1, |
| 140 | + 'All branches', |
| 141 | + [ |
| 142 | + { context: 'Status Check 1' }, |
| 143 | + { context: '{{EXTERNALLY_DEFINED}}' } |
| 144 | + ] |
| 145 | + ) |
| 146 | + ] |
| 147 | + ) |
| 148 | + |
| 149 | + return plugin.sync().then(() => { |
| 150 | + expect(github.request).toHaveBeenLastCalledWith( |
| 151 | + 'POST /repos/{owner}/{repo}/rulesets', |
| 152 | + generateResponseRuleset( |
| 153 | + 1, |
| 154 | + 'All branches', |
| 155 | + [] |
| 156 | + ) |
| 157 | + ) |
| 158 | + }) |
| 159 | + }) |
| 160 | + }) |
| 161 | + |
| 162 | + describe('when {{EXTERNALLY_DEFINED}} is present in "required_status_checks" and status checks exists in GitHub', () => { |
| 163 | + it('it retains the status checks from GitHub and everything else is reset to the safe-settings', () => { |
| 164 | + // Mock the GitHub API response |
| 165 | + github.paginate = jest.fn().mockResolvedValue([ |
| 166 | + generateRequestRuleset( |
| 167 | + 1, |
| 168 | + 'All branches 1', |
| 169 | + [ |
| 170 | + { context: 'Custom Check 1' }, |
| 171 | + { context: 'Custom Check 2' } |
| 172 | + ] |
| 173 | + ), |
| 174 | + generateRequestRuleset( |
| 175 | + 2, |
| 176 | + 'All branches 2', |
| 177 | + [ |
| 178 | + { context: 'Custom Check 3' }, |
| 179 | + { context: 'Custom Check 4' } |
| 180 | + ] |
| 181 | + ), |
| 182 | + generateRequestRuleset( |
| 183 | + 3, |
| 184 | + 'All branches 3', |
| 185 | + [ |
| 186 | + { context: 'Custom Check 5' }, |
| 187 | + { context: 'Custom Check 6' } |
| 188 | + ] |
| 189 | + ) |
| 190 | + ]) |
| 191 | + |
| 192 | + // Initialise safe-settings |
| 193 | + const plugin = configure( |
| 194 | + [ |
| 195 | + generateRequestRuleset( |
| 196 | + 1, |
| 197 | + 'All branches 1', |
| 198 | + [ |
| 199 | + { context: 'Status Check 1' }, |
| 200 | + { context: '{{EXTERNALLY_DEFINED}}' } |
| 201 | + ] |
| 202 | + ), |
| 203 | + generateRequestRuleset( |
| 204 | + 2, |
| 205 | + 'All branches 2', |
| 206 | + [ |
| 207 | + { context: 'Status Check 1' }, |
| 208 | + { context: 'Status Check 2' } |
| 209 | + ] |
| 210 | + ), |
| 211 | + generateRequestRuleset( |
| 212 | + 3, |
| 213 | + 'All branches 3', |
| 214 | + [] |
| 215 | + ) |
| 216 | + ] |
| 217 | + ) |
| 218 | + |
| 219 | + return plugin.sync().then(() => { |
| 220 | + expect(github.request).toHaveBeenNthCalledWith( |
| 221 | + 1, |
| 222 | + 'PUT /repos/{owner}/{repo}/rulesets/{id}', |
| 223 | + generateResponseRuleset( |
| 224 | + 1, |
| 225 | + 'All branches 1', |
| 226 | + [ |
| 227 | + { context: 'Custom Check 1' }, |
| 228 | + { context: 'Custom Check 2' } |
| 229 | + ] |
| 230 | + ) |
| 231 | + ) |
| 232 | + expect(github.request).toHaveBeenNthCalledWith( |
| 233 | + 2, |
| 234 | + 'PUT /repos/{owner}/{repo}/rulesets/{id}', |
| 235 | + generateResponseRuleset( |
| 236 | + 2, |
| 237 | + 'All branches 2', |
| 238 | + [ |
| 239 | + { context: 'Status Check 1' }, |
| 240 | + { context: 'Status Check 2' } |
| 241 | + ] |
| 242 | + ) |
| 243 | + ) |
| 244 | + expect(github.request).toHaveBeenNthCalledWith( |
| 245 | + 3, |
| 246 | + 'PUT /repos/{owner}/{repo}/rulesets/{id}', |
| 247 | + generateResponseRuleset( |
| 248 | + 3, |
| 249 | + 'All branches 3', |
| 250 | + [] |
| 251 | + ) |
| 252 | + ) |
| 253 | + }) |
| 254 | + }) |
| 255 | + }) |
| 256 | + // TODO: Write tests for org rulesets |
| 257 | +}) |
0 commit comments