| 1 | import type { WebhookBody, WebhookPing } from '../webhooks.js'; |
| 2 | |
| 3 | /** Who did it, linked to their profile where they have one. */ |
| 4 | export type WebhookActor |
| 5 | name: string |
| 6 | url: string |
| 7 | avatarUrl: string |
| 8 | }; |
| 9 | |
| 10 | export type WebhookCommit |
| 11 | sha: string |
| 12 | url: string |
| 13 | subject: string |
| 14 | author: string |
| 15 | }; |
| 16 | |
| 17 | /** What a chat message says about an event, before a format escapes and shapes it. `action`, `excerpt` and commit subjects hold user text, unescaped. */ |
| 18 | export type WebhookMessage |
| 19 | /** The repository's or organization's name. */ |
| 20 | context: string |
| 21 | /** Null only for a ping, which nobody did. */ |
| 22 | actor: WebhookActor |
| 23 | /** What happened, after the actor's name: "opened issue #7: Bell count". */ |
| 24 | action: string |
| 25 | url: string |
| 26 | excerpt: string |
| 27 | /** The newest commits of a push. */ |
| 28 | commits: WebhookCommit |
| 29 | /** Commits pushed beyond those listed. */ |
| 30 | moreCommits: number |
| 31 | /** An RGB accent, the way GitHub colours its chat messages. */ |
| 32 | color: number |
| 33 | /** ISO 8601, when the event happened. */ |
| 34 | timestamp: string |
| 35 | }; |
| 36 | |
| 37 | /** The actor and the action as one sentence, for formats with no author line. */ |
| 38 | export function summaryOf({ |
| 39 | return actor ? |
| 40 | } |
| 41 | |
| 42 | // Enough to see the point of a comment in a chat, short enough to keep a channel readable. |
| 43 | const EXCERPT_LENGTH = 500 |
| 44 | |
| 45 | // A push lists this many commits; the rest are counted. |
| 46 | const LISTED_COMMITS = 5 |
| 47 | |
| 48 | // GitHub's Primer colours: green for something new, purple for merged, red for gone, blue for code and talk, grey for edits. |
| 49 | const GREEN = 0x1f883d |
| 50 | const PURPLE = 0x8250df |
| 51 | const RED = 0xcf222e |
| 52 | const BLUE = 0x0969da |
| 53 | const GREY = 0x59636e |
| 54 | |
| 55 | const REVIEWS = { |
| 56 | approved: ['approved', |
| 57 | changes_requested: ['requested changes on', |
| 58 | commented: ['reviewed', |
| 59 | } as const; |
| 60 | |
| 61 | export function messageOf(body |
| 62 | if (body.event |
| 63 | return { |
| 64 | context: body.repository |
| 65 | actor: null, |
| 66 | action: 'Ghost webhook connected', |
| 67 | url: null, |
| 68 | excerpt: `Events: ${body |
| 69 | commits: [], |
| 70 | moreCommits: 0, |
| 71 | color: BLUE, |
| 72 | timestamp: body.createdAt |
| 73 | }; |
| 74 | } |
| 75 | |
| 76 | const { repository, |
| 77 | const actor: |
| 78 | name: sender?.username |
| 79 | url: sender?.htmlUrl |
| 80 | avatarUrl: sender?.avatarUrl |
| 81 | }; |
| 82 | const say = |
| 83 | action: string, |
| 84 | color: number, |
| 85 | url: string |
| 86 | excerpt: string |
| 87 | ): WebhookMessage => |
| 88 | context: repository.fullName |
| 89 | actor, |
| 90 | action, |
| 91 | url, |
| 92 | excerpt: !excerpt?.trim |
| 93 | ? null |
| 94 | : excerpt.trim() |
| 95 | ? `${excerpt.trim |
| 96 | : excerpt.trim() |
| 97 | commits: [], |
| 98 | moreCommits: 0, |
| 99 | color, |
| 100 | timestamp: body.createdAt |
| 101 | }); |
| 102 | /** A message listing the pushed commits, linked to each one. */ |
| 103 | const pushed = |
| 104 | const { commits |
| 105 | return { |
| 106 | ...say(action, |
| 107 | commits: commits.slice |
| 108 | sha: commit.sha |
| 109 | url: `${repository. |
| 110 | subject: commit.subject |
| 111 | author: commit.authorName |
| 112 | })), |
| 113 | moreCommits: Math.max |
| 114 | }; |
| 115 | }; |
| 116 | |
| 117 | const thread = |
| 118 | const about = |
| 119 | ? `${body.pullRequest |
| 120 | : ''; |
| 121 | const onThread = |
| 122 | action: string, |
| 123 | color: number, |
| 124 | excerpt: string |
| 125 | ) => say( |
| 126 | |
| 127 | switch (body.event |
| 128 | case 'push': { |
| 129 | const ref = |
| 130 | // `refs/heads/main` is the branch main, `refs/tags/v1` the tag v1. |
| 131 | const [kind, |
| 132 | ? ['tag', ref |
| 133 | : ['branch', ref |
| 134 | if (body.deleted |
| 135 | if (body.created |
| 136 | return say( |
| 137 | `created ${kind} ${ |
| 138 | GREEN, |
| 139 | `${repository.htmlUrl} |
| 140 | ); |
| 141 | const [only] |
| 142 | return pushed( |
| 143 | `pushed ${commitCount(body |
| 144 | body.commits?.length |
| 145 | ? `${repository.htmlUrl |
| 146 | : `${repository.htmlUrl |
| 147 | ); |
| 148 | } |
| 149 | case 'issue.opened': |
| 150 | return onThread(`opened |
| 151 | case 'issue.edited': |
| 152 | return onThread(`edited |
| 153 | case 'issue.closed': |
| 154 | return onThread(`closed |
| 155 | case 'issue.reopened': |
| 156 | return onThread(`reopened |
| 157 | case 'issue.assigned': |
| 158 | return onThread(`assigned |
| 159 | case 'issue.unassigned': |
| 160 | return onThread( |
| 161 | `unassigned ${body.assignee |
| 162 | GREY, |
| 163 | ); |
| 164 | case 'issue.labeled': |
| 165 | return onThread(`added the label |
| 166 | case 'issue.unlabeled': |
| 167 | return onThread( |
| 168 | `removed the label ${body.label |
| 169 | GREY, |
| 170 | ); |
| 171 | case 'issue.commented': |
| 172 | return onThread( |
| 173 | `commented on ${about}`, |
| 174 | BLUE, |
| 175 | body.comment?.body |
| 176 | ); |
| 177 | case 'issue.comment_edited': |
| 178 | return onThread( |
| 179 | `edited a comment on ${about}`, |
| 180 | GREY, |
| 181 | body.comment?.body |
| 182 | ); |
| 183 | case 'issue.comment_deleted': |
| 184 | return onThread(`deleted a comment on |
| 185 | case 'pull_request.ready_for_review': |
| 186 | return onThread(`marked |
| 187 | case 'pull_request.converted_to_draft': |
| 188 | return onThread(`converted |
| 189 | case 'pull_request.merged': |
| 190 | return onThread(`merged |
| 191 | case 'pull_request.synchronized': |
| 192 | return pushed( |
| 193 | `${body.forced |
| 194 | thread?.htmlUrl ?? |
| 195 | ); |
| 196 | case 'pull_request.reviewed': { |
| 197 | const [verb, |
| 198 | return onThread(`${ |
| 199 | } |
| 200 | case 'pull_request.review_dismissed': |
| 201 | return onThread( |
| 202 | `dismissed a review on ${about}`, |
| 203 | GREY, |
| 204 | body.review?.dismissalMessage |
| 205 | ); |
| 206 | case 'pull_request.review_commented': |
| 207 | return onThread( |
| 208 | `commented on ${body.comment |
| 209 | BLUE, |
| 210 | body.comment?.body |
| 211 | ); |
| 212 | case 'label.created': |
| 213 | case 'label.edited': |
| 214 | case 'label.deleted': |
| 215 | return say( |
| 216 | `${body.event. |
| 217 | body.event === |
| 218 | ? GREEN |
| 219 | : body.event |
| 220 | ? RED |
| 221 | : GREY, |
| 222 | `${repository.htmlUrl} |
| 223 | ); |
| 224 | case 'release.created': |
| 225 | case 'release.published': |
| 226 | case 'release.edited': |
| 227 | case 'release.deleted': |
| 228 | return say( |
| 229 | `${body.event. |
| 230 | body.event === |
| 231 | ? RED |
| 232 | : body.event |
| 233 | ? GREY |
| 234 | : GREEN, |
| 235 | body.release?.htmlUrl |
| 236 | body.event === |
| 237 | ); |
| 238 | case 'star.created': |
| 239 | return say(`starred |
| 240 | case 'star.deleted': |
| 241 | return say(`unstarred |
| 242 | case 'watch.started': |
| 243 | return say(`started watching |
| 244 | case 'fork.created': |
| 245 | return say( |
| 246 | `forked ${repository.fullName |
| 247 | GREEN, |
| 248 | body.fork?.htmlUrl |
| 249 | ); |
| 250 | case 'repository.edited': |
| 251 | return say(`edited the settings of |
| 252 | case 'repository.transferred': |
| 253 | return say(`transferred |
| 254 | case 'member.added': |
| 255 | return say( |
| 256 | `accepted the invitation to collaborate on ${repository.fullName |
| 257 | GREEN, |
| 258 | ); |
| 259 | case 'member.removed': |
| 260 | return say(`removed |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | function commitCount({ commits |
| 265 | return `${commits. |
| 266 | } |