forked from MergeFi/backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub.controller.ts
More file actions
60 lines (54 loc) · 1.61 KB
/
Copy pathgithub.controller.ts
File metadata and controls
60 lines (54 loc) · 1.61 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
import {
Controller,
ForbiddenException,
NotFoundException,
Param,
Post,
Req,
UseGuards,
} from '@nestjs/common';
import { ApiBearerAuth, ApiTags } from '@nestjs/swagger';
import { Throttle } from '@nestjs/throttler';
import { GithubSyncService } from './github-sync.service';
import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard';
import { UserRole } from '../common/enums';
import { User } from '../common/entities';
interface RequestWithUser extends Request {
user: User;
}
@ApiTags('github')
@Controller('github')
export class GithubController {
constructor(private readonly syncService: GithubSyncService) {}
@ApiBearerAuth()
@UseGuards(JwtAuthGuard)
@Throttle({ default: { limit: 5, ttl: 60_000 } })
@Post('sync/:owner/:repo')
async sync(
@Param('owner') owner: string,
@Param('repo') repo: string,
@Req() req: RequestWithUser,
) {
const user = req.user;
const isMaintainerOrAdmin =
user?.roles &&
(user.roles.includes(UserRole.MAINTAINER) ||
user.roles.includes(UserRole.SPONSOR) ||
(user.roles as unknown as string[]).includes('admin'));
if (!isMaintainerOrAdmin) {
throw new ForbiddenException(
'Only maintainers or sponsors may trigger repository synchronization',
);
}
const tracked = await this.syncService.findRepositoryByOwnerAndName(
owner,
repo,
);
if (!tracked) {
throw new NotFoundException(
`Repository ${owner}/${repo} is not tracked by MergeFi. Only registered repositories can be synced.`,
);
}
return this.syncService.syncRepository(owner, repo);
}
}