Skip to content

Commit 80aa00b

Browse files
authored
[FX-4795] Add feature to notify a fallback group (#270)
1 parent be19d78 commit 80aa00b

File tree

5 files changed

+70
-68
lines changed

5 files changed

+70
-68
lines changed

.changeset/cold-ducks-argue.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'davinci-github-actions': minor
3+
---
4+
5+
- add feature to notify a fallback group in slack

notify-about-build-failure/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ The list of arguments, that are used in GH Action:
1717
| `slack-channel-name` | string || | Slack channel name (for example, `#-test-channel`) |
1818
| `github-commit-author-name` | string || | GitHub name of commit author, needed for finding the Slack handle of commit author |
1919
| `github-action-run-url` | string || | Failing GitHub Acton run URL (for example, `https://github.com/toptal/staff-portal/actions/runs/123`), needed for the notification message |
20+
| `fallback-slack-team-id` | string | | | team ID to use when commit author is not found |
2021

2122
### Outputs
2223

notify-about-build-failure/action.yml

+3
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ inputs:
1616
github-action-run-url:
1717
description: Failing GitHub Acton run URL (for example, `https://github.com/toptal/staff-portal/actions/runs/123`), needed for the notification message
1818
required: true
19+
fallback-slack-team-id:
20+
description: team ID to use when commit author is not found
21+
required: false
1922

2023
runs:
2124
using: node20

notify-about-build-failure/dist/index.js

+29-34
Original file line numberDiff line numberDiff line change
@@ -2823,6 +2823,7 @@ const https = __nccwpck_require__(687)
28232823
const slackBotToken = core.getInput('slack-bot-token')
28242824
const topTeamApiKey = core.getInput('top-team-api-key')
28252825
const slackChannelName = core.getInput('slack-channel-name')
2826+
const fallbackSlackTeamId = core.getInput('fallback-slack-team-id')
28262827
const githubCommitAuthorName = core.getInput('github-commit-author-name')
28272828
const githubActionRunUrl = core.getInput('github-action-run-url')
28282829

@@ -2851,7 +2852,7 @@ const SLACK_API_OPTIONS = {
28512852
const getSlackMessage = (
28522853
slackHandle,
28532854
githubActionRunUrl
2854-
) => `Hello <@${slackHandle}>!
2855+
) => `Hello <${slackHandle}>!
28552856
Build ${githubActionRunUrl} is currently failing on master, please fix it to unblock the release candidate or let the proper team know.`
28562857

28572858
const sendSlackMessage = body => {
@@ -2898,7 +2899,6 @@ const communicationChannelsBody = JSON.stringify({
28982899
const getCommunicationChannelsRequest = https.request(
28992900
TOP_TEAM_API_OPTIONS,
29002901
res => {
2901-
let isNotified = false
29022902
let result = ''
29032903

29042904
res.setEncoding('utf8')
@@ -2917,47 +2917,42 @@ const getCommunicationChannelsRequest = https.request(
29172917
}
29182918

29192919
const teams = parsedResult.data.orgUnits.nodes
2920-
2921-
teams.some(team => {
2922-
team.directRoles.some(({ member }) => {
2923-
const github = member.communicationChannels.find(
2920+
const teamMember = teams
2921+
.flatMap(team => team.directRoles.map(role => role.member))
2922+
.find(member => {
2923+
return member.communicationChannels.some(
29242924
channel =>
29252925
channel.kind === 'GITHUB' &&
29262926
channel.value === githubCommitAuthorName
29272927
)
2928+
})
29282929

2929-
if (github) {
2930-
const slack = member.communicationChannels.find(
2931-
channel => channel.kind === 'SLACK'
2932-
)
2933-
2934-
const message = getSlackMessage(slack.value, githubActionRunUrl)
2935-
2936-
const privateMessageChannelId = new URLSearchParams(slack.link).get(
2937-
'id'
2938-
)
2939-
sendSlackMessage({
2940-
text: message,
2941-
channel: privateMessageChannelId,
2942-
})
2930+
if (teamMember) {
2931+
const slack = teamMember.communicationChannels.find(
2932+
channel => channel.kind === 'SLACK'
2933+
)
29432934

2944-
sendSlackMessage({
2945-
text: message,
2946-
channel: slackChannelName,
2947-
})
2935+
const message = getSlackMessage(`@${slack.value}`, githubActionRunUrl)
2936+
const privateMessageChannelId = new URLSearchParams(slack.link).get(
2937+
'id'
2938+
)
29482939

2949-
isNotified = true
2950-
}
2951-
2952-
return isNotified
2940+
sendSlackMessage({
2941+
text: message,
2942+
channel: privateMessageChannelId,
29532943
})
29542944

2955-
return isNotified
2956-
})
2957-
2958-
if (!isNotified) {
2959-
const errorMessage = `Unable to notify commit author ${githubCommitAuthorName}`
2960-
throw new Error(errorMessage)
2945+
sendSlackMessage({
2946+
text: message,
2947+
channel: slackChannelName,
2948+
})
2949+
} else if (fallbackSlackTeamId) {
2950+
sendSlackMessage({
2951+
text: getSlackMessage(`!subteam^${fallbackSlackTeamId}`, githubActionRunUrl),
2952+
channel: slackChannelName,
2953+
})
2954+
} else {
2955+
throw new Error('No slack identifier found')
29612956
}
29622957
})
29632958
}

notify-about-build-failure/index.js

+32-34
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const https = require('https')
44
const slackBotToken = core.getInput('slack-bot-token')
55
const topTeamApiKey = core.getInput('top-team-api-key')
66
const slackChannelName = core.getInput('slack-channel-name')
7+
const fallbackSlackTeamId = core.getInput('fallback-slack-team-id')
78
const githubCommitAuthorName = core.getInput('github-commit-author-name')
89
const githubActionRunUrl = core.getInput('github-action-run-url')
910

@@ -32,7 +33,7 @@ const SLACK_API_OPTIONS = {
3233
const getSlackMessage = (
3334
slackHandle,
3435
githubActionRunUrl
35-
) => `Hello <@${slackHandle}>!
36+
) => `Hello <${slackHandle}>!
3637
Build ${githubActionRunUrl} is currently failing on master, please fix it to unblock the release candidate or let the proper team know.`
3738

3839
const sendSlackMessage = body => {
@@ -79,7 +80,6 @@ const communicationChannelsBody = JSON.stringify({
7980
const getCommunicationChannelsRequest = https.request(
8081
TOP_TEAM_API_OPTIONS,
8182
res => {
82-
let isNotified = false
8383
let result = ''
8484

8585
res.setEncoding('utf8')
@@ -98,47 +98,45 @@ const getCommunicationChannelsRequest = https.request(
9898
}
9999

100100
const teams = parsedResult.data.orgUnits.nodes
101-
102-
teams.some(team => {
103-
team.directRoles.some(({ member }) => {
104-
const github = member.communicationChannels.find(
101+
const teamMember = teams
102+
.flatMap(team => team.directRoles.map(role => role.member))
103+
.find(member => {
104+
return member.communicationChannels.some(
105105
channel =>
106106
channel.kind === 'GITHUB' &&
107107
channel.value === githubCommitAuthorName
108108
)
109+
})
109110

110-
if (github) {
111-
const slack = member.communicationChannels.find(
112-
channel => channel.kind === 'SLACK'
113-
)
114-
115-
const message = getSlackMessage(slack.value, githubActionRunUrl)
116-
117-
const privateMessageChannelId = new URLSearchParams(slack.link).get(
118-
'id'
119-
)
120-
sendSlackMessage({
121-
text: message,
122-
channel: privateMessageChannelId,
123-
})
111+
if (teamMember) {
112+
const slack = teamMember.communicationChannels.find(
113+
channel => channel.kind === 'SLACK'
114+
)
124115

125-
sendSlackMessage({
126-
text: message,
127-
channel: slackChannelName,
128-
})
116+
const message = getSlackMessage(`@${slack.value}`, githubActionRunUrl)
117+
const privateMessageChannelId = new URLSearchParams(slack.link).get(
118+
'id'
119+
)
129120

130-
isNotified = true
131-
}
132-
133-
return isNotified
121+
sendSlackMessage({
122+
text: message,
123+
channel: privateMessageChannelId,
134124
})
135125

136-
return isNotified
137-
})
138-
139-
if (!isNotified) {
140-
const errorMessage = `Unable to notify commit author ${githubCommitAuthorName}`
141-
throw new Error(errorMessage)
126+
sendSlackMessage({
127+
text: message,
128+
channel: slackChannelName,
129+
})
130+
} else if (fallbackSlackTeamId) {
131+
sendSlackMessage({
132+
text: getSlackMessage(
133+
`!subteam^${fallbackSlackTeamId}`,
134+
githubActionRunUrl
135+
),
136+
channel: slackChannelName,
137+
})
138+
} else {
139+
throw new Error('No slack identifier found')
142140
}
143141
})
144142
}

0 commit comments

Comments
 (0)