Skip to content
This repository was archived by the owner on Jul 12, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
__pycache__
.cache
*~
47 changes: 32 additions & 15 deletions changebot/github/github_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def get_file_contents(self, path_to_file):
contents_base64 = response.json()['content']
return base64.b64decode(contents_base64).decode()

def get_issues(self, state, labels):
def get_issues(self, state, labels, exclude_pr=True):
"""
Get a list of issues.

Expand All @@ -111,12 +111,25 @@ def get_issues(self, state, labels):
labels : str
List of comma-separated labels; e.g., ``Closed?``.

exclude_pr : bool
Exclude pull requests from result.

Returns
-------
issue_list : list
A list of matching issue numbers.

"""
url = f'{HOST}/repos/{self.repo}/issues'
kwargs = {'state': state, 'labels': labels}
r = requests.get(url, kwargs)
result = r.json()
return [d['number'] for d in result]
if exclude_pr:
issue_list = [d['number'] for d in result
if 'pull_request' not in d]
else:
issue_list = [d['number'] for d in result]
return issue_list


class IssueHandler(object):
Expand Down Expand Up @@ -196,19 +209,7 @@ def submit_comment(self, body, comment_id=None):
"""

data = {}
data['body'] = body

# Troll mode on special day for new pull request
tt = time.gmtime() # UTC because we're astronomers!
if tt.tm_mon == 4 and tt.tm_mday == 1:
import random

try:
q = random.choice(QUOTES)
except Exception as e:
q = str(e) # Need a way to find out what went wrong

data['body'] += f'\n*{q}*\n'
data['body'] = _insert_special_message(body)

if comment_id is None:
url = self._url_issue_comment
Expand Down Expand Up @@ -346,3 +347,19 @@ def last_commit_date(self):
if last_time == 0:
raise Exception(f'No commit found in {url}')
return last_time


def _insert_special_message(body):
"""Troll mode on special day for new pull request."""
tt = time.gmtime() # UTC because we're astronomers!
if tt.tm_mon != 4 or tt.tm_mday != 1:
return body

import random

try:
q = random.choice(QUOTES)
except Exception as e:
q = str(e) # Need a way to find out what went wrong

return body + f'\n*{q}*\n'
39 changes: 39 additions & 0 deletions changebot/github/tests/test_github_api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from unittest.mock import patch, Mock

from changebot.github import github_api
from changebot.github.github_api import RepoHandler


# TODO: Add more tests to increase coverage.

class TestRepoHandler:
def setup_class(self):
self.repo = RepoHandler('fakerepo/doesnotexist', branch='awesomebot')

@patch('requests.get')
def test_get_issues(self, mock_get):
# http://engineroom.trackmaven.com/blog/real-life-mocking/
mock_response = Mock()
mock_response.json.return_value = [
{'number': 42, 'state': 'open'},
{'number': 55, 'state': 'open',
'pull_request': {'diff_url': 'blah'}}]
mock_get.return_value = mock_response

assert self.repo.get_issues('open', 'Close?') == [42]
assert self.repo.get_issues('open', 'Close?',
exclude_pr=False) == [42, 55]


@patch('time.gmtime')
def test_special_msg(mock_time):
import random
random.seed(1234)
body = 'Hello World\n'

mock_time.return_value = Mock(tm_mon=4, tm_mday=2)
assert github_api._insert_special_message(body) == body

mock_time.return_value = Mock(tm_mon=4, tm_mday=1)
body2 = github_api._insert_special_message(body)
assert '\n*Greetings from Skynet!*\n' in body2