-
Notifications
You must be signed in to change notification settings - Fork 79
Expand file tree
/
Copy pathgit-handler.ts
More file actions
67 lines (59 loc) · 1.73 KB
/
git-handler.ts
File metadata and controls
67 lines (59 loc) · 1.73 KB
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
// Git Handler
import type {
GitCheckoutRequest,
GitCheckoutResult,
Logger
} from '@repo/shared';
import { ErrorCode } from '@repo/shared/errors';
import type { RequestContext } from '../core/types';
import type { GitService } from '../services/git-service';
import { BaseHandler } from './base-handler';
export class GitHandler extends BaseHandler<Request, Response> {
constructor(
private gitService: GitService,
logger: Logger
) {
super(logger);
}
async handle(request: Request, context: RequestContext): Promise<Response> {
const url = new URL(request.url);
const pathname = url.pathname;
switch (pathname) {
case '/api/git/checkout':
return await this.handleCheckout(request, context);
default:
return this.createErrorResponse(
{
message: 'Invalid git endpoint',
code: ErrorCode.UNKNOWN_ERROR
},
context
);
}
}
private async handleCheckout(
request: Request,
context: RequestContext
): Promise<Response> {
const body = await this.parseRequestBody<GitCheckoutRequest>(request);
const sessionId = body.sessionId || context.sessionId;
const result = await this.gitService.cloneRepository(body.repoUrl, {
branch: body.branch,
targetDir: body.targetDir,
sessionId,
depth: body.depth
});
if (result.success) {
const response: GitCheckoutResult = {
success: true,
repoUrl: body.repoUrl,
branch: result.data.branch,
targetDir: result.data.path,
timestamp: new Date().toISOString()
};
return this.createTypedResponse(response, context);
} else {
return this.createErrorResponse(result.error, context);
}
}
}