Skip to content

Commit

Permalink
Merge pull request #1673 from mfocko/deduce-copr-targets
Browse files Browse the repository at this point in the history
Deduce copr targets

TODO:

 Write new tests or update the old ones to cover new functionality.
 Update doc-strings where appropriate.
 Update or write new documentation in packit/packit.dev.



Fixes #1499

RELEASE NOTES BEGIN
Packit now deduces Copr targets for Copr builds when you have set your custom Copr project to be used.
RELEASE NOTES END

Reviewed-by: Tomas Tomecek <[email protected]>
  • Loading branch information
softwarefactory-project-zuul[bot] authored Sep 22, 2022
2 parents 96fb73e + 577f945 commit 0974490
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 31 deletions.
8 changes: 8 additions & 0 deletions packit_service/worker/helpers/build/build_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,14 @@ def configured_build_targets(self) -> Set[str]:
if self.job_tests and not self.job_tests.skip_build:
targets.update(self.job_tests.targets)

if (
self.job_type_build == JobType.copr_build
and (self.job_build and not self.job_build.targets)
and self.is_custom_copr_project_defined() # type: ignore
):
copr_targets = self.get_configured_targets() # type: ignore
targets.update(copr_targets)

return targets or {DEFAULT_VERSION}

@property
Expand Down
10 changes: 10 additions & 0 deletions packit_service/worker/helpers/build/copr_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -918,3 +918,13 @@ def create_copr_project_if_not_exists(self) -> str:
raise ex

return owner

def get_configured_targets(self) -> Set[str]:
"""
Get configured targets of the custom Copr project.
Returns:
Set of Copr targets configured in the custom Copr project.
"""
owner, project = self.job_owner, self.job_project
return self.api.copr_helper.get_chroots(owner=owner, project=project)
110 changes: 79 additions & 31 deletions tests/unit/test_build_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import pytest
from flexmock import flexmock

from packit.copr_helper import CoprHelper
from packit.config import PackageConfig, JobConfig, JobType, JobConfigTriggerType
from packit.config.aliases import get_build_targets
from packit.local_project import LocalProject
Expand All @@ -30,8 +31,27 @@
ONE_KOJI_TARGET_SET = {list(STABLE_KOJI_TARGETS)[0]}


def _mock_targets(jobs, job, job_type):
job_config_trigger_type, job_trigger_model_type = job_type

project_service = flexmock(instance_url="https://github.com")
return CoprBuildJobHelper(
service_config=ServiceConfig.get_service_config(),
package_config=PackageConfig(jobs=jobs),
job_config=job, # BuildHelper looks at all jobs in the end
project=flexmock(
service=project_service, namespace="packit", repo="testing_package"
),
metadata=flexmock(pr_id=None, identifier=None),
db_trigger=flexmock(
job_config_trigger_type=job_config_trigger_type,
job_trigger_model_type=job_trigger_model_type,
),
)


