| 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 | } from './dto/webhook.dto.js'; |
| 35 | import { WebhooksService } |
| 36 | |
| 37 | @ApiTags('Webhooks') |
| 38 | @Controller('repositories/:username/:repo/webhooks') |
| 39 | export class WebhooksController |
| 40 | constructor(private readonly |
| 41 | |
| 42 | @Get() |
| 43 | @ApiOperation({ summary |
| 44 | @ApiOkResponse({ type |
| 45 | @ApiForbiddenResponse({ type |
| 46 | @ApiNotFoundResponse({ type |
| 47 | list( |
| 48 | @Param('username' |
| 49 | @Param('repo' |
| 50 | @Session() session |
| 51 | ): Promise<ListWebhooksResponseDTO |
| 52 | return this.webhooks |
| 53 | username, |
| 54 | repo, |
| 55 | requesterId: session.user |
| 56 | }); |
| 57 | } |
| 58 | |
| 59 | @Post() |
| 60 | @ApiOperation({ |
| 61 | summary: 'Create a webhook', |
| 62 | description: |
| 63 | 'Sends the endpoint a `ping` right away. The response carries the signing secret, which is never shown again. Admins only.', |
| 64 | }) |
| 65 | @ApiCreatedResponse({ type |
| 66 | @ApiBadRequestResponse({ type |
| 67 | @ApiForbiddenResponse({ type |
| 68 | @ApiNotFoundResponse({ type |
| 69 | @ApiServiceUnavailableResponse({ type |
| 70 | create( |
| 71 | @Param('username' |
| 72 | @Param('repo' |
| 73 | @Body() body |
| 74 | @Session() session |
| 75 | ): Promise<CreatedWebhookDTO |
| 76 | return this.webhooks |
| 77 | username, |
| 78 | repo, |
| 79 | requesterId: session.user |
| 80 | url: body.url |
| 81 | events: body.events |
| 82 | }); |
| 83 | } |
| 84 | |
| 85 | @Patch(':webhookId' |
| 86 | @ApiOperation({ summary |
| 87 | @ApiOkResponse({ type |
| 88 | @ApiBadRequestResponse({ type |
| 89 | @ApiForbiddenResponse({ type |
| 90 | @ApiNotFoundResponse({ type |
| 91 | update( |
| 92 | @Param('username' |
| 93 | @Param('repo' |
| 94 | @Param('webhookId' |
| 95 | @Body() body |
| 96 | @Session() session |
| 97 | ): Promise<WebhookDTO |
| 98 | return this.webhooks |
| 99 | username, |
| 100 | repo, |
| 101 | requesterId: session.user |
| 102 | webhookId, |
| 103 | ...body, |
| 104 | }); |
| 105 | } |
| 106 | |
| 107 | @Delete(':webhookId' |
| 108 | @HttpCode(HttpStatus. |
| 109 | @ApiOperation({ |
| 110 | summary: 'Delete a webhook', |
| 111 | description: 'Pending deliveries are dropped. Admins only.', |
| 112 | }) |
| 113 | @ApiNoContentResponse() |
| 114 | @ApiForbiddenResponse({ type |
| 115 | @ApiNotFoundResponse({ type |
| 116 | remove( |
| 117 | @Param('username' |
| 118 | @Param('repo' |
| 119 | @Param('webhookId' |
| 120 | @Session() session |
| 121 | ): Promise<void |
| 122 | return this.webhooks |
| 123 | username, |
| 124 | repo, |
| 125 | requesterId: session.user |
| 126 | webhookId, |
| 127 | }); |
| 128 | } |
| 129 | |
| 130 | @Post(':webhookId/pings' |
| 131 | @HttpCode(HttpStatus. |
| 132 | @ApiOperation({ |
| 133 | summary: 'Send a test delivery', |
| 134 | description: 'Queues a `ping`. Admins only.', |
| 135 | }) |
| 136 | @ApiNoContentResponse() |
| 137 | @ApiForbiddenResponse({ type |
| 138 | @ApiNotFoundResponse({ type |
| 139 | ping( |
| 140 | @Param('username' |
| 141 | @Param('repo' |
| 142 | @Param('webhookId' |
| 143 | @Session() session |
| 144 | ): Promise<void |
| 145 | return this.webhooks |
| 146 | username, |
| 147 | repo, |
| 148 | requesterId: session.user |
| 149 | webhookId, |
| 150 | }); |
| 151 | } |
| 152 | |
| 153 | @Get(':webhookId/deliveries' |
| 154 | @ApiOperation({ |
| 155 | summary: 'List recent deliveries', |
| 156 | description: 'The 50 most recent, each with its attempts. Admins only.', |
| 157 | }) |
| 158 | @ApiOkResponse({ type |
| 159 | @ApiForbiddenResponse({ type |
| 160 | @ApiNotFoundResponse({ type |
| 161 | deliveries( |
| 162 | @Param('username' |
| 163 | @Param('repo' |
| 164 | @Param('webhookId' |
| 165 | @Session() session |
| 166 | ): Promise<ListDeliveriesResponseDTO |
| 167 | return this.webhooks |
| 168 | username, |
| 169 | repo, |
| 170 | requesterId: session.user |
| 171 | webhookId, |
| 172 | }); |
| 173 | } |
| 174 | |
| 175 | @Post(':webhookId/deliveries/:deliveryId/redeliveries' |
| 176 | @HttpCode(HttpStatus. |
| 177 | @ApiOperation({ |
| 178 | summary: 'Redeliver', |
| 179 | description: |
| 180 | 'Sends the same body again as a new delivery, with its own id and retries. Admins only.', |
| 181 | }) |
| 182 | @ApiNoContentResponse() |
| 183 | @ApiForbiddenResponse({ type |
| 184 | @ApiNotFoundResponse({ type |
| 185 | redeliver( |
| 186 | @Param('username' |
| 187 | @Param('repo' |
| 188 | @Param('webhookId' |
| 189 | @Param('deliveryId' |
| 190 | @Session() session |
| 191 | ): Promise<void |
| 192 | return this.webhooks |
| 193 | username, |
| 194 | repo, |
| 195 | requesterId: session.user |
| 196 | webhookId, |
| 197 | deliveryId, |
| 198 | }); |
| 199 | } |
| 200 | } |