Skip to content
This repository has been archived by the owner on Jan 12, 2021. It is now read-only.

Commit

Permalink
Merge pull request #318 from dephell/package-bug
Browse files Browse the repository at this point in the history
Add package bug command
  • Loading branch information
orsinium committed Dec 17, 2019
2 parents 8885f0f + e273108 commit 2f4b7a3
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 1 deletion.
1 change: 1 addition & 0 deletions dephell/commands/discover.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
'jail remove',
'jail try',

'package bug',
'package downloads',
'package install',
'package list',
Expand Down
71 changes: 71 additions & 0 deletions dephell/commands/package_bug.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# built-in
import webbrowser
from argparse import ArgumentParser
from typing import Dict, Optional
from urllib.parse import urlparse

# external
import requests

# app
from ..actions import get_package
from ..config import builders
from .base import BaseCommand


class PackageBugCommand(BaseCommand):
"""Report bug in a package.
"""
@classmethod
def get_parser(cls) -> ArgumentParser:
parser = cls._get_default_parser()
builders.build_config(parser)
builders.build_venv(parser)
builders.build_output(parser)
builders.build_api(parser)
builders.build_other(parser)
parser.add_argument('name', help='package name')
return parser

def __call__(self) -> bool:
dep = get_package(self.args.name, repo=self.config.get('repo'))
dep.repo.get_releases(dep) # fetch metainfo
url = self._get_url(links=dep.links)
if not url:
self.logger.error('cannot find bug tracker URL')
return False
webbrowser.open_new_tab(url=url)
return True

@staticmethod
def _get_url(links: Dict[str, str]) -> Optional[str]:
# try to find githab or gitlub url and use it as a bug tracker
for url in links.values():
if not url.startswith('http'):
url = 'https://' + url
parsed = urlparse(url)
if parsed.hostname not in ('github.com', 'gitlab.com', 'bitbucket.org'):
continue

# build URL
parts = parsed.path.strip('/').split('/')
if len(parts) < 2:
continue
url = 'https://{}/{}/{}/issues/new'.format(parsed.hostname, *parts)

# check that issues aren't disabled for the project
response = requests.head(url)
if response.status_code == 404:
continue

return url

# try to find custom bug tracker by name
for name, url in links.items():
if 'tracker' not in name.lower():
continue
if not url.startswith('http'):
url = 'https://' + url
return url

return None
18 changes: 18 additions & 0 deletions docs/cmd-package-bug.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# dephell package bug

Report bug in a package. The command finds a bug tracker, associated with the package, and opens the tracker URL in a new browser tab.

```bash
$ dephell package bug flask
```

Packages in Conda also supported:

```bash
$ dephell package bug --repo conda textdistance
```

## See also

1. [dephell package show](cmd-package-show) to get information about package.
1. [dephell package search](cmd-package-search) to search packages on [PyPI](https://pypi.org/).
3 changes: 2 additions & 1 deletion docs/index-package.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ Commands to work with single packages.

Get information: [download statistics](cmd-package-downloads), [installed packages](cmd-package-list), [available releases](cmd-package-releases), [package metainfo](cmd-package-show), [search packages](cmd-package-search).

Manage: [install](cmd-package-install), [remove](cmd-package-remove), [remove with dependencies](cmd-package-purge).
Manage: [install](cmd-package-install), [remove](cmd-package-remove), [remove with dependencies](cmd-package-purge), [report bug](cmd-package-bug).

```eval_rst
.. toctree::
:maxdepth: 1
cmd-package-bug
cmd-package-downloads
cmd-package-install
cmd-package-list
Expand Down
26 changes: 26 additions & 0 deletions tests/test_commands/test_package_bug.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import pytest

from dephell.commands import PackageBugCommand


@pytest.mark.parametrize('links, url', [
(
{'home': 'https://bitbucket.org/saaj/torrelque'},
'https://bitbucket.org/saaj/torrelque/issues/new',
),
(
{'repository': 'https://github.com/dephell/dephell'},
'https://github.com/dephell/dephell/issues/new',
),
(
{'home': 'https://gitlab.com/pycqa/flake8'},
'https://gitlab.com/pycqa/flake8/issues/new',
),
(
{'Tracker': 'https://code.djangoproject.com/'},
'https://code.djangoproject.com/',
),
])
def test_get_url(requests_mock, links, url):
requests_mock.head(url)
assert PackageBugCommand._get_url(links=links) == url

0 comments on commit 2f4b7a3

Please sign in to comment.