diff --git a/webhook_server_container/libs/github_api.py b/webhook_server_container/libs/github_api.py index 8f9353e3..cde1caf3 100644 --- a/webhook_server_container/libs/github_api.py +++ b/webhook_server_container/libs/github_api.py @@ -759,6 +759,39 @@ def close_issue_for_merged_or_closed_pr(self, hook_action): issue.edit(state="closed") break + @ignore_exceptions(logger=FLASK_APP.logger) + def delete_remote_tag_for_merged_or_closed_pr(self, hook_action): + pr_tag = f"pr-{self.pull_request.number}" + # run regctl as a container: + base_regctl_command = ( + "podman run --rm --net host -v regctl-conf:/home/appuser/.regctl/ ghcr.io/regclient/regctl:latest" + ) + registry_info = self.container_repository.split("/") + registry_url = "" if len(registry_info) < 3 else registry_info[0] + # First we need to execute regctl login command before we can delete the tag: + rc, _, _ = run_command( + command=f"{base_regctl_command} registry login {registry_url} -u {self.container_repository_username} " + f"-p {self.container_repository_password}", + log_prefix=self.log_prefix, + ) + if rc: + # check if the tag exists: + rc, output, _ = run_command( + command=f"{base_regctl_command} tag ls {self.container_repository}", + log_prefix=self.log_prefix, + ) + if rc and pr_tag in output: + # delete the tag: + rc, _, _ = run_command( + command=f"{base_regctl_command} tag delete {self.container_repository}:{pr_tag}", + log_prefix=self.log_prefix, + ) + if rc: + self.pull_request.create_issue_comment(f"Successfully removed PR tag: {pr_tag}.") + else: + # login command failed add a comment to the PR that the tag was not deleted + self.pull_request.create_issue_comment(f"Failed to delete tag: {pr_tag}. Please delete it manually.") + def process_comment_webhook_data(self): if self.hook_data["action"] in ("action", "deleted"): return @@ -848,6 +881,7 @@ def process_pull_request_webhook_data(self): if hook_action == "closed": self.close_issue_for_merged_or_closed_pr(hook_action=hook_action) + self.delete_remote_tag_for_merged_or_closed_pr(hook_action=hook_action) is_merged = pull_request_data.get("merged") if self.jira_track_pr: diff --git a/webhook_server_container/utils/helpers.py b/webhook_server_container/utils/helpers.py index 19aa7c89..9f93faf4 100644 --- a/webhook_server_container/utils/helpers.py +++ b/webhook_server_container/utils/helpers.py @@ -44,11 +44,11 @@ def run_command( Args: command (str): Command to run log_prefix (str): Prefix for log messages - verify_stderr (bool, default True): Check command stderr + verify_stderr (bool, default False): Check command stderr shell (bool, default False): run subprocess with shell toggle timeout (int, optional): Command wait timeout - capture_output (bool, default False): Capture command output - check (boot, default True): If check is True and the exit code was non-zero, it raises a + capture_output (bool, default True): Capture command output + check (boot, default False): If check is True and the exit code was non-zero, it raises a CalledProcessError Returns: