| 1 | import { |
| 2 | Body, |
| 3 | Controller, |
| 4 | Delete, |
| 5 | Get, |
| 6 | HttpCode, |
| 7 | HttpStatus, |
| 8 | Param, |
| 9 | Patch, |
| 10 | Post, |
| 11 | Query, |
| 12 | Session, |
| 13 | } from '@nestjs/common'; |
| 14 | import { |
| 15 | ApiBadRequestResponse, |
| 16 | ApiConflictResponse, |
| 17 | ApiCreatedResponse, |
| 18 | ApiForbiddenResponse, |
| 19 | ApiNoContentResponse, |
| 20 | ApiNotFoundResponse, |
| 21 | ApiOkResponse, |
| 22 | ApiOperation, |
| 23 | ApiTags, |
| 24 | } from '@nestjs/swagger'; |
| 25 | import { OptionalAuth, type |
| 26 | |
| 27 | import { ErrorResponseDTO } |
| 28 | import { |
| 29 | CreateReleaseRequestDTO, |
| 30 | GetReleasesQueryDTO, |
| 31 | GetReleasesResponseDTO, |
| 32 | ReleaseDTO, |
| 33 | UpdateReleaseRequestDTO, |
| 34 | } from './dto/release.dto.js'; |
| 35 | import { ReleasesService } |
| 36 | |
| 37 | @Controller('repositories/:username/:repo/releases') |
| 38 | @ApiTags('Releases') |
| 39 | export class ReleasesController |
| 40 | constructor(private readonly |
| 41 | |
| 42 | @Get() |
| 43 | @OptionalAuth() |
| 44 | @ApiOperation({ |
| 45 | summary: 'List releases', |
| 46 | description: |
| 47 | 'Newest first. Drafts are listed only for people who can write to the repository. Pages are cursor-based: pass a response `nextCursor` back as `cursor`.', |
| 48 | }) |
| 49 | @ApiOkResponse({ type |
| 50 | @ApiBadRequestResponse({ type |
| 51 | @ApiNotFoundResponse({ type |
| 52 | listReleases( |
| 53 | @Param('username' |
| 54 | @Param('repo' |
| 55 | @Query() query |
| 56 | @Session() session |
| 57 | ): Promise<GetReleasesResponseDTO |
| 58 | return this.releases |
| 59 | username, |
| 60 | repo, |
| 61 | requesterId: session?.user |
| 62 | query, |
| 63 | }); |
| 64 | } |
| 65 | |
| 66 | @Post() |
| 67 | @ApiOperation({ |
| 68 | summary: 'Create a release', |
| 69 | description: |
| 70 | 'Needs write access. A tag that does not exist yet is created at `target`, or at the default branch.', |
| 71 | }) |
| 72 | @ApiCreatedResponse({ type |
| 73 | @ApiBadRequestResponse({ |
| 74 | description: 'The tag name is not one git accepts.', |
| 75 | type: ErrorResponseDTO, |
| 76 | }) |
| 77 | @ApiForbiddenResponse({ type |
| 78 | @ApiNotFoundResponse({ |
| 79 | description: 'The repository, or the target to tag, does not exist.', |
| 80 | type: ErrorResponseDTO, |
| 81 | }) |
| 82 | @ApiConflictResponse({ |
| 83 | description: 'The tag already has a release.', |
| 84 | type: ErrorResponseDTO, |
| 85 | }) |
| 86 | createRelease( |
| 87 | @Param('username' |
| 88 | @Param('repo' |
| 89 | @Body() body |
| 90 | @Session() session |
| 91 | ): Promise<ReleaseDTO |
| 92 | return this.releases |
| 93 | username, |
| 94 | repo, |
| 95 | requesterId: session.user |
| 96 | body, |
| 97 | }); |
| 98 | } |
| 99 | |
| 100 | @Get('latest' |
| 101 | @OptionalAuth() |
| 102 | @ApiOperation({ |
| 103 | summary: 'Read the latest release', |
| 104 | description: |
| 105 | 'The most recently published release that is neither a draft nor a prerelease.', |
| 106 | }) |
| 107 | @ApiOkResponse({ type |
| 108 | @ApiNotFoundResponse({ type |
| 109 | getLatestRelease( |
| 110 | @Param('username' |
| 111 | @Param('repo' |
| 112 | @Session() session |
| 113 | ): Promise<ReleaseDTO |
| 114 | return this.releases |
| 115 | username, |
| 116 | repo, |
| 117 | requesterId: session?.user |
| 118 | }); |
| 119 | } |
| 120 | |
| 121 | @Get('tags/:tag' |
| 122 | @OptionalAuth() |
| 123 | @ApiOperation({ |
| 124 | summary: 'Read a release by tag', |
| 125 | description: 'A tag containing `/` is sent percent-encoded.', |
| 126 | }) |
| 127 | @ApiOkResponse({ type |
| 128 | @ApiNotFoundResponse({ type |
| 129 | getReleaseByTag( |
| 130 | @Param('username' |
| 131 | @Param('repo' |
| 132 | @Param('tag' |
| 133 | @Session() session |
| 134 | ): Promise<ReleaseDTO |
| 135 | return this.releases |
| 136 | username, |
| 137 | repo, |
| 138 | tagName, |
| 139 | requesterId: session?.user |
| 140 | }); |
| 141 | } |
| 142 | |
| 143 | @Patch(':id' |
| 144 | @ApiOperation({ |
| 145 | summary: 'Edit a release', |
| 146 | description: 'Needs write access. Omitted fields are left as they are.', |
| 147 | }) |
| 148 | @ApiOkResponse({ type |
| 149 | @ApiForbiddenResponse({ type |
| 150 | @ApiNotFoundResponse({ type |
| 151 | updateRelease( |
| 152 | @Param('username' |
| 153 | @Param('repo' |
| 154 | @Param('id' |
| 155 | @Body() body |
| 156 | @Session() session |
| 157 | ): Promise<ReleaseDTO |
| 158 | return this.releases |
| 159 | username, |
| 160 | repo, |
| 161 | id, |
| 162 | requesterId: session.user |
| 163 | body, |
| 164 | }); |
| 165 | } |
| 166 | |
| 167 | @Delete(':id' |
| 168 | @HttpCode(HttpStatus. |
| 169 | @ApiOperation({ |
| 170 | summary: 'Delete a release', |
| 171 | description: 'Needs write access. The tag is kept.', |
| 172 | }) |
| 173 | @ApiNoContentResponse() |
| 174 | @ApiForbiddenResponse({ type |
| 175 | @ApiNotFoundResponse({ type |
| 176 | deleteRelease( |
| 177 | @Param('username' |
| 178 | @Param('repo' |
| 179 | @Param('id' |
| 180 | @Session() session |
| 181 | ): Promise<void |
| 182 | return this.releases |
| 183 | username, |
| 184 | repo, |
| 185 | id, |
| 186 | requesterId: session.user |
| 187 | }); |
| 188 | } |
| 189 | } |