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

This pull request sends email in better HTLM style. #5

Merged
merged 2 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
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
4 changes: 3 additions & 1 deletion auto_merger/email.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ def send_email(self, subject_msg, body: List[str]):
send_from = "[email protected]"
send_to = self.recipient_email
print(body)
msg = "<br>".join(body)
whole_body = "".join(body)
msg = ("<html><head><style>table, th, td {border: 1px solid black;}</style></head>"
f"<body>{whole_body}</body></html>")
print(msg)
self.mime_msg["From"] = send_from
self.mime_msg["To"] = ", ".join(send_to)
Expand Down
46 changes: 27 additions & 19 deletions auto_merger/merger.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@


class AutoMerger:
repo_data: List = []
container_name: str = ""
container_dir: Path
current_dir = os.getcwd()
Expand All @@ -55,6 +54,7 @@ def __init__(self, github_labels, blocking_labels, approvals=2):
self.pr_to_merge = {}
self.blocked_body = []
self.approval_body = []
self.repo_data: List = []

def is_correct_repo(self) -> bool:
cmd = ["gh repo view --json name"]
Expand All @@ -71,9 +71,9 @@ def get_gh_json_output(cmd):

def get_gh_pr_list(self):
cmd = ["gh pr list -s open --json number,title,labels,reviews,isDraft"]
repo_data = AutoMerger.get_gh_json_output(cmd=cmd)
for pr in repo_data:
if self.is_draft(pr):
repo_data_output = AutoMerger.get_gh_json_output(cmd=cmd)
for pr in repo_data_output:
if AutoMerger.is_draft(pr):
continue
if self.is_changes_requested(pr):
continue
Expand Down Expand Up @@ -159,20 +159,24 @@ def is_changes_requested(self, pr):
return True
return False

def is_draft(self, pr):
if pr['isDraft']:
return True
@staticmethod
def is_draft(pull_request):
if "isDraft" in pull_request:
if pull_request["isDraft"] in ["True", "true"]:
return True
return False

def check_pr_to_merge(self) -> bool:
if len(self.repo_data) == 0:
return False
for pr in self.repo_data:
if self.is_draft(pr):
if AutoMerger.is_draft(pr):
continue
self.logger.debug(f"PR status: {pr}")
if not self.check_labels_to_merge(pr):
continue
if "reviews" not in pr:
continue
approval_count = self.check_pr_approvals(pr["reviews"])
self.pr_to_merge[self.container_name] = {
"number": pr["number"],
Expand Down Expand Up @@ -240,38 +244,42 @@ def print_blocked_pull_request(self):
# Do not print anything in case we do not have PR.
if not [x for x in self.blocked_pr if self.blocked_pr[x]]:
return 0
self.blocked_body.append(f"Pull requests that are blocked by labels [{', '.join(self.blocking_labels)}]")
self.blocked_body.append(
f"Pull requests that are blocked by labels <b>[{', '.join(self.blocking_labels)}]</b><br><br>"
)

for container, pull_requests in self.blocked_pr.items():
if not pull_requests:
continue
self.blocked_body.append(f"{container}:")
self.blocked_body.append(f"<b>{container}<b>:")
self.blocked_body.append("<table><tr><th>Pull request URL</th><th>Title</th><th>Missing labels</th></tr>")
for pr in pull_requests:
blocked_labels = self.get_blocked_labels(pr["pr_dict"])
self.blocked_body.append(
f"https://github.com/sclorg/{container}/pull/{pr['number']} - "
f"[{pr['pr_dict']['title']}] -> {' '.join(blocked_labels)}"
f"<tr><td>https://github.com/sclorg/{container}/pull/{pr['number']}</td>"
f"<td>{pr['pr_dict']['title']}</td><td><p style='color:red;'>{' '.join(blocked_labels)}</p></td></tr>"
)
self.blocked_body.extend([""])
self.blocked_body.extend(["", ""])
self.blocked_body.append("</table><br><br>")
print('\n'.join(self.blocked_body))

def print_approval_pull_request(self):
# Do not print anything in case we do not have PR.
if not [x for x in self.pr_to_merge if self.pr_to_merge[x]]:
return 0
self.approval_body.append(f"Pull requests that can be merged or missing {self.approvals} approvals")
self.approval_body.append("<table><tr><th>Pull request URL</th><th>Title</th><th>Approval status</th></tr>")
for container, pr in self.pr_to_merge.items():
if not pr:
continue
if int(pr["approvals"]) >= self.approvals:
result_pr = f" -> CAN BE MERGED"
result_pr = f"CAN BE MERGED"
else:
result_pr = f" -> Missing {self.approvals-int(pr['approvals'])} APPROVAL"
result_pr = f"Missing {self.approvals-int(pr['approvals'])} APPROVAL"
self.approval_body.append(
f"https://github.com/sclorg/{container}/pull/{pr['number']} - "
f"[{pr['pr_dict']['title']}]{result_pr}"
f"<tr><td>https://github.com/sclorg/{container}/pull/{pr['number']}</td>"
f"<td>{pr['pr_dict']['title']}</td><td><p style='color:red;'>{result_pr}</p></td></tr>"
)
self.approval_body.extend(["", ""])
self.approval_body.append("</table><br>")
print('\n'.join(self.approval_body))

def send_results(self, recipients):
Expand Down
4 changes: 2 additions & 2 deletions tests/test_correct_repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

def test_get_gh_pr_correct_repo(get_repo_name):
flexmock(AutoMerger).should_receive("get_gh_json_output").and_return(get_repo_name)
auto_merger = AutoMerger()
auto_merger = AutoMerger(github_labels=[], blocking_labels=["pr/missing-review"])
auto_merger.container_name = "s2i-nodejs-container"
assert auto_merger.is_correct_repo()

Expand All @@ -16,6 +16,6 @@ def test_get_gh_pr_wrong_repo(get_repo_wrong_name):
flexmock(AutoMerger).should_receive("get_gh_json_output").and_return(
get_repo_wrong_name
)
auto_merger = AutoMerger()
auto_merger = AutoMerger(github_labels=[], blocking_labels=["pr/missing-review"])
auto_merger.container_name = "s2i-nodejs-container"
assert not auto_merger.is_correct_repo()
18 changes: 6 additions & 12 deletions tests/test_pr_status.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,44 +9,38 @@ def test_get_gh_pr_list(get_pr_missing_ci):
flexmock(AutoMerger).should_receive("get_gh_json_output").and_return(
get_pr_missing_ci
)
auto_merger = AutoMerger()
auto_merger = AutoMerger(github_labels=["READY-to-MERGE"], blocking_labels=["pr/missing-review", 'pr/failing-ci'])
auto_merger.container_name = "s2i-nodejs-container"
auto_merger.get_gh_pr_list()
assert auto_merger.repo_data
flexmock(AutoMerger).should_receive("merge_pull_requests").never()


def test_get_gh_two_pr_labels_missing(get_two_pr_missing_labels):
flexmock(AutoMerger).should_receive("get_gh_json_output").and_return(
get_two_pr_missing_labels
)
auto_merger = AutoMerger()
auto_merger = AutoMerger(github_labels=["READY-to-MERGE"], blocking_labels=["pr/missing-review", 'pr/failing-ci'])
auto_merger.container_name = "s2i-nodejs-container"
auto_merger.get_gh_pr_list()
assert auto_merger.repo_data
assert not auto_merger.check_pr_to_merge()
flexmock(AutoMerger).should_receive("merge_pull_requests").never()


def test_get_gh_pr_missing_ci(get_pr_missing_ci):
flexmock(AutoMerger).should_receive("get_gh_json_output").and_return(
get_pr_missing_ci
)
auto_merger = AutoMerger()
auto_merger = AutoMerger(github_labels=["READY-to-MERGE"], blocking_labels=["pr/missing-review", 'pr/failing-ci'])
auto_merger.container_name = "s2i-nodejs-container"
auto_merger.get_gh_pr_list()
assert auto_merger.repo_data
assert not auto_merger.check_pr_to_merge()
flexmock(AutoMerger).should_receive("merge_pull_requests").never()


def test_get_no_pr_for_merge(get_no_mr_to_check):
flexmock(AutoMerger).should_receive("get_gh_json_output").and_return(
get_no_mr_to_check
)
auto_merger = AutoMerger()
def test_get_no_pr_for_merge():
flexmock(AutoMerger).should_receive("get_gh_json_output").and_return([])
auto_merger = AutoMerger(github_labels=["READY-to-MERGE"], blocking_labels=["pr/missing-review", 'pr/failing-ci'])
auto_merger.container_name = "s2i-nodejs-container"
auto_merger.get_gh_pr_list()
assert not auto_merger.repo_data
assert not auto_merger.check_pr_to_merge()
flexmock(AutoMerger).should_receive("merge_pull_requests").never()
Loading