Skip to content

Instantly share code, notes, and snippets.

@FuhuXia
Last active March 2, 2023 16:03
Show Gist options
  • Save FuhuXia/8d624ec3447035509495d7e54c3e622c to your computer and use it in GitHub Desktop.
Save FuhuXia/8d624ec3447035509495d7e54c3e622c to your computer and use it in GitHub Desktop.
create github issue from slack message
name: Slack to GitHub Issues
on:
workflow_dispatch:
schedule:
- cron: '*/5 * * * *'
jobs:
slack-to-github-issues:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: 3.9
- name: Install dependencies
run: |
pip install slack-sdk PyGithub
- name: Read Slack messages and create GitHub issues
env:
SLACK_BOT_TOKEN: ${{ secrets.SLACK_BOT_TOKEN }}
# this is datagov-alerts channel
SLACK_CHANNEL_ID: C4RGAM1Q8
SLACK_WORKSPACE: gsa-tts.slack.com
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GITHUB_OWNER: GSA
GITHUB_REPO: datagov-alerts
#TODO: remove this requirement
GITHUB_USER: FuhuXia
run: |
python slack-to-github.py
import os
import sys
import re
from slack_sdk import WebClient
from slack_sdk.errors import SlackApiError
from github import Github
BOT_TOKEN = os.environ["SLACK_BOT_TOKEN"]
CHANNEL_ID = os.environ["SLACK_CHANNEL_ID"]
WORKSPACE = os.environ["SLACK_WORKSPACE"]
GITHUB_TOKEN = os.environ["GITHUB_TOKEN"]
USER = os.environ["GITHUB_USER"]
OWNER = os.environ['GITHUB_OWNER']
REPO = os.environ['GITHUB_REPO']
client = WebClient(token=BOT_TOKEN)
# Grab latest slack 100 messages
try:
result = client.conversations_history(channel=CHANNEL_ID, limit=100)
messages = result["messages"]
print("{} messages found".format(len(messages)))
except SlackApiError as e:
sys.exit("Error: {e}")
if not messages:
print("No messages found.")
sys.exit()
g = Github(GITHUB_TOKEN)
repo = g.get_repo(f"{OWNER}/{REPO}")
# get the last issue
# this returns default 30 max 100 issues per page.
issues = repo.get_issues(state="all", sort="created", direction="desc", creator=USER)
last_message_id = issues[0].body[-19:-2] #p1677731234567890
pattern = r"p\d{16}"
match = re.match(pattern, last_message_id)
if not match:
sys.exit("message id not found in the last issue.")
for message in reversed(messages):
# print(f"type: {message.get('type')}")
# print(f"subtype: {message.get('subtype')}")
# print(f"client_msg_id: {message.get('client_msg_id')}")
# print(f"user: {message.get('user')}")
# print(f"ts: {message.get('ts')}")
# print(f"text: {message.get('text')}")
# print(f"files: {message.get('files')}")
# print(message)
# Skip existing issues
message_id = "p{}".format(message['ts'].replace('.', ''))
if message_id <= last_message_id:
continue
# create new issues
issue_data = {}
issue_data['title'] = message['text']
issue_data['body'] = f"Created from Slack message.\n\n## Slack Source\n\nSlack Message ID: [{message_id}](https://{WORKSPACE}/archives/{CHANNEL_ID}/{message_id})."
# issue_data['labels'] = ["critical"]
issue = repo.create_issue(**issue_data)
print(f"Created issue #{issue.number}: {issue.title}")
1. set an initial timestamp, any message prior to it will be ignored, so that we dont have tons of issue created on the launch.
2. filter out certain messages, such as notification when a NewRelic issue is close.
3. auto close the issue if an issue-close NewRelic message or OK email are received.
4. remove the need fpr env variable GITHUB_USER.
5. apply lables to certain top priority messages such as prod server down notifications.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment