|
1 | 1 | import * as assert from 'assert' |
| 2 | +import * as core from '@actions/core' |
| 3 | +import * as github from '@actions/github' |
2 | 4 | import * as refHelper from '../lib/ref-helper' |
3 | 5 | import {IGitCommandManager} from '../lib/git-command-manager' |
4 | 6 |
|
5 | 7 | const commit = '1234567890123456789012345678901234567890' |
| 8 | +const sha256Commit = |
| 9 | + '1234567890123456789012345678901234567890123456789012345678901234' |
6 | 10 | let git: IGitCommandManager |
7 | 11 |
|
8 | 12 | describe('ref-helper tests', () => { |
@@ -37,6 +41,12 @@ describe('ref-helper tests', () => { |
37 | 41 | expect(checkoutInfo.startPoint).toBeFalsy() |
38 | 42 | }) |
39 | 43 |
|
| 44 | + it('getCheckoutInfo sha-256 only', async () => { |
| 45 | + const checkoutInfo = await refHelper.getCheckoutInfo(git, '', sha256Commit) |
| 46 | + expect(checkoutInfo.ref).toBe(sha256Commit) |
| 47 | + expect(checkoutInfo.startPoint).toBeFalsy() |
| 48 | + }) |
| 49 | + |
40 | 50 | it('getCheckoutInfo refs/heads/', async () => { |
41 | 51 | const checkoutInfo = await refHelper.getCheckoutInfo( |
42 | 52 | git, |
@@ -227,4 +237,142 @@ describe('ref-helper tests', () => { |
227 | 237 | '+refs/heads/my/branch:refs/remotes/origin/my/branch' |
228 | 238 | ) |
229 | 239 | }) |
| 240 | + |
| 241 | + describe('checkCommitInfo', () => { |
| 242 | + const repositoryOwner = 'some-owner' |
| 243 | + const repositoryName = 'some-repo' |
| 244 | + const ref = 'refs/pull/123/merge' |
| 245 | + const sha1Head = '1111111111222222222233333333334444444444' |
| 246 | + const sha1Base = 'aaaaaaaaaabbbbbbbbbbccccccccccdddddddddd' |
| 247 | + const sha256Head = |
| 248 | + '1111111111222222222233333333334444444444555555555566666666667777' |
| 249 | + const sha256Base = |
| 250 | + 'aaaaaaaaaabbbbbbbbbbccccccccccddddddddddeeeeeeeeeeffffffffff0000' |
| 251 | + let debugSpy: jest.SpyInstance |
| 252 | + let getOctokitSpy: jest.SpyInstance |
| 253 | + let repoGetSpy: jest.Mock |
| 254 | + let originalEventName: string |
| 255 | + let originalPayload: unknown |
| 256 | + let originalRef: string |
| 257 | + let originalSha: string |
| 258 | + |
| 259 | + function setPullRequestContext( |
| 260 | + expectedHeadSha: string, |
| 261 | + expectedBaseSha: string, |
| 262 | + mergeCommit: string |
| 263 | + ): void { |
| 264 | + ;(github.context as any).eventName = 'pull_request' |
| 265 | + github.context.ref = ref |
| 266 | + github.context.sha = mergeCommit |
| 267 | + ;(github.context as any).payload = { |
| 268 | + action: 'synchronize', |
| 269 | + after: expectedHeadSha, |
| 270 | + number: 123, |
| 271 | + pull_request: { |
| 272 | + base: { |
| 273 | + sha: expectedBaseSha |
| 274 | + } |
| 275 | + }, |
| 276 | + repository: { |
| 277 | + private: false |
| 278 | + } |
| 279 | + } |
| 280 | + } |
| 281 | + |
| 282 | + beforeEach(() => { |
| 283 | + originalEventName = github.context.eventName |
| 284 | + originalPayload = github.context.payload |
| 285 | + originalRef = github.context.ref |
| 286 | + originalSha = github.context.sha |
| 287 | + |
| 288 | + jest.spyOn(github.context, 'repo', 'get').mockReturnValue({ |
| 289 | + owner: repositoryOwner, |
| 290 | + repo: repositoryName |
| 291 | + }) |
| 292 | + debugSpy = jest.spyOn(core, 'debug').mockImplementation(jest.fn()) |
| 293 | + repoGetSpy = jest.fn(async () => ({})) |
| 294 | + getOctokitSpy = jest.spyOn(github, 'getOctokit').mockReturnValue({ |
| 295 | + rest: { |
| 296 | + repos: { |
| 297 | + get: repoGetSpy |
| 298 | + } |
| 299 | + } |
| 300 | + } as any) |
| 301 | + }) |
| 302 | + |
| 303 | + afterEach(() => { |
| 304 | + ;(github.context as any).eventName = originalEventName |
| 305 | + ;(github.context as any).payload = originalPayload |
| 306 | + github.context.ref = originalRef |
| 307 | + github.context.sha = originalSha |
| 308 | + jest.restoreAllMocks() |
| 309 | + }) |
| 310 | + |
| 311 | + it('returns early for SHA-1 merge commit', async () => { |
| 312 | + setPullRequestContext(sha1Head, sha1Base, commit) |
| 313 | + |
| 314 | + await refHelper.checkCommitInfo( |
| 315 | + 'token', |
| 316 | + `Merge ${sha1Head} into ${sha1Base}`, |
| 317 | + repositoryOwner, |
| 318 | + repositoryName, |
| 319 | + ref, |
| 320 | + commit |
| 321 | + ) |
| 322 | + |
| 323 | + expect(getOctokitSpy).not.toHaveBeenCalled() |
| 324 | + expect(repoGetSpy).not.toHaveBeenCalled() |
| 325 | + }) |
| 326 | + |
| 327 | + it('matches SHA-256 merge commit info', async () => { |
| 328 | + const actualHeadSha = |
| 329 | + '9999999999888888888877777777776666666666555555555544444444443333' |
| 330 | + setPullRequestContext(sha256Head, sha256Base, sha256Commit) |
| 331 | + |
| 332 | + await refHelper.checkCommitInfo( |
| 333 | + 'token', |
| 334 | + `Merge ${actualHeadSha} into ${sha256Base}`, |
| 335 | + repositoryOwner, |
| 336 | + repositoryName, |
| 337 | + ref, |
| 338 | + sha256Commit |
| 339 | + ) |
| 340 | + |
| 341 | + expect(getOctokitSpy).toHaveBeenCalledWith( |
| 342 | + 'token', |
| 343 | + expect.objectContaining({ |
| 344 | + userAgent: expect.stringContaining( |
| 345 | + `expected_head_sha=${sha256Head};actual_head_sha=${actualHeadSha}` |
| 346 | + ) |
| 347 | + }) |
| 348 | + ) |
| 349 | + expect(repoGetSpy).toHaveBeenCalledWith({ |
| 350 | + owner: repositoryOwner, |
| 351 | + repo: repositoryName |
| 352 | + }) |
| 353 | + expect(debugSpy).toHaveBeenCalledWith( |
| 354 | + `Expected head sha ${sha256Head}; actual head sha ${actualHeadSha}` |
| 355 | + ) |
| 356 | + expect(debugSpy).not.toHaveBeenCalledWith('Unexpected message format') |
| 357 | + }) |
| 358 | + |
| 359 | + it('does not match 50-char hex as a valid merge', async () => { |
| 360 | + const invalidHeadSha = |
| 361 | + '99999999998888888888777777777766666666665555555555' |
| 362 | + setPullRequestContext(sha1Head, sha1Base, commit) |
| 363 | + |
| 364 | + await refHelper.checkCommitInfo( |
| 365 | + 'token', |
| 366 | + `Merge ${invalidHeadSha} into ${sha1Base}`, |
| 367 | + repositoryOwner, |
| 368 | + repositoryName, |
| 369 | + ref, |
| 370 | + commit |
| 371 | + ) |
| 372 | + |
| 373 | + expect(getOctokitSpy).not.toHaveBeenCalled() |
| 374 | + expect(repoGetSpy).not.toHaveBeenCalled() |
| 375 | + expect(debugSpy).toHaveBeenCalledWith('Unexpected message format') |
| 376 | + }) |
| 377 | + }) |
230 | 378 | }) |
0 commit comments