diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 0000000..87d2920 --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,27 @@ +name: Unit Test + +on: + - push + - pull_request + +jobs: + typing: + runs-on: ubuntu-latest + steps: + - name: Check out repository + uses: actions/checkout@v4 + with: + fetch-depth: 0 + + - name: Set up Python + uses: actions/setup-python@v5 + with: + python-version: '3.10' + + - name: Install dependencies + run: | + python -m pip install --upgrade pip + python -m pip install --upgrade tox + + - name: Run tests + run: tox -e py3 diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 1b65463..7ba5db2 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,6 +1,6 @@ repos: - repo: https://github.com/pre-commit/pre-commit-hooks - rev: v4.4.0 + rev: v4.6.0 hooks: - id: check-added-large-files - id: check-json @@ -10,17 +10,17 @@ repos: - id: trailing-whitespace - repo: https://github.com/psf/black - rev: 23.1.0 + rev: 24.8.0 hooks: - id: black - repo: https://github.com/PyCQA/isort - rev: 5.12.0 + rev: 5.13.2 hooks: - id: isort - repo: https://github.com/PyCQA/flake8 - rev: 6.0.0 + rev: 7.1.1 hooks: - id: flake8 additional_dependencies: diff --git a/README.md b/README.md index 0ac52b3..1d83108 100644 --- a/README.md +++ b/README.md @@ -128,4 +128,3 @@ keys (all optional): - Proposed by: mih (6), jwodder (4), TheChymera (1), mslw (1), jsheunis (1) - Merged by: mih (6), yarikoptic (4), bpoldrack (1), christian-monch (1), jsheunis (1) - PR duration quantiles (days): [0.0, 0.0, 5.0] - diff --git a/setup.cfg b/setup.cfg index d5ca746..9723d48 100644 --- a/setup.cfg +++ b/setup.cfg @@ -41,6 +41,10 @@ install_requires = PyGithub ~= 2.0 ruamel.yaml ~= 0.15 +[options.extras_require] +test = + pytest + [options.packages.find] where = src diff --git a/src/solidation/__main__.py b/src/solidation/__main__.py index 37e550e..27b9af1 100644 --- a/src/solidation/__main__.py +++ b/src/solidation/__main__.py @@ -250,7 +250,7 @@ def to_markdown(self) -> str: if user is None: user = i.user.login s += ( - f"- [{i.title}]({i.html_url}) by {user}" + f"- [{sanitize_md(i.title)}]({i.html_url}) by {user}" f" [{i.repository.full_name}]\n" ) @@ -271,7 +271,10 @@ def to_markdown(self) -> str: if untriaged_issues: s += f"##### Untriaged issues of the last {dayscovered} days\n" for i in sorted(untriaged_issues, key=lambda x: x.created_at): - s += f"- [{i.title}]({i.html_url}) [{i.repository.full_name}]\n" + s += ( + f"- [{sanitize_md(i.title)}]({i.html_url}) " + + f"[{i.repository.full_name}]\n" + ) s += ( f"##### Max {self.config.num_oldest_prs} oldest, open, non-draft" @@ -281,7 +284,7 @@ def to_markdown(self) -> str: (p for p in self.open_prs if not p.draft), key=lambda x: x.created_at )[: self.config.num_oldest_prs]: age = now - ensure_aware(pr.created_at) - s += f"- [{pr.title}]({pr.html_url}) ({age.days} days)\n" + s += f"- [{sanitize_md(pr.title)}]({pr.html_url}) ({age.days} days)\n" n_random_ip = min(self.config.max_random_issues, len(self.open_ip)) if n_random_ip: @@ -297,7 +300,7 @@ def to_markdown(self) -> str: # issue, which slows things down considerably. continue age = now - ensure_aware(i.created_at) # type: ignore[unreachable] - s += f"- [{i.title}]({i.html_url}) ({age.days} days old)\n" + s += f"- [{sanitize_md(i.title)}]({i.html_url}) ({age.days} days old)\n" n_random_ip -= 1 if n_random_ip == 0: break @@ -437,6 +440,11 @@ def ensure_aware(dt: datetime) -> datetime: return dt.replace(tzinfo=timezone.utc) if dt.tzinfo is None else dt +def sanitize_md(s: str) -> str: + # Remove `[]` symbols to ensure correct markdown in the references + return re.sub(r"([\\\[\]])", r"\\\1", s) + + @click.command() @click.option( "-c", diff --git a/tests/test_main.py b/tests/test_main.py new file mode 100644 index 0000000..1031a05 --- /dev/null +++ b/tests/test_main.py @@ -0,0 +1,7 @@ +from solidation.__main__ import sanitize_md + + +def test_sanitize_md() -> None: + assert ( + sanitize_md(r"[gh-actions](deps): Fix \n") == r"\[gh-actions\](deps): Fix \\n" + ) diff --git a/tox.ini b/tox.ini index 740b591..ac832c7 100644 --- a/tox.ini +++ b/tox.ini @@ -1,8 +1,13 @@ [tox] -envlist = lint,typing +envlist = lint,typing,py3 isolated_build = True minversion = 3.3.0 +[testenv] +extras = test +commands = + python -m pytest -v {posargs} tests + [testenv:lint] skip_install = True deps = @@ -11,13 +16,13 @@ deps = flake8-builtins flake8-unused-arguments commands = - flake8 src + flake8 src tests [testenv:typing] deps = mypy commands = - mypy src + mypy src tests [flake8] doctests = True