Skip to content

Commit

Permalink
chore: Do not initialize unnecessary attributes in GitHubEvent. (#79)
Browse files Browse the repository at this point in the history
  • Loading branch information
BURG3R5 authored Aug 9, 2022
2 parents f2cd14e + 53d5ae8 commit a8e5b9f
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 35 deletions.
68 changes: 40 additions & 28 deletions bot/models/github/event.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
Model class that can store all relevant info about all events that the project handles.
"""
from typing import Optional

from ..link import Link
from . import Commit, EventType, Issue, PullRequest, Ref, Repository, User
Expand All @@ -25,37 +26,48 @@ class GitHubEvent:
:keyword links: List of miscellaneous links.
"""

type: EventType
repo: Repository
status: Optional[str]
issue: Optional[Issue]
pull_request: Optional[PullRequest]
ref: Optional[Ref]
user: Optional[User]
comments: Optional[list[str]]
commits: Optional[list[Commit]]
links: Optional[list[Link]]
reviewers: Optional[list[User]]

def __init__(self, event_type: EventType, repo: Repository, **kwargs):
self.type = event_type
self.repo = repo

self.status: str | None = kwargs.get("status", None)

self.issue: Issue | None = kwargs.get("issue", None)
self.pull_request: PullRequest | None = kwargs.get(
"pull_request",
None,
)
self.ref: Ref | None = kwargs.get("ref", None)
self.user: User | None = kwargs.get("user", None)

self.comments: list[str] | None = kwargs.get("comments", None)

self.commits: list[Commit] | None = kwargs.get("commits", None)
self.links: list[Link] | None = kwargs.get("links", None)
self.reviewers: list[User] | None = kwargs.get("reviewers", None)
if "status" in kwargs:
self.status = kwargs["status"]
if "issue" in kwargs:
self.issue = kwargs["issue"]
if "pull_request" in kwargs:
self.pull_request = kwargs["pull_request"]
if "ref" in kwargs:
self.ref = kwargs["ref"]
if "user" in kwargs:
self.user = kwargs["user"]
if "comments" in kwargs:
self.comments = kwargs["comments"]
if "commits" in kwargs:
self.commits = kwargs["commits"]
if "links" in kwargs:
self.links = kwargs["links"]
if "reviewers" in kwargs:
self.comments = kwargs["reviewers"]

def __str__(self):
return f"""(
type={self.type},
repo={self.repo.name},
status={self.status},
issue={self.issue.title},
pull_request={self.pull_request.title},
ref={self.ref.name},
user={self.user.name},
comments={[comment for comment in self.comments]},
commits={[commit.message for commit in self.commits]},
links={[link.url for link in self.links]},
users={[reviewer.name for reviewer in self.reviewers]},
)"""
string = ""
for var, value in vars(self).items():
string += var + "="
if isinstance(value, (list, tuple, set)):
string += str([str(v) for v in value])
else:
string += str(value)
string += ", "
return "(" + string[:-2] + ")"
7 changes: 0 additions & 7 deletions tests/github/data.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,8 @@
}
},
{
"comments": "None",
"commits": "None",
"issue": "None",
"links": "None",
"pull_request": "None",
"ref": "branch-name",
"repo": "<https://github.com/example-org/example-repo|example-org/example-repo>",
"reviewers": "None",
"status": "None",
"type": "EventType.BRANCH_CREATED",
"user": "<https://github.com/example-user|example-user>"
}
Expand Down

0 comments on commit a8e5b9f

Please sign in to comment.