Skip to content

Commit

Permalink
Fix github plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
Aspen Barnes committed Aug 25, 2021
1 parent 7a55dc9 commit 121b8e0
Show file tree
Hide file tree
Showing 7 changed files with 189 additions and 41 deletions.
1 change: 0 additions & 1 deletion .mypy.ini
@@ -1,5 +1,4 @@
[mypy]
plugins = sqlmypy
namespace_packages = True
python_version = 3.8
warn_unused_configs = True
Expand Down
8 changes: 4 additions & 4 deletions .pre-commit-config.yaml
Expand Up @@ -4,7 +4,7 @@ ci:
- mypy
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: 6e2418c5521b7d606e72914dced3253f9ace1205 # frozen: v3.4.0
rev: 38b88246ccc552bffaaf54259d064beeee434539 # frozen: v4.0.1
hooks:
- id: trailing-whitespace
args: ['--markdown-linebreak-ext=md,markdown']
Expand Down Expand Up @@ -33,15 +33,15 @@ repos:
args:
- --remove
- repo: https://github.com/psf/black
rev: 67d5532c3392280de0ce717a1ab728eca2beb698 # frozen: 21.4b0
rev: e3000ace2fd1fcb1c181bb7a8285f1f976bcbdc7 # frozen: 21.7b0
hooks:
- id: black
- repo: https://github.com/pycqa/isort
rev: a6222a8a125ec719724e628a5d3d0d5c60923281 # frozen: 5.8.0
rev: fd5ba70665a37ec301a1f714ed09336048b3be63 # frozen: 5.9.3
hooks:
- id: isort
- repo: https://github.com/pre-commit/pygrep-hooks
rev: 58ace0d0dc6b2439b737a5ea353f836f6a2bad13 # frozen: v1.8.0
rev: 6f51a66bba59954917140ec2eeeaa4d5e630e6ce # frozen: v1.9.0
hooks:
- id: python-no-eval
- id: python-no-log-warn
Expand Down
27 changes: 22 additions & 5 deletions plugins/github.py
@@ -1,6 +1,7 @@
import re

import requests
from requests import HTTPError

from cloudbot import hook
from cloudbot.util import formatting, web
Expand Down Expand Up @@ -30,12 +31,17 @@ def load_shortcuts(bot):


@hook.command("ghissue", "issue")
def issue_cmd(text):
def issue_cmd(text, event):
"""<username|repo> [number] - gets issue [number]'s summary, or the open issue count if no issue is specified"""
args = text.split()
owner, repo = parse_url(
args[0] if args[0] not in shortcuts else shortcuts[args[0]]
)
first = args[0]
shortcut = shortcuts.get(first)
if shortcut:
data = shortcut
else:
data = parse_url(first)

owner, repo = data
issue = args[1] if len(args) > 1 else None

if issue:
Expand All @@ -44,7 +50,16 @@ def issue_cmd(text):
owner, repo, issue
)
)
r.raise_for_status()

try:
r.raise_for_status()
except HTTPError as err:
if err.response.status_code == 404:
return f"Issue #{issue} doesn't exist in {owner}/{repo}"

event.reply(str(err))
raise

j = r.json()

url = web.try_shorten(j["html_url"], service="git.io")
Expand All @@ -61,9 +76,11 @@ def issue_cmd(text):
return "Issue #{} ({}): {} | {}: {}".format(
number, state, url, title, summary
)

r = requests.get(
"https://api.github.com/repos/{}/{}/issues".format(owner, repo)
)

r.raise_for_status()
j = r.json()

Expand Down
6 changes: 5 additions & 1 deletion requirements-dev.txt
Expand Up @@ -7,5 +7,9 @@ pytest-asyncio == 0.15.1
pytest-cov == 2.12.1
pytest-random-order == 1.0.4
responses == 0.13.4
sqlalchemy-stubs == 0.4
types-requests == 2.25.6
types-setuptools == 57.0.2
types-six == 1.16.1
types-toml == 0.1.5
types-freezegun == 1.1.0
-r requirements.txt
4 changes: 2 additions & 2 deletions tests/plugin_tests/conftest.py
Expand Up @@ -11,5 +11,5 @@ def patch_paste():

@pytest.fixture()
def patch_try_shorten():
with patch("cloudbot.util.web.try_shorten", new=lambda x: x):
yield
with patch("cloudbot.util.web.try_shorten", new=lambda x, **kwargs: x) as p:
yield p
156 changes: 156 additions & 0 deletions tests/plugin_tests/github_test.py
@@ -0,0 +1,156 @@
from unittest.mock import MagicMock, call

