@aaronshaf/ger 0.3.1 → 0.3.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/EXAMPLES.md +48 -0
- package/README.md +87 -0
- package/package.json +1 -1
- package/src/api/gerrit.ts +28 -0
- package/src/cli/commands/add-reviewer.ts +135 -0
- package/src/cli/commands/build-status.ts +69 -25
- package/src/cli/commands/push.ts +430 -0
- package/src/cli/commands/search.ts +162 -0
- package/src/cli/commands/show.ts +20 -0
- package/src/cli/index.ts +115 -74
- package/src/schemas/gerrit.ts +43 -0
- package/src/services/commit-hook.ts +314 -0
- package/tests/add-reviewer.test.ts +393 -0
- package/tests/build-status-watch.test.ts +8 -11
- package/tests/build-status.test.ts +149 -0
- package/tests/integration/commit-hook.test.ts +246 -0
- package/tests/search.test.ts +712 -0
- package/tests/unit/commands/push.test.ts +194 -0
- package/tests/unit/patterns/push-patterns.test.ts +148 -0
- package/tests/unit/services/commit-hook.test.ts +132 -0
|
@@ -0,0 +1,246 @@
|
|
|
1
|
+
import { afterAll, afterEach, beforeAll, describe, expect, test } from 'bun:test'
|
|
2
|
+
import { Effect, Layer } from 'effect'
|
|
3
|
+
import { HttpResponse, http } from 'msw'
|
|
4
|
+
import { setupServer } from 'msw/node'
|
|
5
|
+
import { ConfigService } from '@/services/config'
|
|
6
|
+
import { CommitHookService, CommitHookServiceLive } from '@/services/commit-hook'
|
|
7
|
+
import { createMockConfigService } from '../helpers/config-mock'
|
|
8
|
+
|
|
9
|
+
// Sample valid commit-msg hook script
|
|
10
|
+
const VALID_HOOK_SCRIPT = `#!/bin/sh
|
|
11
|
+
# From Gerrit Code Review 3.x
|
|
12
|
+
#
|
|
13
|
+
# Part of Gerrit Code Review (https://www.gerritcodereview.com/)
|
|
14
|
+
|
|
15
|
+
# Add a Change-Id line to commit messages that don't have one
|
|
16
|
+
add_change_id() {
|
|
17
|
+
# ... hook implementation
|
|
18
|
+
echo "Change-Id: I$(git hash-object -t blob /dev/null)"
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
add_change_id
|
|
22
|
+
`
|
|
23
|
+
|
|
24
|
+
// Create MSW server for hook download tests
|
|
25
|
+
const server = setupServer()
|
|
26
|
+
|
|
27
|
+
describe('CommitHookService Integration Tests', () => {
|
|
28
|
+
beforeAll(() => {
|
|
29
|
+
server.listen({ onUnhandledRequest: 'bypass' })
|
|
30
|
+
})
|
|
31
|
+
|
|
32
|
+
afterAll(() => {
|
|
33
|
+
server.close()
|
|
34
|
+
})
|
|
35
|
+
|
|
36
|
+
afterEach(() => {
|
|
37
|
+
server.resetHandlers()
|
|
38
|
+
})
|
|
39
|
+
|
|
40
|
+
describe('installHook', () => {
|
|
41
|
+
test('should successfully download hook from Gerrit server', async () => {
|
|
42
|
+
// Setup handler for successful hook download
|
|
43
|
+
server.use(
|
|
44
|
+
http.get('https://test.gerrit.com/tools/hooks/commit-msg', () => {
|
|
45
|
+
return HttpResponse.text(VALID_HOOK_SCRIPT, { status: 200 })
|
|
46
|
+
}),
|
|
47
|
+
)
|
|
48
|
+
|
|
49
|
+
const mockConfigLayer = Layer.succeed(ConfigService, createMockConfigService())
|
|
50
|
+
|
|
51
|
+
// Note: We can't fully test installHook without git repo context,
|
|
52
|
+
// but we can verify the HTTP request is made correctly
|
|
53
|
+
const effect = Effect.gen(function* () {
|
|
54
|
+
const service = yield* CommitHookService
|
|
55
|
+
yield* service.installHook()
|
|
56
|
+
}).pipe(Effect.provide(CommitHookServiceLive), Effect.provide(mockConfigLayer))
|
|
57
|
+
|
|
58
|
+
// Run with Effect.exit to capture the result without throwing
|
|
59
|
+
const exit = await Effect.runPromiseExit(effect)
|
|
60
|
+
|
|
61
|
+
// The test verifies the service can be constructed and HTTP fetch succeeds
|
|
62
|
+
// It will fail with NotGitRepoError because we're not in a git repo,
|
|
63
|
+
// but it should NOT fail due to HTTP issues
|
|
64
|
+
if (exit._tag === 'Failure') {
|
|
65
|
+
const errorStr = String(exit.cause)
|
|
66
|
+
// Should fail due to git repo issues, not HTTP issues
|
|
67
|
+
expect(errorStr).not.toContain('Failed to download')
|
|
68
|
+
expect(errorStr).not.toContain('fetch')
|
|
69
|
+
}
|
|
70
|
+
})
|
|
71
|
+
|
|
72
|
+
test('should handle 404 error when hook URL is not found', async () => {
|
|
73
|
+
server.use(
|
|
74
|
+
http.get('https://test.gerrit.com/tools/hooks/commit-msg', () => {
|
|
75
|
+
return HttpResponse.text('Not Found', { status: 404 })
|
|
76
|
+
}),
|
|
77
|
+
)
|
|
78
|
+
|
|
79
|
+
const mockConfigLayer = Layer.succeed(ConfigService, createMockConfigService())
|
|
80
|
+
|
|
81
|
+
const effect = Effect.gen(function* () {
|
|
82
|
+
const service = yield* CommitHookService
|
|
83
|
+
yield* service.installHook()
|
|
84
|
+
}).pipe(Effect.provide(CommitHookServiceLive), Effect.provide(mockConfigLayer))
|
|
85
|
+
|
|
86
|
+
const result = await Effect.runPromise(effect).catch((e) => e)
|
|
87
|
+
|
|
88
|
+
// Should fail with HookInstallError due to 404
|
|
89
|
+
expect(result).toBeInstanceOf(Error)
|
|
90
|
+
expect(String(result)).toContain('Failed to download')
|
|
91
|
+
})
|
|
92
|
+
|
|
93
|
+
test('should handle 500 server error gracefully', async () => {
|
|
94
|
+
server.use(
|
|
95
|
+
http.get('https://test.gerrit.com/tools/hooks/commit-msg', () => {
|
|
96
|
+
return HttpResponse.text('Internal Server Error', { status: 500 })
|
|
97
|
+
}),
|
|
98
|
+
)
|
|
99
|
+
|
|
100
|
+
const mockConfigLayer = Layer.succeed(ConfigService, createMockConfigService())
|
|
101
|
+
|
|
102
|
+
const effect = Effect.gen(function* () {
|
|
103
|
+
const service = yield* CommitHookService
|
|
104
|
+
yield* service.installHook()
|
|
105
|
+
}).pipe(Effect.provide(CommitHookServiceLive), Effect.provide(mockConfigLayer))
|
|
106
|
+
|
|
107
|
+
const result = await Effect.runPromise(effect).catch((e) => e)
|
|
108
|
+
|
|
109
|
+
expect(result).toBeInstanceOf(Error)
|
|
110
|
+
expect(String(result)).toContain('Failed to download')
|
|
111
|
+
})
|
|
112
|
+
|
|
113
|
+
test('should reject invalid hook content (not a script)', async () => {
|
|
114
|
+
server.use(
|
|
115
|
+
http.get('https://test.gerrit.com/tools/hooks/commit-msg', () => {
|
|
116
|
+
// Return HTML instead of shell script
|
|
117
|
+
return HttpResponse.text('<html><body>Error page</body></html>', { status: 200 })
|
|
118
|
+
}),
|
|
119
|
+
)
|
|
120
|
+
|
|
121
|
+
const mockConfigLayer = Layer.succeed(ConfigService, createMockConfigService())
|
|
122
|
+
|
|
123
|
+
const effect = Effect.gen(function* () {
|
|
124
|
+
const service = yield* CommitHookService
|
|
125
|
+
yield* service.installHook()
|
|
126
|
+
}).pipe(Effect.provide(CommitHookServiceLive), Effect.provide(mockConfigLayer))
|
|
127
|
+
|
|
128
|
+
const result = await Effect.runPromise(effect).catch((e) => e)
|
|
129
|
+
|
|
130
|
+
expect(result).toBeInstanceOf(Error)
|
|
131
|
+
expect(String(result)).toContain('valid script')
|
|
132
|
+
})
|
|
133
|
+
|
|
134
|
+
test('should handle network timeout', async () => {
|
|
135
|
+
server.use(
|
|
136
|
+
http.get('https://test.gerrit.com/tools/hooks/commit-msg', async () => {
|
|
137
|
+
// Simulate network delay that would cause timeout
|
|
138
|
+
await new Promise((resolve) => setTimeout(resolve, 100))
|
|
139
|
+
return HttpResponse.error()
|
|
140
|
+
}),
|
|
141
|
+
)
|
|
142
|
+
|
|
143
|
+
const mockConfigLayer = Layer.succeed(ConfigService, createMockConfigService())
|
|
144
|
+
|
|
145
|
+
const effect = Effect.gen(function* () {
|
|
146
|
+
const service = yield* CommitHookService
|
|
147
|
+
yield* service.installHook()
|
|
148
|
+
}).pipe(Effect.provide(CommitHookServiceLive), Effect.provide(mockConfigLayer))
|
|
149
|
+
|
|
150
|
+
const result = await Effect.runPromise(effect).catch((e) => e)
|
|
151
|
+
|
|
152
|
+
expect(result).toBeInstanceOf(Error)
|
|
153
|
+
})
|
|
154
|
+
|
|
155
|
+
test('should handle host with trailing slash', async () => {
|
|
156
|
+
// Use a host with trailing slash
|
|
157
|
+
const configWithTrailingSlash = createMockConfigService({
|
|
158
|
+
host: 'https://test.gerrit.com/',
|
|
159
|
+
username: 'testuser',
|
|
160
|
+
password: 'testpass',
|
|
161
|
+
})
|
|
162
|
+
|
|
163
|
+
server.use(
|
|
164
|
+
// The trailing slash should be normalized, so this handler should match
|
|
165
|
+
http.get('https://test.gerrit.com/tools/hooks/commit-msg', () => {
|
|
166
|
+
return HttpResponse.text(VALID_HOOK_SCRIPT, { status: 200 })
|
|
167
|
+
}),
|
|
168
|
+
)
|
|
169
|
+
|
|
170
|
+
const mockConfigLayer = Layer.succeed(ConfigService, configWithTrailingSlash)
|
|
171
|
+
|
|
172
|
+
const effect = Effect.gen(function* () {
|
|
173
|
+
const service = yield* CommitHookService
|
|
174
|
+
yield* service.installHook()
|
|
175
|
+
}).pipe(Effect.provide(CommitHookServiceLive), Effect.provide(mockConfigLayer))
|
|
176
|
+
|
|
177
|
+
// Should not fail due to double slash in URL
|
|
178
|
+
await Effect.runPromise(effect).catch((e) => {
|
|
179
|
+
// May fail for git repo reasons, but should not fail for URL issues
|
|
180
|
+
expect(String(e)).not.toContain('//tools')
|
|
181
|
+
})
|
|
182
|
+
})
|
|
183
|
+
})
|
|
184
|
+
|
|
185
|
+
describe('hook script validation', () => {
|
|
186
|
+
test('should accept sh shebang', async () => {
|
|
187
|
+
server.use(
|
|
188
|
+
http.get('https://test.gerrit.com/tools/hooks/commit-msg', () => {
|
|
189
|
+
return HttpResponse.text('#!/bin/sh\necho "hook"', { status: 200 })
|
|
190
|
+
}),
|
|
191
|
+
)
|
|
192
|
+
|
|
193
|
+
const mockConfigLayer = Layer.succeed(ConfigService, createMockConfigService())
|
|
194
|
+
|
|
195
|
+
const effect = Effect.gen(function* () {
|
|
196
|
+
const service = yield* CommitHookService
|
|
197
|
+
yield* service.installHook()
|
|
198
|
+
}).pipe(Effect.provide(CommitHookServiceLive), Effect.provide(mockConfigLayer))
|
|
199
|
+
|
|
200
|
+
const result = await Effect.runPromise(effect).catch((e) => e)
|
|
201
|
+
|
|
202
|
+
// Should not fail with "not a valid script" error
|
|
203
|
+
expect(String(result)).not.toContain('valid script')
|
|
204
|
+
})
|
|
205
|
+
|
|
206
|
+
test('should accept bash shebang', async () => {
|
|
207
|
+
server.use(
|
|
208
|
+
http.get('https://test.gerrit.com/tools/hooks/commit-msg', () => {
|
|
209
|
+
return HttpResponse.text('#!/bin/bash\necho "hook"', { status: 200 })
|
|
210
|
+
}),
|
|
211
|
+
)
|
|
212
|
+
|
|
213
|
+
const mockConfigLayer = Layer.succeed(ConfigService, createMockConfigService())
|
|
214
|
+
|
|
215
|
+
const effect = Effect.gen(function* () {
|
|
216
|
+
const service = yield* CommitHookService
|
|
217
|
+
yield* service.installHook()
|
|
218
|
+
}).pipe(Effect.provide(CommitHookServiceLive), Effect.provide(mockConfigLayer))
|
|
219
|
+
|
|
220
|
+
const result = await Effect.runPromise(effect).catch((e) => e)
|
|
221
|
+
|
|
222
|
+
// Should not fail with "not a valid script" error
|
|
223
|
+
expect(String(result)).not.toContain('valid script')
|
|
224
|
+
})
|
|
225
|
+
|
|
226
|
+
test('should accept env shebang', async () => {
|
|
227
|
+
server.use(
|
|
228
|
+
http.get('https://test.gerrit.com/tools/hooks/commit-msg', () => {
|
|
229
|
+
return HttpResponse.text('#!/usr/bin/env sh\necho "hook"', { status: 200 })
|
|
230
|
+
}),
|
|
231
|
+
)
|
|
232
|
+
|
|
233
|
+
const mockConfigLayer = Layer.succeed(ConfigService, createMockConfigService())
|
|
234
|
+
|
|
235
|
+
const effect = Effect.gen(function* () {
|
|
236
|
+
const service = yield* CommitHookService
|
|
237
|
+
yield* service.installHook()
|
|
238
|
+
}).pipe(Effect.provide(CommitHookServiceLive), Effect.provide(mockConfigLayer))
|
|
239
|
+
|
|
240
|
+
const result = await Effect.runPromise(effect).catch((e) => e)
|
|
241
|
+
|
|
242
|
+
// Should not fail with "not a valid script" error
|
|
243
|
+
expect(String(result)).not.toContain('valid script')
|
|
244
|
+
})
|
|
245
|
+
})
|
|
246
|
+
})
|