-
-
Notifications
You must be signed in to change notification settings - Fork 164
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Trey <[email protected]>
- Loading branch information
Showing
7 changed files
with
270 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
from dataclasses import dataclass, fields, field | ||
from typing import Optional, Type, TypeVar, Any, TypedDict | ||
|
||
# from github import Github | ||
import github | ||
import github.Repository | ||
import github.Issue | ||
import github.PullRequest | ||
import github.Label | ||
import github.IssueComment | ||
import github.PullRequestComment | ||
import github.NamedUser | ||
from github.PaginatedList import PaginatedList | ||
|
||
T = TypeVar("T") | ||
|
||
|
||
def fill_dataclass_from_dict(dataclass_type: Type[T], data: dict) -> T | None: | ||
if not data: | ||
return None | ||
field_names = {f.name for f in fields(dataclass_type)} | ||
filtered_data = {k: v for k, v in data.items() if k in field_names} | ||
return dataclass_type(**filtered_data) | ||
|
||
|
||
@dataclass | ||
class User: | ||
id: str | ||
url: str | ||
login: str | ||
|
||
|
||
@dataclass | ||
class Comment: | ||
id: int | ||
body: str | ||
|
||
|
||
@dataclass | ||
class Issue: | ||
id: int | ||
number: int | ||
|
||
|
||
@dataclass | ||
class PullRequest: | ||
id: int | ||
number: int | ||
url: str | ||
state: str | ||
locked: bool | ||
user: User | ||
issue_url: Optional[str] = None | ||
|
||
|
||
@dataclass | ||
class Repository: | ||
id: int | ||
name: str | ||
full_name: str | ||
owner: User | ||
|
||
|
||
@dataclass | ||
class Changes: | ||
body: Optional[TypedDict("ChangeProperty", {"from": str})] = None | ||
title: Optional[TypedDict("ChangeProperty", {"from": str})] = None | ||
|
||
|
||
@dataclass | ||
class Context: | ||
event: dict | ||
action: str | ||
sender: User | ||
issue: Optional[Issue] = None | ||
pull_request: Optional[PullRequest] = None | ||
comment: Optional[Comment] = None | ||
repository: Optional[Repository] = None | ||
changes: Optional[Changes] = None | ||
|
||
|
||
@dataclass | ||
class Objects: | ||
github: github.MainClass.Github | ||
dict_context: Context | ||
repository: github.Repository.Repository | ||
sender: Optional[github.NamedUser] = None | ||
issue: Optional[github.Issue.Issue] = None | ||
pull_request: Optional[github.PullRequest.PullRequest] = None | ||
labels: PaginatedList[github.Label.Label] = None | ||
comment: Optional[github.IssueComment.IssueComment | github.PullRequestComment.PullRequestComment] = None | ||
|
||
def __post_init__(self): | ||
print("post_init") | ||
if not self.repository: | ||
print("no repository, returning") | ||
return | ||
|
||
if self.dict_context.issue: | ||
self.issue = self.repository.get_issue(self.dict_context.issue.number) | ||
|
||
if self.dict_context.pull_request: | ||
self.pull_request = self.repository.get_pull(self.dict_context.pull_request.number) | ||
|
||
if self.pull_request: | ||
self.labels = self.pull_request.get_labels() | ||
|
||
self.sender = self.github.get_user(self.dict_context.sender.login) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import base64 | ||
|
||
|
||
def encode_private_key(entire_key: str) -> str: | ||
return base64.b64encode(entire_key.encode("ascii")).decode("ascii") | ||
|
||
|
||
def decode_private_key(raw_private_key) -> str: | ||
return base64.b64decode(raw_private_key.encode("ascii")).decode("ascii") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import os | ||
import re | ||
from textwrap import dedent | ||
import logging | ||
import secrets | ||
|
||
import github.Issue | ||
|
||
from re import match | ||
|
||
import string | ||
|
||
import random | ||
|
||
logger = logging.getLogger(__name__) | ||
if os.environ.get("AWS_EXECUTION_ENV") is not None: | ||
import _types | ||
else: | ||
from .. import _types | ||
|
||
|
||
def title_handler(context_dicts: _types.Context, context_objs: _types.Objects) -> None: | ||
if not re.match(r"^(bug|idea|implement|cleanup):\s*\S.*", context_objs.issue.title): | ||
logger.info(f"Regex title doesn't match. {context_objs.issue.title} doesnt start with bug|idea|implement|cleanup:") | ||
logger.info(f"Commenting on {context_objs.issue.html_url}") | ||
context_objs.issue.create_comment( | ||
dedent( | ||
f""" | ||
Hi @{context_objs.sender.login}, | ||
You have chosen a title that is slightly different to our title standards. Please, if possible, use the format: | ||
(bug, idea, implement or cleanup) : Title | ||
e.g. "bug: xyz page doesn't work" | ||
> If you would like to ignore this message, please reply with the reference `DELREPLY- | ||
{''.join(random.choices(string.ascii_uppercase + string.digits, k=8))}` (you may delete this reply afterwards) | ||
""" | ||
) | ||
) | ||
|
||
|
||
def delete_reply_handler(context_dicts: _types.Context, context_objs: _types.Objects) -> None: | ||
match = re.search(r"DELREPLY-(.{8})", context_dicts.comment.body) | ||
|
||
if not match: | ||
return | ||
|
||
logger.info("Deleting comment due to DELREPLY in body") | ||
|
||
reference_code = match.group(1) | ||
|
||
logger.debug(f"Deleting comment with reference code: {reference_code}") | ||
|
||
context_objs.issue: github.Issue.Issue | ||
|
||
for comment in context_objs.issue.get_comments(): | ||
if f"DELREPLY-{reference_code}" in comment.body.upper(): | ||
# comment.delete() # delete users reply comment | ||
context_objs.issue.get_comment(context_dicts.comment.id).delete() # delete bots comment | ||
break | ||
|
||
|
||
def handler(context_dicts: _types.Context, context_objs: _types.Objects) -> None: | ||
logger.info(f"action: {context_dicts.action}") | ||
match context_dicts.action: | ||
case "opened": | ||
logger.info("Using title handler due to opened issue") | ||
title_handler(context_dicts, context_objs) | ||
case "edited": | ||
if context_dicts.changes.title and context_dicts.changes.title["from"]: | ||
title_handler(context_dicts, context_objs) | ||
case "created": | ||
if context_dicts.comment: | ||
delete_reply_handler(context_dicts, context_objs) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
def add_label(): | ||
if not msg_len == 2: | ||
send_error( | ||
selected_obj, | ||
sender=SENDER["login"], | ||
body=COMMENT["body"], | ||
msg_len=msg_len, | ||
required=2, | ||
example_cmd="add_label bug", | ||
) | ||
|
||
return g.close() | ||
|
||
selected_obj.add_to_labels(msg_stripped[1]) | ||
selected_obj.create_comment(f"Okay @{SENDER['login']}, I have added the label '{msg_stripped[1]}'") |
Oops, something went wrong.