From 68483b85e4f1d805331f9cb3eaac72f0f9996146 Mon Sep 17 00:00:00 2001 From: Richard Bullington-McGuire Date: Tue, 19 Nov 2024 22:52:06 -0500 Subject: [PATCH 1/6] Add GitHub Actions CI --- .flake8 | 7 ++++++ .github/dependabot.yml | 7 ++++++ .github/workflows/build-latest.yml | 38 ++++++++++++++++++++++++++++++ .github/workflows/build-tag.yml | 15 ++++++++++++ .github/workflows/build.yml | 12 ++++++++++ .github/workflows/lint.yml | 27 +++++++++++++++++++++ .gitignore | 1 - freezing/nq/api/webhook.py | 18 +++++++------- freezing/nq/app.py | 2 +- freezing/nq/autolog.py | 16 ++++--------- freezing/nq/config.py | 3 ++- freezing/nq/publish.py | 6 ++--- pyproject.toml | 2 ++ 13 files changed, 127 insertions(+), 27 deletions(-) create mode 100644 .flake8 create mode 100644 .github/dependabot.yml create mode 100644 .github/workflows/build-latest.yml create mode 100644 .github/workflows/build-tag.yml create mode 100644 .github/workflows/build.yml create mode 100644 .github/workflows/lint.yml create mode 100644 pyproject.toml diff --git a/.flake8 b/.flake8 new file mode 100644 index 0000000..e90f3e5 --- /dev/null +++ b/.flake8 @@ -0,0 +1,7 @@ +[flake8] +# Thanks https://www.reddit.com/r/learnpython/comments/rr6y69/comment/hqeqt68/?utm_source=share&utm_medium=web2x&context=3 +ignore = E203, W503, E501 + +max-line-length = 88 +max-complexity = 39 +extend-ignore = E203 diff --git a/.github/dependabot.yml b/.github/dependabot.yml new file mode 100644 index 0000000..631496c --- /dev/null +++ b/.github/dependabot.yml @@ -0,0 +1,7 @@ +--- +version: 2 +updates: + - package-ecosystem: "pip" + directory: "/" # Location of package manifests + schedule: + interval: "weekly" diff --git a/.github/workflows/build-latest.yml b/.github/workflows/build-latest.yml new file mode 100644 index 0000000..7d21867 --- /dev/null +++ b/.github/workflows/build-latest.yml @@ -0,0 +1,38 @@ +--- +name: Build and Deploy Latest + +on: + push: + branches: + - 'master' + +jobs: + + build: + uses: freezingsaddles/freezing-web/.github/workflows/build-docker.yml@1.4.18 + concurrency: build-deploy-and-test + with: + tag: latest + repo: freezing-nq + secrets: inherit + + deploy: + concurrency: build-deploy-and-test + needs: build + runs-on: ubuntu-latest + timeout-minutes: 5 + steps: + + - name: install + uses: appleboy/ssh-action@v1.1.0 + with: + host: ${{ secrets.HOST }} + username: ${{ secrets.USERNAME }} + key: ${{ secrets.KEY }} + port: ${{ secrets.PORT }} + passphrase: ${{ secrets.PASSPHRASE }} + script: | + set -euo pipefail + cd /opt/compose + docker-compose pull freezing-nq + docker-compose up -d freezing-nq diff --git a/.github/workflows/build-tag.yml b/.github/workflows/build-tag.yml new file mode 100644 index 0000000..ddaac82 --- /dev/null +++ b/.github/workflows/build-tag.yml @@ -0,0 +1,15 @@ +--- +name: Build Tag + +on: + push: + tags: + - '*' + +jobs: + build: + uses: freezingsaddles/freezing-web/.github/workflows/build-docker.yml@1.4.18 + with: + tag: ${{ github.ref_name }} + repo: freezing-nq + secrets: inherit diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml new file mode 100644 index 0000000..0146859 --- /dev/null +++ b/.github/workflows/build.yml @@ -0,0 +1,12 @@ +--- +name: Build + +on: pull_request + +jobs: + build: + uses: freezingsaddles/freezing-web/.github/workflows/build-docker.yml@1.4.18 + with: + tag: latest-actions-build + repo: freezing-nq + secrets: inherit diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml new file mode 100644 index 0000000..f929d27 --- /dev/null +++ b/.github/workflows/lint.yml @@ -0,0 +1,27 @@ +--- +name: Lint + +on: pull_request + +jobs: + + # Thanks https://black.readthedocs.io/en/stable/integrations/github_actions.html + lint: + runs-on: ubuntu-latest + steps: + - name: Check out source repository + uses: actions/checkout@v4 + - name: Set up Python environment + uses: actions/setup-python@v5 + with: + python-version: "3.10" + - name: black Lint + uses: psf/black@24.1.1 + - name: isort Lint + uses: isort/isort-action@v1 + with: + requirements-files: "requirements.txt requirements-test.txt" + - name: flake8 Lint + uses: py-actions/flake8@v2 + # There are too many errors to start with to fail on this + continue-on-error: true diff --git a/.gitignore b/.gitignore index 405d8b7..10527ec 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,3 @@ -.* *.pyc *.egg-info /env diff --git a/freezing/nq/api/webhook.py b/freezing/nq/api/webhook.py index 9be948e..79467eb 100644 --- a/freezing/nq/api/webhook.py +++ b/freezing/nq/api/webhook.py @@ -1,18 +1,18 @@ import json -import falcon - -from freezing.nq.autolog import log -from freezing.nq.config import config -from freezing.nq.publish import ActivityPublisher +import falcon +from freezing.model.msg.mq import ActivityUpdate, ActivityUpdateSchema, DefinedTubes from freezing.model.msg.strava import ( + ObjectType, + SubscriptionCallback, + SubscriptionCallbackSchema, SubscriptionUpdate, SubscriptionUpdateSchema, - SubscriptionCallbackSchema, - SubscriptionCallback, - ObjectType, ) -from freezing.model.msg.mq import DefinedTubes, ActivityUpdate, ActivityUpdateSchema + +from freezing.nq.autolog import log +from freezing.nq.config import config +from freezing.nq.publish import ActivityPublisher class WebhookResource: diff --git a/freezing/nq/app.py b/freezing/nq/app.py index aaed005..4f0f286 100644 --- a/freezing/nq/app.py +++ b/freezing/nq/app.py @@ -4,7 +4,7 @@ from freezing.nq.api.health import HealthResource from freezing.nq.api.webhook import WebhookResource -from freezing.nq.publish import configured_publisher, ActivityPublisher +from freezing.nq.publish import ActivityPublisher, configured_publisher class RequireJSON: diff --git a/freezing/nq/autolog.py b/freezing/nq/autolog.py index 0bf093d..5d65f31 100644 --- a/freezing/nq/autolog.py +++ b/freezing/nq/autolog.py @@ -1,7 +1,7 @@ from __future__ import absolute_import -import sys -import logging + import inspect +import logging class EagerFormattingAdapter(logging.LoggerAdapter): @@ -64,16 +64,8 @@ def _getUnterpolatedMessage(self, msg, args): # by casting the left side (the "msg" variable) in this context # to unicode. So we'll do that here - if sys.version_info >= ( - 3, - 0, - ): - # this is most likely unnecessary on python 3, but it's here - # for completeness, in the case of someone manually creating - # a bytestring - unicode_type = str - else: - unicode_type = unicode + # This used to check for Python 2 vs 3, but Python 2 is dead, so: + unicode_type = str # handle the attempt to print utf-8 encoded data, similar to # %-interpolation's handling of unicode formatting non-ascii diff --git a/freezing/nq/config.py b/freezing/nq/config.py index 30c4e45..b3ae4ad 100644 --- a/freezing/nq/config.py +++ b/freezing/nq/config.py @@ -1,5 +1,6 @@ -import os import logging +import os + from envparse import env envfile = os.environ.get("APP_SETTINGS", os.path.join(os.getcwd(), ".env")) diff --git a/freezing/nq/publish.py b/freezing/nq/publish.py index d89d6c0..e4cd5a3 100644 --- a/freezing/nq/publish.py +++ b/freezing/nq/publish.py @@ -4,8 +4,8 @@ import greenstalk from freezing.model.msg.mq import DefinedTubes -from freezing.nq.config import config from freezing.nq.autolog import log +from freezing.nq.config import config class ActivityPublisher: @@ -34,8 +34,8 @@ def publish_message(self, message: Any, dest: DefinedTubes): queue = greenstalk.Client(host=self.host, port=self.port, use=dest.value) try: queue.put(self.serialize_message(message)) - except: - log.exception("Error publishing message.") + except Exception as ex: + log.exception(f"Error publishing message: {ex}") finally: queue.close() diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..5d7bf33 --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,2 @@ +[tool.isort] +profile = "black" From e2cc0382c3e5dc1c318c2db3c8eb3b3472f18b3d Mon Sep 17 00:00:00 2001 From: Richard Bullington-McGuire Date: Tue, 19 Nov 2024 22:56:24 -0500 Subject: [PATCH 2/6] Fix up dev dependencies --- requirements-test.txt | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/requirements-test.txt b/requirements-test.txt index 3b5e2ba..41a1d5c 100644 --- a/requirements-test.txt +++ b/requirements-test.txt @@ -1,5 +1,6 @@ -black==19.10b0 -flake8==3.7.9 -pur==5.3.0 +black==24.3.0 +flake8==7.0.0 +isort==5.13.2 +pur==7.3.1 pytest==3.3.2 pytest-mock==1.6.3 From fc813e311aabc5b4310eb72393c558a8d6ae8d55 Mon Sep 17 00:00:00 2001 From: Richard Bullington-McGuire Date: Tue, 19 Nov 2024 22:57:38 -0500 Subject: [PATCH 3/6] Bump gunicorn version --- requirements.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index b006e02..73178ac 100644 --- a/requirements.txt +++ b/requirements.txt @@ -4,6 +4,6 @@ envparse==0.2.0 falcon==2.0.0 freezing-model @ https://github.com/freezingsaddles/freezing-model/archive/0.7.20.tar.gz greenstalk==1.0.1 -gunicorn==20.0.4 +gunicorn==22.0.4 python-mimeparse==1.6.0 six==1.14.0 From c574e315765a3a75f3018f21ae1cdf7f17e78bc4 Mon Sep 17 00:00:00 2001 From: Richard Bullington-McGuire Date: Tue, 19 Nov 2024 23:00:21 -0500 Subject: [PATCH 4/6] Tweak gunicorn verison --- requirements.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index 73178ac..48ebef8 100644 --- a/requirements.txt +++ b/requirements.txt @@ -4,6 +4,6 @@ envparse==0.2.0 falcon==2.0.0 freezing-model @ https://github.com/freezingsaddles/freezing-model/archive/0.7.20.tar.gz greenstalk==1.0.1 -gunicorn==22.0.4 +gunicorn==22.0.0 python-mimeparse==1.6.0 six==1.14.0 From d0d5ab2ddb91b65b4c8c4c3aab8af84c6ac571e7 Mon Sep 17 00:00:00 2001 From: Richard Bullington-McGuire Date: Tue, 19 Nov 2024 23:02:29 -0500 Subject: [PATCH 5/6] Fix test lint issues --- tests/conftest.py | 2 +- tests/test_api.py | 7 +++---- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index 9a61f0d..3b97d4a 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,8 +1,8 @@ import json -from falcon import testing import pytest import pytest_mock +from falcon import testing from freezing.nq.app import make_app from freezing.nq.config import config diff --git a/tests/test_api.py b/tests/test_api.py index 6eb766e..5bc5f18 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -1,14 +1,13 @@ -import json import copy +import json -import pytest import arrow - +import pytest +from freezing.model.msg.mq import ActivityUpdate, DefinedTubes from freezing.model.msg.strava import AspectType from freezing.nq.config import config from freezing.nq.publish import ActivityPublisher -from freezing.model.msg.mq import DefinedTubes, ActivityUpdate def test_get_webhook(client): From e039c4f6cb6dfa15dd212c35c090aaa4cb38eee1 Mon Sep 17 00:00:00 2001 From: Richard Bullington-McGuire Date: Tue, 19 Nov 2024 23:07:34 -0500 Subject: [PATCH 6/6] Rip out setup.py ver pins --- setup.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/setup.py b/setup.py index 478301a..302968c 100644 --- a/setup.py +++ b/setup.py @@ -13,14 +13,14 @@ """ install_reqs = [ - "envparse==0.2.0", - "falcon==2.0.0", - "greenstalk==1.0.1", - "gunicorn==20.0.4", - "python-mimeparse==1.6.0", - "six==1.14.0", + "envparse", + "falcon", + "greenstalk", + "gunicorn", + "python-mimeparse", + "six", "freezing-model", - "arrow==0.15.5", + "arrow", ] setup(