@pytest.mark.parametrize(
"jobs,job_config_trigger_type,build_chroots,test_chroots",
"jobs,job_type,build_chroots,test_chroots",
[
pytest.param(
[
Expand All @@ -41,7 +61,7 @@
_targets=STABLE_VERSIONS,
)
],
JobConfigTriggerType.pull_request,
(JobConfigTriggerType.pull_request, JobTriggerModelType.pull_request),
set(STABLE_VERSIONS),
set(),
id="build_with_targets",
Expand All @@ -54,7 +74,7 @@
_targets=STABLE_VERSIONS,
)
],
JobConfigTriggerType.pull_request,
(JobConfigTriggerType.pull_request, JobTriggerModelType.pull_request),
set(STABLE_VERSIONS),
set(),
id="build_with_targets&pr_comment",
Expand All @@ -67,7 +87,7 @@
_targets=STABLE_VERSIONS,
)
],
JobConfigTriggerType.release,
(JobConfigTriggerType.release, JobTriggerModelType.release),
set(STABLE_VERSIONS),
set(),
id="build_with_targets&release",
Expand All @@ -80,7 +100,7 @@
_targets=STABLE_VERSIONS,
)
],
JobConfigTriggerType.commit,
(JobConfigTriggerType.commit, JobTriggerModelType.branch_push),
set(STABLE_VERSIONS),
set(),
id="build_with_targets&push",
Expand All @@ -98,7 +118,7 @@
_targets=["different", "os", "target"],
),
],
JobConfigTriggerType.pull_request,
(JobConfigTriggerType.pull_request, JobTriggerModelType.pull_request),
set(STABLE_VERSIONS),
set(),
id="build_with_targets&pull_request_with_pr_and_push_defined",
Expand All @@ -116,7 +136,7 @@
_targets=["different", "os", "target"],
),
],
JobConfigTriggerType.pull_request,
(JobConfigTriggerType.pull_request, JobTriggerModelType.pull_request),
set(STABLE_VERSIONS),
set(),
id="build_with_targets&pr_comment_with_pr_and_push_defined",
Expand All @@ -134,7 +154,7 @@
_targets=STABLE_VERSIONS,
),
],
JobConfigTriggerType.commit,
(JobConfigTriggerType.commit, JobTriggerModelType.branch_push),
set(STABLE_VERSIONS),
set(),
id="build_with_targets&push_with_pr_and_push_defined",
Expand All @@ -146,7 +166,7 @@
trigger=JobConfigTriggerType.pull_request,
)
],
JobConfigTriggerType.pull_request,
(JobConfigTriggerType.pull_request, JobTriggerModelType.pull_request),
{"fedora-stable"},
set(),
id="build_without_targets",
Expand All @@ -158,7 +178,7 @@
trigger=JobConfigTriggerType.pull_request,
)
],
JobConfigTriggerType.pull_request,
(JobConfigTriggerType.pull_request, JobTriggerModelType.pull_request),
{"fedora-stable"},
{"fedora-stable"},
id="test_without_targets",
Expand All @@ -171,7 +191,7 @@
_targets=STABLE_VERSIONS,
)
],
JobConfigTriggerType.pull_request,
(JobConfigTriggerType.pull_request, JobTriggerModelType.pull_request),
set(STABLE_VERSIONS),
set(STABLE_VERSIONS),
id="test_with_targets",
Expand All @@ -187,7 +207,7 @@
trigger=JobConfigTriggerType.pull_request,
),
],
JobConfigTriggerType.pull_request,
(JobConfigTriggerType.pull_request, JobTriggerModelType.pull_request),
{"fedora-stable"},
{"fedora-stable"},
id="build_without_target&test_without_targets",
Expand All @@ -204,7 +224,7 @@
trigger=JobConfigTriggerType.pull_request,
),
],
JobConfigTriggerType.pull_request,
(JobConfigTriggerType.pull_request, JobTriggerModelType.pull_request),
set(STABLE_VERSIONS),
set(STABLE_VERSIONS),
id="build_with_target&test_without_targets",
Expand All @@ -221,7 +241,7 @@
_targets=STABLE_VERSIONS,
),
],
JobConfigTriggerType.pull_request,
(JobConfigTriggerType.pull_request, JobTriggerModelType.pull_request),
set(STABLE_VERSIONS),
set(STABLE_VERSIONS),
id="build_without_target&test_with_targets",
Expand All @@ -238,7 +258,7 @@
_targets=list(ONE_CHROOT_SET),
),
],
JobConfigTriggerType.pull_request,
(JobConfigTriggerType.pull_request, JobTriggerModelType.pull_request),
ONE_CHROOT_SET,
ONE_CHROOT_SET,
id="build_without_target&test_with_one_str_target",
Expand All @@ -258,7 +278,7 @@
trigger=JobConfigTriggerType.commit,
),
],
JobConfigTriggerType.commit,
(JobConfigTriggerType.commit, JobTriggerModelType.branch_push),
{"fedora-stable"},
set(),
id="build[pr+commit]&test[pr]&commit",
Expand All @@ -278,7 +298,7 @@
trigger=JobConfigTriggerType.commit,
),
],
JobConfigTriggerType.pull_request,
(JobConfigTriggerType.pull_request, JobTriggerModelType.pull_request),
{"fedora-stable"},
{"fedora-stable"},
id="build[pr+commit]&test[pr]&pr",
Expand All @@ -295,7 +315,7 @@
trigger=JobConfigTriggerType.commit,
),
],
JobConfigTriggerType.commit,
(JobConfigTriggerType.commit, JobTriggerModelType.branch_push),
{"fedora-stable"},
{"fedora-stable"},
id="build[pr+commit]&test[commit]&commit",
Expand All @@ -312,7 +332,7 @@
),
JobConfig(type=JobType.tests, trigger=JobConfigTriggerType.commit),
],
JobConfigTriggerType.pull_request,
(JobConfigTriggerType.pull_request, JobTriggerModelType.pull_request),
{"fedora-stable"},
set(),
id="build[pr+commit]&test[commit]&pr",
Expand All @@ -335,7 +355,7 @@
type=JobType.tests, trigger=JobConfigTriggerType.pull_request
),
],
JobConfigTriggerType.commit,
(JobConfigTriggerType.commit, JobTriggerModelType.branch_push),
{"fedora-stable"},
set(),
id="build[pr+commit+release]&test[pr]&commit",
Expand All @@ -352,7 +372,7 @@
_targets=list(ONE_CHROOT_SET),
),
],
JobConfigTriggerType.pull_request,
(JobConfigTriggerType.pull_request, JobTriggerModelType.pull_request),
ONE_CHROOT_SET,
ONE_CHROOT_SET,
id="build_with_mixed_build_alias",
Expand All @@ -370,22 +390,15 @@
_targets=["fedora-rawhide"],
),
],
JobConfigTriggerType.pull_request,
(JobConfigTriggerType.pull_request, JobTriggerModelType.pull_request),
set(STABLE_VERSIONS + ["fedora-rawhide"]),
{"fedora-rawhide"},
id="build_with_mixed_build_tests",
),
],
)
def test_targets(jobs, job_config_trigger_type, build_chroots, test_chroots):
copr_build_helper = CoprBuildJobHelper(
service_config=ServiceConfig.get_service_config(),
package_config=PackageConfig(jobs=jobs),
job_config=jobs[0], # BuildHelper looks at all jobs in the end
project=flexmock(),
metadata=flexmock(pr_id=None),
db_trigger=flexmock(job_config_trigger_type=job_config_trigger_type),
)
def test_targets(jobs, job_type, build_chroots, test_chroots):
copr_build_helper = _mock_targets(jobs, jobs[0], job_type)

