Skip to content

Commit

Permalink
add lint rule, PR title should include reference while hotfix (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucemia authored Jul 18, 2024
1 parent a2f2222 commit 5381b25
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
9 changes: 8 additions & 1 deletion src/pr_lint/linter.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from github.Label import Label
from github.PullRequest import PullRequest

from .utils import get_owner
from .utils import extract_reference_number, get_owner


def lint(pr: PullRequest) -> None:
Expand Down Expand Up @@ -37,3 +37,10 @@ def lint(pr: PullRequest) -> None:
assert len(impact_labels) == 1, "There should be exactly one Impact label"

# TODO: check if the PR title is in the correct format (e.g. commitlint convention)

# Check the PR fit the hotfix format
# get the base branch name of the PR
base_branch = pr.base.ref
if base_branch.startswith("stable/"):
# it is a hotfix
assert extract_reference_number(pr.title), "Hotfix PR should have a reference number"
10 changes: 9 additions & 1 deletion src/pr_lint/tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from ..utils import collect_emojis_from_labels, extract_owner_from_title, extract_pure_title
from ..utils import collect_emojis_from_labels, extract_owner_from_title, extract_pure_title, extract_reference_number


def test_extract_owner_from_title() -> None:
Expand All @@ -9,6 +9,14 @@ def test_extract_owner_from_title() -> None:
assert extract_owner_from_title(title) is None


def test_extract_reference_number() -> None:
title = "This is a PR title (#123)"
assert extract_reference_number(title) == "123"

title = "This is a PR title #123"
assert extract_reference_number(title) is None


def test_extract_pure_title() -> None:
title = "🚀 This is a PR title @owner"
assert extract_pure_title(title) == "This is a PR title"
Expand Down
17 changes: 17 additions & 0 deletions src/pr_lint/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,23 @@ def extract_owner_from_title(title: str) -> str | None:
return None


def extract_reference_number(title: str) -> str | None:
"""
Extract the reference number from the title.
Args:
title: The title of the pull request
Returns:
The reference number of the pull request
"""
pr_number = re.findall(r"\(#(\d+)\)", title.strip())
if pr_number:
return pr_number[0]

return None


def get_owner(pr: PullRequest) -> str | None:
"""
Get the owner of a pull request.
Expand Down

0 comments on commit 5381b25

Please sign in to comment.