ReviewOS

also looking at this

stacks/buddy

fix: give GitLab/Bitbucket pipelines the review job, and stop cleanup lying

#1432
Merged glennmichael123 wants to merge fix/pipelines-and-cleanup into main
5 files +152 -8
test/branch-cleanup.test.tsadded+67-0
Changes to test/branch-cleanup.test.ts
@@ -0,0 +1,67 @@
1import { describe, expect, it } from 'bun:test'
2import { GitHubProvider } from '../src/git/github-provider'
3import { Logger } from '../src/utils/logger'
4
5/** A provider whose git commands are scripted, recording what was run. */
6function provider(script: (args: string[]) => Promise<string>) {
7 const prov = new GitHubProvider('token', 'owner', 'repo', false, undefined, 'https://api.github.test', Logger.silent())
8 const ran: string[][] = []
9 ;(prov as never as { runCommand: (cmd: string, args: string[]) => Promise<string> }).runCommand
10 = async (_cmd, args) => {
11 ran.push(args)
12 return script(args)
13 }
14 return { prov, ran }
15}
16
17/**
18 * `deleteBranch` swallowed every failure "because branch deletion failures are
19 * not critical" — so `cleanupStaleBranches`, whose per-branch `catch` fills the
20 * `failed` bucket, could never reach it. Every branch was reported deleted
21 * whether the push succeeded or not, and `buddy update-check` printed
22 * "N branches deleted, 0 failed" over a remote that still had them.
23 */
24describe('deleting a branch', () => {
25 it('success case - a successful push resolves', async () => {
26 const { prov, ran } = provider(async () => '')
27
28 await expect(prov.deleteBranch('buddy/update-x')).resolves.toBeUndefined()
29 expect(ran[0]).toEqual(['push', 'origin', '--delete', 'buddy/update-x'])
30 })
31
32 it('failure case - a refused push is reported, not swallowed', async () => {
33 // No permission, network, protected branch: the caller asked for a
34 // deletion and did not get one. GitLab and Bitbucket already throw here.
35 const { prov } = provider(async (args) => {
36 if (args[0] === 'push')
37 throw new Error('remote: error: GH006: Protected branch update failed')
38 return ''
39 })
40
41 await expect(prov.deleteBranch('main')).rejects.toThrow('Protected branch')
42 })
43
44 it('edge case - a branch that is already gone counts as deleted', async () => {
45 // The outcome the caller wanted has already happened. Failing cleanup
46 // over it would leave the same branch "failing" on every run.
47 const { prov } = provider(async (args) => {
48 if (args[0] === 'push')
49 throw new Error("error: unable to delete 'buddy/old': remote ref does not exist")
50 return ''
51 })
52
53 await expect(prov.deleteBranch('buddy/old')).resolves.toBeUndefined()
54 })
55
56 it('success case - the local tracking branch is tidied even when the push fails', async () => {
57 const { prov, ran } = provider(async (args) => {
58 if (args[0] === 'push')
59 throw new Error('fatal: could not read from remote')
60 return ''
61 })
62
63 await prov.deleteBranch('buddy/x').catch(() => {})
64
65 expect(ran.some(args => args[0] === 'branch' && args[1] === '-D')).toBe(true)
66 })
67})