Read-only mirror — the public repositories of the Forgejo instance, refreshed every 10 minutes. The live instance (fg.dieschoenewolke.de) is reachable only over the VPN; clone here with https://git.dieschoenewolke.de/git/<owner>/<repo>.git.
summaryrefslogtreecommitdiff
path: root/__test__/ref-helper.test.ts
blob: 961bc992b70240d6d42c11c0495517a727cd5029 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
import {jest, describe, it, expect, beforeEach, afterEach} from '@jest/globals'
import * as assert from 'assert'

// Mutable mock github context
const mockGithubContext: any = {
  eventName: '',
  payload: {},
  repo: {owner: 'some-owner', repo: 'some-repo'},
  ref: '',
  sha: ''
}

// Mock @actions/core
const mockDebug = jest.fn()
jest.unstable_mockModule('@actions/core', () => ({
  debug: mockDebug,
  info: jest.fn(),
  warning: jest.fn(),
  error: jest.fn(),
  setFailed: jest.fn()
}))

// Mock @actions/github
const mockGetOctokit = jest.fn()
jest.unstable_mockModule('@actions/github', () => ({
  context: mockGithubContext,
  getOctokit: mockGetOctokit
}))

// Dynamic imports after mocking
const refHelper = await import('../src/ref-helper.js')
type IGitCommandManager =
  import('../src/git-command-manager.js').IGitCommandManager

const commit = '1234567890123456789012345678901234567890'
const sha256Commit =
  '1234567890123456789012345678901234567890123456789012345678901234'
let git: IGitCommandManager

