|
| 1 | +/* |
| 2 | + * Copyright 2025 The Backstage Authors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +import { ActionsRegistryService } from '@backstage/backend-plugin-api/alpha'; |
| 17 | +import { PermissionsService } from '@backstage/backend-plugin-api'; |
| 18 | +import { NotAllowedError } from '@backstage/errors'; |
| 19 | +import { AuthorizeResult } from '@backstage/plugin-permission-common'; |
| 20 | +import { announcementEntityPermissions } from '@backstage-community/plugin-announcements-common'; |
| 21 | +import { DateTime } from 'luxon'; |
| 22 | +import { v4 as uuid } from 'uuid'; |
| 23 | +import { PersistenceContext } from '../service/persistence'; |
| 24 | + |
| 25 | +const { announcementCreatePermission } = announcementEntityPermissions; |
| 26 | + |
| 27 | +/** |
| 28 | + * Registers the `announcements:create-announcement` action. |
| 29 | + * @internal |
| 30 | + */ |
| 31 | +export function createCreateAnnouncementAction(options: { |
| 32 | + actionsRegistry: ActionsRegistryService; |
| 33 | + persistenceContext: PersistenceContext; |
| 34 | + permissions: PermissionsService; |
| 35 | +}) { |
| 36 | + const { actionsRegistry, persistenceContext, permissions } = options; |
| 37 | + |
| 38 | + actionsRegistry.register({ |
| 39 | + name: 'announcements:create-announcement', |
| 40 | + title: 'Create Announcement', |
| 41 | + description: |
| 42 | + 'Create a new announcement. Requires the announcement.entity.create permission.', |
| 43 | + attributes: { |
| 44 | + readOnly: false, |
| 45 | + destructive: false, |
| 46 | + idempotent: false, |
| 47 | + }, |
| 48 | + visibilityPermission: announcementCreatePermission, |
| 49 | + schema: { |
| 50 | + input: z => |
| 51 | + z.object({ |
| 52 | + title: z.string().describe('Title of the announcement'), |
| 53 | + excerpt: z.string().describe('Short summary shown in listing views'), |
| 54 | + body: z.string().describe('Full body text of the announcement'), |
| 55 | + publisher: z |
| 56 | + .string() |
| 57 | + .describe('User or team publishing the announcement'), |
| 58 | + active: z |
| 59 | + .boolean() |
| 60 | + .default(true) |
| 61 | + .describe('Whether the announcement is active'), |
| 62 | + start_at: z |
| 63 | + .string() |
| 64 | + .describe('ISO 8601 datetime when the announcement becomes active'), |
| 65 | + until_date: z |
| 66 | + .string() |
| 67 | + .optional() |
| 68 | + .describe( |
| 69 | + 'ISO 8601 datetime when the announcement expires (optional)', |
| 70 | + ), |
| 71 | + category: z |
| 72 | + .string() |
| 73 | + .optional() |
| 74 | + .describe('Category slug to assign to the announcement'), |
| 75 | + on_behalf_of: z |
| 76 | + .string() |
| 77 | + .optional() |
| 78 | + .describe('Optional entity ref the announcement is on behalf of'), |
| 79 | + tags: z |
| 80 | + .array(z.string()) |
| 81 | + .optional() |
| 82 | + .describe('Tag slugs to attach to the announcement'), |
| 83 | + sendNotification: z |
| 84 | + .boolean() |
| 85 | + .default(false) |
| 86 | + .describe( |
| 87 | + 'Whether to send a notification when the announcement is created', |
| 88 | + ), |
| 89 | + }), |
| 90 | + output: z => |
| 91 | + z.object({ |
| 92 | + id: z.string(), |
| 93 | + title: z.string(), |
| 94 | + excerpt: z.string(), |
| 95 | + body: z.string(), |
| 96 | + publisher: z.string(), |
| 97 | + active: z.boolean(), |
| 98 | + created_at: z.string(), |
| 99 | + start_at: z.string(), |
| 100 | + until_date: z.string().nullable().optional(), |
| 101 | + updated_at: z.string(), |
| 102 | + category: z |
| 103 | + .object({ slug: z.string(), title: z.string() }) |
| 104 | + .optional(), |
| 105 | + on_behalf_of: z.string().optional(), |
| 106 | + }), |
| 107 | + }, |
| 108 | + async action({ input, credentials }) { |
| 109 | + const decision = await permissions.authorize( |
| 110 | + [{ permission: announcementCreatePermission }], |
| 111 | + { credentials }, |
| 112 | + ); |
| 113 | + |
| 114 | + if (decision[0].result === AuthorizeResult.DENY) { |
| 115 | + throw new NotAllowedError( |
| 116 | + 'Unauthorized: missing announcement.entity.create permission', |
| 117 | + ); |
| 118 | + } |
| 119 | + |
| 120 | + const now = DateTime.now(); |
| 121 | + const startAt = DateTime.fromISO(input.start_at); |
| 122 | + const untilDate = input.until_date |
| 123 | + ? DateTime.fromISO(input.until_date) |
| 124 | + : undefined; |
| 125 | + |
| 126 | + const announcement = |
| 127 | + await persistenceContext.announcementsStore.insertAnnouncement({ |
| 128 | + id: uuid(), |
| 129 | + title: input.title, |
| 130 | + excerpt: input.excerpt, |
| 131 | + body: input.body, |
| 132 | + publisher: input.publisher, |
| 133 | + active: input.active, |
| 134 | + sendNotification: input.sendNotification, |
| 135 | + category: input.category, |
| 136 | + on_behalf_of: input.on_behalf_of, |
| 137 | + tags: input.tags, |
| 138 | + created_at: now, |
| 139 | + start_at: startAt, |
| 140 | + until_date: untilDate, |
| 141 | + updated_at: now, |
| 142 | + }); |
| 143 | + |
| 144 | + return { |
| 145 | + output: { |
| 146 | + id: announcement.id, |
| 147 | + title: announcement.title, |
| 148 | + excerpt: announcement.excerpt, |
| 149 | + body: announcement.body, |
| 150 | + publisher: announcement.publisher, |
| 151 | + active: announcement.active, |
| 152 | + created_at: announcement.created_at.toUTC().toISO()!, |
| 153 | + start_at: announcement.start_at.toUTC().toISO()!, |
| 154 | + until_date: announcement.until_date |
| 155 | + ? announcement.until_date.toUTC().toISO() |
| 156 | + : undefined, |
| 157 | + updated_at: announcement.updated_at.toUTC().toISO()!, |
| 158 | + category: announcement.category, |
| 159 | + on_behalf_of: announcement.on_behalf_of, |
| 160 | + }, |
| 161 | + }; |
| 162 | + }, |
| 163 | + }); |
| 164 | +} |
0 commit comments