Skip to content

Commit

Permalink
refactor: slack handle notification
Browse files Browse the repository at this point in the history
  • Loading branch information
thedaviddias committed Aug 3, 2023
1 parent 5c643a1 commit b328405
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 53 deletions.
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

52 changes: 8 additions & 44 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as core from '@actions/core'
import axios from 'axios'

import { getOwnerAndRepo } from './utils/getOwnerAndRepo'
import { getOctokit } from './utils/getOctokit'
import { getInputs } from './utils/getInputs'
Expand All @@ -10,12 +10,8 @@ import { getBaseUrlFromContext } from './utils/getBaseUrlFromContext'
import { generateRepoLink } from './utils/generateRepoLink'
import { generateReleaseLink } from './utils/generateReleaseLink'
import { getCommitOfTag } from './utils/getCommitOfTag'
import { generatePRListString } from './utils/generatePRListString'
import { generateSlackMessage } from './utils/generateSlackMessage'
import { parseTicketNumberFromTitle } from './utils/parseTicketNumberFromTitle'
import { generateJiraTicketLink } from './utils/generateJiraTicketLink'
import { compareSemVer } from './utils/compareSemVer'
import { getCurrentDate } from './utils/getCurrentDate'
import { handleSlackNotification } from './utils/handleSlackNotification'

// eslint-disable-next-line @typescript-eslint/require-await
export async function run(): Promise<void> {
Expand Down Expand Up @@ -122,48 +118,16 @@ export async function run(): Promise<void> {
return
}

core.info('Slack Webhook URL is provided, generating Slack message.')

const contributorsCommitsWithTicketLinks = contributorsCommits.map((commit) => {
const ticketNumber = options.jiraTicketPrefix
? parseTicketNumberFromTitle(commit.prTitle, options.jiraTicketPrefix)
: null
const ticketLink =
ticketNumber && options.jiraInstanceUrl
? generateJiraTicketLink(ticketNumber, options.jiraInstanceUrl)
: null
return {
...commit,
jiraTicketLink: ticketLink,
jiraTicketPrefix: options.jiraTicketPrefix,
}
})

const prListString = generatePRListString(
contributorsCommitsWithTicketLinks,
options.jiraTicketPrefix,
options.jiraInstanceUrl,
options.contributorReplaceChar,
options.contributorReplaceRegex
)

const currentDate = getCurrentDate(options.timeZoneOffset)

const slackData = generateSlackMessage(
return handleSlackNotification({
contributorsCommits,
currentTagCommit,
options,
slackWebhookUrls,
repoLink,
releaseLink,
releaseVersion,
currentTagCommit,
prListString,
options,
repo,
currentDate
)

for (const url of slackWebhookUrls) {
await axios.post(url.trim(), slackData)
core.info(`Message to Slack`)
}
})
} catch (error) {
// If an error occurs during the script execution, fail the GitHub Action and output the error message
if (error instanceof Error) {
Expand Down
16 changes: 8 additions & 8 deletions src/utils/getContributorCommits.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import * as github from '@actions/github'
import * as core from '@actions/core'

export type ContributorCommits = {
contributor: string
prTitle: string
prUrl: string
prNumber: number
}

/**
* Gets the commits from contributors.
*
Expand All @@ -15,14 +22,7 @@ export async function getContributorCommits(
owner: string,
repo: string,
commits: string[]
): Promise<
Array<{
contributor: string
prTitle: string
prUrl: string
prNumber: number
}>
> {
): Promise<ContributorCommits[]> {
const contributorsCommits = []

for (const commit of commits) {
Expand Down
101 changes: 101 additions & 0 deletions src/utils/handleSlackNotification.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import * as core from '@actions/core'
import axios from 'axios'

import { generateJiraTicketLink } from './generateJiraTicketLink'
import { generatePRListString } from './generatePRListString'
import { generateSlackMessage } from './generateSlackMessage'
import { getCurrentDate } from './getCurrentDate'
import { GetInputsType } from './getInputs'
import { parseTicketNumberFromTitle } from './parseTicketNumberFromTitle'
import { ContributorCommits } from './getContributorCommits'
import { RestEndpointMethodTypes } from '@octokit/action'

type SlackNotification = {
contributorsCommits: ContributorCommits[]
currentTagCommit: RestEndpointMethodTypes['git']['getCommit']['response']
options: GetInputsType
slackWebhookUrls: string[]
repoLink: string
releaseLink: string
releaseVersion: string
repo: string
}

/**
* Handles the Slack notification process. It generates and sends a Slack message containing information about a GitHub release, including a list of pull requests that contributed to the release.
*
* @param contributorsCommits - The list of commits made by contributors to the GitHub repository.
* @param currentTagCommit - The commit of the current tag of the release.
* @param options - Configuration options for the GitHub action.
* @param slackWebhookUrls - The list of URLs to send Slack notifications to.
* @param repoLink - The URL to the GitHub repository.
* @param releaseLink - The URL to the GitHub release.
* @param releaseVersion - The version number of the GitHub release.
* @param repo - The name of the GitHub repository.
*
* @returns {Promise<void>} - A promise that resolves when all Slack messages have been sent.
*
* @async
*/
export async function handleSlackNotification({
contributorsCommits,
currentTagCommit,
options,
slackWebhookUrls,
repoLink,
releaseLink,
releaseVersion,
repo,
}: SlackNotification) {
core.info('Slack Webhook URL is provided, generating Slack message.')

// Map through each commit contributed by contributors
const contributorsCommitsWithTicketLinks = contributorsCommits.map((commit) => {
// If a Jira ticket prefix is provided, parse the ticket number from the pull request title
const ticketNumber = options.jiraTicketPrefix
? parseTicketNumberFromTitle(commit.prTitle, options.jiraTicketPrefix)
: null
// If a ticket number was found and a Jira instance URL is provided, generate a link to the Jira ticket
const ticketLink =
ticketNumber && options.jiraInstanceUrl
? generateJiraTicketLink(ticketNumber, options.jiraInstanceUrl)
: null
// Return the commit along with the potentially found Jira ticket link and prefix
return {
...commit,
jiraTicketLink: ticketLink,
jiraTicketPrefix: options.jiraTicketPrefix,
}
})

// Generate a string listing all pull requests, potentially with Jira ticket links and contributor replacements
const prListString = generatePRListString(
contributorsCommitsWithTicketLinks,
options.jiraTicketPrefix,
options.jiraInstanceUrl,
options.contributorReplaceChar,
options.contributorReplaceRegex
)

// Get the current date in the specified time zone
const currentDate = getCurrentDate(options.timeZoneOffset)

// Generate the Slack message data, which includes information about the repository, the release, the commit of the current tag,
// the list of pull requests, and the current date
const slackData = generateSlackMessage(
repoLink,
releaseLink,
releaseVersion,
currentTagCommit,
prListString,
options,
repo,
currentDate
)

// Post the Slack message data to each provided Slack webhook URL
for (const url of slackWebhookUrls) {
await axios.post(url.trim(), slackData)
core.info(`Message sent to Slack`)
}
}

0 comments on commit b328405

Please sign in to comment.