| 1 | import { schema } from '@ghost/db'; |
| 2 | import { |
| 3 | ApiExtraModels, |
| 4 | ApiProperty, |
| 5 | ApiPropertyOptional, |
| 6 | getSchemaPath, |
| 7 | } from '@nestjs/swagger'; |
| 8 | import { Type } |
| 9 | import { |
| 10 | IsArray, |
| 11 | IsBoolean, |
| 12 | IsIn, |
| 13 | IsInt, |
| 14 | IsISO8601, |
| 15 | IsOptional, |
| 16 | IsString, |
| 17 | MaxLength, |
| 18 | ValidateNested, |
| 19 | } from 'class-validator'; |
| 20 | import { PullRequestReviewDTO } |
| 21 | import { LabelDTO } |
| 22 | |
| 23 | export class IssueDTO |
| 24 | @ApiProperty() |
| 25 | @IsString() |
| 26 | id: string; |
| 27 | |
| 28 | @ApiProperty({ |
| 29 | description: 'Per repository, and what the URL carries.', |
| 30 | }) |
| 31 | @IsInt() |
| 32 | number: number; |
| 33 | |
| 34 | @ApiProperty() |
| 35 | @IsString() |
| 36 | title: string; |
| 37 | |
| 38 | @ApiProperty({ type |
| 39 | @IsString() |
| 40 | @IsOptional() |
| 41 | body: string |
| 42 | |
| 43 | @ApiProperty({ |
| 44 | enumName: 'IssueState', |
| 45 | enum: schema.issueState |
| 46 | }) |
| 47 | @IsIn(schema. |
| 48 | state: (typeof |
| 49 | |
| 50 | @ApiProperty({ |
| 51 | description: |
| 52 | 'True when this number belongs to a pull request; its page lives under `/pulls`.', |
| 53 | }) |
| 54 | @IsBoolean() |
| 55 | isPullRequest: boolean; |
| 56 | |
| 57 | @ApiProperty() |
| 58 | @IsString() |
| 59 | authorUsername: string; |
| 60 | |
| 61 | @ApiProperty({ type |
| 62 | @IsString() |
| 63 | @IsOptional() |
| 64 | closedByUsername: string |
| 65 | |
| 66 | @ApiProperty({ type |
| 67 | @ValidateNested({ each |
| 68 | @Type(() => |
| 69 | labels: LabelDTO |
| 70 | |
| 71 | @ApiProperty({ example |
| 72 | @IsArray() |
| 73 | assignees: string[] |
| 74 | |
| 75 | @ApiProperty() |
| 76 | @IsInt() |
| 77 | commentCount: number; |
| 78 | |
| 79 | @ApiProperty({ type |
| 80 | @IsISO8601() |
| 81 | @IsOptional() |
| 82 | closedAt: string |
| 83 | |
| 84 | @ApiProperty() |
| 85 | @IsISO8601() |
| 86 | createdAt: string; |
| 87 | |
| 88 | @ApiProperty() |
| 89 | @IsISO8601() |
| 90 | updatedAt: string; |
| 91 | |
| 92 | @ApiProperty({ |
| 93 | description: |
| 94 | 'True when the requesting user is the author or can write to the repository.', |
| 95 | }) |
| 96 | @IsBoolean() |
| 97 | viewerCanEdit: boolean; |
| 98 | } |
| 99 | |
| 100 | export class IssueDetailDTO |
| 101 | |
| 102 | export const ISSUE_SORTS = |
| 103 | export type IssueSort |
| 104 | |
| 105 | export class GetIssuesQueryDTO |
| 106 | @ApiPropertyOptional({ |
| 107 | enumName: 'IssueStateFilter', |
| 108 | enum: [...schema |
| 109 | default: 'open', |
| 110 | }) |
| 111 | @IsIn([...schema |
| 112 | @IsOptional() |
| 113 | state?: ( |
| 114 | |
| 115 | @ApiPropertyOptional({ |
| 116 | description: 'Full-text search over title and body.', |
| 117 | }) |
| 118 | @IsString() |
| 119 | @IsOptional() |
| 120 | @MaxLength(200) |
| 121 | q?: string |
| 122 | |
| 123 | @ApiPropertyOptional({ description |
| 124 | @IsString() |
| 125 | @IsOptional() |
| 126 | author?: string |
| 127 | |
| 128 | @ApiPropertyOptional({ description |
| 129 | @IsString() |
| 130 | @IsOptional() |
| 131 | assignee?: string |
| 132 | |
| 133 | @ApiPropertyOptional({ |
| 134 | description: |
| 135 | 'Comma-separated label names. An issue must carry all of them.', |
| 136 | example: 'bug,help wanted', |
| 137 | }) |
| 138 | @IsString() |
| 139 | @IsOptional() |
| 140 | labels?: string |
| 141 | |
| 142 | @ApiPropertyOptional({ enum |
| 143 | @IsIn(ISSUE_SORTS) |
| 144 | @IsOptional() |
| 145 | sort?: IssueSort |
| 146 | |
| 147 | @ApiPropertyOptional({ enum |
| 148 | @IsIn(['asc' |
| 149 | @IsOptional() |
| 150 | direction?: 'asc |
| 151 | |
| 152 | @ApiPropertyOptional() |
| 153 | @IsString() |
| 154 | @IsOptional() |
| 155 | cursor?: string |
| 156 | |
| 157 | @ApiPropertyOptional({ minimum |
| 158 | @Type(() => |
| 159 | @IsInt() |
| 160 | @IsOptional() |
| 161 | limit?: number |
| 162 | } |
| 163 | |
| 164 | export class GetIssuesResponseDTO |
| 165 | @ApiProperty({ type |
| 166 | @ValidateNested({ each |
| 167 | @Type(() => |
| 168 | issues: IssueDTO |
| 169 | |
| 170 | @ApiProperty({ description |
| 171 | @IsInt() |
| 172 | total: number; |
| 173 | |
| 174 | @ApiProperty() |
| 175 | @IsInt() |
| 176 | openCount: number; |
| 177 | |
| 178 | @ApiProperty() |
| 179 | @IsInt() |
| 180 | closedCount: number; |
| 181 | |
| 182 | @ApiProperty({ type |
| 183 | @IsString() |
| 184 | @IsOptional() |
| 185 | nextCursor: string |
| 186 | |
| 187 | @ApiProperty() |
| 188 | @IsBoolean() |
| 189 | hasMore: boolean; |
| 190 | } |
| 191 | |
| 192 | export class IssueTimelineEventDTO |
| 193 | @ApiProperty() |
| 194 | @IsString() |
| 195 | id: string; |
| 196 | |
| 197 | @ApiProperty({ |
| 198 | enumName: 'IssueEventType', |
| 199 | enum: schema.issueEventType |
| 200 | }) |
| 201 | @IsIn(schema. |
| 202 | type: (typeof |
| 203 | |
| 204 | @ApiProperty() |
| 205 | @IsString() |
| 206 | actorUsername: string; |
| 207 | |
| 208 | @ApiProperty({ type |
| 209 | @IsString() |
| 210 | @IsOptional() |
| 211 | actorImage: string |
| 212 | |
| 213 | @ApiProperty({ type |
| 214 | @IsString() |
| 215 | @IsOptional() |
| 216 | labelName: string |
| 217 | |
| 218 | @ApiProperty({ type |
| 219 | @IsString() |
| 220 | @IsOptional() |
| 221 | assigneeUsername: string |
| 222 | |
| 223 | @ApiProperty({ type |
| 224 | @IsString() |
| 225 | @IsOptional() |
| 226 | oldTitle: string |
| 227 | |
| 228 | @ApiProperty({ type |
| 229 | @IsString() |
| 230 | @IsOptional() |
| 231 | newTitle: string |
| 232 | |
| 233 | @ApiProperty({ |
| 234 | type: String, |
| 235 | nullable: true, |
| 236 | description: |
| 237 | 'The commit that closed the issue, or the merge commit of a `merged` event.', |
| 238 | }) |
| 239 | @IsString() |
| 240 | @IsOptional() |
| 241 | commitSha: string |
| 242 | |
| 243 | @ApiProperty({ |
| 244 | type: String, |
| 245 | nullable: true, |
| 246 | example: 'octocat/ghost', |
| 247 | description: 'Repository of the pull request that closed the issue.', |
| 248 | }) |
| 249 | @IsString() |
| 250 | @IsOptional() |
| 251 | sourceRepository: string |
| 252 | |
| 253 | @ApiProperty({ type |
| 254 | @IsInt() |
| 255 | @IsOptional() |
| 256 | sourceNumber: number |
| 257 | |
| 258 | @ApiProperty() |
| 259 | @IsISO8601() |
| 260 | createdAt: string; |
| 261 | } |
| 262 | |
| 263 | export class IssueTimelineCommentDTO |
| 264 | @ApiProperty({ enum |
| 265 | @IsIn(['comment' |
| 266 | kind: 'comment |
| 267 | |
| 268 | @ApiProperty() |
| 269 | @IsString() |
| 270 | id: string; |
| 271 | |
| 272 | @ApiProperty() |
| 273 | @IsString() |
| 274 | body: string; |
| 275 | |
| 276 | @ApiProperty() |
| 277 | @IsString() |
| 278 | authorUsername: string; |
| 279 | |
| 280 | @ApiProperty({ type |
| 281 | @IsString() |
| 282 | @IsOptional() |
| 283 | authorImage: string |
| 284 | |
| 285 | @ApiProperty() |
| 286 | @IsISO8601() |
| 287 | createdAt: string; |
| 288 | |
| 289 | @ApiProperty() |
| 290 | @IsISO8601() |
| 291 | updatedAt: string; |
| 292 | } |
| 293 | |
| 294 | export class IssueTimelineEventItemDTO |
| 295 | @ApiProperty({ enum |
| 296 | @IsIn(['event' |
| 297 | kind: 'event |
| 298 | |
| 299 | @ApiProperty() |
| 300 | @IsString() |
| 301 | id: string; |
| 302 | |
| 303 | @ApiProperty({ type |
| 304 | @ValidateNested() |
| 305 | @Type(() => |
| 306 | event: IssueTimelineEventDTO |
| 307 | |
| 308 | @ApiProperty() |
| 309 | @IsISO8601() |
| 310 | createdAt: string; |
| 311 | } |
| 312 | |
| 313 | export class IssueReferenceRepositoryDTO |
| 314 | @ApiProperty({ example |
| 315 | @IsString() |
| 316 | username: string; |
| 317 | |
| 318 | @ApiProperty({ example |
| 319 | @IsString() |
| 320 | slug: string; |
| 321 | } |
| 322 | |
| 323 | export class IssueReferenceSourceDTO |
| 324 | @ApiProperty() |
| 325 | @IsInt() |
| 326 | number: number; |
| 327 | |
| 328 | @ApiProperty() |
| 329 | @IsString() |
| 330 | title: string; |
| 331 | |
| 332 | @ApiProperty({ |
| 333 | enumName: 'IssueState', |
| 334 | enum: schema.issueState |
| 335 | }) |
| 336 | @IsIn(schema. |
| 337 | state: (typeof |
| 338 | |
| 339 | @ApiProperty() |
| 340 | @IsBoolean() |
| 341 | isPullRequest: boolean; |
| 342 | } |
| 343 | |
| 344 | export class IssueTimelineReferenceDTO |
| 345 | @ApiProperty({ enum |
| 346 | @IsIn(['reference' |
| 347 | kind: 'reference |
| 348 | |
| 349 | @ApiProperty() |
| 350 | @IsString() |
| 351 | id: string; |
| 352 | |
| 353 | @ApiProperty({ |
| 354 | enumName: 'IssueReferenceSourceType', |
| 355 | enum: schema.issueReferenceSource |
| 356 | }) |
| 357 | @IsIn(schema. |
| 358 | sourceType: (typeof |
| 359 | |
| 360 | @ApiProperty() |
| 361 | @IsString() |
| 362 | actorUsername: string; |
| 363 | |
| 364 | @ApiProperty({ type |
| 365 | @ValidateNested() |
| 366 | @Type(() => |
| 367 | repository: IssueReferenceRepositoryDTO |
| 368 | |
| 369 | @ApiProperty({ |
| 370 | type: IssueReferenceSourceDTO, |
| 371 | nullable: true, |
| 372 | description: |
| 373 | 'The issue or pull request the mention was written in; null for a commit.', |
| 374 | }) |
| 375 | @ValidateNested() |
| 376 | @Type(() => |
| 377 | @IsOptional() |
| 378 | source: IssueReferenceSourceDTO |
| 379 | |
| 380 | @ApiProperty({ type |
| 381 | @IsString() |
| 382 | @IsOptional() |
| 383 | commitSha: string |
| 384 | |
| 385 | @ApiProperty() |
| 386 | @IsISO8601() |
| 387 | createdAt: string; |
| 388 | } |
| 389 | |
| 390 | @ApiExtraModels( |
| 391 | IssueTimelineCommentDTO, |
| 392 | IssueTimelineEventItemDTO, |
| 393 | IssueTimelineReferenceDTO, |
| 394 | PullRequestReviewDTO, |
| 395 | ) |
| 396 | export class GetIssueTimelineResponseDTO |
| 397 | @ApiProperty({ |
| 398 | description: |
| 399 | 'Comments, events and mentions from elsewhere interleaved oldest-first, exactly as rendered.', |
| 400 | type: 'array', |
| 401 | items: { |
| 402 | oneOf: [ |
| 403 | { $ref: getSchemaPath( |
| 404 | { $ref: getSchemaPath( |
| 405 | { $ref: getSchemaPath( |
| 406 | { $ref: getSchemaPath( |
| 407 | ], |
| 408 | }, |
| 409 | }) |
| 410 | timeline: Array |
| 411 | | IssueTimelineCommentDTO |
| 412 | | IssueTimelineEventItemDTO |
| 413 | | IssueTimelineReferenceDTO |
| 414 | | PullRequestReviewDTO |
| 415 | >; |
| 416 | } |