Skip to content

Commit

Permalink
[FX-4795] Add feature to notify a fallback group (#270)
Browse files Browse the repository at this point in the history
  • Loading branch information
dmaklygin authored Jan 30, 2024
1 parent be19d78 commit 80aa00b
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 68 deletions.
5 changes: 5 additions & 0 deletions .changeset/cold-ducks-argue.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'davinci-github-actions': minor
---

- add feature to notify a fallback group in slack
1 change: 1 addition & 0 deletions notify-about-build-failure/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ The list of arguments, that are used in GH Action:
| `slack-channel-name` | string || | Slack channel name (for example, `#-test-channel`) |
| `github-commit-author-name` | string || | GitHub name of commit author, needed for finding the Slack handle of commit author |
| `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 |
| `fallback-slack-team-id` | string | | | team ID to use when commit author is not found |

### Outputs

Expand Down
3 changes: 3 additions & 0 deletions notify-about-build-failure/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ inputs:
github-action-run-url:
description: Failing GitHub Acton run URL (for example, `https://github.com/toptal/staff-portal/actions/runs/123`), needed for the notification message
required: true
fallback-slack-team-id:
description: team ID to use when commit author is not found
required: false

runs:
using: node20
Expand Down
63 changes: 29 additions & 34 deletions notify-about-build-failure/dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2823,6 +2823,7 @@ const https = __nccwpck_require__(687)
const slackBotToken = core.getInput('slack-bot-token')
const topTeamApiKey = core.getInput('top-team-api-key')
const slackChannelName = core.getInput('slack-channel-name')
const fallbackSlackTeamId = core.getInput('fallback-slack-team-id')
const githubCommitAuthorName = core.getInput('github-commit-author-name')
const githubActionRunUrl = core.getInput('github-action-run-url')

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

const sendSlackMessage = body => {
Expand Down Expand Up @@ -2898,7 +2899,6 @@ const communicationChannelsBody = JSON.stringify({
const getCommunicationChannelsRequest = https.request(
TOP_TEAM_API_OPTIONS,
res => {
let isNotified = false
let result = ''

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

const teams = parsedResult.data.orgUnits.nodes

teams.some(team => {
team.directRoles.some(({ member }) => {
const github = member.communicationChannels.find(
const teamMember = teams
.flatMap(team => team.directRoles.map(role => role.member))
.find(member => {
return member.communicationChannels.some(
channel =>
channel.kind === 'GITHUB' &&
channel.value === githubCommitAuthorName
)
})

if (github) {
const slack = member.communicationChannels.find(
channel => channel.kind === 'SLACK'
)

const message = getSlackMessage(slack.value, githubActionRunUrl)

const privateMessageChannelId = new URLSearchParams(slack.link).get(
'id'
)
sendSlackMessage({
text: message,
channel: privateMessageChannelId,
})
if (teamMember) {
const slack = teamMember.communicationChannels.find(
channel => channel.kind === 'SLACK'
)

sendSlackMessage({
text: message,
channel: slackChannelName,
})
const message = getSlackMessage(`@${slack.value}`, githubActionRunUrl)
const privateMessageChannelId = new URLSearchParams(slack.link).get(
'id'
)

isNotified = true
}

return isNotified
sendSlackMessage({
text: message,
channel: privateMessageChannelId,
})

return isNotified
})

if (!isNotified) {
const errorMessage = `Unable to notify commit author ${githubCommitAuthorName}`
throw new Error(errorMessage)
sendSlackMessage({
text: message,
channel: slackChannelName,
})
} else if (fallbackSlackTeamId) {
sendSlackMessage({
text: getSlackMessage(`!subteam^${fallbackSlackTeamId}`, githubActionRunUrl),
channel: slackChannelName,
})
} else {
throw new Error('No slack identifier found')
}
})
}
Expand Down
66 changes: 32 additions & 34 deletions notify-about-build-failure/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const https = require('https')
const slackBotToken = core.getInput('slack-bot-token')
const topTeamApiKey = core.getInput('top-team-api-key')
const slackChannelName = core.getInput('slack-channel-name')
const fallbackSlackTeamId = core.getInput('fallback-slack-team-id')
const githubCommitAuthorName = core.getInput('github-commit-author-name')
const githubActionRunUrl = core.getInput('github-action-run-url')

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

const sendSlackMessage = body => {
Expand Down Expand Up @@ -79,7 +80,6 @@ const communicationChannelsBody = JSON.stringify({
const getCommunicationChannelsRequest = https.request(
TOP_TEAM_API_OPTIONS,
res => {
let isNotified = false
let result = ''

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

const teams = parsedResult.data.orgUnits.nodes

teams.some(team => {
team.directRoles.some(({ member }) => {
const github = member.communicationChannels.find(
const teamMember = teams
.flatMap(team => team.directRoles.map(role => role.member))
.find(member => {
return member.communicationChannels.some(
channel =>
channel.kind === 'GITHUB' &&
channel.value === githubCommitAuthorName
)
})

if (github) {
const slack = member.communicationChannels.find(
channel => channel.kind === 'SLACK'
)

const message = getSlackMessage(slack.value, githubActionRunUrl)

const privateMessageChannelId = new URLSearchParams(slack.link).get(
'id'
)
sendSlackMessage({
text: message,
channel: privateMessageChannelId,
})
if (teamMember) {
const slack = teamMember.communicationChannels.find(
channel => channel.kind === 'SLACK'
)

sendSlackMessage({
text: message,
channel: slackChannelName,
})
const message = getSlackMessage(`@${slack.value}`, githubActionRunUrl)
const privateMessageChannelId = new URLSearchParams(slack.link).get(
'id'
)

isNotified = true
}

return isNotified
sendSlackMessage({
text: message,
channel: privateMessageChannelId,
})

return isNotified
})

if (!isNotified) {
const errorMessage = `Unable to notify commit author ${githubCommitAuthorName}`
throw new Error(errorMessage)
sendSlackMessage({
text: message,
channel: slackChannelName,
})
} else if (fallbackSlackTeamId) {
sendSlackMessage({
text: getSlackMessage(
`!subteam^${fallbackSlackTeamId}`,
githubActionRunUrl
),
channel: slackChannelName,
})
} else {
throw new Error('No slack identifier found')
}
})
}
Expand Down

0 comments on commit 80aa00b

Please sign in to comment.