Skip to content

Commit 121b8e0

Browse files
author
Aspen Barnes
committed
Fix github plugin
1 parent 7a55dc9 commit 121b8e0

File tree

7 files changed

+189
-41
lines changed

7 files changed

+189
-41
lines changed

.mypy.ini

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
[mypy]
2-
plugins = sqlmypy
32
namespace_packages = True
43
python_version = 3.8
54
warn_unused_configs = True

.pre-commit-config.yaml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ ci:
44
- mypy
55
repos:
66
- repo: https://github.com/pre-commit/pre-commit-hooks
7-
rev: 6e2418c5521b7d606e72914dced3253f9ace1205 # frozen: v3.4.0
7+
rev: 38b88246ccc552bffaaf54259d064beeee434539 # frozen: v4.0.1
88
hooks:
99
- id: trailing-whitespace
1010
args: ['--markdown-linebreak-ext=md,markdown']
@@ -33,15 +33,15 @@ repos:
3333
args:
3434
- --remove
3535
- repo: https://github.com/psf/black
36-
rev: 67d5532c3392280de0ce717a1ab728eca2beb698 # frozen: 21.4b0
36+
rev: e3000ace2fd1fcb1c181bb7a8285f1f976bcbdc7 # frozen: 21.7b0
3737
hooks:
3838
- id: black
3939
- repo: https://github.com/pycqa/isort
40-
rev: a6222a8a125ec719724e628a5d3d0d5c60923281 # frozen: 5.8.0
40+
rev: fd5ba70665a37ec301a1f714ed09336048b3be63 # frozen: 5.9.3
4141
hooks:
4242
- id: isort
4343
- repo: https://github.com/pre-commit/pygrep-hooks
44-
rev: 58ace0d0dc6b2439b737a5ea353f836f6a2bad13 # frozen: v1.8.0
44+
rev: 6f51a66bba59954917140ec2eeeaa4d5e630e6ce # frozen: v1.9.0
4545
hooks:
4646
- id: python-no-eval
4747
- id: python-no-log-warn

plugins/github.py

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import re
22

33
import requests
4+
from requests import HTTPError
45

56
from cloudbot import hook
67
from cloudbot.util import formatting, web
@@ -30,12 +31,17 @@ def load_shortcuts(bot):
3031

3132

3233
@hook.command("ghissue", "issue")
33-
def issue_cmd(text):
34+
def issue_cmd(text, event):
3435
"""<username|repo> [number] - gets issue [number]'s summary, or the open issue count if no issue is specified"""
3536
args = text.split()
36-
owner, repo = parse_url(
37-
args[0] if args[0] not in shortcuts else shortcuts[args[0]]
38-
)
37+
first = args[0]
38+
shortcut = shortcuts.get(first)
39+
if shortcut:
40+
data = shortcut
41+
else:
42+
data = parse_url(first)
43+
44+
owner, repo = data
3945
issue = args[1] if len(args) > 1 else None
4046

4147
if issue:
@@ -44,7 +50,16 @@ def issue_cmd(text):
4450
owner, repo, issue
4551
)
4652
)
47-
r.raise_for_status()
53+
54+
try:
55+
r.raise_for_status()
56+
except HTTPError as err:
57+
if err.response.status_code == 404:
58+
return f"Issue #{issue} doesn't exist in {owner}/{repo}"
59+
60+
event.reply(str(err))
61+
raise
62+
4863
j = r.json()
4964

5065
url = web.try_shorten(j["html_url"], service="git.io")
@@ -61,9 +76,11 @@ def issue_cmd(text):
6176
return "Issue #{} ({}): {} | {}: {}".format(
6277
number, state, url, title, summary
6378
)
79+
6480
r = requests.get(
6581
"https://api.github.com/repos/{}/{}/issues".format(owner, repo)
6682
)
83+
6784
r.raise_for_status()
6885
j = r.json()
6986

requirements-dev.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,9 @@ pytest-asyncio == 0.15.1
77
pytest-cov == 2.12.1
88
pytest-random-order == 1.0.4
99
responses == 0.13.4
10-
sqlalchemy-stubs == 0.4
10+
types-requests == 2.25.6
11+
types-setuptools == 57.0.2
12+
types-six == 1.16.1
13+
types-toml == 0.1.5
14+
types-freezegun == 1.1.0
1115
-r requirements.txt

tests/plugin_tests/conftest.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@ def patch_paste():
1111

1212
@pytest.fixture()
1313
def patch_try_shorten():
14-
with patch("cloudbot.util.web.try_shorten", new=lambda x: x):
15-
yield
14+
with patch("cloudbot.util.web.try_shorten", new=lambda x, **kwargs: x) as p:
15+
yield p

