Skip to content

Commit 5deb470

Browse files
committed
Add the logic for checking affected files
Signed-off-by: Cristian Le <[email protected]>
1 parent 48eb494 commit 5deb470

File tree

1 file changed

+36
-3
lines changed
  • packit_service/worker/checker

1 file changed

+36
-3
lines changed

packit_service/worker/checker/copr.py

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
# SPDX-License-Identifier: MIT
33

44
import logging
5+
from pathlib import Path
6+
7+
from packit.config import JobConfigTriggerType
58

69
from packit_service.constants import (
710
INTERNAL_TF_BUILDS_AND_TESTS_NOT_ALLOWED,
@@ -140,12 +143,42 @@ def _pre_check(self) -> bool:
140143
return True
141144

142145

143-
class AreFilesChanged(Checker):
146+
class AreFilesChanged(Checker, GetCoprBuildJobHelperForIdMixin):
144147
"""
145148
Check if any files under the current package's `paths` field is changed.
146149
If not, then just skip the current copr build job.
147150
"""
148151

152+
def get_files_changed(self) -> list[Path]:
153+
"""
154+
Get the list of files changed in the current commit or the current pullrequest
155+
"""
156+
# Get the changes object
157+
if self.job_config.trigger == JobConfigTriggerType.pull_request:
158+
changes = self.copr_build_helper.pull_request_object.changes
159+
elif self.job_config.trigger == JobConfigTriggerType.commit:
160+
branch = self.job_config.branch or self.project.default_branch
161+
changes = self.copr_build_helper.project.get_commit(branch)
162+
else:
163+
raise AssertionError(f"Trigger not supported: {self.job_config.trigger}")
164+
# The paths here are relative to the git repo root
165+
return [Path(file) for file in changes.files]
166+
149167
def pre_check(self) -> bool:
150-
# TODO: Implement the logic to check if relevant files are changed
151-
return True
168+
if self.job_config.trigger == JobConfigTriggerType.release:
169+
# For releases we don't do any checks
170+
return True
171+
# FIXME: This is probably unnecessary
172+
package_config = self.package_config.get_package_config_for(self.job_config)
173+
# The paths that we need to check for files changed
174+
paths = package_config["paths"]
175+
# Early check if the git root was included, in which case we don't need to
176+
# check the files changed
177+
# TODO: refine this check when gitignore-like patterns are supported
178+
if "./" in paths:
179+
return True
180+
for changed_file in self.get_files_changed():
181+
# Check if any of the files changed are under the paths that are being tracked
182+
if any(changed_file.is_relative_to(p) for p in paths):
183+
return True
184+
return False

0 commit comments

Comments
 (0)