Skip to content

Commit

Permalink
Add slackbot Action
Browse files Browse the repository at this point in the history
  • Loading branch information
gordlin committed Nov 12, 2024
1 parent 416ec60 commit 1d31343
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .github/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
transformers
protobuf
sentencepiece
torch
41 changes: 41 additions & 0 deletions .github/slack-bot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import os
import json
from transformers import pipeline

# Get the PR body and labels from environment variables
pr_body = os.environ.get('PR_BODY', '')
pr_labels_json = os.environ.get('PR_LABELS_JSON', '[]')
pr_labels = json.loads(pr_labels_json)

# Summarization (example model, could be adjusted)
summarizer = pipeline('summarization', model='tuner007/pegasus_summarizer')
neutral_summary = summarizer(pr_body, max_length=200, min_length=25, do_sample=False)[0]['summary_text']

# PR label types with associated metadata
pr_label_types = {
'PR: Active': ['Active', 'https://github.com/search?q=org%3Aramp4-pcar4+state%3A%22open%22+type%3A%22pr%22+label%3A%22PR%3A+Active%22&type=pullrequests'],
'PR: Build': ['Build', 'https://github.com/search?q=org%3Aramp4-pcar4+state%3A%22open%22+type%3A%22pr%22+label%3A%22PR%3A+Active%22%2C%22PR%3A+Build%22+&type=pullrequests'],
'PR: Frontend': ['Frontend', 'https://github.com/search?q=org%3Aramp4-pcar4+state%3A%22open%22+type%3A%22pr%22+label%3A%22PR%3A+Active%22%2C%22PR%3A+Frontend%22+&type=pullrequests'],
'PR: Geo': ['Geo', 'https://github.com/search?q=org%3Aramp4-pcar4+state%3A%22open%22+type%3A%22pr%22+label%3A%22PR%3A+Active%22%2C%22PR%3A+Geo%22+&type=pullrequests']
}

# Format PR label links as Slack mrkdwn links
pr_label_str = ", ".join(
f"<{pr_label_types[label['name']][1]}|{pr_label_types[label['name']][0]}>"
for label in pr_labels if pr_label_types.get(label['name'])
)
if (len(pr_label_str) == 0):
pr_label_str = "None"

# Handle regular labels (not in pr_label_types)
reg_label_str = ", ".join(
label['name'] for label in pr_labels if pr_label_types.get(label['name']) is None
)
if (len(reg_label_str) == 0):
reg_label_str = "None"

# Write all results to environment
with open(os.environ['GITHUB_ENV'], 'a') as env_file:
env_file.write(f'NEUTRAL_SUMMARY={neutral_summary}\n')
env_file.write(f'PR_LABEL_STR={pr_label_str}\n')
env_file.write(f'REG_LABEL_STR={reg_label_str}\n')
77 changes: 77 additions & 0 deletions .github/workflows/slack-bot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
name: slack-PR-message
on:
pull_request_target:
types:
- opened

permissions:
contents: read
pull-requests: read

jobs:
post-to-slack:
runs-on: ubuntu-20.04
steps:
- name: Check user permissions (allow only those w/ write access)
id: permission_check
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
user_permission=$(curl -s -H "Authorization: Bearer $GITHUB_TOKEN" \
"https://api.github.com/repos/${{ github.repository }}/collaborators/${{ github.event.pull_request.user.login }}/permission" | jq -r .permission)
if [[ "$user_permission" != "write" && "$user_permission" != "admin" ]]; then
echo "User does not have write access. Exiting."
exit 1
fi
- name: Checkout
uses: actions/checkout@v4

- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: '3.x'

- name: Install Python dependency
run: pip install -r .github/requirements.txt

- name: Summarize PR body and process labels
id: summarize
env:
PR_LABELS_JSON: ${{ toJSON(github.event.pull_request.labels) }}
PR_BODY: ${{ github.event.pull_request.body }}
run: python .github/slack-bot.py

- name: Post to a Slack channel
id: slack
uses: slackapi/[email protected]
with:
channel-id: 'C08080W3W0Y'
payload: |
{
"blocks": [
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "New *storylines-editor* PR by *${{ github.event.pull_request.user.login }}*"
}
},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "*Title*: ${{ github.event.pull_request.title }}\n*PR Type*: ${{ env.PR_LABEL_STR }}\n*Other labels*: ${{ env.REG_LABEL_STR }}\n*Link*: ${{ github.event.pull_request.html_url }}"
}
},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "*PR Summary*:\n${{ env.NEUTRAL_SUMMARY }}"
}
}
]
}
env:
SLACK_BOT_TOKEN: ${{ secrets.SLACK_BOT_TOKEN }}

0 comments on commit 1d31343

Please sign in to comment.