Skip to content
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
3 changes: 2 additions & 1 deletion webhook_server_container/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ async def process_webhook(request: Request) -> Dict[str, Any]:
return process_failed_msg

try:
ProcessGithubWehook(hook_data=hook_data, headers=request.headers)
api = ProcessGithubWehook(hook_data=hook_data, headers=request.headers)
api.process()
return {"status": requests.codes.ok, "message": "process success", "log_prefix": log_prefix}

except Exception as ex:
Expand Down
46 changes: 29 additions & 17 deletions webhook_server_container/libs/github_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,7 @@ def __init__(self, hook_data: Dict[Any, Any], headers: Headers):
self.log_prefix = self.prepare_log_prefix()
self._repo_data_from_config()

github_event: str = self.headers["X-GitHub-Event"]
event_log: str = f"Event type: {github_event}. event ID: {self.x_github_delivery}"
self.github_event: str = self.headers["X-GitHub-Event"]

self.github_app_api = get_repository_github_app_api(
config_=self.config, repository_name=self.repository_full_name
Expand Down Expand Up @@ -178,9 +177,12 @@ def __init__(self, hook_data: Dict[Any, Any], headers: Headers):
</details>
"""

if github_event == "ping":
def process(self) -> None:
if self.github_event == "ping":
return

event_log: str = f"Event type: {self.github_event}. event ID: {self.x_github_delivery}"

try:
self.pull_request = self._get_pull_request()
self.log_prefix = self.prepare_log_prefix(pull_request=self.pull_request)
Expand Down Expand Up @@ -214,21 +216,21 @@ def __init__(self, hook_data: Dict[Any, Any], headers: Headers):
f"Committer {self.parent_committer} is not in {reviewers_and_approvers}"
)

if github_event == "issue_comment":
if self.github_event == "issue_comment":
self.process_comment_webhook_data()

elif github_event == "pull_request":
elif self.github_event == "pull_request":
self.process_pull_request_webhook_data()

elif github_event == "pull_request_review":
elif self.github_event == "pull_request_review":
self.process_pull_request_review_webhook_data()

elif github_event == "check_run":
elif self.github_event == "check_run":
self.process_pull_request_check_run_webhook_data()

except NoPullRequestError:
LOGGER.info(f"{self.log_prefix} {event_log}")
if github_event == "push":
if self.github_event == "push":
self.process_push_webhook_data()

@property
Expand Down Expand Up @@ -955,19 +957,32 @@ def process_pull_request_webhook_data(self) -> None:
)

if hook_action in ("labeled", "unlabeled"):
_check_for_merge: bool = False
_reviewer: Optional[str] = None
action_labeled = hook_action == "labeled"
labeled = self.hook_data["label"]["name"].lower()
if labeled == CAN_BE_MERGED_STR:
return

LOGGER.info(f"{self.log_prefix} PR {self.pull_request.number} {hook_action} with {labeled}")
if labeled.startswith(APPROVED_BY_LABEL_PREFIX):
_reviewer = labeled.split(APPROVED_BY_LABEL_PREFIX)[-1]

if labeled.startswith(CHANGED_REQUESTED_BY_LABEL_PREFIX):
_reviewer = labeled.split(CHANGED_REQUESTED_BY_LABEL_PREFIX)[-1]

if _reviewer in self.approvers:
_check_for_merge = True

if self.verified_job and labeled == VERIFIED_LABEL_STR:
_check_for_merge = True
if action_labeled:
self.set_verify_check_success()
else:
self.set_verify_check_queued()

return self.check_if_can_be_merged()
if _check_for_merge:
self.check_if_can_be_merged()

def process_push_webhook_data(self) -> None:
tag = re.search(r"refs/tags/?(.*)", self.hook_data["ref"])
Expand Down Expand Up @@ -1029,8 +1044,6 @@ def manage_reviewed_by_label(self, review_state: str, action: str, reviewed_user
)
label_prefix = None
label_to_remove = None
check_if_can_be_merged = False

pull_request_labels = self.pull_request_labels_names()

if review_state in ("approved", LGTM_STR):
Expand All @@ -1044,11 +1057,11 @@ def manage_reviewed_by_label(self, review_state: str, action: str, reviewed_user
_remove_label = f"{CHANGED_REQUESTED_BY_LABEL_PREFIX}{reviewed_user}"
if _remove_label in pull_request_labels:
label_to_remove = _remove_label
check_if_can_be_merged = True

elif review_state == "changes_requested":
label_prefix = CHANGED_REQUESTED_BY_LABEL_PREFIX
_remove_label = f"{APPROVED_BY_LABEL_PREFIX}{reviewed_user}"

if _remove_label in pull_request_labels:
label_to_remove = _remove_label

Expand All @@ -1063,9 +1076,6 @@ def manage_reviewed_by_label(self, review_state: str, action: str, reviewed_user
if label_to_remove:
self._remove_label(label=label_to_remove)

if check_if_can_be_merged:
self.check_if_can_be_merged()

if action == DELETE_STR:
self._remove_label(label=reviewer_label)
else:
Expand Down Expand Up @@ -1198,7 +1208,8 @@ def user_commands(self, command: str, reviewed_user: str, issue_comment_id: int)

elif _command == "retest":
if self.skip_if_pull_request_already_merged():
return self.pull_request.create_issue_comment(not_running_msg)
self.pull_request.create_issue_comment(not_running_msg)
return

_target_tests: List[str] = _args.split()
for _test in _target_tests:
Expand Down Expand Up @@ -1249,7 +1260,8 @@ def user_commands(self, command: str, reviewed_user: str, issue_comment_id: int)

elif _command == WIP_STR:
if self.skip_if_pull_request_already_merged():
return self.pull_request.create_issue_comment(not_running_msg)
self.pull_request.create_issue_comment(not_running_msg)
return

self.create_comment_reaction(issue_comment_id=issue_comment_id, reaction=REACTIONS.ok)
wip_for_title: str = f"{WIP_STR.upper()}:"
Expand Down