| 1 | import { |
| 2 | Body, |
| 3 | Controller, |
| 4 | Delete, |
| 5 | Get, |
| 6 | HttpCode, |
| 7 | HttpStatus, |
| 8 | Param, |
| 9 | Patch, |
| 10 | Post, |
| 11 | Session, |
| 12 | } from '@nestjs/common'; |
| 13 | import { |
| 14 | ApiBadRequestResponse, |
| 15 | ApiCreatedResponse, |
| 16 | ApiForbiddenResponse, |
| 17 | ApiNoContentResponse, |
| 18 | ApiNotFoundResponse, |
| 19 | ApiOkResponse, |
| 20 | ApiOperation, |
| 21 | ApiServiceUnavailableResponse, |
| 22 | ApiTags, |
| 23 | } from '@nestjs/swagger'; |
| 24 | import type { UserSession |
| 25 | |
| 26 | import { ErrorResponseDTO } |
| 27 | import { |
| 28 | CreatedWebhookDTO, |
| 29 | CreateWebhookRequestDTO, |
| 30 | ListDeliveriesResponseDTO, |
| 31 | ListWebhooksResponseDTO, |
| 32 | UpdateWebhookRequestDTO, |
| 33 | WebhookDTO, |
| 34 | WebhookSecretDTO, |
| 35 | } from './dto/webhook.dto.js'; |
| 36 | import { WebhooksService } |
| 37 | |
| 38 | @ApiTags('Webhooks') |
| 39 | @Controller('repositories/:username/:repo/webhooks') |
| 40 | export class RepositoryWebhooksController |
| 41 | constructor(private readonly |
| 42 | |
| 43 | @Get() |
| 44 | @ApiOperation({ summary |
| 45 | @ApiOkResponse({ type |
| 46 | @ApiForbiddenResponse({ type |
| 47 | @ApiNotFoundResponse({ type |
| 48 | async list( |
| 49 | @Param('username' |
| 50 | @Param('repo' |
| 51 | @Session() session |
| 52 | ): Promise<ListWebhooksResponseDTO |
| 53 | return this.webhooks |
| 54 | } |
| 55 | |
| 56 | @Post() |
| 57 | @ApiOperation({ |
| 58 | summary: 'Create a webhook', |
| 59 | description: |
| 60 | 'Sends the endpoint a `ping` right away. The response carries the signing secret, which is never shown again. Admins only.', |
| 61 | }) |
| 62 | @ApiCreatedResponse({ type |
| 63 | @ApiBadRequestResponse({ type |
| 64 | @ApiForbiddenResponse({ type |
| 65 | @ApiNotFoundResponse({ type |
| 66 | @ApiServiceUnavailableResponse({ type |
| 67 | async create( |
| 68 | @Param('username' |
| 69 | @Param('repo' |
| 70 | @Body() body |
| 71 | @Session() session |
| 72 | ): Promise<CreatedWebhookDTO |
| 73 | return this.webhooks |
| 74 | await this.owner |
| 75 | body, |
| 76 | ); |
| 77 | } |
| 78 | |
| 79 | @Patch(':webhookId' |
| 80 | @ApiOperation({ summary |
| 81 | @ApiOkResponse({ type |
| 82 | @ApiBadRequestResponse({ type |
| 83 | @ApiForbiddenResponse({ type |
| 84 | @ApiNotFoundResponse({ type |
| 85 | async update( |
| 86 | @Param('username' |
| 87 | @Param('repo' |
| 88 | @Param('webhookId' |
| 89 | @Body() body |
| 90 | @Session() session |
| 91 | ): Promise<WebhookDTO |
| 92 | return this.webhooks |
| 93 | await this.owner |
| 94 | webhookId, |
| 95 | body, |
| 96 | ); |
| 97 | } |
| 98 | |
| 99 | @Delete(':webhookId' |
| 100 | @HttpCode(HttpStatus. |
| 101 | @ApiOperation({ |
| 102 | summary: 'Delete a webhook', |
| 103 | description: 'Pending deliveries are dropped. Admins only.', |
| 104 | }) |
| 105 | @ApiNoContentResponse() |
| 106 | @ApiForbiddenResponse({ type |
| 107 | @ApiNotFoundResponse({ type |
| 108 | async remove( |
| 109 | @Param('username' |
| 110 | @Param('repo' |
| 111 | @Param('webhookId' |
| 112 | @Session() session |
| 113 | ): Promise<void |
| 114 | return this.webhooks |
| 115 | await this.owner |
| 116 | webhookId, |
| 117 | ); |
| 118 | } |
| 119 | |
| 120 | @Post(':webhookId/secret' |
| 121 | @HttpCode(HttpStatus. |
| 122 | @ApiOperation({ |
| 123 | summary: "Replace a webhook's signing secret", |
| 124 | description: |
| 125 | 'The old secret stops working at once, pending retries included. The response is the only time the new one is shown. Admins only.', |
| 126 | }) |
| 127 | @ApiOkResponse({ type |
| 128 | @ApiForbiddenResponse({ type |
| 129 | @ApiNotFoundResponse({ type |
| 130 | @ApiServiceUnavailableResponse({ type |
| 131 | async rollSecret( |
| 132 | @Param('username' |
| 133 | @Param('repo' |
| 134 | @Param('webhookId' |
| 135 | @Session() session |
| 136 | ): Promise<WebhookSecretDTO |
| 137 | return this.webhooks |
| 138 | await this.owner |
| 139 | webhookId, |
| 140 | ); |
| 141 | } |
| 142 | |
| 143 | @Post(':webhookId/pings' |
| 144 | @HttpCode(HttpStatus. |
| 145 | @ApiOperation({ |
| 146 | summary: 'Send a test delivery', |
| 147 | description: 'Queues a `ping`. Admins only.', |
| 148 | }) |
| 149 | @ApiNoContentResponse() |
| 150 | @ApiForbiddenResponse({ type |
| 151 | @ApiNotFoundResponse({ type |
| 152 | async ping( |
| 153 | @Param('username' |
| 154 | @Param('repo' |
| 155 | @Param('webhookId' |
| 156 | @Session() session |
| 157 | ): Promise<void |
| 158 | return this.webhooks |
| 159 | await this.owner |
| 160 | webhookId, |
| 161 | ); |
| 162 | } |
| 163 | |
| 164 | @Get(':webhookId/deliveries' |
| 165 | @ApiOperation({ |
| 166 | summary: 'List recent deliveries', |
| 167 | description: 'The 50 most recent, each with its attempts. Admins only.', |
| 168 | }) |
| 169 | @ApiOkResponse({ type |
| 170 | @ApiForbiddenResponse({ type |
| 171 | @ApiNotFoundResponse({ type |
| 172 | async deliveries( |
| 173 | @Param('username' |
| 174 | @Param('repo' |
| 175 | @Param('webhookId' |
| 176 | @Session() session |
| 177 | ): Promise<ListDeliveriesResponseDTO |
| 178 | return this.webhooks |
| 179 | await this.owner |
| 180 | webhookId, |
| 181 | ); |
| 182 | } |
| 183 | |
| 184 | @Post(':webhookId/deliveries/:deliveryId/redeliveries' |
| 185 | @HttpCode(HttpStatus. |
| 186 | @ApiOperation({ |
| 187 | summary: 'Redeliver', |
| 188 | description: |
| 189 | 'Sends the same body again as a new delivery, with its own id and retries. Admins only.', |
| 190 | }) |
| 191 | @ApiNoContentResponse() |
| 192 | @ApiForbiddenResponse({ type |
| 193 | @ApiNotFoundResponse({ type |
| 194 | async redeliver( |
| 195 | @Param('username' |
| 196 | @Param('repo' |
| 197 | @Param('webhookId' |
| 198 | @Param('deliveryId' |
| 199 | @Session() session |
| 200 | ): Promise<void |
| 201 | return this.webhooks |
| 202 | await this.owner |
| 203 | webhookId, |
| 204 | deliveryId, |
| 205 | ); |
| 206 | } |
| 207 | |
| 208 | private owner(username |
| 209 | return this.webhooks |
| 210 | username, |
| 211 | repo, |
| 212 | requesterId: session.user |
| 213 | }); |
| 214 | } |
| 215 | } |