| 1 | import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger'; |
| 2 | import { |
| 3 | IsISO8601, |
| 4 | IsInt, |
| 5 | IsOptional, |
| 6 | IsString, |
| 7 | MaxLength, |
| 8 | Validate, |
| 9 | ValidateNested, |
| 10 | ValidatorConstraint, |
| 11 | type ValidatorConstraintInterface, |
| 12 | } from 'class-validator'; |
| 13 | import { Type } |
| 14 | |
| 15 | import { |
| 16 | isSafeTreePath, |
| 17 | MAX_TREE_PATH_LENGTH, |
| 18 | } from '../../../lib/git/tree/tree-path.js'; |
| 19 | |
| 20 | const TREE_ENTRY_TYPES = [ |
| 21 | |
| 22 | @ValidatorConstraint({ name: |
| 23 | export class SafeTreePathConstraint |
| 24 | validate(value: |
| 25 | return isSafeTreePath(value |
| 26 | } |
| 27 | |
| 28 | defaultMessage() { |
| 29 | return 'path must be a repository-relative directory, without "." or ".." segments'; |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | export class GetRepositoryContentsQueryDTO |
| 34 | @ApiPropertyOptional({ |
| 35 | description: |
| 36 | 'Directory to list, relative to the repository root. Omit for the root. Slashes may be sent percent-encoded (`src%2Fdeep`); they are decoded once, so an already-decoded `src/deep` works too.', |
| 37 | example: 'src/services', |
| 38 | maxLength: MAX_TREE_PATH_LENGTH, |
| 39 | }) |
| 40 | @IsString() |
| 41 | @MaxLength(MAX_TREE_PATH_LENGTH) |
| 42 | @Validate(SafeTreePathConstraint) |
| 43 | @IsOptional() |
| 44 | path?: string |
| 45 | |
| 46 | @ApiPropertyOptional({ |
| 47 | description: |
| 48 | 'Branch or commit sha to list. Accepts `main`, `refs/heads/main` or a commit sha. Omit for the default branch.', |
| 49 | example: 'main', |
| 50 | }) |
| 51 | @IsString() |
| 52 | @IsOptional() |
| 53 | ref?: string |
| 54 | } |
| 55 | |
| 56 | export class CommitSummaryDTO |
| 57 | @ApiProperty({ description |
| 58 | @IsString() |
| 59 | sha: string; |
| 60 | |
| 61 | @ApiProperty({ |
| 62 | description: 'Commit subject - the first line of the message.', |
| 63 | }) |
| 64 | @IsString() |
| 65 | subject: string; |
| 66 | |
| 67 | @ApiProperty({ description |
| 68 | @IsISO8601() |
| 69 | committedAt: string; |
| 70 | } |
| 71 | |
| 72 | export class TreeEntryDTO |
| 73 | @ApiProperty({ description |
| 74 | @IsString() |
| 75 | name: string; |
| 76 | |
| 77 | @ApiProperty({ description |
| 78 | @IsString() |
| 79 | path: string; |
| 80 | |
| 81 | @ApiProperty({ |
| 82 | enumName: 'TreeEntryType', |
| 83 | enum: TREE_ENTRY_TYPES, |
| 84 | description: '`tree` is a directory, `commit` a submodule.', |
| 85 | }) |
| 86 | @IsString() |
| 87 | type: (typeof |
| 88 | |
| 89 | @ApiProperty({ |
| 90 | description: 'Git file mode, e.g. `100644`.', |
| 91 | example: '100644', |
| 92 | }) |
| 93 | @IsString() |
| 94 | mode: string; |
| 95 | |
| 96 | @ApiProperty({ description |
| 97 | @IsString() |
| 98 | oid: string; |
| 99 | |
| 100 | @ApiProperty({ |
| 101 | type: Number, |
| 102 | nullable: true, |
| 103 | description: 'Blob size in bytes; `null` for trees and submodules.', |
| 104 | }) |
| 105 | @IsInt() |
| 106 | @IsOptional() |
| 107 | size: number |
| 108 | |
| 109 | @ApiProperty({ |
| 110 | type: CommitSummaryDTO, |
| 111 | nullable: true, |
| 112 | description: |
| 113 | 'Newest commit touching this entry, or anything beneath it for a tree. `null` only if the index has not caught up with the ref.', |
| 114 | }) |
| 115 | @ValidateNested() |
| 116 | @Type(() => |
| 117 | @IsOptional() |
| 118 | lastCommit: CommitSummaryDTO |
| 119 | } |
| 120 | |
| 121 | export class GetRepositoryContentsResponseDTO |
| 122 | @ApiProperty({ |
| 123 | description: 'Ref that was listed, always fully qualified.', |
| 124 | example: 'refs/heads/main', |
| 125 | }) |
| 126 | @IsString() |
| 127 | ref: string; |
| 128 | |
| 129 | @ApiProperty({ |
| 130 | description: |
| 131 | 'Normalized directory that was listed, with a trailing slash. Empty at the root.', |
| 132 | example: 'src/services/', |
| 133 | }) |
| 134 | @IsString() |
| 135 | path: string; |
| 136 | |
| 137 | @ApiProperty({ |
| 138 | description: 'Commits reachable from the ref. `0` for an unborn ref.', |
| 139 | example: 128, |
| 140 | }) |
| 141 | @IsInt() |
| 142 | commitCount: number; |
| 143 | |
| 144 | @ApiProperty({ |
| 145 | type: CommitSummaryDTO, |
| 146 | nullable: true, |
| 147 | description: |
| 148 | 'Tip commit of the ref. `null` when nothing has been pushed yet.', |
| 149 | }) |
| 150 | @ValidateNested() |
| 151 | @Type(() => |
| 152 | @IsOptional() |
| 153 | commit: CommitSummaryDTO |
| 154 | |
| 155 | @ApiProperty({ |
| 156 | type: [TreeEntryDTO], |
| 157 | description: |
| 158 | 'One level of the directory: directories first (submodules among them), then files, each group alphabetical. Case is a minor difference, so `readme.md` and `README.md` sit together rather than in separate blocks, and embedded numbers order naturally (`file2` before `file10`). Empty for an unborn ref or a path that is not a directory.', |
| 159 | }) |
| 160 | @ValidateNested({ each |
| 161 | @Type(() => |
| 162 | entries: TreeEntryDTO |
| 163 | } |