Skip to content

Commit

Permalink
fix the rule
Browse files Browse the repository at this point in the history
  • Loading branch information
lucemia committed Jun 14, 2024
1 parent a9acdc2 commit 907b473
Show file tree
Hide file tree
Showing 15 changed files with 87 additions and 122 deletions.
41 changes: 41 additions & 0 deletions src/pr_lint/formatter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import re

from github.PullRequest import PullRequest

emoji_pattern = re.compile(
"["
"\U0001F600-\U0001F64F" # Emoticons
"\U0001F300-\U0001F5FF" # Symbols & pictographs
"\U0001F680-\U0001F6FF" # Transport & map symbols
"\U0001F1E0-\U0001F1FF" # Flags (iOS)
"\U0001F900-\U0001F9FF" # Supplemental Symbols and Pictographs
"\U0001FA70-\U0001FAFF" # Symbols and Pictographs Extended-A
"\u2600-\u26FF" # Miscellaneous Symbols
"\u2700-\u27BF" # Dingbats
"]"
)


def format(pr: PullRequest) -> None:
"""
Format a pull request:
1. Extract emojis from labels and insert them to the PR title
Args:
pr: The pull request to format
"""

labels = pr.get_labels()
# remove all emojis from the left of title
cleaned_title = pr.title.lstrip("".join(emoji_pattern.findall(pr.title))).strip()

emojis = set()
for label in labels:
if _emjojis := emoji_pattern.findall(label.name):
emojis.update(_emjojis)

new_title = f"{''.join(emojis)} {cleaned_title}"
if new_title != pr.title:
pr.edit(title=new_title)
print(f"Updated PR title: {pr.title} -> {new_title}")
41 changes: 41 additions & 0 deletions src/pr_lint/linter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import re

from github.Label import Label
from github.PullRequest import PullRequest


def lint(pr: PullRequest) -> None:
"""
Lint a pull request:
1. checks if the PR title ends with a username (owner)
2. The username must be a collaborator
3. checks if there is exactly one Type label
4. checks if there is exactly one Impact label
Args:
pr: The pull request to lint
"""

title = pr.title
repo = pr.base.repo

pr_owners = re.findall(r"(@[\w]+)$", title)
assert pr_owners, "PR title should end with a GitHub username"
pr_owner = pr_owners[0][1:]
assert repo.has_in_collaborators(pr_owner), f"{pr_owner} is not a collaborator"

labels = pr.get_labels()

type_labels: list[Label] = []
impact_labels: list[Label] = []

for label in labels:
if "Type:" in label.name:
type_labels.append(label)
elif "Impact:" in label.name:
impact_labels.append(label)

assert len(type_labels) == 1, "There should be exactly one Type label"
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)
73 changes: 5 additions & 68 deletions src/pr_lint/main.py
Original file line number Diff line number Diff line change
@@ -1,51 +1,14 @@
import os
import re

import typer
from github import Github, Label
from github import Github

emoji_pattern = re.compile(
"["
"\U0001F600-\U0001F64F" # Emoticons
"\U0001F300-\U0001F5FF" # Symbols & pictographs
"\U0001F680-\U0001F6FF" # Transport & map symbols
"\U0001F1E0-\U0001F1FF" # Flags (iOS)
"\U0001F900-\U0001F9FF" # Supplemental Symbols and Pictographs
"\U0001FA70-\U0001FAFF" # Symbols and Pictographs Extended-A
"\u2600-\u26FF" # Miscellaneous Symbols
"\u2700-\u27BF" # Dingbats
"]+"
)
from .formatter import format
from .linter import lint

app = typer.Typer()


def extract_emoji(string: str) -> set[str]:
"""
Extracts emojis from a string
Args:
string: The string to extract emojis from
Returns:
A set of emojis
"""
return set(emoji_pattern.findall(string))


def clean_title(input_string: str) -> str:
# remove starting and ending emojis
input_string = input_string.strip().rstrip("".join(extract_emoji(input_string)))
input_string = input_string.strip().lstrip("".join(extract_emoji(input_string)))
input_string = input_string.strip()

# TODO:
# rewrite title's type and scope?
# remove starting type and scope [\w]+:
# input_string = re.sub(r"^[\w\-\(\)\.]+:", "", input_string).strip()
return input_string


@app.command()
def main() -> None:
token = os.getenv("GITHUB_TOKEN")
Expand All @@ -65,34 +28,8 @@ def main() -> None:
# Get the pull request
pr = repo.get_pull(int(pr_number))

labels = pr.get_labels()

type_labels: list[Label.Label] = []
other_labels: list[Label.Label] = []

for label in labels:
if "Type:" in label.name:
type_labels.append(label)
else:
other_labels.append(label)

assert len(type_labels) == 1, "There should be exactly one Type label"

# format new title as [Type Emoji][Title][Other Emojis]
type_emoji = "".join(extract_emoji(type_labels[0].name))
other_emojis = set()
for label in other_labels:
other_emojis.update(extract_emoji(label.name))
other_emojis_result = "".join(other_emojis)

new_title = clean_title(pr.title)
new_title = f"{type_emoji}{new_title}{other_emojis_result}"

if new_title != pr.title:
pr.edit(title=new_title)
print(f"Title changed from {pr.title} to {new_title}")
else:
print("Title is already correctly formatted")
lint(pr)
format(pr)


if __name__ == "__main__":
Expand Down
Empty file removed src/pr_lint/tests/__init__.py
Empty file.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

44 changes: 0 additions & 44 deletions src/pr_lint/tests/test_main.py

This file was deleted.

0 comments on commit 907b473

Please sign in to comment.