-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: migrate the changelog command (#21)
* feat(changelog): add a changelog command to display the commits of the last 3 tags. Also include a link to the ticket if a ticket_id is detected in the commit message. --------- Co-authored-by: Grégoire Paris <[email protected]>
- Loading branch information
Showing
15 changed files
with
587 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,181 @@ | ||
import type { Block, KnownBlock, View } from '@slack/types'; | ||
import { generateChangelog } from '@/changelog/utils/generateChangelog'; | ||
import { slackifyChangelog } from '@/changelog/utils/slackifyChangelog'; | ||
import { getProjectsByChannelId } from '@/core/services/data'; | ||
import { fetchProjectById, fetchProjectTags } from '@/core/services/gitlab'; | ||
import type { SlackOption } from '@/core/typings/SlackOption'; | ||
|
||
interface ChangelogModalData { | ||
channelId?: string; | ||
projectId?: number; | ||
projectOptions?: SlackOption[]; | ||
releaseTagName?: string; | ||
} | ||
|
||
export async function buildChangelogModalView({ | ||
channelId, | ||
projectId, | ||
projectOptions, | ||
releaseTagName, | ||
}: ChangelogModalData): Promise<View> { | ||
if (channelId !== undefined && projectOptions === undefined) { | ||
const dataProjects = await getProjectsByChannelId(channelId); | ||
const projects = await Promise.all( | ||
dataProjects.map(async (dataProject) => | ||
fetchProjectById(Number(dataProject.projectId)) | ||
) | ||
); | ||
|
||
projectOptions = projects | ||
.sort((a, b) => | ||
a.path_with_namespace.localeCompare(b.path_with_namespace) | ||
) | ||
.map((project) => ({ | ||
text: { | ||
type: 'plain_text', | ||
text: project.path_with_namespace, | ||
}, | ||
value: project.id.toString(), | ||
})) as SlackOption[]; | ||
} | ||
|
||
if (!projectOptions || projectOptions.length === 0) { | ||
throw new Error( | ||
'No Gitlab project has been found on this channel :homer-stressed:' | ||
); | ||
} | ||
|
||
if (projectId === undefined) { | ||
projectId = parseInt(projectOptions[0].value, 10); | ||
} | ||
|
||
const tags = (await fetchProjectTags(projectId)).slice(0, 3); | ||
const previousReleaseTagName = releaseTagName ?? tags[0]?.name; | ||
const changelog = previousReleaseTagName | ||
? await generateChangelog(projectId, previousReleaseTagName) | ||
: ''; | ||
|
||
const previousReleaseOptions = tags.map(({ name }) => ({ | ||
text: { | ||
type: 'plain_text', | ||
text: name, | ||
}, | ||
value: name, | ||
})) as SlackOption[]; | ||
|
||
return { | ||
type: 'modal', | ||
callback_id: 'changelog-modal', | ||
title: { | ||
type: 'plain_text', | ||
text: 'Changelog', | ||
}, | ||
submit: { | ||
type: 'plain_text', | ||
text: 'Ok', | ||
}, | ||
notify_on_close: false, | ||
blocks: [ | ||
{ | ||
type: 'input', | ||
block_id: 'changelog-project-block', | ||
dispatch_action: true, | ||
element: { | ||
type: 'static_select', | ||
action_id: 'changelog-select-project-action', | ||
options: projectOptions, | ||
initial_option: projectOptions?.[0], | ||
placeholder: { | ||
type: 'plain_text', | ||
text: 'Select the project', | ||
}, | ||
}, | ||
label: { | ||
type: 'plain_text', | ||
text: 'Project', | ||
}, | ||
}, | ||
previousReleaseOptions.length > 0 | ||
? [ | ||
{ | ||
type: 'input', | ||
block_id: 'changelog-release-tag-block', | ||
dispatch_action: true, | ||
element: { | ||
type: 'static_select', | ||
action_id: 'changelog-select-release-tag-action', | ||
initial_option: previousReleaseOptions[0], | ||
options: previousReleaseOptions, | ||
placeholder: { | ||
type: 'plain_text', | ||
text: 'Select the previous release tag', | ||
}, | ||
}, | ||
label: { | ||
type: 'plain_text', | ||
text: 'Previous release tag', | ||
}, | ||
}, | ||
{ | ||
type: 'context', | ||
block_id: 'changelog-release-tag-info-block', | ||
elements: [ | ||
{ | ||
type: 'plain_text', | ||
text: 'This should be changed only if the previous release has been aborted.', | ||
}, | ||
], | ||
}, | ||
] | ||
: [ | ||
{ | ||
type: 'section', | ||
text: { | ||
type: 'mrkdwn', | ||
text: '*Previous release tag*', | ||
}, | ||
}, | ||
{ | ||
type: 'section', | ||
text: { | ||
type: 'mrkdwn', | ||
text: 'No previous release tag has been found.', | ||
}, | ||
}, | ||
], | ||
{ | ||
type: 'section', | ||
block_id: 'changelog-preview-title-block', | ||
text: { | ||
type: 'mrkdwn', | ||
text: '*Preview*', | ||
}, | ||
}, | ||
{ | ||
type: 'section', | ||
block_id: 'changelog-preview-block', | ||
text: { | ||
type: 'mrkdwn', | ||
text: changelog | ||
? slackifyChangelog(changelog) | ||
: 'No change has been found.', | ||
}, | ||
}, | ||
changelog && { | ||
type: 'input', | ||
block_id: 'changelog-markdown-block', | ||
label: { | ||
type: 'plain_text', | ||
text: 'Markdown', | ||
}, | ||
element: { | ||
type: 'plain_text_input', | ||
multiline: true, | ||
initial_value: changelog, | ||
}, | ||
}, | ||
] | ||
.flat() | ||
.filter(Boolean) as (KnownBlock | Block)[], | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { logger } from '@/core/services/logger'; | ||
import type { BlockActionsPayload } from '@/core/typings/BlockActionPayload'; | ||
import { updateChangelog } from './utils/updateChangelog'; | ||
import { updateChangelogProject } from './utils/updateChangelogProject'; | ||
|
||
export async function changelogBlockActionsHandler( | ||
payload: BlockActionsPayload | ||
): Promise<void> { | ||
await Promise.all( | ||
payload.actions.map(async (action) => { | ||
const { action_id } = action; | ||
|
||
switch (action_id) { | ||
case 'changelog-select-project-action': | ||
return updateChangelogProject(payload); | ||
|
||
case 'changelog-select-release-tag-action': | ||
return updateChangelog(payload); | ||
|
||
default: | ||
logger.error( | ||
new Error(`Unknown changelog block action: ${action_id}`) | ||
); | ||
} | ||
}) | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import type { Response } from 'express'; | ||
import { GENERIC_ERROR_MESSAGE, HTTP_STATUS_NO_CONTENT } from '@/constants'; | ||
import { slackBotWebClient } from '@/core/services/slack'; | ||
import type { SlackExpressRequest } from '@/core/typings/SlackSlashCommand'; | ||
import { buildChangelogModalView } from './buildChangelogModalView'; | ||
|
||
export async function changelogRequestHandler( | ||
req: SlackExpressRequest, | ||
res: Response | ||
) { | ||
const { channel_id, trigger_id, user_id } = req.body; | ||
|
||
res.sendStatus(HTTP_STATUS_NO_CONTENT); | ||
|
||
try { | ||
await slackBotWebClient.views.open({ | ||
trigger_id, | ||
view: await buildChangelogModalView({ channelId: channel_id }), | ||
}); | ||
} catch (error) { | ||
await slackBotWebClient.chat.postEphemeral({ | ||
channel: channel_id, | ||
user: user_id, | ||
text: error instanceof Error ? error.message : GENERIC_ERROR_MESSAGE, | ||
}); | ||
} | ||
} |
Oops, something went wrong.