Skip to content

Commit a7a864b

Browse files
committed
feat(Slack): optionally receive api documentation url to display on slack messages
1 parent ac099c0 commit a7a864b

File tree

8 files changed

+77
-45
lines changed

8 files changed

+77
-45
lines changed

src/locale/en-us.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,7 @@ export const LOCALE_EN_US: Locale = {
1616
'status.unchanged': 'UNCHANGED',
1717

1818
'required.required': 'Required',
19-
'required.optional': 'Optional'
19+
'required.optional': 'Optional',
20+
21+
'button.goto-api-documentation': 'Go to API documentation'
2022
}

src/locale/ko-kr.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,7 @@ export const LOCALE_KO_KR: Locale = {
1616
'status.unchanged': '변경되지 않음',
1717

1818
'required.required': '필수',
19-
'required.optional': '선택'
19+
'required.optional': '선택',
20+
21+
'button.goto-api-documentation': 'API 문서 바로가기'
2022
}

src/main.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ export async function run(): Promise<void> {
4848
config.slackConfig.token,
4949
config.slackConfig.channelId,
5050
config.slackConfig.memberIdListToMention,
51-
config.githubConfig
51+
config.githubConfig,
52+
config.apiDocumentationUrl
5253
)
5354
// eslint-disable-next-line github/array-foreach
5455
diffFromExternalLibrary.forEach(diff => {

src/services/slack.ts

+35-19
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { DiffOutputItem } from 'openapi-diff-node'
22
import {
33
RichTextBlock,
4+
RichTextElement,
45
RichTextList,
56
RichTextQuote,
67
RichTextSection,
@@ -61,7 +62,8 @@ export class Slack {
6162
private token: string,
6263
private channelId: string,
6364
private memberIdListToMention: string[],
64-
private githubConfig: Config['githubConfig']
65+
private githubConfig: Config['githubConfig'],
66+
private apiDocumentationUrl?: string
6567
) {
6668
this.client = new WebClient(token)
6769
}
@@ -86,6 +88,37 @@ export class Slack {
8688
)} `
8789
const color = STATUS_TO_LOCALE_KEY[diff.status].color
8890

91+
const additionalInfoList: RichTextElement[] = [
92+
{
93+
type: 'text',
94+
text: this.githubConfig.headCommitInfo.sha.slice(0, 7),
95+
style: {
96+
code: true
97+
}
98+
},
99+
{
100+
type: 'text',
101+
text: ` - `
102+
},
103+
{
104+
type: 'link',
105+
url: `https://github.com/${this.githubConfig.repository}/commit/${this.githubConfig.headCommitInfo.sha}`,
106+
text: this.githubConfig.headCommitInfo.message
107+
}
108+
]
109+
110+
if (this.apiDocumentationUrl) {
111+
additionalInfoList.push({
112+
type: 'text',
113+
text: ` | `
114+
})
115+
additionalInfoList.push({
116+
type: 'link',
117+
url: this.apiDocumentationUrl,
118+
text: `${translate('button.goto-api-documentation')}`
119+
})
120+
}
121+
89122
const res = await this.client.chat.postMessage({
90123
channel: this.channelId,
91124
text: mainText,
@@ -145,24 +178,7 @@ export class Slack {
145178
elements: [
146179
{
147180
type: 'rich_text_section',
148-
elements: [
149-
{
150-
type: 'text',
151-
text: this.githubConfig.headCommitInfo.sha.slice(0, 7),
152-
style: {
153-
code: true
154-
}
155-
},
156-
{
157-
type: 'text',
158-
text: ` - `
159-
},
160-
{
161-
type: 'link',
162-
url: `https://github.com/${this.githubConfig.repository}/commit/${this.githubConfig.headCommitInfo.sha}`,
163-
text: this.githubConfig.headCommitInfo.message
164-
}
165-
]
181+
elements: additionalInfoList
166182
}
167183
]
168184
}

src/types/config.ts

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ export interface Config {
44
initialDelayInMilliseconds: number
55
slackConfig: SlackDisabledOptions | SlackEnabledOptions
66
githubConfig: GithubOptions
7+
apiDocumentationUrl?: string
78
}
89

910
interface SlackDisabledOptions {

src/types/github-input.ts

+5
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,9 @@ export interface GithubInput {
3838
* The last commit message which triggered the action
3939
*/
4040
headCommitMessage?: string
41+
42+
/**
43+
* URL of the api documentation - Optional
44+
*/
45+
apiDocumentationUrl?: string
4146
}

src/types/locale.ts

+2
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,6 @@ export interface Locale {
1515

1616
'required.required': string
1717
'required.optional': string
18+
19+
'button.goto-api-documentation': string
1820
}

src/utils/validate-input.ts

+26-23
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ export function validateInputAndSetConfig(): Config {
5353
? process.env.MEMBER_ID_LIST_TO_MENTION
5454
: core.getInput('member_id_list_to_mention')
5555

56+
const apiDocumentationUrl = isLocal
57+
? process.env.API_DOCUMENTATION_URL
58+
: core.getInput('api_documentation_url')
59+
5660
if (memberIdListToMention !== undefined && memberIdListToMention !== '') {
5761
// check if memberIdListToMention is a string of comma separated strings
5862
const memberIdListToMentionArray = memberIdListToMention.split(',')
@@ -121,39 +125,38 @@ export function validateInputAndSetConfig(): Config {
121125
headFile
122126
}
123127

128+
let slackConfig: Config['slackConfig']
129+
124130
if (isSlackEnabled === 'true') {
125131
if (
126132
slackAccessToken !== '' &&
127133
slackAccessToken !== undefined &&
128134
slackChannelId !== '' &&
129135
slackChannelId !== undefined
130136
) {
131-
return {
132-
env,
133-
locale,
134-
initialDelayInMilliseconds: 500,
135-
slackConfig: {
136-
enabled: true,
137-
token: slackAccessToken,
138-
channelId: slackChannelId,
139-
memberIdListToMention: memberIdListToMention?.split(',') ?? []
140-
},
141-
githubConfig
137+
slackConfig = {
138+
enabled: true,
139+
token: slackAccessToken,
140+
channelId: slackChannelId,
141+
memberIdListToMention: memberIdListToMention?.split(',') ?? []
142142
}
143+
} else {
144+
throw new Error(
145+
'slack_enabled is set to true, but slack_access_token and slack_channel_id are not set'
146+
)
143147
}
144-
145-
throw new Error(
146-
'slack_enabled is set to true, but slack_access_token and slack_channel_id are not set'
147-
)
148148
} else {
149-
return {
150-
env,
151-
locale,
152-
initialDelayInMilliseconds: 500,
153-
slackConfig: {
154-
enabled: false
155-
},
156-
githubConfig
149+
slackConfig = {
150+
enabled: false
157151
}
158152
}
153+
154+
return {
155+
env,
156+
locale,
157+
initialDelayInMilliseconds: 500,
158+
slackConfig,
159+
githubConfig,
160+
apiDocumentationUrl
161+
}
159162
}

0 commit comments

Comments
 (0)