Skip to content

Commit

Permalink
merging PR Chaosthebot#550: Re Reopen Chaosthebot#443 according to Ch…
Browse files Browse the repository at this point in the history
…aosthebot#364 - Twitter Api Working

Chaosthebot#550: Re Reopen Chaosthebot#443 according to Chaosthebot#364 - Twitter Api Working

Description:
Fix encryptation problems, regenerate twitter keys @PlasmaPower 


✅ PR passed with a vote of 6 for and 0 against, a weighted total of 6.0 and a threshold of 6.0, and a current meritocracy review.

Vote record:
@Leigende: 1
@PlasmaPower: 1
@andrewda: 1
@eamanu: 1
@kylerschin: 1
@rudehn: 1
  • Loading branch information
eamanu authored and chaosbot committed Jun 11, 2017
1 parent a5dd723 commit 6b112d0
Show file tree
Hide file tree
Showing 14 changed files with 131 additions and 7 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ server/issue_commands_ran.json
.migrated
ansible/*.retry
db.sqlite
*privkey
17 changes: 16 additions & 1 deletion chaos.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import schedule
import cron
import shutil
import datetime

# this import must happen before any github api stuff gets imported. it sets
# up caching on the api functions so we don't run out of api requests
Expand All @@ -27,6 +28,11 @@
# Currently imported just for the sideeffect (not currently being used)
import encryption # noqa: F401

# To make post in Twitter
import twitter_api as ta
# import twitter_api.misc
# import twitter_api.Twitter


class LessThanFilter(logging.Filter):
"""
Expand Down Expand Up @@ -69,12 +75,21 @@ def main():

api = gh.API(settings.GITHUB_USER, settings.GITHUB_SECRET)

# Api Twitter
api_twitter = ta.API_TWITTER(settings.TWITTER_API_KEYS_FILE)

log.info("checking if I crashed before...")
ta.Twitter.PostTwitter(datetime.datetime.ctime(datetime.datetime.now()) +
" - checking if I crashed before...",
api_twitter.GetApi())

# check if chaosbot is not on the tip of the master branch
check_for_prev_crash(api, log)

log.info("starting up and entering event loop")
ta.Twitter.PostTwitter(datetime.datetime.ctime(datetime.datetime.now()) +
" - starting up and entering event loop",
api_twitter.GetApi())

os.system("pkill uwsgi")

Expand All @@ -86,7 +101,7 @@ def main():
"--daemonize", "/root/workspace/Chaos/log/uwsgi.log"])

# Schedule all cron jobs to be run
cron.schedule_jobs(api)
cron.schedule_jobs(api, api_twitter)

log.info("Setting description to {desc}".format(desc=settings.REPO_DESCRIPTION))
github_api.repos.set_desc(api, settings.URN, settings.REPO_DESCRIPTION)
Expand Down
4 changes: 2 additions & 2 deletions cron/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
from .poll_issue_close_stale import poll_issue_close_stale


def schedule_jobs(api):
def schedule_jobs(api, api_twitter):
schedule.every(settings.PULL_REQUEST_POLLING_INTERVAL_SECONDS).seconds.do(
lambda: poll_pull_requests(api))
lambda: poll_pull_requests(api, api_twitter))
schedule.every(settings.ISSUE_COMMENT_POLLING_INTERVAL_SECONDS).seconds.do(
lambda: poll_read_issue_comments(api))
schedule.every(settings.ISSUE_CLOSE_STALE_INTERVAL_SECONDS).seconds.do(
Expand Down
17 changes: 15 additions & 2 deletions cron/poll_pull_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,19 @@
import sys
from os.path import join, abspath, dirname
from lib.db.models import MeritocracyMentioned
import datetime

import settings
import github_api as gh

from twitter_api import Twitter as tw

THIS_DIR = dirname(abspath(__file__))

__log = logging.getLogger("pull_requests")


def poll_pull_requests(api):
def poll_pull_requests(api, api_twitter):
__log.info("looking for PRs")

# get voting window
Expand Down Expand Up @@ -93,7 +96,6 @@ def poll_pull_requests(api):

if is_approved:
__log.info("PR %d status: will be approved", pr_num)

gh.prs.post_accepted_status(
api, settings.URN, pr, seconds_since_updated, voting_window, votes, vote_total,
threshold, meritocracy_satisfied)
Expand All @@ -104,13 +106,24 @@ def poll_pull_requests(api):
try:
sha = gh.prs.merge_pr(api, settings.URN, pr, votes, vote_total,
threshold, meritocracy_satisfied)
message_twitter = datetime.datetime.ctime(
datetime.datetime.now()) +\
" - PR {pr_num} approved for merging".format(pr_num=pr_num)
tw.PostTwitter(message_twitter, api_twitter)
# some error, like suddenly there's a merge conflict, or some
# new commits were introduced between finding this ready pr and
# merging it
# Make a tweet
except gh.exceptions.CouldntMerge:
__log.info("couldn't merge PR %d for some reason, skipping",
pr_num)
gh.issues.label_issue(api, settings.URN, pr_num, ["can't merge"])
message_twitter = datetime.datetime.ctime(
datetime.datetime.now()) +\
"Couldn't merge PR {pr_num} for some reason, \
skipping".format(pr_num=pr_num)

tw.PostTwitter(message_twitter, api_twitter)
continue

gh.comments.leave_accept_comment(
Expand Down
2 changes: 1 addition & 1 deletion encryption.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,4 @@ def decrypt(ciphertext):
return decrypt


decrypt = create_decryptor("/etc/privkey", "server/pubkey.txt")
decrypt = create_decryptor("privkey", "server/pubkey.txt")
4 changes: 3 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ sh==1.12.13
six==1.10.0
flake8==3.3.0
unidiff==0.5.4
python-twitter==3.3
pymysql
hug==2.3.0
uWSGI==2.0.15
peewee
peewee
15 changes: 15 additions & 0 deletions settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,21 @@

HOMEPAGE = "http://chaosthebot.com"

# To Twitter Posts
_twitter_api_keys = 'api_twitter.keys'

# Look for local Keys first
_twitter_api_keys_file = join(THIS_DIR, _twitter_api_keys)

# fall back to system twitter keys
if not exists(_twitter_api_keys):
_twitter_api_keys_file = join('/etc/', _twitter_api_keys)

if exists(_twitter_api_keys_file):
TWITTER_API_KEYS_FILE = _twitter_api_keys_file
else:
TWITTER_API_KEYS_FILE = None

# TEST SETTING PLEASE IGNORE
TEST = False

Expand Down
14 changes: 14 additions & 0 deletions twitter_api/Twitter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import logging

log = logging.getLogger("twitter")


def PostTwitter(message, api_twitter):
if len(message) > 140:
print('Post has more of 140 chars')
api = api_twitter
try:
api.PostUpdate(message)
except:
log.exception("Failed to post to Twitter")
return 0
17 changes: 17 additions & 0 deletions twitter_api/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from . import misc
import twitter


__all__ = ["misc", "twitter"]


class API_TWITTER():
def __init__(self, path):
self.__twitter_keys = misc.GetKeys(path)
self.__api = twitter.Api(consumer_key=str(self.__twitter_keys['consumer_key']),
consumer_secret=str(self.__twitter_keys['consumer_secret']),
access_token_key=str(self.__twitter_keys['access_token']),
access_token_secret=str(self.__twitter_keys['access_secret']))

def GetApi(self):
return self.__api
42 changes: 42 additions & 0 deletions twitter_api/misc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
from encryption import decrypt


def GetKeys(twitter_keys_path):
consumer_key = ''
consumer_secret = ''
access_token = ''
access_secret = ''
PATH = 'twitter_keys/'
l_files = ['consumer_key', 'consumer_secret', 'access_token', 'access_secret']

for k in l_files:
f = open(PATH + k, 'rb')
key = f.read()
if (k == 'consumer_key'):
consumer_key = decrypt(key)
if (k == 'consumer_secret'):
consumer_secret = decrypt(key)
if (k == 'access_token'):
access_token = decrypt(key)
if (k == 'access_secret'):
access_secret = decrypt(key)
f.close()
"""
for k in keys:
try:
values = k.split('\n')[0].split('=')[1].strip()
if(k.split('\n')[0].split('=')[0].strip() == 'consumer_key'):
consumer_key = decrypt(values)
elif(k.split('\n')[0].split('=')[0].strip() == 'consumer_secret'):
consumer_secret = decrypt(values)
elif(k.split('\n')[0].split('=')[0].strip() == 'access_token'):
access_token = decrypt(values)
elif(k.split('\n')[0].split('=')[0].strip() == 'access_secret'):
access_secret = decrypt(values)
except IndexError:
# Maybe there are a '\n' between keys
continue
"""
return {'consumer_key': consumer_key, 'consumer_secret': consumer_secret,
'access_token': access_token, 'access_secret': access_secret}
1 change: 1 addition & 0 deletions twitter_keys/access_secret
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
�WÈ]p�\�E�}���3��N�|f${��j�&�c����4%�fCI�e}���C�[�/u��c��f�6�Hy�ު�6�M��ա\�8�����@ܸH�|��ą��1�/�C4cn�N���ORQ���m|h[���f�(�bY�D�7}��G(z-L�r��)�����Þ�"�F��J�]JN��v�i���nR�x�W}?:i��ׅv˸��<�uL��X@�����u�3KO�NVL��ڳ���f
Expand Down
Binary file added twitter_keys/access_token
Binary file not shown.
2 changes: 2 additions & 0 deletions twitter_keys/consumer_key
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
9��Jl?p@LՄ �% �&��u�������:k{󕳩���VX������\�@<Gv�'�0!*&��6�<��3����rm�bO�$8�t��@���"�P�%� �6@���!(�􈕷�)�,���ߔ��N5��sN�p[�P���#a��䚣��J:!}M�_�d9��a�� �� �Խ:��I��[���Ie�VBE��_�}�k����c`����۵�M�Y
�����/���2����g��
Expand Down
2 changes: 2 additions & 0 deletions twitter_keys/consumer_secret
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
2%�j��^kC��h��Gk��m��0�C�Η�x?�y������������G)�M� h)����t/�Ro�.�c�EW<��,Đn)�ۛ�{�K���:1�'3a3y��<�u��g������a'R"��V�i/�v�P�� Rvme�� ���EK&Z���>������q���3���A0����0����a��@�Y�L�?�P�"Y#�|=@�l�%c@_��f'��'Pp�
4�6}��k�l
Expand Down

0 comments on commit 6b112d0

Please sign in to comment.