import pytest
from requests import HTTPError

from plugins import github


def test_github(mock_requests, mock_bot, patch_try_shorten):
owner = "foo"
repo = "bar"
num = 123
mock_requests.add(
"GET",
f"https://api.github.com/repos/{owner}/{repo}/issues/{num}",
json={
"html_url": "https://foo.bar.example",
"number": num,
"title": "This is a test",
"body": "Test issue",
"state": "open",
"user": {"login": "linuxdaemon"},
},
)

github.load_shortcuts(mock_bot)
event = MagicMock()
res = github.issue_cmd(f"{owner}/{repo} {num}", event)
expected = "Issue #123 (\x033\x02Opened\x02\x0f by linuxdaemon): https://foo.bar.example | This is a test: Test issue"
assert res == expected
assert event.mock_calls == []


def test_github_error(mock_requests, mock_bot, patch_try_shorten):
owner = "foo"
repo = "bar"
num = 123
mock_requests.add(
"GET",
f"https://api.github.com/repos/{owner}/{repo}/issues/{num}",
json={},
status=403,
)

github.load_shortcuts(mock_bot)
event = MagicMock()
with pytest.raises(HTTPError):
github.issue_cmd(f"{owner}/{repo} {num}", event)

assert event.mock_calls == [
call.reply(
"403 Client Error: Forbidden for url: https://api.github.com/repos/foo/bar/issues/123"
)
]


def test_github_closed(mock_requests, mock_bot, patch_try_shorten):
owner = "foo"
repo = "bar"
num = 123
github.shortcuts[repo] = (owner, repo)
mock_requests.add(
"GET",
f"https://api.github.com/repos/{owner}/{repo}/issues/{num}",
json={
"html_url": "https://foo.bar.example",
"number": num,
"title": "This is a test",
"body": "Test issue",
"state": "closed",
"closed_by": {"login": "A_D"},
"user": {"login": "linuxdaemon"},
},
)

github.load_shortcuts(mock_bot)
event = MagicMock()
res = github.issue_cmd(f"{repo} {num}", event)
expected = "Issue #123 (\x034\x02Closed\x02\x0f by A_D): https://foo.bar.example | This is a test: Test issue"
assert res == expected
assert event.mock_calls == []


def test_github_shortcut(mock_requests, mock_bot, patch_try_shorten):
owner = "foo"
repo = "bar"
num = 123
github.shortcuts[repo] = (owner, repo)
mock_requests.add(
"GET",
f"https://api.github.com/repos/{owner}/{repo}/issues/{num}",
json={
"html_url": "https://foo.bar.example",
"number": num,
"title": "This is a test",
"body": "Test issue",
"state": "open",
"user": {"login": "linuxdaemon"},
},
)

github.load_shortcuts(mock_bot)
event = MagicMock()
res = github.issue_cmd(f"{repo} {num}", event)
expected = "Issue #123 (\x033\x02Opened\x02\x0f by linuxdaemon): https://foo.bar.example | This is a test: Test issue"
assert res == expected
assert event.mock_calls == []


def test_github_no_num_no_issues(mock_requests, mock_bot, patch_try_shorten):
owner = "foo"
repo = "bar"
mock_requests.add(
"GET", f"https://api.github.com/repos/{owner}/{repo}/issues", json=[]
)

github.load_shortcuts(mock_bot)
event = MagicMock()
res = github.issue_cmd(f"{owner}/{repo}", event)
expected = "Repository has no open issues."
assert res == expected
assert event.mock_calls == []


def test_github_no_num(mock_requests, mock_bot, patch_try_shorten):
owner = "foo"
repo = "bar"
mock_requests.add(
"GET", f"https://api.github.com/repos/{owner}/{repo}/issues", json=[{}]
)

github.load_shortcuts(mock_bot)
event = MagicMock()
res = github.issue_cmd(f"{owner}/{repo}", event)
expected = "Repository has 1 open issues."
assert res == expected
assert event.mock_calls == []


def test_github_no_exist(mock_requests, mock_bot, patch_try_shorten):
owner = "foo"
repo = "bar"
num = 123
mock_requests.add(
"GET",
f"https://api.github.com/repos/{owner}/{repo}/issues/{num}",
json={},
status=404,
)

github.load_shortcuts(mock_bot)
event = MagicMock()
res = github.issue_cmd(f"{owner}/{repo} {num}", event)
expected = "Issue #123 doesn't exist in foo/bar"
assert res == expected
assert event.mock_calls == []
28 changes: 0 additions & 28 deletions tests/test_json.py

This file was deleted.

0 comments on commit 121b8e0

Please sign in to comment.