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

Ignore commands triggered in code #51

Open
wants to merge 2 commits into
base: v2
Choose a base branch
from
Open
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
14 changes: 11 additions & 3 deletions MoMMI/Modules/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import re
import asyncio
import subprocess
from typing import Match, Tuple, List, Optional, Any, Set, Dict, DefaultDict, cast, Union
from typing import Match, Tuple, List, Optional, Any, Set, Dict, DefaultDict, cast, Union, Iterator
from urllib.parse import quote, quote_plus
from collections import defaultdict
import aiohttp
Expand All @@ -24,6 +24,7 @@
REG_PATH = re.compile(r"\[(?:(\S+)\/\/)?(.+?)(?:(?::|#L)(\d+)(?:-L?(\d+))?)?\]", re.I)
REG_ISSUE = re.compile(r"\[(?:(\S+)#|#)?([0-9]+)\]")
REG_COMMIT = re.compile(r"\[(?:(\S+)@)?([0-9a-f]{40})\]", re.I)
REG_CODE_MARKDOWN = re.compile(r"(?<!\\)`([^`]+)(?<!\\)`")
REG_GIT_HEADER_PAGENUM = re.compile(r"[?&]page=(\d+)[^,]+rel=\"last\"")

REG_AUTOLABEL = re.compile(r"\[(\w+?)\]", re.I)
Expand Down Expand Up @@ -334,6 +335,13 @@ async def issue_auto_label(type: str, message: Any, meta: str) -> None:
# handling of stuff like [2000] and [world.dm]


def ignore_message_code_markdown(message: Message, expression: re.Pattern, iterator: Iterator[Match[str]]) -> re.Match:
for k in expression.finditer(''.join(REG_CODE_MARKDOWN.findall(message.content))):
for i in iterator:
if i.group(0) != k.group(0):
yield i


@always_command("github_issue")
async def issue_command(channel: MChannel, match: Match, message: Message) -> None:
try:
Expand All @@ -349,7 +357,7 @@ async def issue_command(channel: MChannel, match: Match, message: Message) -> No
for repo_config in cfg:
repo = repo_config["repo"]

for match in REG_ISSUE.finditer(message.content):
for match in ignore_message_code_markdown(message, REG_ISSUE, REG_ISSUE.finditer(message.content)):
#logger.debug("did match")
prefix = match.group(1)

Expand All @@ -367,7 +375,7 @@ async def issue_command(channel: MChannel, match: Match, message: Message) -> No
if messages >= GITHUB_ISSUE_MAX_MESSAGES:
return

for match in REG_COMMIT.finditer(message.content):
for match in ignore_message_code_markdown(message, REG_COMMIT, REG_COMMIT.finditer(message.content)):
prefix = match.group(1)

if not is_repo_valid_for_command(repo_config, channel, prefix):
Expand Down