Edit on GitHub

sqlmesh.integrations.github.shared

 1from __future__ import annotations
 2
 3import datetime
 4import typing as t
 5
 6from sqlmesh.core.notification_target import NotificationStatus
 7from sqlmesh.core.user import User
 8from sqlmesh.utils.pydantic import PydanticModel
 9
10if t.TYPE_CHECKING:
11    from github.Repository import Repository
12
13NOTIFICATION_STATUS_TO_EMOJI = {
14    NotificationStatus.FAILURE: ":x:",
15    NotificationStatus.WARNING: ":bangbang:",
16    NotificationStatus.INFO: ":information_source:",
17    NotificationStatus.PROGRESS: ":rocket:",
18    NotificationStatus.SUCCESS: ":white_check_mark:",
19}
20
21
22class PullRequestInfo(PydanticModel):
23    """Contains information related to a pull request that can be used to construct other objects/URLs"""
24
25    owner: str
26    repo: str
27    pr_number: int
28
29    @property
30    def full_repo_path(self) -> str:
31        return "/".join([self.owner, self.repo])
32
33    @classmethod
34    def create_from_pull_request_url(cls, pull_request_url: str) -> PullRequestInfo:
35        _, _, _, _, owner, repo, _, pr_number = pull_request_url.split("/")
36        return cls(
37            owner=owner,
38            repo=repo,
39            pr_number=pr_number,
40        )
41
42
43def add_comment_to_pr(
44    repo: Repository,
45    pull_request_info: PullRequestInfo,
46    notification_status: NotificationStatus,
47    msg: str,
48    user_to_append_to: t.Optional[User] = None,
49) -> None:
50    emoji = NOTIFICATION_STATUS_TO_EMOJI[notification_status]
51    msg = f"{datetime.datetime.utcnow().isoformat(sep=' ', timespec='seconds')} - {emoji} {msg} {emoji}"
52    issue = repo.get_issue(pull_request_info.pr_number)
53    if user_to_append_to:
54        for comment in issue.get_comments():
55            if comment.user.name == user_to_append_to.github_username:
56                comment.edit("\n".join([comment.body, msg]))
57                return
58    issue.create_comment(msg)
class PullRequestInfo(sqlmesh.utils.pydantic.PydanticModel):
23class PullRequestInfo(PydanticModel):
24    """Contains information related to a pull request that can be used to construct other objects/URLs"""
25
26    owner: str
27    repo: str
28    pr_number: int
29
30    @property
31    def full_repo_path(self) -> str:
32        return "/".join([self.owner, self.repo])
33
34    @classmethod
35    def create_from_pull_request_url(cls, pull_request_url: str) -> PullRequestInfo:
36        _, _, _, _, owner, repo, _, pr_number = pull_request_url.split("/")
37        return cls(
38            owner=owner,
39            repo=repo,
40            pr_number=pr_number,
41        )

Contains information related to a pull request that can be used to construct other objects/URLs

@classmethod
def create_from_pull_request_url( cls, pull_request_url: str) -> sqlmesh.integrations.github.shared.PullRequestInfo:
34    @classmethod
35    def create_from_pull_request_url(cls, pull_request_url: str) -> PullRequestInfo:
36        _, _, _, _, owner, repo, _, pr_number = pull_request_url.split("/")
37        return cls(
38            owner=owner,
39            repo=repo,
40            pr_number=pr_number,
41        )
Inherited Members
pydantic.main.BaseModel
BaseModel
parse_obj
parse_raw
parse_file
from_orm
construct
copy
schema
schema_json
validate
update_forward_refs
sqlmesh.utils.pydantic.PydanticModel
Config
dict
json
missing_required_fields
extra_fields
all_fields
required_fields
def add_comment_to_pr( repo: github.Repository.Repository, pull_request_info: sqlmesh.integrations.github.shared.PullRequestInfo, notification_status: sqlmesh.core.notification_target.NotificationStatus, msg: str, user_to_append_to: Optional[sqlmesh.core.user.User] = None) -> None:
44def add_comment_to_pr(
45    repo: Repository,
46    pull_request_info: PullRequestInfo,
47    notification_status: NotificationStatus,
48    msg: str,
49    user_to_append_to: t.Optional[User] = None,
50) -> None:
51    emoji = NOTIFICATION_STATUS_TO_EMOJI[notification_status]
52    msg = f"{datetime.datetime.utcnow().isoformat(sep=' ', timespec='seconds')} - {emoji} {msg} {emoji}"
53    issue = repo.get_issue(pull_request_info.pr_number)
54    if user_to_append_to:
55        for comment in issue.get_comments():
56            if comment.user.name == user_to_append_to.github_username:
57                comment.edit("\n".join([comment.body, msg]))
58                return
59    issue.create_comment(msg)