From 85642c97b2de4065eb94faf460cfc428a256adc1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Magalh=C3=A3es?= Date: Thu, 26 Sep 2024 16:24:27 +0100 Subject: [PATCH] refactor: new black structure Removed travis and the badge. Made code simpler with black. Added dummy test for pytest execution. --- .github/workflows/main.yml | 8 +++ .gitignore | 2 + .travis.yml | 27 ---------- README.md | 3 +- pytest.ini | 3 ++ setup.py | 54 ++++++++------------ src/examples/__init__.py | 13 +---- src/examples/app.py | 42 ++++++---------- src/examples/base.py | 24 +++------ src/examples/blob.py | 19 +++---- src/examples/labels.py | 84 +++++++++++++++---------------- src/examples/orgs.py | 13 +---- src/examples/repo.py | 13 +---- src/examples/search.py | 13 +---- src/examples/user.py | 13 +---- src/github/__init__.py | 13 +---- src/github/base.py | 86 ++++++++++++++++---------------- src/github/blob.py | 14 ++---- src/github/orgs.py | 14 ++---- src/github/repo.py | 68 ++++++++++++------------- src/github/search.py | 98 ++++++++++++++++++++++--------------- src/github/test/__init__.py | 26 ++++++++++ src/github/test/base.py | 37 ++++++++++++++ src/github/user.py | 14 ++---- 24 files changed, 322 insertions(+), 379 deletions(-) delete mode 100644 .travis.yml create mode 100644 pytest.ini create mode 100644 src/github/test/__init__.py create mode 100644 src/github/test/base.py diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 5387d14..6a033b6 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -12,4 +12,12 @@ jobs: - uses: actions/checkout@v4 - run: python --version - run: pip install -r requirements.txt + - run: | + pip install black + black . --check + if: matrix.python-version == '3.12' + - run: | + pip install pytest + pytest - run: python setup.py test + if: matrix.python-version != '3.12' && matrix.python-version != 'latest' diff --git a/.gitignore b/.gitignore index a7e9cb6..6069a12 100644 --- a/.gitignore +++ b/.gitignore @@ -3,6 +3,8 @@ session.shelve* +/.venv + /dist /build /src/github_api-python.egg-info diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index 9e19c53..0000000 --- a/.travis.yml +++ /dev/null @@ -1,27 +0,0 @@ -dist: trusty -language: python -python: - - "2.7" - - "3.4" - - "3.5" - - "3.6" - - "pypy3" -matrix: - include: - - python: 3.8 - dist: xenial - - python: 3.9 - dist: xenial - - python: pypy - dist: xenial - - python: pypy3 - dist: xenial -before_install: - - pip install --upgrade pip setuptools -install: - - pip install -r requirements.txt - - if [[ $TRAVIS_PYTHON_VERSION != pypy* ]]; then pip install coveralls; fi -env: - - PIP_TRUSTED_HOST="pypi.python.org pypi.org files.pythonhosted.org" -script: if [[ $TRAVIS_PYTHON_VERSION != pypy* ]]; then coverage run --source=github setup.py test; else python setup.py test; fi -after_success: if [[ $TRAVIS_PYTHON_VERSION != pypy* ]]; then coveralls; fi diff --git a/README.md b/README.md index cea9801..5f321e8 100644 --- a/README.md +++ b/README.md @@ -19,8 +19,7 @@ GitHub API is currently licensed under the [Apache License, Version 2.0](http:// ## Build Automation -[![Build Status](https://app.travis-ci.com/hivesolutions/github-api.svg?branch=master)](https://travis-ci.com/github/hivesolutions/github-api) -[![Build Status GitHub](https://github.com/hivesolutions/github-api/workflows/Main%20Workflow/badge.svg)](https://github.com/hivesolutions/github-api/actions) +[![Build Status](https://github.com/hivesolutions/github-api/workflows/Main%20Workflow/badge.svg)](https://github.com/hivesolutions/github-api/actions) [![Coverage Status](https://coveralls.io/repos/hivesolutions/github-api/badge.svg?branch=master)](https://coveralls.io/r/hivesolutions/github-api?branch=master) [![PyPi Status](https://img.shields.io/pypi/v/github-api-python.svg)](https://pypi.python.org/pypi/github-api-python) [![License](https://img.shields.io/badge/license-Apache%202.0-blue.svg)](https://www.apache.org/licenses/) diff --git a/pytest.ini b/pytest.ini new file mode 100644 index 0000000..df65886 --- /dev/null +++ b/pytest.ini @@ -0,0 +1,3 @@ +[pytest] +python_files = *.py +testpaths = src/github/test diff --git a/setup.py b/setup.py index 89a5f10..4cc11cf 100644 --- a/setup.py +++ b/setup.py @@ -2,7 +2,7 @@ # -*- coding: utf-8 -*- # Hive GitHub API -# Copyright (c) 2008-2020 Hive Solutions Lda. +# Copyright (c) 2008-2024 Hive Solutions Lda. # # This file is part of Hive GitHub API. # @@ -22,16 +22,7 @@ __author__ = "João Magalhães " """ The author(s) of the module """ -__version__ = "1.0.0" -""" The version of the module """ - -__revision__ = "$LastChangedRevision$" -""" The revision number of the module """ - -__date__ = "$LastChangedDate$" -""" The last change date of the module """ - -__copyright__ = "Copyright (c) 2008-2020 Hive Solutions Lda." +__copyright__ = "Copyright (c) 2008-2024 Hive Solutions Lda." """ The copyright for the module """ __license__ = "Apache License, Version 2.0" @@ -41,25 +32,20 @@ import setuptools setuptools.setup( - name = "github-api-python", - version = "0.2.2", - author = "Hive Solutions Lda.", - author_email = "development@hive.pt", - description = "GitHub API Client", - license = "Apache License, Version 2.0", - keywords = "github api", - url = "http://github-api.hive.pt", - zip_safe = False, - packages = [ - "github" - ], - package_dir = { - "" : os.path.normpath("src") - }, - install_requires = [ - "appier" - ], - classifiers = [ + name="github-api-python", + version="0.2.2", + author="Hive Solutions Lda.", + author_email="development@hive.pt", + description="GitHub API Client", + license="Apache License, Version 2.0", + keywords="github api", + url="http://github-api.hive.pt", + zip_safe=False, + packages=["github"], + test_suite="github.test", + package_dir={"": os.path.normpath("src")}, + install_requires=["appier"], + classifiers=[ "Development Status :: 3 - Alpha", "Topic :: Utilities", "License :: OSI Approved :: Apache Software License", @@ -74,8 +60,10 @@ "Programming Language :: Python :: 3.4", "Programming Language :: Python :: 3.5", "Programming Language :: Python :: 3.6", - "Programming Language :: Python :: 3.7" + "Programming Language :: Python :: 3.7", ], - long_description = open(os.path.join(os.path.dirname(__file__), "README.md"), "rb").read().decode("utf-8"), - long_description_content_type = "text/markdown" + long_description=open(os.path.join(os.path.dirname(__file__), "README.md"), "rb") + .read() + .decode("utf-8"), + long_description_content_type="text/markdown", ) diff --git a/src/examples/__init__.py b/src/examples/__init__.py index 75ba479..9563235 100644 --- a/src/examples/__init__.py +++ b/src/examples/__init__.py @@ -2,7 +2,7 @@ # -*- coding: utf-8 -*- # Hive GitHub API -# Copyright (c) 2008-2020 Hive Solutions Lda. +# Copyright (c) 2008-2024 Hive Solutions Lda. # # This file is part of Hive GitHub API. # @@ -19,16 +19,7 @@ # You should have received a copy of the Apache License along with # Hive GitHub API. If not, see . -__version__ = "1.0.0" -""" The version of the module """ - -__revision__ = "$LastChangedRevision$" -""" The revision number of the module """ - -__date__ = "$LastChangedDate$" -""" The last change date of the module """ - -__copyright__ = "Copyright (c) 2008-2020 Hive Solutions Lda." +__copyright__ = "Copyright (c) 2008-2024 Hive Solutions Lda." """ The copyright for the module """ __license__ = "Apache License, Version 2.0" diff --git a/src/examples/app.py b/src/examples/app.py index 1d4c0ac..d5ff37f 100644 --- a/src/examples/app.py +++ b/src/examples/app.py @@ -2,7 +2,7 @@ # -*- coding: utf-8 -*- # Hive GitHub API -# Copyright (c) 2008-2020 Hive Solutions Lda. +# Copyright (c) 2008-2024 Hive Solutions Lda. # # This file is part of Hive GitHub API. # @@ -22,16 +22,7 @@ __author__ = "João Magalhães " """ The author(s) of the module """ -__version__ = "1.0.0" -""" The version of the module """ - -__revision__ = "$LastChangedRevision$" -""" The revision number of the module """ - -__date__ = "$LastChangedDate$" -""" The last change date of the module """ - -__copyright__ = "Copyright (c) 2008-2020 Hive Solutions Lda." +__copyright__ = "Copyright (c) 2008-2024 Hive Solutions Lda." """ The copyright for the module """ __license__ = "Apache License, Version 2.0" @@ -41,14 +32,11 @@ from . import base + class GithubApp(appier.WebApp): def __init__(self, *args, **kwargs): - appier.WebApp.__init__( - self, - name = "github", - *args, **kwargs - ) + appier.WebApp.__init__(self, name="github", *args, **kwargs) @appier.route("/", "GET") def index(self): @@ -57,7 +45,8 @@ def index(self): @appier.route("/me", "GET") def me(self): url = self.ensure_api() - if url: return self.redirect(url) + if url: + return self.redirect(url) api = self.get_api() user = api.self_user() return user @@ -72,26 +61,24 @@ def oauth(self): error = self.field("error") appier.verify( not error, - message = "Invalid OAuth response (%s)" % error, - exception = appier.OperationalError + message="Invalid OAuth response (%s)" % error, + exception=appier.OperationalError, ) api = self.get_api() access_token = api.oauth_access(code) self.session["gh.access_token"] = access_token - return self.redirect( - self.url_for("github.index") - ) + return self.redirect(self.url_for("github.index")) @appier.exception_handler(appier.OAuthAccessError) def oauth_error(self, error): - if "gh.access_token" in self.session: del self.session["gh.access_token"] - return self.redirect( - self.url_for("github.index") - ) + if "gh.access_token" in self.session: + del self.session["gh.access_token"] + return self.redirect(self.url_for("github.index")) def ensure_api(self): access_token = self.session.get("gh.access_token", None) - if access_token: return + if access_token: + return api = base.get_api() return api.oauth_authorize() @@ -101,6 +88,7 @@ def get_api(self): api.access_token = access_token return api + if __name__ == "__main__": app = GithubApp() app.serve() diff --git a/src/examples/base.py b/src/examples/base.py index 6de3ac2..982c343 100644 --- a/src/examples/base.py +++ b/src/examples/base.py @@ -2,7 +2,7 @@ # -*- coding: utf-8 -*- # Hive GitHub API -# Copyright (c) 2008-2020 Hive Solutions Lda. +# Copyright (c) 2008-2024 Hive Solutions Lda. # # This file is part of Hive GitHub API. # @@ -22,16 +22,7 @@ __author__ = "João Magalhães " """ The author(s) of the module """ -__version__ = "1.0.0" -""" The version of the module """ - -__revision__ = "$LastChangedRevision$" -""" The revision number of the module """ - -__date__ = "$LastChangedDate$" -""" The last change date of the module """ - -__copyright__ = "Copyright (c) 2008-2020 Hive Solutions Lda." +__copyright__ = "Copyright (c) 2008-2024 Hive Solutions Lda." """ The copyright for the module """ __license__ = "Apache License, Version 2.0" @@ -41,11 +32,12 @@ import github + def get_api(): return github.API( - username = appier.conf("GITHUB_USERNAME"), - password = appier.conf("GITHUB_PASSWORD"), - client_id = appier.conf("GITHUB_ID"), - client_secret = appier.conf("GITHUB_SECRET"), - redirect_url = appier.conf("GITHUB_REDIRECT_URL") + username=appier.conf("GITHUB_USERNAME"), + password=appier.conf("GITHUB_PASSWORD"), + client_id=appier.conf("GITHUB_ID"), + client_secret=appier.conf("GITHUB_SECRET"), + redirect_url=appier.conf("GITHUB_REDIRECT_URL"), ) diff --git a/src/examples/blob.py b/src/examples/blob.py index 056d8c1..9f15061 100644 --- a/src/examples/blob.py +++ b/src/examples/blob.py @@ -2,7 +2,7 @@ # -*- coding: utf-8 -*- # Hive GitHub API -# Copyright (c) 2008-2020 Hive Solutions Lda. +# Copyright (c) 2008-2024 Hive Solutions Lda. # # This file is part of Hive GitHub API. # @@ -22,16 +22,7 @@ __author__ = "João Magalhães & Hugo Gomes " """ The author(s) of the module """ -__version__ = "1.0.0" -""" The version of the module """ - -__revision__ = "$LastChangedRevision$" -""" The revision number of the module """ - -__date__ = "$LastChangedDate$" -""" The last change date of the module """ - -__copyright__ = "Copyright (c) 2008-2020 Hive Solutions Lda." +__copyright__ = "Copyright (c) 2008-2024 Hive Solutions Lda." """ The copyright for the module """ __license__ = "Apache License, Version 2.0" @@ -41,6 +32,10 @@ if __name__ == "__main__": api = base.get_api() - print(api.blobs_data("hivesolutions", "appier", "fe0c75cd7960bf177d2ed6aa478e188ee7a2db85")) + print( + api.blobs_data( + "hivesolutions", "appier", "fe0c75cd7960bf177d2ed6aa478e188ee7a2db85" + ) + ) else: __path__ = [] diff --git a/src/examples/labels.py b/src/examples/labels.py index 6ef5f86..8881968 100644 --- a/src/examples/labels.py +++ b/src/examples/labels.py @@ -2,7 +2,7 @@ # -*- coding: utf-8 -*- # Hive GitHub API -# Copyright (c) 2008-2020 Hive Solutions Lda. +# Copyright (c) 2008-2024 Hive Solutions Lda. # # This file is part of Hive GitHub API. # @@ -22,16 +22,7 @@ __author__ = "João Magalhães " """ The author(s) of the module """ -__version__ = "1.0.0" -""" The version of the module """ - -__revision__ = "$LastChangedRevision$" -""" The revision number of the module """ - -__date__ = "$LastChangedDate$" -""" The last change date of the module """ - -__copyright__ = "Copyright (c) 2008-2020 Hive Solutions Lda." +__copyright__ = "Copyright (c) 2008-2024 Hive Solutions Lda." """ The copyright for the module """ __license__ = "Apache License, Version 2.0" @@ -50,7 +41,8 @@ REPOS = [] -def ensure_labels(owner, repo, labels = LABELS, protected = PROTECTED, cleanup = True): + +def ensure_labels(owner, repo, labels=LABELS, protected=PROTECTED, cleanup=True): # prints a small information about the repository that # is going to have its labels updated print("Fixing %s/%s..." % (owner, repo)) @@ -65,8 +57,10 @@ def ensure_labels(owner, repo, labels = LABELS, protected = PROTECTED, cleanup = # adds the name prefix (name without emoji) to the label, effectively # empowering it for a better comparison operation - for label in labels: label["prefix"] = label["name"].split(appier.legacy.u(" "), 1)[0] - for label in _labels: label["prefix"] = label["name"].split(appier.legacy.u(" "), 1)[0] + for label in labels: + label["prefix"] = label["name"].split(appier.legacy.u(" "), 1)[0] + for label in _labels: + label["prefix"] = label["name"].split(appier.legacy.u(" "), 1)[0] # builds both maps of labels indexing all of the labels by their # prefix name (name without emoji) @@ -76,74 +70,74 @@ def ensure_labels(owner, repo, labels = LABELS, protected = PROTECTED, cleanup = for prefix, label in appier.legacy.iteritems(labels_m): if prefix in _labels_m: _label = _labels_m[prefix] - name = appier.legacy.bytes(_label["name"], encoding= "utf-8", force = True) - is_equal = label["name"] == _label["name"] and\ - label["description"] == _label["description"] and\ - label["color"] == _label["color"] + name = appier.legacy.bytes(_label["name"], encoding="utf-8", force=True) + is_equal = ( + label["name"] == _label["name"] + and label["description"] == _label["description"] + and label["color"] == _label["color"] + ) if not is_equal: print("Updating label %s..." % name) api.update_label_repo(owner, repo, name, label) else: - name = appier.legacy.bytes(label["name"], encoding= "utf-8", force = True) + name = appier.legacy.bytes(label["name"], encoding="utf-8", force=True) print("Creating label %s..." % name) api.create_label_repo(owner, repo, label) - if not cleanup: return + if not cleanup: + return for prefix, label in appier.legacy.iteritems(_labels_m): - if prefix in labels_m: continue - if prefix in protected: continue - name = appier.legacy.bytes(label["name"], encoding= "utf-8", force = True) + if prefix in labels_m: + continue + if prefix in protected: + continue + name = appier.legacy.bytes(label["name"], encoding="utf-8", force=True) print("Deleting label %s..." % name) api.delete_label_repo(owner, repo, name) -def res_data(name, dir_path = None): + +def res_data(name, dir_path=None): if dir_path == None: base_path = os.path.dirname(os.path.abspath(__file__)) dir_path = os.path.join(base_path, "res") file_path = os.path.join(dir_path, name) file = open(file_path, "rb") - try: data = file.read() - finally: file.close() - if appier.legacy.is_bytes(data): data = data.decode("utf-8") + try: + data = file.read() + finally: + file.close() + if appier.legacy.is_bytes(data): + data = data.decode("utf-8") data_j = json.loads(data) return data_j + if __name__ == "__main__": owner = appier.conf("OWNER", None) - repos = appier.conf("REPOS", [], cast = list) - config_load = appier.conf("CONFIG_LOAD", False, cast = bool) - config_paths = appier.conf("CONFIG_PATH", None, cast = list) + repos = appier.conf("REPOS", [], cast=list) + config_load = appier.conf("CONFIG_LOAD", False, cast=bool) + config_paths = appier.conf("CONFIG_PATH", None, cast=list) config_load = config_load or bool(config_paths) if config_load: - for config_path in (config_paths or []): + for config_path in config_paths or []: labels = list(LABELS) protected = list(PROTECTED) - config = res_data("config.json", dir_path = config_path) + config = res_data("config.json", dir_path=config_path) owner = config.get("owner", owner) repos = config.get("repos", repos) protected = config.get("protected", protected) labels_paths = config.get("labels", []) for labels_path in labels_paths: - labels_file = res_data(labels_path, dir_path = config_path) + labels_file = res_data(labels_path, dir_path=config_path) labels.extend(labels_file) for repo in repos: - ensure_labels( - owner, - repo, - labels = labels, - protected = protected - ) + ensure_labels(owner, repo, labels=labels, protected=protected) else: labels = list(LABELS) protected = list(PROTECTED) for repo in repos: - ensure_labels( - owner, - repo, - labels = labels, - protected = protected - ) + ensure_labels(owner, repo, labels=labels, protected=protected) else: __path__ = [] diff --git a/src/examples/orgs.py b/src/examples/orgs.py index 54f57c5..70e3b2f 100644 --- a/src/examples/orgs.py +++ b/src/examples/orgs.py @@ -2,7 +2,7 @@ # -*- coding: utf-8 -*- # Hive GitHub API -# Copyright (c) 2008-2020 Hive Solutions Lda. +# Copyright (c) 2008-2024 Hive Solutions Lda. # # This file is part of Hive GitHub API. # @@ -22,16 +22,7 @@ __author__ = "João Magalhães " """ The author(s) of the module """ -__version__ = "1.0.0" -""" The version of the module """ - -__revision__ = "$LastChangedRevision$" -""" The revision number of the module """ - -__date__ = "$LastChangedDate$" -""" The last change date of the module """ - -__copyright__ = "Copyright (c) 2008-2020 Hive Solutions Lda." +__copyright__ = "Copyright (c) 2008-2024 Hive Solutions Lda." """ The copyright for the module """ __license__ = "Apache License, Version 2.0" diff --git a/src/examples/repo.py b/src/examples/repo.py index e327756..0feb570 100644 --- a/src/examples/repo.py +++ b/src/examples/repo.py @@ -2,7 +2,7 @@ # -*- coding: utf-8 -*- # Hive GitHub API -# Copyright (c) 2008-2020 Hive Solutions Lda. +# Copyright (c) 2008-2024 Hive Solutions Lda. # # This file is part of Hive GitHub API. # @@ -22,16 +22,7 @@ __author__ = "João Magalhães " """ The author(s) of the module """ -__version__ = "1.0.0" -""" The version of the module """ - -__revision__ = "$LastChangedRevision$" -""" The revision number of the module """ - -__date__ = "$LastChangedDate$" -""" The last change date of the module """ - -__copyright__ = "Copyright (c) 2008-2020 Hive Solutions Lda." +__copyright__ = "Copyright (c) 2008-2024 Hive Solutions Lda." """ The copyright for the module """ __license__ = "Apache License, Version 2.0" diff --git a/src/examples/search.py b/src/examples/search.py index dccf6a3..8c03ccc 100644 --- a/src/examples/search.py +++ b/src/examples/search.py @@ -2,7 +2,7 @@ # -*- coding: utf-8 -*- # Hive GitHub API -# Copyright (c) 2008-2020 Hive Solutions Lda. +# Copyright (c) 2008-2024 Hive Solutions Lda. # # This file is part of Hive GitHub API. # @@ -22,16 +22,7 @@ __author__ = "Hugo Gomes " """ The author(s) of the module """ -__version__ = "1.0.0" -""" The version of the module """ - -__revision__ = "$LastChangedRevision$" -""" The revision number of the module """ - -__date__ = "$LastChangedDate$" -""" The last change date of the module """ - -__copyright__ = "Copyright (c) 2008-2020 Hive Solutions Lda." +__copyright__ = "Copyright (c) 2008-2024 Hive Solutions Lda." """ The copyright for the module """ __license__ = "Apache License, Version 2.0" diff --git a/src/examples/user.py b/src/examples/user.py index c7105ec..0f39e7f 100644 --- a/src/examples/user.py +++ b/src/examples/user.py @@ -2,7 +2,7 @@ # -*- coding: utf-8 -*- # Hive GitHub API -# Copyright (c) 2008-2020 Hive Solutions Lda. +# Copyright (c) 2008-2024 Hive Solutions Lda. # # This file is part of Hive GitHub API. # @@ -22,16 +22,7 @@ __author__ = "João Magalhães " """ The author(s) of the module """ -__version__ = "1.0.0" -""" The version of the module """ - -__revision__ = "$LastChangedRevision$" -""" The revision number of the module """ - -__date__ = "$LastChangedDate$" -""" The last change date of the module """ - -__copyright__ = "Copyright (c) 2008-2020 Hive Solutions Lda." +__copyright__ = "Copyright (c) 2008-2024 Hive Solutions Lda." """ The copyright for the module """ __license__ = "Apache License, Version 2.0" diff --git a/src/github/__init__.py b/src/github/__init__.py index a7cbbc8..929f665 100644 --- a/src/github/__init__.py +++ b/src/github/__init__.py @@ -2,7 +2,7 @@ # -*- coding: utf-8 -*- # Hive GitHub API -# Copyright (c) 2008-2020 Hive Solutions Lda. +# Copyright (c) 2008-2024 Hive Solutions Lda. # # This file is part of Hive GitHub API. # @@ -19,16 +19,7 @@ # You should have received a copy of the Apache License along with # Hive GitHub API. If not, see . -__version__ = "1.0.0" -""" The version of the module """ - -__revision__ = "$LastChangedRevision$" -""" The revision number of the module """ - -__date__ = "$LastChangedDate$" -""" The last change date of the module """ - -__copyright__ = "Copyright (c) 2008-2020 Hive Solutions Lda." +__copyright__ = "Copyright (c) 2008-2024 Hive Solutions Lda." """ The copyright for the module """ __license__ = "Apache License, Version 2.0" diff --git a/src/github/base.py b/src/github/base.py index 3bdee25..c58230e 100644 --- a/src/github/base.py +++ b/src/github/base.py @@ -2,7 +2,7 @@ # -*- coding: utf-8 -*- # Hive GitHub API -# Copyright (c) 2008-2020 Hive Solutions Lda. +# Copyright (c) 2008-2024 Hive Solutions Lda. # # This file is part of Hive GitHub API. # @@ -22,16 +22,7 @@ __author__ = "João Magalhães " """ The author(s) of the module """ -__version__ = "1.0.0" -""" The version of the module """ - -__revision__ = "$LastChangedRevision$" -""" The revision number of the module """ - -__date__ = "$LastChangedDate$" -""" The last change date of the module """ - -__copyright__ = "Copyright (c) 2008-2020 Hive Solutions Lda." +__copyright__ = "Copyright (c) 2008-2024 Hive Solutions Lda." """ The copyright for the module """ __license__ = "Apache License, Version 2.0" @@ -52,12 +43,11 @@ will be performed, this value will be used for the construction of the base URL that is going to be used by the API """ -SCOPE = ( - "user:email", -) +SCOPE = ("user:email",) """ The list of permissions to be used to create the scope string for the OAuth value """ + class API( appier.OAuth2API, blob.BLOBAPI, @@ -90,32 +80,37 @@ def get_many(self, url, **kwargs): page = 1 result = [] while True: - items = self.get(url, page = page, **kwargs) - if isinstance(items, dict): items = items["items"] - if not items: break + items = self.get(url, page=page, **kwargs) + if isinstance(items, dict): + items = items["items"] + if not items: + break result.extend(items) page += 1 return result - def get_cached(self, url, retries = 5, sleep = 1.0, **kwargs): + def get_cached(self, url, retries=5, sleep=1.0, **kwargs): while True: retries -= 1 - contents, file = self.get(url, handle = True, **kwargs) + contents, file = self.get(url, handle=True, **kwargs) code = file.getcode() - if not code == 202: break - if not retries > 0: break + if not code == 202: + break + if not retries > 0: + break time.sleep(sleep) return contents - def oauth_authorize(self, state = None): + def oauth_authorize(self, state=None): url = self.login_url + "oauth/authorize" values = dict( - client_id = self.client_id, - redirect_uri = self.redirect_url, - response_type = "code", - scope = " ".join(self.scope) + client_id=self.client_id, + redirect_uri=self.redirect_url, + response_type="code", + scope=" ".join(self.scope), ) - if state: values["state"] = state + if state: + values["state"] = state data = appier.legacy.urlencode(values) url = url + "?" + data return url @@ -124,13 +119,13 @@ def oauth_access(self, code): url = self.login_url + "oauth/access_token" contents = self.post( url, - auth = False, - token = False, - client_id = self.client_id, - client_secret = self.client_secret, - grant_type = "authorization_code", - redirect_uri = self.redirect_url, - code = code + auth=False, + token=False, + client_id=self.client_id, + client_secret=self.client_secret, + grant_type="authorization_code", + redirect_uri=self.redirect_url, + code=code, ) contents = contents.decode("utf-8") contents = appier.legacy.parse_qs(contents) @@ -139,19 +134,20 @@ def oauth_access(self, code): return self.access_token def _build_url(self): - if self.is_oauth(): self.base_url = "https://%s/" % API_DOMAIN; return + if self.is_oauth(): + self.base_url = "https://%s/" % API_DOMAIN + return if not self.username: - raise appier.OperationalError(message = "No username provided") + raise appier.OperationalError(message="No username provided") if not self.password: - raise appier.OperationalError(message = "No password provided") - self.base_url = "https://%s:%s@%s/" % ( - self.username, - self.password, - API_DOMAIN - ) + raise appier.OperationalError(message="No password provided") + self.base_url = "https://%s:%s@%s/" % (self.username, self.password, API_DOMAIN) def _get_mode(self): - if self.username and self.password: return appier.OAuthAPI.DIRECT_MODE - elif self.client_id and self.client_secret: return appier.OAuthAPI.OAUTH_MODE - elif self.access_token: return appier.OAuthAPI.OAUTH_MODE + if self.username and self.password: + return appier.OAuthAPI.DIRECT_MODE + elif self.client_id and self.client_secret: + return appier.OAuthAPI.OAUTH_MODE + elif self.access_token: + return appier.OAuthAPI.OAUTH_MODE return appier.OAuthAPI.UNSET_MODE diff --git a/src/github/blob.py b/src/github/blob.py index 1c989aa..5102a7b 100644 --- a/src/github/blob.py +++ b/src/github/blob.py @@ -2,7 +2,7 @@ # -*- coding: utf-8 -*- # Hive GitHub API -# Copyright (c) 2008-2020 Hive Solutions Lda. +# Copyright (c) 2008-2024 Hive Solutions Lda. # # This file is part of Hive GitHub API. # @@ -22,21 +22,13 @@ __author__ = "João Magalhães & Hugo Gomes " """ The author(s) of the module """ -__version__ = "1.0.0" -""" The version of the module """ - -__revision__ = "$LastChangedRevision$" -""" The revision number of the module """ - -__date__ = "$LastChangedDate$" -""" The last change date of the module """ - -__copyright__ = "Copyright (c) 2008-2020 Hive Solutions Lda." +__copyright__ = "Copyright (c) 2008-2024 Hive Solutions Lda." """ The copyright for the module """ __license__ = "Apache License, Version 2.0" """ The license for the module """ + class BLOBAPI(object): def blobs_data(self, owner, repo, file_sha): diff --git a/src/github/orgs.py b/src/github/orgs.py index d1ee5bc..e348cff 100644 --- a/src/github/orgs.py +++ b/src/github/orgs.py @@ -2,7 +2,7 @@ # -*- coding: utf-8 -*- # Hive GitHub API -# Copyright (c) 2008-2020 Hive Solutions Lda. +# Copyright (c) 2008-2024 Hive Solutions Lda. # # This file is part of Hive GitHub API. # @@ -22,21 +22,13 @@ __author__ = "João Magalhães " """ The author(s) of the module """ -__version__ = "1.0.0" -""" The version of the module """ - -__revision__ = "$LastChangedRevision$" -""" The revision number of the module """ - -__date__ = "$LastChangedDate$" -""" The last change date of the module """ - -__copyright__ = "Copyright (c) 2008-2020 Hive Solutions Lda." +__copyright__ = "Copyright (c) 2008-2024 Hive Solutions Lda." """ The copyright for the module """ __license__ = "Apache License, Version 2.0" """ The license for the module """ + class OrgAPI(object): def get_org(self, org): diff --git a/src/github/repo.py b/src/github/repo.py index c24f359..9d35145 100644 --- a/src/github/repo.py +++ b/src/github/repo.py @@ -2,7 +2,7 @@ # -*- coding: utf-8 -*- # Hive GitHub API -# Copyright (c) 2008-2020 Hive Solutions Lda. +# Copyright (c) 2008-2024 Hive Solutions Lda. # # This file is part of Hive GitHub API. # @@ -22,16 +22,7 @@ __author__ = "João Magalhães " """ The author(s) of the module """ -__version__ = "1.0.0" -""" The version of the module """ - -__revision__ = "$LastChangedRevision$" -""" The revision number of the module """ - -__date__ = "$LastChangedDate$" -""" The last change date of the module """ - -__copyright__ = "Copyright (c) 2008-2020 Hive Solutions Lda." +__copyright__ = "Copyright (c) 2008-2024 Hive Solutions Lda." """ The copyright for the module """ __license__ = "Apache License, Version 2.0" @@ -41,16 +32,17 @@ import appier + class RepoAPI(object): - def get_repo(self, owner, repo, type = "owner"): + def get_repo(self, owner, repo, type="owner"): url = self.base_url + "repos/%s/%s" % (owner, repo) - contents = self.get(url, type = type) + contents = self.get(url, type=type) return contents - def issues_repo(self, owner, repo, state = "all"): + def issues_repo(self, owner, repo, state="all"): url = self.base_url + "repos/%s/%s/issues" % (owner, repo) - contents = self.get_many(url, state = state) + contents = self.get_many(url, state=state) return contents def stats_contrib_repo(self, owner, repo): @@ -68,9 +60,9 @@ def stats_participation_repo(self, owner, repo): contents = self.get_cached(url) return contents - def contents_repo(self, owner, repo, path, ref = None): + def contents_repo(self, owner, repo, path, ref=None): url = self.base_url + "repos/%s/%s/contents/%s" % (owner, repo, path) - contents = self.get_cached(url, ref = ref) + contents = self.get_cached(url, ref=ref) return contents def create_contents_repo( @@ -79,23 +71,27 @@ def create_contents_repo( repo, path, content, - message = None, - sha = None, - branch = None, - committer = None, - author = None + message=None, + sha=None, + branch=None, + committer=None, + author=None, ): message = message or "%s '%s' file" % ("Updated" if sha else "Created", path) content = appier.legacy.bytes(content) content_b64 = base64.b64encode(content) content_b64 = appier.legacy.str(content_b64) - data_j = dict(message = message, content = content_b64) - if sha: data_j["sha"] = sha - if branch: data_j["branch"] = branch - if committer: data_j["committer"] = committer - if author: data_j["author"] = author + data_j = dict(message=message, content=content_b64) + if sha: + data_j["sha"] = sha + if branch: + data_j["branch"] = branch + if committer: + data_j["committer"] = committer + if author: + data_j["author"] = author url = self.base_url + "repos/%s/%s/contents/%s" % (owner, repo, path) - contents = self.put(url, data_j = data_j) + contents = self.put(url, data_j=data_j) return contents def issue_repo(self, owner, repo, number): @@ -115,12 +111,12 @@ def event_issue_repo(self, owner, repo, number): def create_issue_repo(self, owner, repo, issue): url = self.base_url + "repos/%s/%s/issues" % (owner, repo) - contents = self.post(url, data_j = issue) + contents = self.post(url, data_j=issue) return contents def update_issue_repo(self, owner, repo, number, issue): url = self.base_url + "repos/%s/%s/issues/%s" % (owner, repo, number) - contents = self.patch(url, data_j = issue) + contents = self.patch(url, data_j=issue) return contents def labels_repo(self, owner, repo): @@ -130,19 +126,23 @@ def labels_repo(self, owner, repo): def create_label_repo(self, owner, repo, label): url = self.base_url + "repos/%s/%s/labels" % (owner, repo) - contents = self.post(url, data_j = label) + contents = self.post(url, data_j=label) return contents def update_label_repo(self, owner, repo, name, label): url = self.base_url + "repos/%s/%s/labels/%s" % ( - owner, repo, appier.legacy.quote(name) + owner, + repo, + appier.legacy.quote(name), ) - contents = self.patch(url, data_j = label) + contents = self.patch(url, data_j=label) return contents def delete_label_repo(self, owner, repo, name): url = self.base_url + "repos/%s/%s/labels/%s" % ( - owner, repo, appier.legacy.quote(name) + owner, + repo, + appier.legacy.quote(name), ) contents = self.delete(url) return contents diff --git a/src/github/search.py b/src/github/search.py index 1cbca87..3f5e7e3 100644 --- a/src/github/search.py +++ b/src/github/search.py @@ -2,7 +2,7 @@ # -*- coding: utf-8 -*- # Hive GitHub API -# Copyright (c) 2008-2020 Hive Solutions Lda. +# Copyright (c) 2008-2024 Hive Solutions Lda. # # This file is part of Hive GitHub API. # @@ -22,68 +22,88 @@ __author__ = "Hugo Gomes " """ The author(s) of the module """ -__version__ = "1.0.0" -""" The version of the module """ - -__revision__ = "$LastChangedRevision$" -""" The revision number of the module """ - -__date__ = "$LastChangedDate$" -""" The last change date of the module """ - -__copyright__ = "Copyright (c) 2008-2020 Hive Solutions Lda." +__copyright__ = "Copyright (c) 2008-2024 Hive Solutions Lda." """ The copyright for the module """ __license__ = "Apache License, Version 2.0" """ The license for the module """ + class SearchAPI(object): - def repositories_search(self, query, text_match = False, *args, **kwargs): + def repositories_search(self, query, text_match=False, *args, **kwargs): url = self.base_url + "search/repositories" - kwargs.update(q = query) - headers = {} if not text_match else {"Accept": "application/vnd.github.v3.text-match+json"} - contents = self.get_many(url, headers = headers, **kwargs) + kwargs.update(q=query) + headers = ( + {} + if not text_match + else {"Accept": "application/vnd.github.v3.text-match+json"} + ) + contents = self.get_many(url, headers=headers, **kwargs) return contents - def commits_search(self, query, text_match = False, *args, **kwargs): + def commits_search(self, query, text_match=False, *args, **kwargs): url = self.base_url + "search/commits" - kwargs.update(q = query) - headers = {} if not text_match else {"Accept": "application/vnd.github.v3.text-match+json"} - contents = self.get_many(url, headers = headers, **kwargs) + kwargs.update(q=query) + headers = ( + {} + if not text_match + else {"Accept": "application/vnd.github.v3.text-match+json"} + ) + contents = self.get_many(url, headers=headers, **kwargs) return contents - def code_search(self, query, text_match = False, *args, **kwargs): + def code_search(self, query, text_match=False, *args, **kwargs): url = self.base_url + "search/code" - kwargs.update(q = query) - headers = {} if not text_match else {"Accept": "application/vnd.github.v3.full.text-match+json"} - contents = self.get_many(url, headers = headers, **kwargs) + kwargs.update(q=query) + headers = ( + {} + if not text_match + else {"Accept": "application/vnd.github.v3.full.text-match+json"} + ) + contents = self.get_many(url, headers=headers, **kwargs) return contents - def issues_search(self, query, text_match = False, *args, **kwargs): + def issues_search(self, query, text_match=False, *args, **kwargs): url = self.base_url + "search/issues" - kwargs.update(q = query) - headers = {} if not text_match else {"Accept": "application/vnd.github.v3.text-match+json"} - contents = self.get_many(url, headers = headers, **kwargs) + kwargs.update(q=query) + headers = ( + {} + if not text_match + else {"Accept": "application/vnd.github.v3.text-match+json"} + ) + contents = self.get_many(url, headers=headers, **kwargs) return contents - def users_search(self, query, text_match = False, *args, **kwargs): + def users_search(self, query, text_match=False, *args, **kwargs): url = self.base_url + "search/issues" - kwargs.update(q = query) - headers = {} if not text_match else {"Accept": "application/vnd.github.v3.text-match+json"} - contents = self.get_many(url, headers = headers, **kwargs) + kwargs.update(q=query) + headers = ( + {} + if not text_match + else {"Accept": "application/vnd.github.v3.text-match+json"} + ) + contents = self.get_many(url, headers=headers, **kwargs) return contents - def topics_search(self, query, text_match = False, *args, **kwargs): + def topics_search(self, query, text_match=False, *args, **kwargs): url = self.base_url + "search/topics" - kwargs.update(q = query) - headers = {} if not text_match else {"Accept": "application/vnd.github.v3.text-match+json"} - contents = self.get_many(url, headers = headers, **kwargs) + kwargs.update(q=query) + headers = ( + {} + if not text_match + else {"Accept": "application/vnd.github.v3.text-match+json"} + ) + contents = self.get_many(url, headers=headers, **kwargs) return contents - def labels_search(self, repository_id, query, text_match = False, *args, **kwargs): + def labels_search(self, repository_id, query, text_match=False, *args, **kwargs): url = self.base_url + "search/labels" - kwargs.update(dict(repository_id = repository_id, q = query)) - headers = {} if not text_match else {"Accept": "application/vnd.github.v3.text-match+json"} - contents = self.get_many(url, headers = headers, **kwargs) + kwargs.update(dict(repository_id=repository_id, q=query)) + headers = ( + {} + if not text_match + else {"Accept": "application/vnd.github.v3.text-match+json"} + ) + contents = self.get_many(url, headers=headers, **kwargs) return contents diff --git a/src/github/test/__init__.py b/src/github/test/__init__.py new file mode 100644 index 0000000..123f3c2 --- /dev/null +++ b/src/github/test/__init__.py @@ -0,0 +1,26 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- + +# Hive GitHub API +# Copyright (c) 2008-2024 Hive Solutions Lda. +# +# This file is part of Hive GitHub API. +# +# Hive GitHub API is free software: you can redistribute it and/or modify +# it under the terms of the Apache License as published by the Apache +# Foundation, either version 2.0 of the License, or (at your option) any +# later version. +# +# Hive GitHub API is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# Apache License for more details. +# +# You should have received a copy of the Apache License along with +# Hive GitHub API. If not, see . + +__copyright__ = "Copyright (c) 2008-2024 Hive Solutions Lda." +""" The copyright for the module """ + +__license__ = "Apache License, Version 2.0" +""" The license for the module """ diff --git a/src/github/test/base.py b/src/github/test/base.py new file mode 100644 index 0000000..b0b82e9 --- /dev/null +++ b/src/github/test/base.py @@ -0,0 +1,37 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- + +# Hive GitHub API +# Copyright (c) 2008-2024 Hive Solutions Lda. +# +# This file is part of Hive GitHub API. +# +# Hive GitHub API is free software: you can redistribute it and/or modify +# it under the terms of the Apache License as published by the Apache +# Foundation, either version 2.0 of the License, or (at your option) any +# later version. +# +# Hive GitHub API is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# Apache License for more details. +# +# You should have received a copy of the Apache License along with +# Hive GitHub API. If not, see . + +__author__ = "João Magalhães " +""" The author(s) of the module """ + +__copyright__ = "Copyright (c) 2008-2024 Hive Solutions Lda." +""" The copyright for the module """ + +__license__ = "Apache License, Version 2.0" +""" The license for the module """ + +import unittest + + +class BaseTest(unittest.TestCase): + + def test_basic(self): + self.assertEqual(1 + 1, 2) diff --git a/src/github/user.py b/src/github/user.py index 24e68f7..a6886d3 100644 --- a/src/github/user.py +++ b/src/github/user.py @@ -2,7 +2,7 @@ # -*- coding: utf-8 -*- # Hive GitHub API -# Copyright (c) 2008-2020 Hive Solutions Lda. +# Copyright (c) 2008-2024 Hive Solutions Lda. # # This file is part of Hive GitHub API. # @@ -22,21 +22,13 @@ __author__ = "João Magalhães " """ The author(s) of the module """ -__version__ = "1.0.0" -""" The version of the module """ - -__revision__ = "$LastChangedRevision$" -""" The revision number of the module """ - -__date__ = "$LastChangedDate$" -""" The last change date of the module """ - -__copyright__ = "Copyright (c) 2008-2020 Hive Solutions Lda." +__copyright__ = "Copyright (c) 2008-2024 Hive Solutions Lda." """ The copyright for the module """ __license__ = "Apache License, Version 2.0" """ The license for the module """ + class UserAPI(object): def get_user(self, username):