Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save cburgmer/657fd5d4e36532cce1a2c3f0547ae603 to your computer and use it in GitHub Desktop.
Save cburgmer/657fd5d4e36532cce1a2c3f0547ae603 to your computer and use it in GitHub Desktop.
Slack Bot reminder for Bitbucket pull requests
import os
import sys
import requests
import json
POST_URL = 'https://slack.com/api/chat.postMessage'
REPOSITORY_PR = 'https://{{ your corporate server }}/rest/api/1.0/projects/{{ your project }}/repos/{{ your repo }}/pull-requests?state=OPEN'
SLACK_CHANNEL = '#{{ your channel }}'
SLACK_API_TOKEN = ''
BITBUCKET_UID = ''
BITBUCKET_PWD = ''
INITIAL_MESSAGE = """\
<!here|here> I'm not angry, I'm happiness challenged.
Review these PRs:
"""
def format_pull_requests(json_obj):
lines = []
for i in json_obj['values']:
line = '- *{0}* <{1}|{2}> - by {3}'.format(i['fromRef']['displayId'],i['links']['self'][0]['href'],i['title'],i['author']['user']['displayName'])
lines.append(line)
return lines
def fetch_pulls():
lines = []
"""
Returns a formatted string list of open pull request messages.
"""
response = requests.get(REPOSITORY_PR, auth=(BITBUCKET_UID, BITBUCKET_PWD))
if(response.ok):
json_obj = json.loads(response.content)
lines += format_pull_requests(json_obj)
return lines
def send_to_slack(text):
payload = {
'token': SLACK_API_TOKEN,
'channel': SLACK_CHANNEL,
'as_user': True,
'text': text
}
response = requests.post(POST_URL, data=payload)
answer = response.json()
if not answer['ok']:
raise Exception(answer['error'])
def cli():
lines = fetch_pulls()
if lines:
text = INITIAL_MESSAGE + '\n'.join(lines)
send_to_slack(text)
#print(text)
if __name__ == '__main__':
cli()
@cburgmer
Copy link
Author

A corporate enterprise version (supports API 1.0 of Bitbucket server), that does not wait for 2 days until notifying.

Always prefer trunk based development!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment