| 1 | import { |
| 2 | Body, |
| 3 | Controller, |
| 4 | Get, |
| 5 | Param, |
| 6 | ParseIntPipe, |
| 7 | Patch, |
| 8 | Post, |
| 9 | Query, |
| 10 | Session, |
| 11 | StreamableFile, |
| 12 | } from '@nestjs/common'; |
| 13 | import { |
| 14 | ApiConflictResponse, |
| 15 | ApiCreatedResponse, |
| 16 | ApiNotFoundResponse, |
| 17 | ApiOkResponse, |
| 18 | ApiOperation, |
| 19 | ApiTags, |
| 20 | } from '@nestjs/swagger'; |
| 21 | import { OptionalAuth, type |
| 22 | |
| 23 | import { ErrorResponseDTO } |
| 24 | import { |
| 25 | ComparePatchQueryDTO, |
| 26 | CompareQueryDTO, |
| 27 | CompareResponseDTO, |
| 28 | } from './dto/compare.dto.js'; |
| 29 | import { |
| 30 | CreatePullRequestRequestDTO, |
| 31 | UpdatePullRequestRequestDTO, |
| 32 | } from './dto/create-pull-request.dto.js'; |
| 33 | import { |
| 34 | GetPullRequestsQueryDTO, |
| 35 | GetPullRequestsResponseDTO, |
| 36 | PullRequestDetailDTO, |
| 37 | PullRequestDTO, |
| 38 | } from './dto/pull-request.dto.js'; |
| 39 | import { |
| 40 | GetPullRequestCommitsQueryDTO, |
| 41 | GetPullRequestCommitsResponseDTO, |
| 42 | GetPullRequestFilesResponseDTO, |
| 43 | GetPullRequestPatchQueryDTO, |
| 44 | MergePullRequestRequestDTO, |
| 45 | MergePullRequestResponseDTO, |
| 46 | } from './dto/pull-request-changes.dto.js'; |
| 47 | import { PullRequestsService } |
| 48 | |
| 49 | @Controller('repositories/:username/:repo/pulls') |
| 50 | @ApiTags('Pull requests') |
| 51 | export class PullRequestsController |
| 52 | constructor(private readonly |
| 53 | |
| 54 | @Post() |
| 55 | @ApiOperation({ summary |
| 56 | @ApiCreatedResponse({ type |
| 57 | @ApiNotFoundResponse({ type |
| 58 | @ApiConflictResponse({ type |
| 59 | createPullRequest( |
| 60 | @Param('username' |
| 61 | @Param('repo' |
| 62 | @Body() body |
| 63 | @Session() session |
| 64 | ) { |
| 65 | return this.pullRequests |
| 66 | username, |
| 67 | repo, |
| 68 | requesterId: session.user |
| 69 | body, |
| 70 | }); |
| 71 | } |
| 72 | |
| 73 | @Get() |
| 74 | @OptionalAuth() |
| 75 | @ApiOperation({ summary |
| 76 | @ApiOkResponse({ type |
| 77 | @ApiNotFoundResponse({ type |
| 78 | getPullRequests( |
| 79 | @Param('username' |
| 80 | @Param('repo' |
| 81 | @Query() query |
| 82 | @Session() session |
| 83 | ) { |
| 84 | return this.pullRequests |
| 85 | username, |
| 86 | repo, |
| 87 | requesterId: session?.user |
| 88 | query, |
| 89 | }); |
| 90 | } |
| 91 | |
| 92 | @Get('compare/patch' |
| 93 | @ApiOperation({ summary |
| 94 | @ApiOkResponse({ schema |
| 95 | @ApiNotFoundResponse({ type |
| 96 | async comparePatch( |
| 97 | @Param('username' |
| 98 | @Param('repo' |
| 99 | @Query() query |
| 100 | @Session() session |
| 101 | ) { |
| 102 | return new StreamableFile |
| 103 | await this.pullRequests |
| 104 | username, |
| 105 | repo, |
| 106 | requesterId: session.user |
| 107 | base: query.base |
| 108 | head: query.head |
| 109 | path: query.path |
| 110 | }), |
| 111 | { type: 'text/plain; charset=utf-8' |
| 112 | ); |
| 113 | } |
| 114 | |
| 115 | @Get('compare' |
| 116 | @ApiOperation({ summary |
| 117 | @ApiOkResponse({ type |
| 118 | @ApiNotFoundResponse({ type |
| 119 | compare( |
| 120 | @Param('username' |
| 121 | @Param('repo' |
| 122 | @Query() query |
| 123 | @Session() session |
| 124 | ) { |
| 125 | return this.pullRequests |
| 126 | username, |
| 127 | repo, |
| 128 | requesterId: session.user |
| 129 | base: query.base |
| 130 | head: query.head |
| 131 | }); |
| 132 | } |
| 133 | |
| 134 | @Get(':number' |
| 135 | @OptionalAuth() |
| 136 | @ApiOperation({ summary |
| 137 | @ApiOkResponse({ type |
| 138 | @ApiNotFoundResponse({ type |
| 139 | getPullRequest( |
| 140 | @Param('username' |
| 141 | @Param('repo' |
| 142 | @Param('number' |
| 143 | @Session() session |
| 144 | ) { |
| 145 | return this.pullRequests |
| 146 | username, |
| 147 | repo, |
| 148 | number, |
| 149 | requesterId: session?.user |
| 150 | }); |
| 151 | } |
| 152 | |
| 153 | @Get(':number/commits' |
| 154 | @OptionalAuth() |
| 155 | @ApiOperation({ summary |
| 156 | @ApiOkResponse({ type |
| 157 | @ApiNotFoundResponse({ type |
| 158 | getCommits( |
| 159 | @Param('username' |
| 160 | @Param('repo' |
| 161 | @Param('number' |
| 162 | @Query() query |
| 163 | @Session() session |
| 164 | ) { |
| 165 | return this.pullRequests |
| 166 | username, |
| 167 | repo, |
| 168 | number, |
| 169 | requesterId: session?.user |
| 170 | limit: query.limit |
| 171 | cursor: query.cursor |
| 172 | }); |
| 173 | } |
| 174 | |
| 175 | @Get(':number/files' |
| 176 | @OptionalAuth() |
| 177 | @ApiOperation({ summary |
| 178 | @ApiOkResponse({ type |
| 179 | @ApiNotFoundResponse({ type |
| 180 | getFiles( |
| 181 | @Param('username' |
| 182 | @Param('repo' |
| 183 | @Param('number' |
| 184 | @Session() session |
| 185 | ) { |
| 186 | return this.pullRequests |
| 187 | username, |
| 188 | repo, |
| 189 | number, |
| 190 | requesterId: session?.user |
| 191 | }); |
| 192 | } |
| 193 | |
| 194 | @Get(':number/patch' |
| 195 | @OptionalAuth() |
| 196 | @ApiOperation({ summary |
| 197 | @ApiOkResponse({ schema |
| 198 | @ApiNotFoundResponse({ type |
| 199 | async getPatch( |
| 200 | @Param('username' |
| 201 | @Param('repo' |
| 202 | @Param('number' |
| 203 | @Query() query |
| 204 | @Session() session |
| 205 | ) { |
| 206 | return new StreamableFile |
| 207 | await this.pullRequests |
| 208 | username, |
| 209 | repo, |
| 210 | number, |
| 211 | requesterId: session?.user |
| 212 | path: query.path |
| 213 | }), |
| 214 | { type: 'text/plain; charset=utf-8' |
| 215 | ); |
| 216 | } |
| 217 | |
| 218 | @Post(':number/merge' |
| 219 | @ApiOperation({ summary |
| 220 | @ApiOkResponse({ type |
| 221 | @ApiNotFoundResponse({ type |
| 222 | @ApiConflictResponse({ type |
| 223 | mergePullRequest( |
| 224 | @Param('username' |
| 225 | @Param('repo' |
| 226 | @Param('number' |
| 227 | @Body() body |
| 228 | @Session() session |
| 229 | ) { |
| 230 | return this.pullRequests |
| 231 | username, |
| 232 | repo, |
| 233 | number, |
| 234 | requesterId: session.user |
| 235 | title: body.title |
| 236 | message: body.message |
| 237 | method: body.method |
| 238 | }); |
| 239 | } |
| 240 | |
| 241 | @Patch(':number' |
| 242 | @ApiOperation({ summary |
| 243 | @ApiOkResponse({ type |
| 244 | @ApiNotFoundResponse({ type |
| 245 | updatePullRequest( |
| 246 | @Param('username' |
| 247 | @Param('repo' |
| 248 | @Param('number' |
| 249 | @Body() body |
| 250 | @Session() session |
| 251 | ) { |
| 252 | return this.pullRequests |
| 253 | username, |
| 254 | repo, |
| 255 | number, |
| 256 | requesterId: session.user |
| 257 | body, |
| 258 | }); |
| 259 | } |
| 260 | |
| 261 | @Patch(':number/close' |
| 262 | @ApiOperation({ summary |
| 263 | @ApiOkResponse({ type |
| 264 | @ApiNotFoundResponse({ type |
| 265 | @ApiConflictResponse({ type |
| 266 | closePullRequest( |
| 267 | @Param('username' |
| 268 | @Param('repo' |
| 269 | @Param('number' |
| 270 | @Session() session |
| 271 | ) { |
| 272 | return this.pullRequests |
| 273 | username, |
| 274 | repo, |
| 275 | number, |
| 276 | requesterId: session.user |
| 277 | }); |
| 278 | } |
| 279 | |
| 280 | @Post(':number/ready' |
| 281 | @ApiOperation({ summary |
| 282 | @ApiOkResponse({ type |
| 283 | @ApiNotFoundResponse({ type |
| 284 | @ApiConflictResponse({ type |
| 285 | markReadyForReview( |
| 286 | @Param('username' |
| 287 | @Param('repo' |
| 288 | @Param('number' |
| 289 | @Session() session |
| 290 | ) { |
| 291 | return this.pullRequests |
| 292 | username, |
| 293 | repo, |
| 294 | number, |
| 295 | requesterId: session.user |
| 296 | draft: false, |
| 297 | }); |
| 298 | } |
| 299 | |
| 300 | @Post(':number/draft' |
| 301 | @ApiOperation({ summary |
| 302 | @ApiOkResponse({ type |
| 303 | @ApiNotFoundResponse({ type |
| 304 | @ApiConflictResponse({ type |
| 305 | convertToDraft( |
| 306 | @Param('username' |
| 307 | @Param('repo' |
| 308 | @Param('number' |
| 309 | @Session() session |
| 310 | ) { |
| 311 | return this.pullRequests |
| 312 | username, |
| 313 | repo, |
| 314 | number, |
| 315 | requesterId: session.user |
| 316 | draft: true, |
| 317 | }); |
| 318 | } |
| 319 | } |