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

Fix searching for base build when triggering OpenScanHub scan #2518

Merged
merged 1 commit into from
Sep 12, 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
42 changes: 27 additions & 15 deletions packit_service/worker/handlers/copr.py
Original file line number Diff line number Diff line change
Expand Up @@ -646,26 +646,38 @@ def get_base_srpm_model(
base_build_owner = self.copr_build_helper.job_owner_for_job_config(
base_build_job
)

def get_srpm_build(commit_sha):
logger.debug(
f"Searching for base build for {target_branch_commit} commit "
f"in {base_build_owner}/{base_build_project_name} Copr project in our DB. "
)

builds = CoprBuildTargetModel.get_all_by(
commit_sha=commit_sha,
project_name=base_build_project_name,
owner=base_build_owner,
target="fedora-rawhide-x86_64",
status=BuildStatus.success,
)
try:
return next(iter(builds)).get_srpm_build()
except StopIteration:
return None

target_branch_commit = (
self.copr_build_helper.pull_request_object.target_branch_head_commit
)

logger.debug(
f"Searching for base build for {target_branch_commit} commit "
f"in {base_build_owner}/{base_build_project_name} Copr project in our DB. "
)

builds = CoprBuildTargetModel.get_all_by(
commit_sha=target_branch_commit,
project_name=base_build_project_name,
owner=base_build_owner,
target="fedora-rawhide-x86_64",
status=BuildStatus.success,
)
if srpm_build := get_srpm_build(target_branch_commit):
return srpm_build

try:
return next(iter(builds)).get_srpm_build()
except StopIteration:
for target_branch_commit in self.copr_build_helper.project.get_commits(
self.copr_build_helper.pull_request_object.target_branch
)[1:]:
if srpm_build := get_srpm_build(target_branch_commit):
return srpm_build
else:
logger.debug("No matching base build found in our DB.")
return None

Expand Down
36 changes: 25 additions & 11 deletions tests/unit/test_scan.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Copyright Contributors to the Packit project.
# SPDX-License-Identifier: MIT

import pytest
from flexmock import flexmock

from packit.api import PackitAPI
Expand All @@ -16,22 +17,34 @@
from packit_service.worker.helpers.build import CoprBuildJobHelper


def test_handle_scan():
@pytest.mark.parametrize(
"build_models",
[
[("abcdef", [flexmock(get_srpm_build=lambda: flexmock(url="base-srpm-url"))])],
[
("abcdef", []),
(
"fedcba",
[flexmock(get_srpm_build=lambda: flexmock(url="base-srpm-url"))],
),
],
],
)
def test_handle_scan(build_models):
srpm_mock = flexmock(url="https://some-url/my-srpm.src.rpm")
flexmock(AbstractCoprBuildEvent).should_receive("from_event_dict").and_return(
flexmock(chroot="fedora-rawhide-x86_64", build_id="123", pr_id=12)
)
flexmock(copr).should_receive("download_file").twice().and_return(True)

base_copr_model = flexmock(get_srpm_build=lambda: flexmock(url="base-srpm-url"))

flexmock(CoprBuildTargetModel).should_receive("get_all_by").with_args(
commit_sha="abcdef",
project_name="commit-project",
owner="user-123",
target="fedora-rawhide-x86_64",
status=BuildStatus.success,
).and_return([base_copr_model])
for commit_sha, models in build_models:
flexmock(CoprBuildTargetModel).should_receive("get_all_by").with_args(
commit_sha=commit_sha,
project_name="commit-project",
owner="user-123",
target="fedora-rawhide-x86_64",
status=BuildStatus.success,
).and_return(models).once()

flexmock(PackitAPI).should_receive("run_osh_build").once().and_return(
'some\nmultiline\noutput\n{"id": 123}\nand\nmore\n{"url": "scan-url"}\n'
Expand All @@ -53,7 +66,8 @@ def test_handle_scan():
project = flexmock(
get_pr=lambda pr_id: flexmock(
target_branch="main", target_branch_head_commit="abcdef"
)
),
get_commits=lambda ref: ["abcdef", "fedcba"],
)

ScanHelper(
Expand Down
Loading