This repository has been archived by the owner on Jan 12, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 117
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #318 from dephell/package-bug
Add package bug command
- Loading branch information
Showing
5 changed files
with
118 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,6 +44,7 @@ | |
'jail remove', | ||
'jail try', | ||
|
||
'package bug', | ||
'package downloads', | ||
'package install', | ||
'package list', | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |