Skip to content

Commit

Permalink
Add sort commit list option (#116)
Browse files Browse the repository at this point in the history
  • Loading branch information
duckdum authored Jul 29, 2024
1 parent 15fc48c commit 32e3360
Show file tree
Hide file tree
Showing 15 changed files with 308 additions and 192 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/release-notification.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,5 @@ jobs:
uses: ./
with:
slack-bot-token: ${{ secrets.SLACK_BOT_TOKEN }}
slack-channel: ${{ secrets.SLACK_CHANNEL }}
slack-channel-id: ${{ secrets.SLACK_CHANNEL }}
github-token: ${{ secrets.GITHUB_TOKEN }}
72 changes: 36 additions & 36 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,49 +5,50 @@ This GitHub Action sends notifications to a Slack channel whenever a pull reques
## Features

1. **Initial Commit List Notification**: When a pull request is created (opened), the action sends a message to a designated Slack channel with the PR details:
- **Message**:
```
New release pull request created: "PR title"
Branch: source_branch -> target_branch
```
- Additionally, a thread is created with a list of commits related to the PR:
```
Commits in this pull request:
- commit1 by @SlackUser1
- commit2 by @commit.author
```
- If `github-to-slack-map` is provided, the action will use the Slack user instead of GitHub usernames to tag users in Slack messages.

- **Message**:
```
New release pull request created: "PR title"
Branch: source_branch -> target_branch
```
- Additionally, a thread is created with a list of commits related to the PR:
```
Commits in this pull request:
- commit1 by @SlackUser1
- commit2 by @commit.author
```
- If `github-to-slack-map` is provided, the action will use the Slack user instead of GitHub usernames to tag users in Slack messages.
2. **Slack Message Timestamp in PR Description**: The action updates the PR description to include the Slack message timestamp (`Slack message_ts`). This allows the timestamp to be retrieved later for sending follow-up messages in the same Slack thread when new commits are pushed to the branch.
3. **New Commits Notification**: When a pull request is updated (synchronized), the action retrieves the Slack message timestamp from the PR body (description) and sends a message with the last commit added to the PR using the GitHub username of the commit author:
- **Message**:
```
New commit added: last commit by @githubUser
```
- **Message**:
```
New commit added: last commit by @githubUser
```
4. **PR Merged Notification**: When a pull request is merged (closed), the action fetches the Slack message timestamp and sends a notification to the same Slack thread, informing the team that the PR has been merged using the GitHub username of the user who merged the PR:
- **Message**:
```
Pull request "PR title" was merged by @githubUser
```
- **Message**:
```
Pull request "PR title" was merged by @githubUser
```
## Inputs
| Input | Required | Description | Default Value |
|------------------------------|----------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------|
| slack-bot-token | required | The Slack bot token. | N/A |
| slack-channel | required | The Slack channel ID where the notifications will be sent. | N/A |
| github-token | required | The GitHub token (typically `${{ secrets.GITHUB_TOKEN }}`). | N/A |
| github-to-slack-map | optional | A JSON string mapping GitHub usernames to Slack user IDs for tagging in messages. If not provided, GitHub usernames will be used in the Slack messages. Example: `{"githubUsername1": "slackUserID1", "githubUsername2": "slackUserID2"}`. | N/A |
| initial-message-template | optional | Template for the initial message when a PR is created. | `New release pull request created: \<${prUrl}\|\${prTitle}>\n*From*: ${branchName} → *To*: ${targetBranch}` |
| commit-list-message-template | optional | Template for the message with the list of commits when a PR is created. | `Commits in this pull request:\n${commitListMessage}\n\n\<${changelogUrl}\|Full Changelog: ${branchName} to ${targetBranch}>` |
| update-message-template | optional | Template for the message when a PR is updated with new commits. | `New commit added: \<${commitUrl}\|\${commitMessage}> by @${githubUser}` |
| close-message-template | optional | Template for the message when a PR is merged. | `Pull request \<${prUrl}\|\${prTitle}> was merged by @${mergedBy}` |
| Input | Required | Description | Default Value |
| ---------------------------- | -------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | ----------------------------------------------------------------------------------------------------------------------------- |
| slack-bot-token | required | The Slack bot token. | N/A |
| slack-channel-id | required | The Slack channel ID where the notifications will be sent. | N/A |
| github-token | required | The GitHub token (typically `${{ secrets.GITHUB_TOKEN }}`). | N/A |
| github-to-slack-map | optional | A JSON string mapping GitHub usernames to Slack user IDs for tagging in messages. If not provided, GitHub usernames will be used in the Slack messages. Example: `{"githubUsername1": "slackUserID1", "githubUsername2": "slackUserID2"}`. | N/A |
| initial-message-template | optional | Template for the initial message when a PR is created. | `New release pull request created: \<${prUrl}\|\${prTitle}>\n*From*: ${branchName} → *To*: ${targetBranch}` |
| commit-list-message-template | optional | Template for the message with the list of commits when a PR is created. | `Commits in this pull request:\n${commitListMessage}\n\n\<${changelogUrl}\|Full Changelog: ${branchName} to ${targetBranch}>` |
| update-message-template | optional | Template for the message when a PR is updated with new commits. | `New commit added: \<${commitUrl}\|\${commitMessage}> by @${githubUser}` |
| close-message-template | optional | Template for the message when a PR is merged. | `Pull request \<${prUrl}\|\${prTitle}> was merged by @${mergedBy}` |
To use this action, create a workflow file in your repository (e.g., `.github/workflows/release-notifications.yml`) with the following content:
```yml
name: Release Notification on PR Opened or Updated
Expand All @@ -67,7 +68,7 @@ jobs:
with:
slack-token: ${{ secrets.SLACK_BOT_TOKEN }}
github-token: ${{ secrets.GITHUB_TOKEN }}
slack-channel: 'AB123DEF456'
slack-channel-id: 'AB123DEF456'
github-to-slack-map: |
{
"githubUsername1": "slackUserID1",
Expand All @@ -91,8 +92,8 @@ jobs:

1. In your app settings, go to "OAuth & Permissions".
2. Scroll down to "Bot Token Scopes" and add the following scopes:
- `chat:write`
- `chat:write.public`
- `chat:write`
- `chat:write.public`
3. Click "Save Changes".

#### Install App to Workspace:
Expand All @@ -111,7 +112,6 @@ jobs:

Ensure that the GitHub token (`GITHUB_TOKEN`) has read and write permissions. You can configure this in your repository settings under `Settings` > `Actions` > `General` > `Workflow permissions`.


## License

This project is licensed under the MIT License - see the LICENSE file for details.
6 changes: 5 additions & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ inputs:
slack-bot-token:
description: 'Slack bot token'
required: true
slack-channel:
slack-channel-id:
description: 'Slack channel ID'
required: true
github-token:
Expand All @@ -29,6 +29,10 @@ inputs:
description: 'Template for the message when a PR is merged'
required: false
default: 'Pull request <${prUrl}|${prTitle}> was merged by @${mergedBy}'
sort-commits:
description: 'Sort commits by types and scopes'
required: false
default: 'false'
runs:
using: 'node20'
main: 'dist/index.js'
Expand Down
4 changes: 2 additions & 2 deletions dist/handlePRClosed.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* Handles the event when a pull request is closed.
* @param slackToken - Slack bot token.
* @param slackChannel - Slack channel ID.
* @param slackChannelId - Slack channel ID.
* @param closeMessageTemplate - Template for the close Slack message.
*/
export declare function handlePRClosed(slackToken: string, slackChannel: string, closeMessageTemplate: string): Promise<void>;
export declare function handlePRClosed(slackToken: string, slackChannelId: string, closeMessageTemplate: string): Promise<void>;
5 changes: 3 additions & 2 deletions dist/handlePROpened.d.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
/**
* Handles the event when a pull request is opened.
* @param slackToken - Slack bot token.
* @param slackChannel - Slack channel ID.
* @param slackChannelId - Slack channel ID.
* @param githubToken - GitHub token.
* @param initialMessageTemplate - Template for the initial Slack message.
* @param commitListMessageTemplate - Template for the commit list Slack message.
* @param githubToSlackMap - Optional mapping of GitHub usernames to Slack user IDs.
* @param sortCommits - Flag to sort commits by types and scopes.
*/
export declare function handlePROpened(slackToken: string, slackChannel: string, githubToken: string, initialMessageTemplate: string, commitListMessageTemplate: string, githubToSlackMap?: Record<string, string>): Promise<void>;
export declare function handlePROpened(slackToken: string, slackChannelId: string, githubToken: string, initialMessageTemplate: string, commitListMessageTemplate: string, githubToSlackMap?: Record<string, string>, sortCommits?: boolean): Promise<void>;
4 changes: 2 additions & 2 deletions dist/handlePRUpdated.d.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/**
* Handles the event when a pull request is updated with new commits.
* @param slackToken - Slack bot token.
* @param slackChannel - Slack channel ID.
* @param slackChannelId - Slack channel ID.
* @param githubToken - GitHub token.
* @param updateMessageTemplate - Template for the update Slack message.
*/
export declare function handlePRUpdated(slackToken: string, slackChannel: string, githubToken: string, updateMessageTemplate: string): Promise<void>;
export declare function handlePRUpdated(slackToken: string, slackChannelId: string, githubToken: string, updateMessageTemplate: string): Promise<void>;
Loading

0 comments on commit 32e3360

Please sign in to comment.