-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmessages.ts
164 lines (145 loc) · 4.84 KB
/
messages.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import * as github from '@actions/github'
import { Bits, Blocks, Md, Message, SlackMessageDto } from 'slack-block-builder'
import { dedent } from 'ts-dedent'
import { P, match } from 'ts-pattern'
import { z } from 'zod'
import { COLORS, MEMBERS, NONEXSISTANT_SHA } from './constants'
import { InputSchema } from './inputs'
import { getOctoClient } from './clients'
export async function createThreadMainMessageSurface(
inputs: z.infer<typeof InputSchema>
): Promise<SlackMessageDto> {
const commitMessages = await getAssociatedCommitMessages(inputs.before_ref)
const message = Message({
channel: inputs.channel_id,
text: match(inputs.phase)
.with('start', () => '배포 진행중 :loading:')
.with('finish', () => '배포 완료 :ballot_box_with_check:')
.with('failure', () => '배포 실패 :x:')
.with('cancelled', () => '배포 취소 :black_square_for_stop:')
.otherwise(() => ''),
ts: match(inputs)
.with(
{ phase: P.union('finish', 'failure', 'cancelled') },
({ thread_ts }) => thread_ts
)
.otherwise(() => undefined)
})
.blocks(
Blocks.Divider(),
Blocks.Section({
text: dedent`
${Md.group(inputs.group_id)}
${Md.listBullet(await createFormattedJiraIssueLinks(commitMessages))}
`
})
)
.attachments(
Bits.Attachment({
color: match(inputs.phase)
.with('start', () => COLORS.PENDING)
.with('finish', () => COLORS.SUCCESS)
.with('failure', () => COLORS.FAILURE)
.with('cancelled', () => COLORS.CANCEL)
.otherwise(() => COLORS.CANCEL)
}).blocks(
Blocks.Section({
text: dedent`
서비스 : ${inputs.service_name}
배포 환경 : ${inputs.environment}
구분 : ${Md.user(MEMBERS[github.context.actor])}, ${inputs.team}
Run ID : ${createGithubRunLink()}
진행 상태 : ${match(inputs.phase)
.with('start', () => '배포 진행중 :loading:')
.with('finish', () => '배포 완료 :ballot_box_with_check:')
.with('failure', () => '배포 실패 :x:')
.with('cancelled', () => '배포 취소 :black_square_for_stop:')
.otherwise(() => '')}
`
})
)
)
.buildToObject()
return message
}
function createGithubRunLink() {
return Md.link(
`https://github.com/${github.context.repo.owner}/${github.context.repo.repo}/actions/runs/${github.context.runId}`,
String(github.context.runId)
)
}
export function createDirectMessageToActor(
permaLink: string | undefined
): SlackMessageDto {
if (!permaLink) throw new Error('permaLink is missing')
const message = Message({ channel: MEMBERS[github.context.actor] })
.blocks(
Blocks.Section({
text: dedent`배포가 시작되었습니다. 변경 사항을 확인해주세요. ${Md.link(permaLink, '스레드로 가기>>')}`
})
)
.buildToObject()
return message
}
export async function createFormattedJiraIssueLinks(commitMessages: string[]) {
return [
...new Set(
commitMessages
.map(message =>
isJiraTicket(message)
? `${Md.link(createJiraIssueLink(extractJiraIssueKey(message)), message)}`
: ''
)
.filter(Boolean)
)
]
}
async function getAssociatedCommitMessages(
beforeRef: string
): Promise<string[]> {
const octoClient = getOctoClient()
const baseRef = await commitShaOrReleaseTag(beforeRef)
const headRef = github.context.sha
const associatedCommits =
await octoClient.rest.repos.compareCommitsWithBasehead({
owner: github.context.repo.owner,
repo: github.context.repo.repo,
basehead: `${baseRef}...${headRef}`
})
return [
...new Set(
associatedCommits.data.commits.map(commit => commit.commit.message)
)
]
}
async function getPreviousRelease() {
const octoClient = getOctoClient()
const latestTwoReleases = await octoClient.rest.repos.listReleases({
owner: github.context.repo.owner,
repo: github.context.repo.repo,
per_page: 2
})
const previousRelease = latestTwoReleases?.data.at(-1)
if (!previousRelease) {
throw new Error('No previous release found')
}
return previousRelease
}
async function commitShaOrReleaseTag(beforeRef: string): Promise<string> {
return isExistingSha(beforeRef)
? beforeRef
: (await getPreviousRelease()).tag_name
}
function isExistingSha(sha: string) {
return sha !== NONEXSISTANT_SHA
}
export function extractJiraIssueKey(title: string): string {
const match = title.trim().match(/^\[(\w+-\d+)\]/)
return match ? match[1] : ''
}
function isJiraTicket(message: string): boolean {
return !!extractJiraIssueKey(message)
}
function createJiraIssueLink(issueKey: string): string {
return issueKey ? `https://billynco.atlassian.net/browse/${issueKey}` : ''
}