2017-02-08 16:05:24 -05:00
|
|
|
# See readme.md for instructions on running this code.
|
2016-12-23 22:44:50 -05:00
|
|
|
from __future__ import absolute_import
|
2016-12-15 19:37:04 -05:00
|
|
|
from __future__ import print_function
|
2016-12-23 22:44:50 -05:00
|
|
|
from . import github
|
2016-12-15 19:37:04 -05:00
|
|
|
import json
|
|
|
|
import logging
|
2016-12-23 22:44:50 -05:00
|
|
|
import os
|
|
|
|
import requests
|
2016-12-15 19:37:04 -05:00
|
|
|
|
|
|
|
|
|
|
|
class InputError(IndexError):
|
|
|
|
'''raise this when there is an error with the information the user has entered'''
|
|
|
|
|
|
|
|
|
|
|
|
class GitHubHandler(object):
|
|
|
|
'''
|
|
|
|
This plugin allows you to comment on a GitHub issue, under a certain repository.
|
2017-02-16 16:56:32 -05:00
|
|
|
It looks for messages starting with '@mention-bot'.
|
2016-12-15 19:37:04 -05:00
|
|
|
'''
|
|
|
|
|
|
|
|
def usage(self):
|
|
|
|
return '''
|
|
|
|
This bot will allow users to comment on a GitHub issue.
|
2017-02-16 16:56:32 -05:00
|
|
|
Users should preface messages with '@mention-bot'.
|
2016-12-15 19:37:04 -05:00
|
|
|
You will need to have a GitHub account.
|
|
|
|
|
|
|
|
Before running this, make sure to get a GitHub OAuth token.
|
|
|
|
The token will need to be authorized for the following scopes:
|
|
|
|
'gist, public_repo, user'.
|
2016-12-23 22:44:50 -05:00
|
|
|
Store it in the '~/.github_auth.conf' file, along with your username, in the format:
|
|
|
|
github_repo = <repo_name> (The name of the repo to post to)
|
|
|
|
github_repo_owner = <repo_owner> (The owner of the repo to post to)
|
|
|
|
github_username = <username> (The username of the GitHub bot)
|
|
|
|
github_token = <oauth_token> (The personal access token for the GitHub bot)
|
|
|
|
|
|
|
|
Leave the first two options blank.
|
|
|
|
|
|
|
|
Please use this format in your message to the bot:
|
|
|
|
'<repository_owner>/<repository>/<issue_number>/<your_comment>'.
|
2016-12-15 19:37:04 -05:00
|
|
|
'''
|
|
|
|
|
2017-06-11 09:03:39 -04:00
|
|
|
def handle_message(self, message, bot_handler, state_handler):
|
2016-12-15 19:37:04 -05:00
|
|
|
original_content = message['content']
|
|
|
|
original_sender = message['sender_email']
|
|
|
|
|
2017-06-11 09:03:39 -04:00
|
|
|
handle_input(bot_handler, original_content, original_sender)
|
2016-12-15 19:37:04 -05:00
|
|
|
|
|
|
|
handler_class = GitHubHandler
|
|
|
|
|
|
|
|
|
2016-12-23 22:44:50 -05:00
|
|
|
def send_to_github(repo_owner, repo, issue, comment_body):
|
|
|
|
session = github.auth()
|
2016-12-15 19:37:04 -05:00
|
|
|
comment = {
|
|
|
|
'body': comment_body
|
|
|
|
}
|
|
|
|
r = session.post('https://api.github.com/repos/%s/%s/issues/%s/comments' % (repo_owner, repo, issue),
|
|
|
|
json.dumps(comment))
|
|
|
|
|
|
|
|
return r.status_code
|
|
|
|
|
|
|
|
|
|
|
|
def get_values_message(original_content):
|
|
|
|
# gets rid of whitespace around the edges, so that they aren't a problem in the future
|
|
|
|
message_content = original_content.strip()
|
|
|
|
# splits message by '/' which will work if the information was entered correctly
|
|
|
|
message_content = message_content.split('/')
|
|
|
|
try:
|
|
|
|
# this will work if the information was entered correctly
|
2016-12-23 22:44:50 -05:00
|
|
|
user = github.get_username()
|
2016-12-15 19:37:04 -05:00
|
|
|
repo_owner = message_content[2]
|
|
|
|
repo = message_content[3]
|
|
|
|
issue = message_content[4]
|
|
|
|
comment_body = message_content[5]
|
|
|
|
|
|
|
|
return dict(user=user, repo_owner=repo_owner, repo=repo, issue=issue, comment_body=comment_body)
|
|
|
|
except IndexError:
|
|
|
|
raise InputError
|
|
|
|
|
|
|
|
|
2017-06-11 09:03:39 -04:00
|
|
|
def handle_input(bot_handler, original_content, original_sender):
|
2016-12-15 19:37:04 -05:00
|
|
|
try:
|
|
|
|
params = get_values_message(original_content)
|
|
|
|
|
2016-12-23 22:44:50 -05:00
|
|
|
status_code = send_to_github(params['repo_owner'], params['repo'],
|
2016-12-15 19:37:04 -05:00
|
|
|
params['issue'], params['comment_body'])
|
|
|
|
|
|
|
|
if status_code == 201:
|
|
|
|
# sending info to github was successful!
|
|
|
|
reply_message = "You commented on issue number " + params['issue'] + " under " + \
|
|
|
|
params['repo_owner'] + "'s repository " + params['repo'] + "!"
|
|
|
|
|
2017-06-11 09:03:39 -04:00
|
|
|
send_message(bot_handler, reply_message, original_sender)
|
2016-12-15 19:37:04 -05:00
|
|
|
|
|
|
|
elif status_code == 404:
|
|
|
|
# this error could be from an error with the OAuth token
|
|
|
|
reply_message = "Error code: " + str(status_code) + " :( There was a problem commenting on issue number " \
|
|
|
|
+ params['issue'] + " under " + \
|
|
|
|
params['repo_owner'] + "'s repository " + params['repo'] + \
|
|
|
|
". Do you have the right OAuth token?"
|
|
|
|
|
2017-06-11 09:03:39 -04:00
|
|
|
send_message(bot_handler, reply_message, original_sender)
|
2016-12-15 19:37:04 -05:00
|
|
|
|
|
|
|
else:
|
|
|
|
# sending info to github did not work
|
|
|
|
reply_message = "Error code: " + str(status_code) +\
|
|
|
|
" :( There was a problem commenting on issue number " \
|
|
|
|
+ params['issue'] + " under " + \
|
|
|
|
params['repo_owner'] + "'s repository " + params['repo'] + \
|
|
|
|
". Did you enter the information in the correct format?"
|
|
|
|
|
2017-06-11 09:03:39 -04:00
|
|
|
send_message(bot_handler, reply_message, original_sender)
|
2016-12-15 19:37:04 -05:00
|
|
|
except InputError:
|
|
|
|
message = "It doesn't look like the information was entered in the correct format." \
|
|
|
|
" Did you input it like this? " \
|
|
|
|
"'/<username>/<repository_owner>/<repository>/<issue_number>/<your_comment>'."
|
2017-06-11 09:03:39 -04:00
|
|
|
send_message(bot_handler, message, original_sender)
|
2016-12-15 19:37:04 -05:00
|
|
|
logging.error('there was an error with the information you entered')
|
|
|
|
|
|
|
|
|
2017-06-11 09:03:39 -04:00
|
|
|
def send_message(bot_handler, message, original_sender):
|
2016-12-15 19:37:04 -05:00
|
|
|
# function for sending a message
|
2017-06-11 09:03:39 -04:00
|
|
|
bot_handler.send_message(dict(
|
2016-12-15 19:37:04 -05:00
|
|
|
type='private',
|
|
|
|
to=original_sender,
|
|
|
|
content=message,
|
|
|
|
))
|