Skip to content

Commit

Permalink
improve pre-commit hooks, replace flake8 & pylint with ruff (#2441)
Browse files Browse the repository at this point in the history
  • Loading branch information
ITProKyle authored Jul 23, 2024
1 parent a2c43c4 commit fd06ee7
Show file tree
Hide file tree
Showing 652 changed files with 8,215 additions and 13,145 deletions.
34 changes: 18 additions & 16 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
{
"name": "Ubuntu",
"build": {
"dockerfile": "Dockerfile",
"args": { "VARIANT": "ubuntu-22.04" }
},
"remoteUser": "vscode",
"customizations": {
"vscode": {
"extensions": [
"ms-python.python"
]
}
},
"features": {
"ghcr.io/devcontainers/features/docker-in-docker:2": {}
}
"build": {
"args": {
"VARIANT": "ubuntu-22.04"
},
"dockerfile": "Dockerfile"
},
"customizations": {
"vscode": {
"extensions": [
"ms-python.python"
]
}
},
"features": {
"ghcr.io/devcontainers/features/docker-in-docker:2": {}
},
"name": "Ubuntu",
"remoteUser": "vscode"
}
4 changes: 2 additions & 2 deletions .github/CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

To view our [Getting Started] guide for developers and [Contribution Requirements], please refer to the official [documentation].

[Contribution Requirements]: https://docs.onica.com/projects/runway/page/developers/contributing.html
[Getting Started]: https://docs.onica.com/projects/runway/page/developers/getting_started.html
[contribution requirements]: https://docs.onica.com/projects/runway/page/developers/contributing.html
[documentation]: https://docs.onica.com/projects/runway
[getting started]: https://docs.onica.com/projects/runway/page/developers/getting_started.html
5 changes: 2 additions & 3 deletions .github/ISSUE_TEMPLATE/feature_request.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
---
name: Feature request
about: Suggest an idea for this project
title: "[REQUEST] feature"
title: '[REQUEST] feature'
labels: feature, priority:low, status:review_needed

---

**Is your feature request related to a problem? Please describe.**
A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]
A clear and concise description of what the problem is. Ex. I'm always frustrated when ...

**Describe the solution you'd like**
A clear and concise description of what you want to happen.
Expand Down
3 changes: 1 addition & 2 deletions .github/ISSUE_TEMPLATE/general_question.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
---
name: General Question
about: General question about the project, usage, design, etc.
title: "[QUESTION]"
title: '[QUESTION]'
labels: priority:low, status:review_needed, question

---

**Question**
Expand Down
1 change: 1 addition & 0 deletions .github/pull_request_template.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
# Checklist

<!-- You can erase any parts of this template not applicable to your Pull Request. -->

- [ ] Have you followed the guidelines in our [Contribution Requirements](https://docs.onica.com/projects/runway/page/developers/contributing.html)?
- [ ] Have you checked to ensure there aren't other open [Pull Requests](../../../pulls) for the same update/change?
- [ ] Does your submission pass tests?
Expand Down
10 changes: 2 additions & 8 deletions .github/scripts/urlshortener/Makefile
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
install:
@poetry install

lint: lint-flake8 lint-pylint

lint-flake8:
@poetry run flake8 update_urls.py

lint-pylint:
@poetry run pylint update_urls.py \
--rcfile=./../../../pyproject.toml
lint:
@echo "no linters configured currently"

test:
@poetry run pytest ./test_update_urls.py \
Expand Down
41 changes: 18 additions & 23 deletions .github/scripts/urlshortener/test_update_urls.py
Original file line number Diff line number Diff line change
@@ -1,74 +1,71 @@
"""Tests for update_urls."""
"""Tests for update_urls.""" # noqa: INP001

# pylint: disable=no-member
# ruff: noqa: S101
from typing import TYPE_CHECKING
from unittest.mock import ANY, Mock, call, patch

import boto3
import pytest
from botocore.stub import Stubber
from click.testing import CliRunner
from mypy_boto3_dynamodb.service_resource import Table

from update_urls import command, handler, put_item, sanitize_version

if TYPE_CHECKING:
from mypy_boto3_dynamodb.service_resource import Table

def test_sanitize_version():

def test_sanitize_version() -> None:
"""Test sanitize_version."""
assert sanitize_version(None, None, "1.0.0") == "1.0.0"
assert sanitize_version(None, None, "v1.0.0") == "1.0.0"
assert sanitize_version(None, None, "refs/tags/1.0.0") == "1.0.0"
assert sanitize_version(None, None, "refs/tags/v1.0.0") == "1.0.0"
assert sanitize_version(None, None, "refs/tags/v1.0.0-dev1") == "1.0.0-dev1"

with pytest.raises(ValueError):
with pytest.raises(ValueError): # noqa: PT011
assert not sanitize_version(None, None, "refs/tags/stable")


def test_put_item():
def test_put_item() -> None:
"""Test put_item."""
table_name = "test-table"
id_val = "my_id"
target = "my_target"
table: Table = boto3.resource("dynamodb").Table(table_name)
stubber = Stubber(table.meta.client)

stubber.add_response(
"put_item", {"Attributes": {"id": {"S": id_val}, "target": {"S": target}}}
)
stubber.add_response("put_item", {"Attributes": {"id": {"S": id_val}, "target": {"S": target}}})

with stubber:
assert not put_item(table, id_val, target)


@patch("update_urls.put_item")
def test_handler(mock_put_item: Mock):
def test_handler(mock_put_item: Mock) -> None:
"""Test handler."""
table = Mock()
assert not handler(table, "test-bucket", "us-west-2", "1.0.0", True)
calls = [
call(
table=table,
id_val="runway/latest/linux",
target="https://test-bucket.s3-us-west-2.amazonaws.com/"
"runway/1.0.0/linux/runway",
target="https://test-bucket.s3-us-west-2.amazonaws.com/runway/1.0.0/linux/runway",
),
call(
table=table,
id_val="runway/1.0.0/linux",
target="https://test-bucket.s3-us-west-2.amazonaws.com/"
"runway/1.0.0/linux/runway",
target="https://test-bucket.s3-us-west-2.amazonaws.com/runway/1.0.0/linux/runway",
),
call(
table=table,
id_val="runway/latest/osx",
target="https://test-bucket.s3-us-west-2.amazonaws.com/"
"runway/1.0.0/osx/runway",
target="https://test-bucket.s3-us-west-2.amazonaws.com/runway/1.0.0/osx/runway",
),
call(
table=table,
id_val="runway/1.0.0/osx",
target="https://test-bucket.s3-us-west-2.amazonaws.com/"
"runway/1.0.0/osx/runway",
target="https://test-bucket.s3-us-west-2.amazonaws.com/runway/1.0.0/osx/runway",
),
call(
table=table,
Expand All @@ -89,16 +86,14 @@ def test_handler(mock_put_item: Mock):
call(
table=table,
id_val="runway/1.1.0/linux",
target="https://test-bucket.s3-us-east-1.amazonaws.com/"
"runway/1.1.0/linux/runway",
target="https://test-bucket.s3-us-east-1.amazonaws.com/runway/1.1.0/linux/runway",
)
)
calls.append(
call(
table=table,
id_val="runway/1.1.0/osx",
target="https://test-bucket.s3-us-east-1.amazonaws.com/"
"runway/1.1.0/osx/runway",
target="https://test-bucket.s3-us-east-1.amazonaws.com/runway/1.1.0/osx/runway",
)
)
calls.append(
Expand All @@ -114,7 +109,7 @@ def test_handler(mock_put_item: Mock):


@patch("update_urls.handler")
def test_command(mock_handler: Mock):
def test_command(mock_handler: Mock) -> None:
"""Test command."""
runner = CliRunner()
result = runner.invoke(
Expand Down
19 changes: 8 additions & 11 deletions .github/scripts/urlshortener/update_urls.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
"""Update Runway release URLs."""
"""Update Runway release URLs.""" # noqa: INP001

# pylint: disable=no-member
from __future__ import annotations

import logging
from typing import TYPE_CHECKING, Optional, Union
from typing import TYPE_CHECKING

import boto3
import click
Expand All @@ -18,16 +17,14 @@
HDLR.setFormatter(logging.Formatter(logging.BASIC_FORMAT))

ID_TEMPLATE = "runway/{release}/{os}"
TARGET_TEMPLATE = (
"https://{bucket_name}.s3-{region}.amazonaws.com/runway/{version}/{os}/runway"
)
TARGET_TEMPLATE = "https://{bucket_name}.s3-{region}.amazonaws.com/runway/{version}/{os}/runway"

OS_NAMES = ["linux", "osx", "windows"]


def sanitize_version(
_ctx: Optional[click.Context],
_param: Optional[Union[click.Option, click.Parameter]],
_ctx: click.Context | None,
_param: click.Option | click.Parameter | None,
value: str,
) -> str:
"""Sanitize a version number by stripping git tag ref and leading "v".
Expand Down Expand Up @@ -67,7 +64,7 @@ def handler(
"""Handle the command.
Core logic executed by the command aside from boto3 session/resource
initializeion and logging setup.
initialization and logging setup.
Args:
table: DynamoDB table resource.
Expand Down Expand Up @@ -122,7 +119,7 @@ def handler(
"table_name",
metavar="<table>",
required=True,
help="Name of the DynamoDB table containing entries for the URL " "shortener.",
help="Name of the DynamoDB table containing entries for the URL shortener.",
)
@click.option(
"--version",
Expand Down Expand Up @@ -156,4 +153,4 @@ def command(


if __name__ == "__main__":
command() # pylint: disable=E
command()
61 changes: 30 additions & 31 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
minimum_pre_commit_version: 2.6.0
default_language_version:
node: system

exclude: |
(?x)^(
(.*/)?package-lock\.json|
(.*/)?poetry\.lock
)$
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.3.0
rev: v4.6.0
hooks:
- id: check-json
- id: check-merge-conflict
- id: check-toml
- id: check-yaml
args:
- --unsafe # needed for parsing CFN
Expand All @@ -14,56 +22,47 @@ repos:
- id: file-contents-sorter
files: |
(?x)^(
\.dockerignore|
\.gitignore|
\.vscode/dictionaries/.*\.txt|
MANIFEST.in
)$
- id: pretty-format-json
args: [--autofix, --indent, '4']
files: |
exclude: |
(?x)^(
\.vscode/.*\.json
(.*)?(angular|cdk|package|tsconfig(\.spec)?|tslint)\.json
)$
- id: pretty-format-json
args: [--autofix, --indent, '2']
files: |
(?x)^(
(.*)?(cdk|tsconfig|tslint).json
(.*)?(angular|cdk|package|tsconfig(\.spec)?|tslint)\.json
)$
- id: trailing-whitespace
- repo: https://github.com/pappasam/toml-sort
rev: v0.23.1
hooks:
- id: toml-sort-fix
- repo: https://github.com/ITProKyle/pre-commit-hook-yamlfmt
rev: v0.2.0
rev: v0.3.0
hooks:
- id: yamlfmt
args: [--mapping, '2', --offset, '2', --sequence, '4']
files: |
exclude: |
(?x)^(
\.github/(?!dependabot).*\.(yaml|yml)|
\.markdownlint.yml|
\.pre-commit-config.yaml|
\.readthedocs.yml|
buildspec.yml
tests/unit/module/staticsite/fixtures/expected_yaml/.*\.(yaml|yml)|
docs/runway-example\.yml
)$
- repo: https://github.com/timothycrosley/isort
rev: 5.12.0
hooks:
- id: isort
- repo: https://github.com/psf/black
rev: 24.1.1
hooks:
- id: black
args:
- --color
- repo: https://github.com/pycqa/flake8
rev: 4.0.1
- repo: https://github.com/executablebooks/mdformat
rev: 0.7.17
hooks:
- id: flake8
- id: mdformat
additional_dependencies:
- flake8-bugbear
- flake8-docstrings
- flake8-print==5.0.0
- flake8-use-fstring
- mdformat-frontmatter
- mdformat-gfm
- mdformat-gfm-alerts
- mdformat-tables
- repo: https://github.com/igorshubovych/markdownlint-cli
rev: v0.31.1
rev: v0.41.0
hooks:
- id: markdownlint
Loading

0 comments on commit fd06ee7

Please sign in to comment.