tests/plugin_tests/github_test.py

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
from unittest.mock import MagicMock, call
2+
3+
import pytest
4+
from requests import HTTPError
5+
6+
from plugins import github
7+
8+
9+
def test_github(mock_requests, mock_bot, patch_try_shorten):
10+
owner = "foo"
11+
repo = "bar"
12+
num = 123
13+
mock_requests.add(
14+
"GET",
15+
f"https://api.github.com/repos/{owner}/{repo}/issues/{num}",
16+
json={
17+
"html_url": "https://foo.bar.example",
18+
"number": num,
19+
"title": "This is a test",
20+
"body": "Test issue",
21+
"state": "open",
22+
"user": {"login": "linuxdaemon"},
23+
},
24+
)
25+
26+
github.load_shortcuts(mock_bot)
27+
event = MagicMock()
28+
res = github.issue_cmd(f"{owner}/{repo} {num}", event)
29+
expected = "Issue #123 (\x033\x02Opened\x02\x0f by linuxdaemon): https://foo.bar.example | This is a test: Test issue"
30+
assert res == expected
31+
assert event.mock_calls == []
32+
33+
34+
def test_github_error(mock_requests, mock_bot, patch_try_shorten):
35+
owner = "foo"
36+
repo = "bar"
37+
num = 123
38+
mock_requests.add(
39+
"GET",
40+
f"https://api.github.com/repos/{owner}/{repo}/issues/{num}",
41+
json={},
42+
status=403,
43+
)
44+
45+
github.load_shortcuts(mock_bot)
46+
event = MagicMock()
47+
with pytest.raises(HTTPError):
48+
github.issue_cmd(f"{owner}/{repo} {num}", event)
49+
50+
assert event.mock_calls == [
51+
call.reply(
52+
"403 Client Error: Forbidden for url: https://api.github.com/repos/foo/bar/issues/123"
53+
)
54+
]
55+
56+
57+
def test_github_closed(mock_requests, mock_bot, patch_try_shorten):
58+
owner = "foo"
59+
repo = "bar"
60+
num = 123
61+
github.shortcuts[repo] = (owner, repo)
62+
mock_requests.add(
63+
"GET",
64+
f"https://api.github.com/repos/{owner}/{repo}/issues/{num}",
65+
json={
66+
"html_url": "https://foo.bar.example",
67+
"number": num,
68+
"title": "This is a test",
69+
"body": "Test issue",
70+
"state": "closed",
71+
"closed_by": {"login": "A_D"},
72+
"user": {"login": "linuxdaemon"},
73+
},
74+
)
75+
76+
github.load_shortcuts(mock_bot)
77+
event = MagicMock()
78+
res = github.issue_cmd(f"{repo} {num}", event)
79+
expected = "Issue #123 (\x034\x02Closed\x02\x0f by A_D): https://foo.bar.example | This is a test: Test issue"
80+
assert res == expected
81+
assert event.mock_calls == []
82+
83+
84+
def test_github_shortcut(mock_requests, mock_bot, patch_try_shorten):
85+
owner = "foo"
86+
repo = "bar"
87+
num = 123
88+
github.shortcuts[repo] = (owner, repo)
89+
mock_requests.add(
90+
"GET",
91+
f"https://api.github.com/repos/{owner}/{repo}/issues/{num}",
92+
json={
93+
"html_url": "https://foo.bar.example",
94+
"number": num,
95+
"title": "This is a test",
96+
"body": "Test issue",
97+
"state": "open",
98+
"user": {"login": "linuxdaemon"},
99+
},
100+
)
101+
102+
github.load_shortcuts(mock_bot)
103+
event = MagicMock()
104+
res = github.issue_cmd(f"{repo} {num}", event)
105+
expected = "Issue #123 (\x033\x02Opened\x02\x0f by linuxdaemon): https://foo.bar.example | This is a test: Test issue"
106+
assert res == expected
107+
assert event.mock_calls == []
108+
109+
110+
def test_github_no_num_no_issues(mock_requests, mock_bot, patch_try_shorten):
111+
owner = "foo"
112+
repo = "bar"
113+
mock_requests.add(
114+
"GET", f"https://api.github.com/repos/{owner}/{repo}/issues", json=[]
115+
)
116+
117+
github.load_shortcuts(mock_bot)
118+
event = MagicMock()
119+
res = github.issue_cmd(f"{owner}/{repo}", event)
120+
expected = "Repository has no open issues."
121+
assert res == expected
122+
assert event.mock_calls == []
123+
124+
125+
def test_github_no_num(mock_requests, mock_bot, patch_try_shorten):
126+
owner = "foo"
127+
repo = "bar"
128+
mock_requests.add(
129+
"GET", f"https://api.github.com/repos/{owner}/{repo}/issues", json=[{}]
130+
)
131+
132+
github.load_shortcuts(mock_bot)
133+
event = MagicMock()
134+
res = github.issue_cmd(f"{owner}/{repo}", event)
135+
expected = "Repository has 1 open issues."
136+
assert res == expected
137+
assert event.mock_calls == []
138+
139+
140+
def test_github_no_exist(mock_requests, mock_bot, patch_try_shorten):
141+
owner = "foo"
142+
repo = "bar"
143+
num = 123
144+
mock_requests.add(
145+
"GET",
146+
f"https://api.github.com/repos/{owner}/{repo}/issues/{num}",
147+
json={},
148+
status=404,
149+
)
150+
151+
github.load_shortcuts(mock_bot)
152+
event = MagicMock()
153+
res = github.issue_cmd(f"{owner}/{repo} {num}", event)
154+
expected = "Issue #123 doesn't exist in foo/bar"
155+
assert res == expected
156+
assert event.mock_calls == []

tests/test_json.py

Lines changed: 0 additions & 28 deletions
This file was deleted.

0 commit comments

Comments
 (0)