From 88303f24246ff16f685fe79151c45db6cbaa7346 Mon Sep 17 00:00:00 2001 From: Jiri Kyjovsky Date: Oct 13 2022 13:58:22 +0000 Subject: frontend: add optional argument pkg_name to webhooks API If user wants to build when new tag is created and name of the tag doesn't contain copr package name, then user should use this optional pkg_name argument to match it. --- diff --git a/frontend/coprs_frontend/coprs/logic/packages_logic.py b/frontend/coprs_frontend/coprs/logic/packages_logic.py index 18c0c3b..9a515b4 100644 --- a/frontend/coprs_frontend/coprs/logic/packages_logic.py +++ b/frontend/coprs_frontend/coprs/logic/packages_logic.py @@ -1,5 +1,6 @@ import json import re +from typing import List from sqlalchemy import bindparam, Integer, func from sqlalchemy.sql import true, text @@ -13,6 +14,7 @@ from coprs import helpers from coprs.logic import users_logic from coprs.logic import builds_logic +from coprs.models import Package from copr_common.enums import StatusEnum log = app.logger @@ -131,7 +133,9 @@ class PackagesLogic(object): return package @classmethod - def get_for_webhook_rebuild(cls, copr_id, webhook_secret, clone_url, commits, ref_type, ref): + def get_for_webhook_rebuild( + cls, copr_id, webhook_secret, clone_url, commits, ref_type, ref, pkg_name: str + ) -> List[Package]: clone_url_stripped = re.sub(r'(\.git)?/*$', '', clone_url) packages = (models.Package.query.join(models.Copr) @@ -152,20 +156,23 @@ class PackagesLogic(object): if not package.copr.active_copr_chroots: continue - if cls.commits_belong_to_package(package, commits, ref_type, ref): - result += [package] + if cls._belongs_to_package(package, commits, ref_type, ref, pkg_name): + result.append(package) return result @classmethod - def commits_belong_to_package(cls, package, commits, ref_type, ref): + def _belongs_to_package( + cls, package: Package, commits, ref_type: str, ref: str, pkg_name: str + ) -> bool: if ref_type == "tag": matches = re.search(r'(.*)-[^-]+-[^-]+$', ref) - if matches and package.name != matches.group(1): - return False - else: - return True + return (matches and package.name == matches.group(1)) or package.name == pkg_name + + return cls.commits_belong_to_package(package, commits, ref) + @classmethod + def commits_belong_to_package(cls, package: Package, commits, ref: str) -> bool: committish = package.source_json_dict.get("committish") or '' if committish and not ref.endswith(committish): return False diff --git a/frontend/coprs_frontend/coprs/views/webhooks_ns/webhooks_general.py b/frontend/coprs_frontend/coprs/views/webhooks_ns/webhooks_general.py index 230596a..eae2ae3 100644 --- a/frontend/coprs_frontend/coprs/views/webhooks_ns/webhooks_general.py +++ b/frontend/coprs_frontend/coprs/views/webhooks_ns/webhooks_general.py @@ -1,3 +1,5 @@ +from typing import Optional + import flask from functools import wraps @@ -73,7 +75,8 @@ def package_name_required(route): @webhooks_ns.route("/bitbucket///", methods=["POST"]) -def webhooks_bitbucket_push(copr_id, uuid): +@webhooks_ns.route("/bitbucket////", methods=["POST"]) +def webhooks_bitbucket_push(copr_id, uuid, pkg_name: Optional[str] = None): # For the documentation of the data we receive see: # https://confluence.atlassian.com/bitbucket/event-payloads-740262817.html copr = ComplexLogic.get_copr_by_id_safe(copr_id) @@ -100,7 +103,7 @@ def webhooks_bitbucket_push(copr_id, uuid): return "Bad Request", 400 packages = PackagesLogic.get_for_webhook_rebuild( - copr_id, uuid, clone_url, commits, ref_type, ref + copr_id, uuid, clone_url, commits, ref_type, ref, pkg_name ) for package in packages: @@ -113,7 +116,8 @@ def webhooks_bitbucket_push(copr_id, uuid): @webhooks_ns.route("/github///", methods=["POST"]) -def webhooks_git_push(copr_id, uuid): +@webhooks_ns.route("/github////", methods=["POST"]) +def webhooks_git_push(copr_id: int, uuid, pkg_name: Optional[str] = None): if flask.request.headers["X-GitHub-Event"] == "ping": return "OK", 200 # For the documentation of the data we receive see: @@ -146,7 +150,9 @@ def webhooks_git_push(copr_id, uuid): except KeyError: return "Bad Request", 400 - packages = PackagesLogic.get_for_webhook_rebuild(copr_id, uuid, clone_url, commits, ref_type, ref) + packages = PackagesLogic.get_for_webhook_rebuild( + copr_id, uuid, clone_url, commits, ref_type, ref, pkg_name + ) committish = (ref if ref_type == 'tag' else payload.get('after', '')) for package in packages: @@ -159,7 +165,8 @@ def webhooks_git_push(copr_id, uuid): @webhooks_ns.route("/gitlab///", methods=["POST"]) -def webhooks_gitlab_push(copr_id, uuid): +@webhooks_ns.route("/gitlab////", methods=["POST"]) +def webhooks_gitlab_push(copr_id: int, uuid, pkg_name: Optional[str] = None): # For the documentation of the data we receive see: # https://gitlab.com/help/user/project/integrations/webhooks#events copr = ComplexLogic.get_copr_by_id_safe(copr_id) @@ -192,7 +199,9 @@ def webhooks_gitlab_push(copr_id, uuid): except KeyError: return "Bad Request", 400 - packages = PackagesLogic.get_for_webhook_rebuild(copr_id, uuid, clone_url, commits, ref_type, ref) + packages = PackagesLogic.get_for_webhook_rebuild( + copr_id, uuid, clone_url, commits, ref_type, ref, pkg_name + ) committish = (ref if ref_type == 'tag' else payload.get('after', '')) for package in packages: