Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DX: lint PRs with @compwa/commitlint-config #186

Merged
merged 5 commits into from
Oct 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions .github/workflows/pr-linting.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# cspell:ignore agilepathway commitlint kode

name: PR linting
on:
pull_request:
Expand All @@ -16,7 +14,7 @@ jobs:
name: Check labels
runs-on: ubuntu-20.04
steps:
- uses: docker://agilepathway/pull-request-label-checker:latest
- uses: docker://agilepathway/pull-request-label-checker:latest # cspell:ignore agilepathway
with:
any_of: >-
🐛 Bug,✨ Feature,⚙️ Enhancement,⚠️ Interface,❗ Behavior,📝 Docs,🔨 Maintenance,🖱️ DX
Expand All @@ -28,5 +26,8 @@ jobs:
runs-on: ubuntu-20.04
steps:
- uses: actions/checkout@v3
- run: npm install @commitlint/config-conventional
- uses: JulienKode/[email protected]
- run: npm install @compwa/commitlint-config
- name: Create commitlint config
run: |
echo "module.exports = {extends: ['@compwa/commitlint-config']}" > commitlint.config.js
- uses: JulienKode/[email protected] # cspell:ignore kode
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
*.xml
*.yaml
*.yml
commitlint.config.js

# Build files
*.egg-info/
Expand Down
3 changes: 1 addition & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,7 @@
"src/repoma/.template/.cspell.json": true,
"src/repoma/.template/.gitpod.yml": true,
"src/repoma/.template/.prettierrc": true,
"src/repoma/.template/.taplo.toml": true,
"src/repoma/.template/commitlint.config.js": true
"src/repoma/.template/.taplo.toml": true
},
"yaml.schemas": {
"https://citation-file-format.github.io/1.2.0/schema.json": "CITATION.cff",
Expand Down
11 changes: 0 additions & 11 deletions commitlint.config.js

This file was deleted.

1 change: 1 addition & 0 deletions src/repoma/.github/workflows/pr-linting.yml
1 change: 0 additions & 1 deletion src/repoma/.template/commitlint.config.js

This file was deleted.

16 changes: 13 additions & 3 deletions src/repoma/check_dev_files/commitlint.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
"""Check :file:`commitlint.config.js` config file."""
from repoma.utilities import CONFIG_PATH, update_file
"""Remove :file:`commitlint.config.js` config file.

See https://github.com/ComPWA/repo-maintenance/issues/177.
"""
import os

from repoma.errors import PrecommitError


def main() -> None:
update_file(CONFIG_PATH.commitlint, in_template_folder=True)
path = "commitlint.config.js"
if not os.path.exists(path):
return
os.remove(path)
msg = f"Remove outdated {path}"
raise PrecommitError(msg)
15 changes: 14 additions & 1 deletion src/repoma/check_dev_files/github_workflows.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Check :file:`.github/workflows` folder content."""
import os
import re
import shutil
from pathlib import Path
from typing import List, Tuple

Expand All @@ -9,7 +10,7 @@
from ruamel.yaml.scalarstring import DoubleQuotedScalarString

from repoma.errors import PrecommitError
from repoma.utilities import CONFIG_PATH, REPOMA_DIR, write
from repoma.utilities import CONFIG_PATH, REPOMA_DIR, hash_file, write
from repoma.utilities.executor import Executor
from repoma.utilities.precommit import PrecommitConfig
from repoma.utilities.project_info import get_pypi_name
Expand Down Expand Up @@ -42,6 +43,7 @@ def main(
skip_tests,
test_extras,
)
executor(_update_pr_linting)
executor(_recommend_vscode_extension)
executor.finalize()

Expand Down Expand Up @@ -70,6 +72,17 @@ def update() -> None:
executor.finalize()


def _update_pr_linting() -> None:
filename = "pr-linting.yml"
input_path = REPOMA_DIR / CONFIG_PATH.github_workflow_dir / filename
output_path = CONFIG_PATH.github_workflow_dir / filename
output_path.parent.mkdir(exist_ok=True)
if not output_path.exists() or hash_file(input_path) != hash_file(output_path):
shutil.copyfile(input_path, output_path)
msg = f'Updated "{output_path}" workflow'
raise PrecommitError(msg)


def _update_ci_workflow(
allow_deprecated: bool,
doc_apt_packages: List[str],
Expand Down
2 changes: 1 addition & 1 deletion src/repoma/check_dev_files/release_drafter.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""Check :file:`commitlint.config.js` config file."""
"""Update Release Drafter Action."""
import os
from typing import Any, Dict

Expand Down
15 changes: 14 additions & 1 deletion src/repoma/utilities/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Collection of helper functions that are shared by all sub-hooks."""

import hashlib
import io
import os
import re
Expand All @@ -14,7 +15,6 @@
class _ConfigFilePaths(NamedTuple):
citation: Path = Path("CITATION.cff")
codecov: Path = Path("codecov.yml")
commitlint: Path = Path("commitlint.config.js")
cspell: Path = Path(".cspell.json")
editorconfig: Path = Path(".editorconfig")
github_workflow_dir: Path = Path(".github/workflows")
Expand All @@ -40,6 +40,19 @@ class _ConfigFilePaths(NamedTuple):
REPOMA_DIR = Path(repoma.__file__).parent.absolute()


def hash_file(path: Union[Path, str]) -> str:
# https://stackoverflow.com/a/22058673
buffer_size = 65_536
sha256 = hashlib.sha256()
with open(path, "rb") as f:
while True:
data = f.read(buffer_size)
if not data:
break
sha256.update(data)
return sha256.hexdigest()


def read(input: Union[Path, io.TextIOBase, str]) -> str: # noqa: A002
if isinstance(input, (Path, str)):
with open(input) as input_stream:
Expand Down