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