2
0

github-api-helper.test.ts 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import * as core from '@actions/core'
  2. import * as github from '@actions/github'
  3. import * as githubApiHelper from '../lib/github-api-helper'
  4. describe('github-api-helper object format', () => {
  5. let getOctokitSpy: jest.SpyInstance
  6. let debugSpy: jest.SpyInstance
  7. let request: jest.Mock
  8. function mockHashAlgorithmApi(hashAlgorithm: string): void {
  9. request = jest.fn(async () => ({
  10. data: {
  11. hash_algorithm: hashAlgorithm
  12. }
  13. }))
  14. getOctokitSpy = jest.spyOn(github, 'getOctokit').mockReturnValue({
  15. request
  16. } as any)
  17. }
  18. beforeEach(() => {
  19. debugSpy = jest.spyOn(core, 'debug').mockImplementation(jest.fn())
  20. })
  21. afterEach(() => {
  22. jest.restoreAllMocks()
  23. })
  24. it('detects SHA-256 from the repository hash algorithm endpoint', async () => {
  25. mockHashAlgorithmApi('sha256')
  26. await expect(
  27. githubApiHelper.tryGetRepositoryObjectFormat('token', 'owner', 'repo')
  28. ).resolves.toEqual({format: 'sha256', succeeded: true})
  29. expect(getOctokitSpy).toHaveBeenCalledWith(
  30. 'token',
  31. expect.objectContaining({baseUrl: 'https://api.github.com'})
  32. )
  33. expect(request).toHaveBeenCalledWith(
  34. 'GET /repos/{owner}/{repo}/hash-algorithm',
  35. {owner: 'owner', repo: 'repo'}
  36. )
  37. })
  38. it('detects SHA-1 from the repository hash algorithm endpoint', async () => {
  39. mockHashAlgorithmApi('sha1')
  40. await expect(
  41. githubApiHelper.tryGetRepositoryObjectFormat('token', 'owner', 'repo')
  42. ).resolves.toEqual({format: 'sha1', succeeded: true})
  43. })
  44. it('detects object format from an existing commit without API calls', async () => {
  45. const commitSha =
  46. '9422233ca7ee1b17f1e905d0e141faf0c401556c41cdc6acd71c6bd685da2e92'
  47. getOctokitSpy = jest.spyOn(github, 'getOctokit')
  48. await expect(
  49. githubApiHelper.tryGetRepositoryObjectFormat(
  50. 'token',
  51. 'owner',
  52. 'repo',
  53. undefined,
  54. commitSha
  55. )
  56. ).resolves.toEqual({format: 'sha256', succeeded: true})
  57. expect(getOctokitSpy).not.toHaveBeenCalled()
  58. })
  59. it('returns unsuccessful when the hash algorithm endpoint value is not recognized', async () => {
  60. mockHashAlgorithmApi('unknown')
  61. await expect(
  62. githubApiHelper.tryGetRepositoryObjectFormat('token', 'owner', 'repo')
  63. ).resolves.toEqual({format: '', succeeded: false})
  64. expect(debugSpy).toHaveBeenCalledWith(
  65. 'Unable to determine repository object format from hash-algorithm endpoint'
  66. )
  67. })
  68. it('returns unsuccessful when the hash algorithm API lookup fails', async () => {
  69. request = jest.fn(async () => {
  70. throw new Error('not found')
  71. })
  72. jest.spyOn(github, 'getOctokit').mockReturnValue({
  73. request
  74. } as any)
  75. await expect(
  76. githubApiHelper.tryGetRepositoryObjectFormat('token', 'owner', 'repo')
  77. ).resolves.toEqual({format: '', succeeded: false})
  78. expect(debugSpy).toHaveBeenCalledWith(
  79. 'Unable to determine repository object format from hash-algorithm endpoint: not found'
  80. )
  81. })
  82. })