-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
# ReadyToReview - Slack Notifier | ||
|
||
**ReadyToReview** notifies you when a pull request is ready for review, i.e. when | ||
- a non-draft PR is opened or | ||
- a draft PR is ready to review. | ||
|
||
### Setup | ||
|
||
1. Generate your Slack webhook. You can do it [here](https://slack.com/apps/A0F7XDUAZ-incoming-webhooks). | ||
1. Add created webhook as a secret named `SLACK_WEBHOOK` using GitHub Action's Secret. See your project Settings -> Secrets. | ||
|
||
### Example usage | ||
|
||
```yaml | ||
on: | ||
pull_request: | ||
types: [opened, ready_for_review] | ||
name: Notify about PR ready for review | ||
jobs: | ||
slackNotification: | ||
name: Slack Notification | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v2 | ||
- name: Slack Notification | ||
uses: kv109/[email protected] | ||
env: | ||
SLACK_CHANNEL: your-slack-channel # required | ||
SLACK_WEBHOOK: ${{ secrets.SLACK_WEBHOOK }} # required | ||
``` | ||
```yaml | ||
on: | ||
pull_request: | ||
types: [opened, ready_for_review] | ||
name: Notify about PR ready for review | ||
jobs: | ||
slackNotification: | ||
name: Slack Notification | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v2 | ||
- name: Slack Notification | ||
uses: kv109/[email protected] | ||
env: | ||
FORMAT: | # Format is fully customizable. | ||
My custom message format! :smile: | ||
Title: *{ pull_request.title }* | ||
Author: { pull_request.user.login } | ||
URL: { pull_request.html_url } | ||
IGNORE_DRAFTS: false # Notify also about drafts. Default: true. | ||
SLACK_CHANNEL: your-slack-channel | ||
SLACK_WEBHOOK: ${{ secrets.SLACK_WEBHOOK }} | ||
USERNAME: Your username | ||
``` | ||
## Formatting | ||
Any string inside brackets is replaced with a value taken from an actual event payload. | ||
All available values can be found [here](https://developer.github.com/v3/activity/events/types/#webhook-payload-example-28). |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
name: 'Ready to review - Slack notifier' | ||
description: 'Notifies you when a pull request is ready for review' | ||
runs: | ||
using: 'node12' | ||
main: 'index.js' |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
const Core = require('@actions/core'); | ||
const Github = require('@actions/github'); | ||
const Slack = require('node-slack'); | ||
|
||
const DefaultFormat = `:rocket: New PR ready to review! :rocket:\nTitle: *{ pull_request.title }*\nAuthor: { pull_request.user.login }\nURL: { pull_request.html_url }`; | ||
|
||
try { | ||
e = process.env; | ||
config = { | ||
channel: e.SLACK_CHANNEL, | ||
format: e.FORMAT || DefaultFormat, | ||
hookUrl: e.SLACK_WEBHOOK, | ||
ignoreDrafts: e.IGNORE_DRAFTS || true, | ||
username: e.USERNAME || 'ReadyToReviewBot' | ||
}; | ||
|
||
if (!config.channel) { | ||
Core.setFailed("Slack channel is not set. Set it with\nenv:\n\tSLACK_CHANNEL: your-channel"); | ||
} | ||
if (!config.hookUrl) { | ||
Core.setFailed("SLACK_WEBHOOK is not set. Set it with\nenv:\n\tSLACK_WEBHOOK: ${{ secrets.SLACK_WEBHOOK }}\n"); | ||
} | ||
|
||
const payload = Github.context.payload; | ||
|
||
if (payload.action !== "ready_for_review" && payload.action !== "opened") { | ||
return | ||
} | ||
|
||
const pr = payload.pull_request; | ||
const slack = new Slack(config.hookUrl); | ||
// console.log(JSON.stringify(payload, null, 2)); | ||
|
||
if (payload.pull_request.draft && config.ignoreDrafts === true) { | ||
return | ||
} | ||
|
||
let message = config.format; | ||
config.format.match(/\{.+\}/g).forEach(template => { | ||
const templateWithoutBrackets = template | ||
.replace(/^\{\s?/, "") | ||
.replace(/\s?\}$/, ""); | ||
|
||
const keys = templateWithoutBrackets.split(".") | ||
console.log(keys) | ||
let value = payload; | ||
keys.forEach(key => { | ||
try { | ||
value = value[key]; | ||
} catch (error) { | ||
console.log(error); | ||
} | ||
}); | ||
console.log(value); | ||
message = message.replace(template, value); | ||
}); | ||
|
||
slack.send({ | ||
text: message, | ||
channel: '#' + config.channel, | ||
username: config.username | ||
}); | ||
} catch (error) { | ||
Core.setFailed(error.message); | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.