assert copr_build_helper.package_config.jobs
assert [j.type for j in copr_build_helper.package_config.jobs]
Expand All @@ -394,6 +407,41 @@ def test_targets(jobs, job_config_trigger_type, build_chroots, test_chroots):
assert copr_build_helper.configured_tests_targets == test_chroots


def test_deduced_copr_targets():
jobs = [
JobConfig(
type=JobType.copr_build,
trigger=JobConfigTriggerType.commit,
owner="mf",
project="custom-copr-targets",
),
JobConfig(
type=JobType.copr_build,
trigger=JobConfigTriggerType.release,
),
JobConfig(
type=JobType.copr_build,
trigger=JobConfigTriggerType.pull_request,
),
JobConfig(
type=JobType.tests,
trigger=JobConfigTriggerType.commit,
),
]
job_type = (JobConfigTriggerType.commit, JobTriggerModelType.branch_push)
copr_build_helper = _mock_targets(jobs, jobs[0], job_type)
flexmock(CoprHelper).should_receive("get_chroots").with_args(
owner=jobs[0].owner,
project=jobs[0].project,
).and_return({"opensuse-tumbleweed-x86_64"})

assert copr_build_helper.package_config.jobs
assert [j.type for j in copr_build_helper.package_config.jobs]

assert copr_build_helper.configured_build_targets == {"opensuse-tumbleweed-x86_64"}
assert copr_build_helper.configured_tests_targets == {"opensuse-tumbleweed-x86_64"}


@pytest.mark.parametrize(
"jobs,job_config_trigger_type,build_targets_override,"
"tests_targets_override,build_targets,test_targets",
Expand Down

0 comments on commit 0974490

Please sign in to comment.