describe('ref-helper tests', () => {
  beforeEach(() => {
    git = {} as unknown as IGitCommandManager
    jest.clearAllMocks()
  })

  it('getCheckoutInfo requires git', async () => {
    const git = null as unknown as IGitCommandManager
    try {
      await refHelper.getCheckoutInfo(git, 'refs/heads/my/branch', commit)
      throw new Error('Should not reach here')
    } catch (err) {
      expect((err as any)?.message).toBe('Arg git cannot be empty')
    }
  })

  it('getCheckoutInfo requires ref or commit', async () => {
    try {
      await refHelper.getCheckoutInfo(git, '', '')
      throw new Error('Should not reach here')
    } catch (err) {
      expect((err as any)?.message).toBe(
        'Args ref and commit cannot both be empty'
      )
    }
  })

  it('getCheckoutInfo sha only', async () => {
    const checkoutInfo = await refHelper.getCheckoutInfo(git, '', commit)
    expect(checkoutInfo.ref).toBe(commit)
    expect(checkoutInfo.startPoint).toBeFalsy()
  })

  it('getCheckoutInfo sha-256 only', async () => {
    const checkoutInfo = await refHelper.getCheckoutInfo(git, '', sha256Commit)
    expect(checkoutInfo.ref).toBe(sha256Commit)
    expect(checkoutInfo.startPoint).toBeFalsy()
  })

  it('getCheckoutInfo refs/heads/', async () => {
    const checkoutInfo = await refHelper.getCheckoutInfo(
      git,
      'refs/heads/my/branch',
      commit
    )
    expect(checkoutInfo.ref).toBe('my/branch')
    expect(checkoutInfo.startPoint).toBe('refs/remotes/origin/my/branch')
  })

  it('getCheckoutInfo refs/pull/', async () => {
    const checkoutInfo = await refHelper.getCheckoutInfo(
      git,
      'refs/pull/123/merge',
      commit
    )
    expect(checkoutInfo.ref).toBe('refs/remotes/pull/123/merge')
    expect(checkoutInfo.startPoint).toBeFalsy()
  })

  it('getCheckoutInfo refs/tags/', async () => {
    const checkoutInfo = await refHelper.getCheckoutInfo(
      git,
      'refs/tags/my-tag',
      commit
    )
    expect(checkoutInfo.ref).toBe('refs/tags/my-tag')
    expect(checkoutInfo.startPoint).toBeFalsy()
  })

  it('getCheckoutInfo refs/', async () => {
    const checkoutInfo = await refHelper.getCheckoutInfo(
      git,
      'refs/gh/queue/main/pr-123',
      commit
    )
    expect(checkoutInfo.ref).toBe(commit)
    expect(checkoutInfo.startPoint).toBeFalsy()
  })

  it('getCheckoutInfo refs/ without commit', async () => {
    const checkoutInfo = await refHelper.getCheckoutInfo(
      git,
      'refs/non-standard-ref',
      ''
    )
    expect(checkoutInfo.ref).toBe('refs/non-standard-ref')
    expect(checkoutInfo.startPoint).toBeFalsy()
  })

  it('getCheckoutInfo unqualified branch only', async () => {
    git.branchExists = jest.fn(async (remote: boolean, pattern: string) => {
      return true
    })

    const checkoutInfo = await refHelper.getCheckoutInfo(git, 'my/branch', '')

    expect(checkoutInfo.ref).toBe('my/branch')
    expect(checkoutInfo.startPoint).toBe('refs/remotes/origin/my/branch')
  })

  it('getCheckoutInfo unqualified tag only', async () => {
    git.branchExists = jest.fn(async (remote: boolean, pattern: string) => {
      return false
    })
    git.tagExists = jest.fn(async (pattern: string) => {
      return true
    })

    const checkoutInfo = await refHelper.getCheckoutInfo(git, 'my-tag', '')

    expect(checkoutInfo.ref).toBe('refs/tags/my-tag')
    expect(checkoutInfo.startPoint).toBeFalsy()
  })

  it('getCheckoutInfo unqualified ref only, not a branch or tag', async () => {
    git.branchExists = jest.fn(async (remote: boolean, pattern: string) => {
      return false
    })
    git.tagExists = jest.fn(async (pattern: string) => {
      return false
    })

    try {
      await refHelper.getCheckoutInfo(git, 'my-ref', '')
      throw new Error('Should not reach here')
    } catch (err) {
      expect((err as any)?.message).toBe(
        "A branch or tag with the name 'my-ref' could not be found"
      )
    }
  })

  it('getRefSpec requires ref or commit', async () => {
    assert.throws(
      () => refHelper.getRefSpec('', ''),
      /Args ref and commit cannot both be empty/
    )
  })

  it('getRefSpec sha + refs/heads/', async () => {
    const refSpec = refHelper.getRefSpec('refs/heads/my/branch', commit)
    expect(refSpec.length).toBe(1)
    expect(refSpec[0]).toBe(`+${commit}:refs/remotes/origin/my/branch`)
  })

  it('getRefSpec sha + refs/pull/', async () => {
    const refSpec = refHelper.getRefSpec('refs/pull/123/merge', commit)
    expect(refSpec.length).toBe(1)
    expect(refSpec[0]).toBe(`+${commit}:refs/remotes/pull/123/merge`)
  })

  it('getRefSpec sha + refs/tags/', async () => {
    const refSpec = refHelper.getRefSpec('refs/tags/my-tag', commit)
    expect(refSpec.length).toBe(1)
    expect(refSpec[0]).toBe(`+refs/tags/my-tag:refs/tags/my-tag`)
  })

  it('getRefSpec sha + refs/tags/ with fetchTags', async () => {
    const refSpec = refHelper.getRefSpec('refs/tags/my-tag', commit, true)
    expect(refSpec.length).toBe(1)
    expect(refSpec[0]).toBe('+refs/tags/*:refs/tags/*')
  })

  it('getRefSpec sha + refs/heads/ with fetchTags', async () => {
    const refSpec = refHelper.getRefSpec('refs/heads/my/branch', commit, true)
    expect(refSpec.length).toBe(2)
    expect(refSpec[0]).toBe('+refs/tags/*:refs/tags/*')
    expect(refSpec[1]).toBe(`+${commit}:refs/remotes/origin/my/branch`)
  })

  it('getRefSpec sha only', async () => {
    const refSpec = refHelper.getRefSpec('', commit)
    expect(refSpec.length).toBe(1)
    expect(refSpec[0]).toBe(commit)
  })

  it('getRefSpec unqualified ref only', async () => {
    const refSpec = refHelper.getRefSpec('my-ref', '')
    expect(refSpec.length).toBe(2)
    expect(refSpec[0]).toBe('+refs/heads/my-ref*:refs/remotes/origin/my-ref*')
    expect(refSpec[1]).toBe('+refs/tags/my-ref*:refs/tags/my-ref*')
  })

  it('getRefSpec unqualified ref only with fetchTags', async () => {
    const refSpec = refHelper.getRefSpec('my-ref', '', true)
    expect(refSpec.length).toBe(2)
    expect(refSpec[0]).toBe('+refs/tags/*:refs/tags/*')
    expect(refSpec[1]).toBe('+refs/heads/my-ref*:refs/remotes/origin/my-ref*')
  })

  it('getRefSpec refs/heads/ only', async () => {
    const refSpec = refHelper.getRefSpec('refs/heads/my/branch', '')
    expect(refSpec.length).toBe(1)
    expect(refSpec[0]).toBe(
      '+refs/heads/my/branch:refs/remotes/origin/my/branch'
    )
  })

  it('getRefSpec refs/pull/ only', async () => {
    const refSpec = refHelper.getRefSpec('refs/pull/123/merge', '')
    expect(refSpec.length).toBe(1)
    expect(refSpec[0]).toBe('+refs/pull/123/merge:refs/remotes/pull/123/merge')
  })

  it('getRefSpec refs/tags/ only', async () => {
    const refSpec = refHelper.getRefSpec('refs/tags/my-tag', '')
    expect(refSpec.length).toBe(1)
    expect(refSpec[0]).toBe('+refs/tags/my-tag:refs/tags/my-tag')
  })

  it('getRefSpec refs/tags/ only with fetchTags', async () => {
    const refSpec = refHelper.getRefSpec('refs/tags/my-tag', '', true)
    expect(refSpec.length).toBe(1)
    expect(refSpec[0]).toBe('+refs/tags/*:refs/tags/*')
  })

  it('getRefSpec refs/heads/ only with fetchTags', async () => {
    const refSpec = refHelper.getRefSpec('refs/heads/my/branch', '', true)
    expect(refSpec.length).toBe(2)
    expect(refSpec[0]).toBe('+refs/tags/*:refs/tags/*')
    expect(refSpec[1]).toBe(
      '+refs/heads/my/branch:refs/remotes/origin/my/branch'
    )
  })

  describe('checkCommitInfo', () => {
    const repositoryOwner = 'some-owner'
    const repositoryName = 'some-repo'
    const ref = 'refs/pull/123/merge'
    const sha1Head = '1111111111222222222233333333334444444444'
    const sha1Base = 'aaaaaaaaaabbbbbbbbbbccccccccccdddddddddd'
    const sha256Head =
      '1111111111222222222233333333334444444444555555555566666666667777'
    const sha256Base =
      'aaaaaaaaaabbbbbbbbbbccccccccccddddddddddeeeeeeeeeeffffffffff0000'
    let repoGetSpy: jest.Mock<any>
    let originalEventName: string
    let originalPayload: unknown
    let originalRef: string
    let originalSha: string

    function setPullRequestContext(
      expectedHeadSha: string,
      expectedBaseSha: string,
      mergeCommit: string
    ): void {
      mockGithubContext.eventName = 'pull_request'
      mockGithubContext.ref = ref
      mockGithubContext.sha = mergeCommit
      mockGithubContext.payload = {
        action: 'synchronize',
        after: expectedHeadSha,
        number: 123,
        pull_request: {
          base: {
            sha: expectedBaseSha
          }
        },
        repository: {
          private: false
        }
      }
    }

    beforeEach(() => {
      originalEventName = mockGithubContext.eventName
      originalPayload = mockGithubContext.payload
      originalRef = mockGithubContext.ref
      originalSha = mockGithubContext.sha

      mockGithubContext.repo = {
        owner: repositoryOwner,
        repo: repositoryName
      }

      repoGetSpy = jest.fn(async () => ({}))
      mockGetOctokit.mockReturnValue({
        rest: {
          repos: {
            get: repoGetSpy
          }
        }
      } as any)
    })

    afterEach(() => {
      mockGithubContext.eventName = originalEventName
      mockGithubContext.payload = originalPayload
      mockGithubContext.ref = originalRef
      mockGithubContext.sha = originalSha
      jest.clearAllMocks()
    })

    it('returns early for SHA-1 merge commit', async () => {
      setPullRequestContext(sha1Head, sha1Base, commit)

      await refHelper.checkCommitInfo(
        'token',
        `Merge ${sha1Head} into ${sha1Base}`,
        repositoryOwner,
        repositoryName,
        ref,
        commit
      )

      expect(mockGetOctokit).not.toHaveBeenCalled()
      expect(repoGetSpy).not.toHaveBeenCalled()
    })

    it('matches SHA-256 merge commit info', async () => {
      const actualHeadSha =
        '9999999999888888888877777777776666666666555555555544444444443333'
      setPullRequestContext(sha256Head, sha256Base, sha256Commit)

      await refHelper.checkCommitInfo(
        'token',
        `Merge ${actualHeadSha} into ${sha256Base}`,
        repositoryOwner,
        repositoryName,
        ref,
        sha256Commit
      )

      expect(mockGetOctokit).toHaveBeenCalledWith(
        'token',
        expect.objectContaining({
          userAgent: expect.stringContaining(
            `expected_head_sha=${sha256Head};actual_head_sha=${actualHeadSha}`
          )
        })
      )
      expect(repoGetSpy).toHaveBeenCalledWith({
        owner: repositoryOwner,
        repo: repositoryName
      })
      expect(mockDebug).toHaveBeenCalledWith(
        `Expected head sha ${sha256Head}; actual head sha ${actualHeadSha}`
      )
      expect(mockDebug).not.toHaveBeenCalledWith('Unexpected message format')
    })

    it('does not match 50-char hex as a valid merge', async () => {
      const invalidHeadSha =
        '99999999998888888888777777777766666666665555555555'
      setPullRequestContext(sha1Head, sha1Base, commit)

      await refHelper.checkCommitInfo(
        'token',
        `Merge ${invalidHeadSha} into ${sha1Base}`,
        repositoryOwner,
        repositoryName,
        ref,
        commit
      )

      expect(mockGetOctokit).not.toHaveBeenCalled()
      expect(repoGetSpy).not.toHaveBeenCalled()
      expect(mockDebug).toHaveBeenCalledWith('Unexpected message format')
    })
  })
})