diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md new file mode 100644 index 0000000..d9e2d8e --- /dev/null +++ b/.github/PULL_REQUEST_TEMPLATE.md @@ -0,0 +1,4 @@ +- [ ] I have read the [section on commits and pull requests](https://github.com/NaturalHistoryMuseum/ckanext-gbif/blob/main/CONTRIBUTING.md#commits-and-pull-requests) in `CONTRIBUTING.md` + + +Describe your changes, tagging relevant issues where possible. diff --git a/.github/workflows/bump.yml b/.github/workflows/bump.yml index 8b17261..1bb1860 100644 --- a/.github/workflows/bump.yml +++ b/.github/workflows/bump.yml @@ -7,12 +7,12 @@ on: jobs: bump-version: - if: "!startsWith(github.event.head_commit.message, 'bump:')" + name: Bump version and create changelog runs-on: ubuntu-latest - name: "Bump version and create changelog" + if: "!startsWith(github.event.head_commit.message, 'bump:')" steps: - - name: Check out - uses: actions/checkout@v3 + - name: Checkout source code + uses: actions/checkout@v4 with: token: ${{ secrets.PERSONAL_ACCESS_TOKEN }} fetch-depth: 0 diff --git a/.github/workflows/pull-requests.yml b/.github/workflows/pull-requests.yml new file mode 100644 index 0000000..134e108 --- /dev/null +++ b/.github/workflows/pull-requests.yml @@ -0,0 +1,27 @@ +name: Validate pull requests + +on: + pull_request: + types: [opened, edited, reopened, synchronize] + +jobs: + validate-commits: + name: Validate commit messages + runs-on: ubuntu-latest + steps: + - name: Checkout source code + uses: actions/checkout@v4 + - name: Check commit message format + uses: webiny/action-conventional-commits@v1.3.0 + with: + allowed-commit-types: 'feat,fix,refactor,perf,docs,style,test,build,ci,chore,new,patch,revert,ui,merge' + pre-commit: + name: Run pre-commit checks + runs-on: ubuntu-latest + steps: + - name: Checkout source code + uses: actions/checkout@v4 + - name: Setup Python + uses: actions/setup-python@v5 + - name: Run pre-commit + uses: pre-commit/action@v3.0.1 diff --git a/.github/workflows/pypi-publish.yml b/.github/workflows/pypi-publish.yml index 6866a49..bf30290 100644 --- a/.github/workflows/pypi-publish.yml +++ b/.github/workflows/pypi-publish.yml @@ -10,17 +10,16 @@ permissions: jobs: deploy: + name: Deploy package to PyPI runs-on: ubuntu-latest steps: - - name: Check out - uses: actions/checkout@v3 + - name: Checkout source code + uses: actions/checkout@v4 with: token: ${{ secrets.PERSONAL_ACCESS_TOKEN }} fetch-depth: 0 - name: Set up Python - uses: actions/setup-python@v3 - with: - python-version: '3.x' + uses: actions/setup-python@v5 - name: Install dependencies run: | python -m pip install --upgrade pip diff --git a/.github/workflows/sync.yml b/.github/workflows/sync.yml index bc66f38..4fec115 100644 --- a/.github/workflows/sync.yml +++ b/.github/workflows/sync.yml @@ -7,11 +7,11 @@ on: jobs: sync-branches: + name: Sync dev and patch branches to latest commit runs-on: ubuntu-latest - name: "Sync dev and patch branches to latest commit" steps: - - name: Check out - uses: actions/checkout@v3 + - name: Checkout source code + uses: actions/checkout@v4 with: token: ${{ secrets.PERSONAL_ACCESS_TOKEN }} fetch-depth: 0 diff --git a/.github/workflows/main.yml b/.github/workflows/tests.yml similarity index 77% rename from .github/workflows/main.yml rename to .github/workflows/tests.yml index b705c67..7b72b7f 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/tests.yml @@ -3,18 +3,18 @@ name: Tests on: push: workflow_dispatch: + pull_request: + types: [opened, edited, reopened, synchronize] jobs: test: + name: Run tests runs-on: ubuntu-latest - steps: - name: Checkout source code - uses: actions/checkout@v3 - + uses: actions/checkout@v4 - name: Build images run: docker compose build - - name: Run tests env: COVERALLS_REPO_TOKEN: ${{ secrets.COVERALLS_REPO_TOKEN }} diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 2d3b017..04e9d79 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,33 +1,34 @@ exclude: /(vendor|dist)/ repos: - repo: https://github.com/pre-commit/pre-commit-hooks - rev: v4.3.0 + rev: v5.0.0 hooks: - id: check-merge-conflict - id: detect-private-key - id: end-of-file-fixer - id: name-tests-test - args: ["--pytest-test-first"] + args: ['--pytest-test-first'] exclude: ^tests/helpers/ - id: trailing-whitespace - repo: https://github.com/commitizen-tools/commitizen - rev: v2.37.0 + rev: v3.30.0 hooks: - id: commitizen - additional_dependencies: ["cz-nhm"] - - repo: https://github.com/psf/black - rev: 22.10.0 + additional_dependencies: ['cz-nhm'] + - repo: https://github.com/astral-sh/ruff-pre-commit + rev: v0.7.1 hooks: - - id: black + - id: ruff + args: [ '--fix', '--select', 'I', '--select', 'F401', '--fix-only' ] + - id: ruff-format - repo: https://github.com/PyCQA/docformatter - rev: v1.5.0 + rev: eb1df34 hooks: - id: docformatter - # these can't be pulled directly from the config atm, not sure why - args: ["-i", "--wrap-summaries=88", "--wrap-descriptions=88", - "--pre-summary-newline", "--make-summary-multi-line"] + args: [ '-i', '--config', './pyproject.toml' ] + additional_dependencies: ['tomli'] - repo: https://github.com/pre-commit/mirrors-prettier - rev: v3.0.0-alpha.4 + rev: v4.0.0-alpha.8 hooks: - id: prettier types_or: [ javascript, vue, less, sass, scss, css ] diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index b2ba540..0e07008 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -41,7 +41,7 @@ This extension and [several others](https://github.com/search?q=topic:ckan+org:N The current core team consists of: - Josh ([@jrdh](https://github.com/jrdh)) - Technical Lead -- Ginger ([@alycejenni](https://github.com/alycejenni)) - Software Engineer +- Ginger ([@alycejenni](https://github.com/alycejenni)) - Senior Software Engineer ## Questions @@ -81,7 +81,10 @@ The process is generally as follows: 3. Make your changes, and commit often; each commit should only contain one change. See below for specifics on how to word your commits. 4. Push your changes back to your fork. 5. [Open a pull request](https://docs.github.com/en/get-started/quickstart/contributing-to-projects#making-a-pull-request) in this repository, with the base branch set to **dev** and the compare branch set to your new branch. Provide a summary of your changes in the description. -6. If the automatic tests fail (these may take a while), please go back to your code and try to make them pass. You may have to update the tests themselves. You don't have to close the pull request while you're doing this; it'll update as you add further commits. +6. There are several automated checks that will run when you open the pull request. Try to make all of them pass. If you do not at least _attempt_ to make them pass, we will not merge your pull request. + 1. Tests. Update them so that they pass, if necessary. New tests are always welcome in any pull request, but if you have added a new feature that has decreased the coverage, new tests are required. + 2. Commit format validation. If you have not followed the conventional commits format for one or more of your commits, this will fail. + 3. Code format validation. If you have not formatted your code correctly (using Ruff, docformatter, and/or Prettier), this will fail. 7. Wait for feedback from one of the core maintainers. If it's been a week or so and we haven't responded, we may not have seen it. You can find other places to contact us in [SUPPORT.md](./.github/SUPPORT.md). ### Commits @@ -142,9 +145,9 @@ cz c ##### pre-commit -pre-commit is a tool that runs a variety of checks and modifications before a commit is made. You can check the [.pre-commit-config.yaml](./.pre-commit-config.yaml) file to see eaxtly what it's currently configured to do for this repository, but of particular note: +pre-commit is a tool that runs a variety of checks and modifications before a commit is made. You can check the [.pre-commit-config.yaml](./.pre-commit-config.yaml) file to see exactly what it's currently configured to do for this repository, but of particular note: -- reformats Python code with [Black](https://github.com/psf/black) +- reformats Python code with [Ruff](https://docs.astral.sh/ruff) - reformats JavaScript and stylesheets with [Prettier](https://prettier.io) - reformats docstrings with [docformatter](https://github.com/PyCQA/docformatter) - checks your commit message is correcly formatted @@ -161,15 +164,15 @@ pre-commit run Don't forget to stage any modifications that it makes! Once it runs without failing, then you can make your commit. -Something to remember is that empty docstrings will cause conflicts between Black and docformatter and the checks will fail repeatedly - so don't leave your docstrings empty! +Something to remember is that empty docstrings will cause conflicts between Ruff and docformatter and the checks will fail repeatedly - so don't leave your docstrings empty! ### Code changes and style guide -We generally use external style guides and tools to help us maintain standardised code. Black and Prettier will be run with pre-commit. +We generally use external style guides and tools to help us maintain standardised code. Ruff and Prettier will be run with pre-commit. #### Python -We follow the [Black style](https://black.readthedocs.io/en/stable/the_black_code_style/current_style.html), with the notable exception that we use single quotes. +We use [Ruff](https://docs.astral.sh/ruff) to format our code, using defaults for everything except quote style (we use single quotes). We also _mostly_ use [CKAN's style](http://docs.ckan.org/en/latest/contributing/python.html), with the following exceptions: - prefer `f''` strings over `.format()` @@ -178,7 +181,7 @@ We also _mostly_ use [CKAN's style](http://docs.ckan.org/en/latest/contributing/ #### JavaScript and stylesheets (CSS, LESS, etc) -We use [Prettier](https://prettier.io) to format these files. +We use [Prettier](https://prettier.io) to format these files. As with Ruff, we use defaults for everything except quote style (we use single quotes). #### Accessibility diff --git a/README.md b/README.md index a11a8ba..d7740dd 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,13 @@ - + + + + The Natural History Museum logo. + # ckanext-gbif -[![Tests](https://img.shields.io/github/actions/workflow/status/NaturalHistoryMuseum/ckanext-gbif/main.yml?style=flat-square)](https://github.com/NaturalHistoryMuseum/ckanext-gbif/actions/workflows/main.yml) +[![Tests](https://img.shields.io/github/actions/workflow/status/NaturalHistoryMuseum/ckanext-gbif/tests.yml?style=flat-square)](https://github.com/NaturalHistoryMuseum/ckanext-gbif/actions/workflows/tests.yml) [![Coveralls](https://img.shields.io/coveralls/github/NaturalHistoryMuseum/ckanext-gbif/main?style=flat-square)](https://coveralls.io/github/NaturalHistoryMuseum/ckanext-gbif) [![CKAN](https://img.shields.io/badge/ckan-2.9.7-orange.svg?style=flat-square)](https://github.com/ckan/ckan) [![Python](https://img.shields.io/badge/python-3.6%20%7C%203.7%20%7C%203.8-blue.svg?style=flat-square)](https://www.python.org/) diff --git a/ckanext/gbif/lib/__init__.py b/ckanext/gbif/lib/__init__.py index 196d2ad..9d2a2dd 100644 --- a/ckanext/gbif/lib/__init__.py +++ b/ckanext/gbif/lib/__init__.py @@ -9,5 +9,5 @@ def main(): pass -if __name__ == u'__main__': +if __name__ == '__main__': main() diff --git a/ckanext/gbif/lib/helpers.py b/ckanext/gbif/lib/helpers.py index de5e3a1..4819174 100644 --- a/ckanext/gbif/lib/helpers.py +++ b/ckanext/gbif/lib/helpers.py @@ -20,7 +20,7 @@ def dqi_parse_errors(errors): Convert each DQI status string into a more detailed dict. :param errors: a list of error names - :return: a list of dicts of information about each error + :returns: a list of dicts of information about each error """ if not errors: return [] @@ -36,7 +36,7 @@ def dqi_get_severity(errors, gbif_id): :param errors: a list of errors :param gbif_id: the GBIF occurrence id for this record - :return: the status to show + :returns: the status to show """ if not gbif_id: return 'unknown' @@ -83,9 +83,9 @@ def gbif_get_classification(gbif_record): def gbif_get_geography(occurrence): - ''' + """ :param occurrence: - ''' + """ geography = [] for geographic_part in ['continent', 'country', 'stateprovince']: value = occurrence.get(geographic_part, None) @@ -113,7 +113,7 @@ def get_gbif_record_url(pkg, res, rec): :param pkg: the package dict :param res: the resource dict :param rec: the record dict - :return: the link to the GBIF view for this record/resource/package combo + :returns: the link to the GBIF view for this record/resource/package combo """ # return the url for package/resource/record combo requested return toolkit.url_for( @@ -133,7 +133,7 @@ def build_gbif_nav_item(package_name, resource_id, record_id, version=None): :param resource_id: the resource id :param record_id: the record id :param version: the version of the record, or None if no version is present - :return: a nav items + :returns: a nav items """ kwargs = { 'package_name': package_name, diff --git a/ckanext/gbif/logic/action.py b/ckanext/gbif/logic/action.py index 4fa7b20..2cdfa2a 100644 --- a/ckanext/gbif/logic/action.py +++ b/ckanext/gbif/logic/action.py @@ -5,7 +5,6 @@ # Created by the Natural History Museum in London, UK import requests - from ckan.plugins import toolkit @@ -16,17 +15,17 @@ def gbif_record_show(context, data_dict): :param context: CKAN context :param data_dict: dict of parameters, only one is required: gbif_id """ - gbif_id = toolkit.get_or_bust(data_dict, "gbif_id") + gbif_id = toolkit.get_or_bust(data_dict, 'gbif_id') try: response = requests.get( - f"https://api.gbif.org/v1/occurrence/{gbif_id}", timeout=5 + f'https://api.gbif.org/v1/occurrence/{gbif_id}', timeout=5 ) except requests.Timeout: - raise toolkit.ObjectNotFound("GBIF request timed out") + raise toolkit.ObjectNotFound('GBIF request timed out') # if there was an error getting the record, raise a not found error if 400 <= response.status_code < 600: raise toolkit.ObjectNotFound( - f"GBIF request failed with code {response.status_code}" + f'GBIF request failed with code {response.status_code}' ) else: return response.json() diff --git a/ckanext/gbif/plugin.py b/ckanext/gbif/plugin.py index f402738..ccf83c1 100644 --- a/ckanext/gbif/plugin.py +++ b/ckanext/gbif/plugin.py @@ -5,13 +5,14 @@ # Created by the Natural History Museum in London, UK from ckan.plugins import SingletonPlugin, implements, interfaces, toolkit + from ckanext.gbif import routes from ckanext.gbif.lib import helpers from ckanext.gbif.logic.action import gbif_record_show class GBIFPlugin(SingletonPlugin): - '''GBIF plugin - Data Quality Indicators''' + """GBIF plugin - Data Quality Indicators""" implements(interfaces.IActions, inherit=True) implements(interfaces.IConfigurer) diff --git a/ckanext/gbif/routes/gbif.py b/ckanext/gbif/routes/gbif.py index 5a01202..7554a6f 100644 --- a/ckanext/gbif/routes/gbif.py +++ b/ckanext/gbif/routes/gbif.py @@ -4,9 +4,8 @@ # This file is part of ckanext-gbif # Created by the Natural History Museum in London, UK -from flask import Blueprint - from ckan.plugins import toolkit +from flask import Blueprint blueprint = Blueprint( name='gbif', diff --git a/docker-compose.yml b/docker-compose.yml index 862ad2b..8153169 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,5 +1,3 @@ -version: "3" - services: ckan: build: diff --git a/docs/_scripts/gen_api_pages.py b/docs/_scripts/gen_api_pages.py index e34a1bf..f441fa3 100644 --- a/docs/_scripts/gen_api_pages.py +++ b/docs/_scripts/gen_api_pages.py @@ -1,6 +1,5 @@ # !/usr/bin/env python # encoding: utf-8 - """ Generate the code reference pages and navigation. diff --git a/pyproject.toml b/pyproject.toml index 4dee1c7..0b45269 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -21,7 +21,7 @@ classifiers = [ dependencies = [ "python-dateutil", "requests", - "ckantools>=0.3.0" + "ckantools>=0.4.1" ] [project.optional-dependencies] @@ -66,9 +66,8 @@ version_files = [ "CITATION.cff:^version" ] -[tool.black] -line-length = 88 -skip_string_normalization = true +[tool.ruff.format] +quote-style = "single" [tool.pylint] max-line-length = 88 diff --git a/tests/test_actions.py b/tests/test_actions.py index 0668ade..ea4f2fa 100644 --- a/tests/test_actions.py +++ b/tests/test_actions.py @@ -1,8 +1,7 @@ -from unittest.mock import patch, MagicMock, call +from unittest.mock import MagicMock, call, patch import pytest import requests - from ckan.plugins import toolkit from ckanext.gbif.logic.action import gbif_record_show @@ -31,7 +30,7 @@ def test_failure(self, requests_mock): ) def test_timeout(self, requests_mock): - gbif_id = "test" + gbif_id = 'test' # we mock the entire requests module so we need to put the Timeout class back # before we use it requests_mock.Timeout = requests.Timeout @@ -39,7 +38,7 @@ def test_timeout(self, requests_mock): with pytest.raises(toolkit.ObjectNotFound): gbif_record_show(MagicMock(), dict(gbif_id=gbif_id)) assert requests_mock.get.call_args == call( - f"https://api.gbif.org/v1/occurrence/{gbif_id}", timeout=5 + f'https://api.gbif.org/v1/occurrence/{gbif_id}', timeout=5 ) def test_missing_gbif_id(self, requests_mock): diff --git a/tests/test_helpers.py b/tests/test_helpers.py index d0fd161..f08b355 100644 --- a/tests/test_helpers.py +++ b/tests/test_helpers.py @@ -1,7 +1,7 @@ from unittest.mock import MagicMock -from ckanext.gbif.lib.errors import GBIF_ERRORS, DQI_MAJOR_ERRORS, DQI_MINOR_ERRORS -from ckanext.gbif.lib.helpers import dqi_parse_errors, dqi_get_severity +from ckanext.gbif.lib.errors import DQI_MAJOR_ERRORS, DQI_MINOR_ERRORS, GBIF_ERRORS +from ckanext.gbif.lib.helpers import dqi_get_severity, dqi_parse_errors class TestDQIParseErrors: