ClickHouse/tests/ci/rerun_helper.py

55 lines
1.8 KiB
Python
Raw Normal View History

2021-12-01 14:23:51 +00:00
#!/usr/bin/env python3
from typing import List, Optional
2021-12-01 14:23:51 +00:00
from commit_status_helper import get_commit
from github import Github
from github.CommitStatus import CommitStatus
from pr_info import PRInfo
2021-12-01 14:23:51 +00:00
CommitStatuses = List[CommitStatus]
2021-12-01 14:23:51 +00:00
class RerunHelper:
def __init__(self, gh: Github, pr_info: PRInfo, check_name: str):
2021-12-01 14:23:51 +00:00
self.gh = gh
self.pr_info = pr_info
self.check_name = check_name
commit = get_commit(gh, self.pr_info.sha)
if commit is None:
raise ValueError(f"unable to receive commit for {pr_info.sha}")
self.pygh_commit = commit
self.statuses = self.ger_filtered_statuses()
2021-12-01 14:23:51 +00:00
def is_already_finished_by_status(self) -> bool:
2021-12-01 14:23:51 +00:00
# currently we agree even for failed statuses
for status in self.statuses:
if self.check_name in status.context and status.state in (
"success",
"failure",
):
2021-12-01 14:23:51 +00:00
return True
return False
2022-07-18 00:22:32 +00:00
def get_finished_status(self) -> Optional[CommitStatus]:
2022-07-18 00:22:32 +00:00
for status in self.statuses:
if self.check_name in status.context:
return status
return None
def ger_filtered_statuses(self) -> CommitStatuses:
"""
Squash statuses to latest state
1. context="first", state="success", update_time=1
2. context="second", state="success", update_time=2
3. context="first", stat="failure", update_time=3
=========>
1. context="second", state="success"
2. context="first", stat="failure"
"""
filt = {}
for status in sorted(
self.pygh_commit.get_statuses(), key=lambda x: x.updated_at
):
filt[status.context] = status
return list(filt.values())