2
0

input-helper.test.ts 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. import * as core from '@actions/core'
  2. import * as fsHelper from '../lib/fs-helper'
  3. import * as github from '@actions/github'
  4. import * as inputHelper from '../lib/input-helper'
  5. import * as path from 'path'
  6. import * as workflowContextHelper from '../lib/workflow-context-helper'
  7. import {IGitSourceSettings} from '../lib/git-source-settings'
  8. const originalGitHubWorkspace = process.env['GITHUB_WORKSPACE']
  9. const gitHubWorkspace = path.resolve('/checkout-tests/workspace')
  10. // Inputs for mock @actions/core
  11. let inputs = {} as any
  12. // Shallow clone original @actions/github context
  13. let originalContext = {...github.context}
  14. describe('input-helper tests', () => {
  15. beforeAll(() => {
  16. // Mock getInput
  17. jest.spyOn(core, 'getInput').mockImplementation((name: string) => {
  18. return inputs[name]
  19. })
  20. // Mock error/warning/info/debug
  21. jest.spyOn(core, 'error').mockImplementation(jest.fn())
  22. jest.spyOn(core, 'warning').mockImplementation(jest.fn())
  23. jest.spyOn(core, 'info').mockImplementation(jest.fn())
  24. jest.spyOn(core, 'debug').mockImplementation(jest.fn())
  25. // Mock github context
  26. jest.spyOn(github.context, 'repo', 'get').mockImplementation(() => {
  27. return {
  28. owner: 'some-owner',
  29. repo: 'some-repo'
  30. }
  31. })
  32. github.context.ref = 'refs/heads/some-ref'
  33. github.context.sha = '1234567890123456789012345678901234567890'
  34. // Mock ./fs-helper directoryExistsSync()
  35. jest
  36. .spyOn(fsHelper, 'directoryExistsSync')
  37. .mockImplementation((path: string) => path == gitHubWorkspace)
  38. // Mock ./workflowContextHelper getOrganizationId()
  39. jest
  40. .spyOn(workflowContextHelper, 'getOrganizationId')
  41. .mockImplementation(() => Promise.resolve(123456))
  42. // GitHub workspace
  43. process.env['GITHUB_WORKSPACE'] = gitHubWorkspace
  44. })
  45. beforeEach(() => {
  46. // Reset inputs
  47. inputs = {}
  48. })
  49. afterAll(() => {
  50. // Restore GitHub workspace
  51. delete process.env['GITHUB_WORKSPACE']
  52. if (originalGitHubWorkspace) {
  53. process.env['GITHUB_WORKSPACE'] = originalGitHubWorkspace
  54. }
  55. // Restore @actions/github context
  56. github.context.ref = originalContext.ref
  57. github.context.sha = originalContext.sha
  58. // Restore
  59. jest.restoreAllMocks()
  60. })
  61. it('sets defaults', async () => {
  62. const settings: IGitSourceSettings = await inputHelper.getInputs()
  63. expect(settings).toBeTruthy()
  64. expect(settings.authToken).toBeFalsy()
  65. expect(settings.clean).toBe(true)
  66. expect(settings.commit).toBeTruthy()
  67. expect(settings.commit).toBe('1234567890123456789012345678901234567890')
  68. expect(settings.fetchDepth).toBe(1)
  69. expect(settings.lfs).toBe(false)
  70. expect(settings.ref).toBe('refs/heads/some-ref')
  71. expect(settings.repositoryName).toBe('some-repo')
  72. expect(settings.repositoryOwner).toBe('some-owner')
  73. expect(settings.repositoryPath).toBe(gitHubWorkspace)
  74. expect(settings.setSafeDirectory).toBe(true)
  75. expect(settings.allowUnsafePrCheckout).toBe(false)
  76. })
  77. it('qualifies ref', async () => {
  78. let originalRef = github.context.ref
  79. try {
  80. github.context.ref = 'some-unqualified-ref'
  81. const settings: IGitSourceSettings = await inputHelper.getInputs()
  82. expect(settings).toBeTruthy()
  83. expect(settings.commit).toBe('1234567890123456789012345678901234567890')
  84. expect(settings.ref).toBe('refs/heads/some-unqualified-ref')
  85. } finally {
  86. github.context.ref = originalRef
  87. }
  88. })
  89. it('requires qualified repo', async () => {
  90. inputs.repository = 'some-unqualified-repo'
  91. try {
  92. await inputHelper.getInputs()
  93. throw 'should not reach here'
  94. } catch (err) {
  95. expect(`(${(err as any).message}`).toMatch(
  96. "Invalid repository 'some-unqualified-repo'"
  97. )
  98. }
  99. })
  100. it('roots path', async () => {
  101. inputs.path = 'some-directory/some-subdirectory'
  102. const settings: IGitSourceSettings = await inputHelper.getInputs()
  103. expect(settings.repositoryPath).toBe(
  104. path.join(gitHubWorkspace, 'some-directory', 'some-subdirectory')
  105. )
  106. })
  107. it('sets ref to empty when explicit sha', async () => {
  108. inputs.ref = '1111111111222222222233333333334444444444'
  109. const settings: IGitSourceSettings = await inputHelper.getInputs()
  110. expect(settings.ref).toBeFalsy()
  111. expect(settings.commit).toBe('1111111111222222222233333333334444444444')
  112. })
  113. it('sets sha to empty when explicit ref', async () => {
  114. inputs.ref = 'refs/heads/some-other-ref'
  115. const settings: IGitSourceSettings = await inputHelper.getInputs()
  116. expect(settings.ref).toBe('refs/heads/some-other-ref')
  117. expect(settings.commit).toBeFalsy()
  118. })
  119. it('sets workflow organization ID', async () => {
  120. const settings: IGitSourceSettings = await inputHelper.getInputs()
  121. expect(settings.workflowOrganizationId).toBe(123456)
  122. })
  123. describe('unsafe PR checkout guard', () => {
  124. const forkPayload = {
  125. repository: {id: 100},
  126. pull_request: {
  127. head: {
  128. sha: '1234567890123456789012345678901234567890',
  129. repo: {id: 200, full_name: 'attacker/fork'}
  130. },
  131. merge_commit_sha: 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'
  132. }
  133. }
  134. it('allows the default self-checkout on a fork pull_request_target', async () => {
  135. const originalEvent = github.context.eventName
  136. const originalPayload = github.context.payload
  137. try {
  138. github.context.eventName = 'pull_request_target'
  139. github.context.payload = forkPayload as any
  140. // Simulate a rebase/fast-forward merge where the base tip (event SHA)
  141. // equals the PR head SHA. The default self-checkout must still succeed.
  142. github.context.sha = '1234567890123456789012345678901234567890'
  143. const settings: IGitSourceSettings = await inputHelper.getInputs()
  144. expect(settings.commit).toBe('1234567890123456789012345678901234567890')
  145. } finally {
  146. github.context.eventName = originalEvent
  147. github.context.payload = originalPayload
  148. }
  149. })
  150. it('refuses an explicit fork repository on pull_request_target', async () => {
  151. const originalEvent = github.context.eventName
  152. const originalPayload = github.context.payload
  153. try {
  154. github.context.eventName = 'pull_request_target'
  155. github.context.payload = forkPayload as any
  156. inputs.repository = 'attacker/fork'
  157. await expect(inputHelper.getInputs()).rejects.toThrow(
  158. /Refusing to check out fork pull request code/
  159. )
  160. } finally {
  161. github.context.eventName = originalEvent
  162. github.context.payload = originalPayload
  163. }
  164. })
  165. })
  166. })