Skip to content

Commit f7c6dd2

Browse files
committed
Add the logic for checking affected files
1 parent ca41716 commit f7c6dd2

File tree

1 file changed

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

1 file changed

+46
-3
lines changed

packit_service/worker/checker/copr.py

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

44
import logging
5+
from pathlib import Path
6+
from typing import Optional
7+
8+
from packit.config import JobConfigTriggerType
59

610
from packit_service.constants import (
711
INTERNAL_TF_BUILDS_AND_TESTS_NOT_ALLOWED,
@@ -139,11 +143,50 @@ def _pre_check(self) -> bool:
139143
return False
140144
return True
141145

142-
class AreFilesChanged(Checker):
146+
class AreFilesChanged(Checker, GetCoprBuildJobHelperForIdMixin):
143147
"""
144148
Check if any files under the current package's `paths` field is changed.
145149
If not, then just skip the current copr build job.
146150
"""
151+
def get_previous_git_ref(self) -> Optional[str]:
152+
"""
153+
Get the previous git ref if any.
154+
"""
155+
if self.job_config.trigger == JobConfigTriggerType.pull_request:
156+
# For PRs return the target branch
157+
return self.copr_build_helper.pull_request_object.target_branch
158+
elif self.job_config.trigger == JobConfigTriggerType.commit:
159+
# For branch commits return the effective `HEAD^` commit
160+
current_commit = self.copr_build_helper.metadata.commit_sha
161+
return f"{current_commit}^"
162+
elif self.job_config.trigger == JobConfigTriggerType.release:
163+
# For releases we don't do any checks
164+
return None
165+
166+
def get_files_changed(self, previous_git_ref: str) -> list[Path]:
167+
"""
168+
Get the list of files changed between the current git ref and the previous one.
169+
"""
170+
# FIXME: Implement the relevant git diff
171+
return []
172+
147173
def pre_check(self) -> bool:
148-
# TODO: Implement the logic to check if relevant files are changed
149-
return True
174+
# Get the previous git ref
175+
if not (previous_git_ref := self.get_previous_git_ref()):
176+
# If we don't have a previous git ref (e.g. it's a release trigger)
177+
# then we don't check the files and run regardless
178+
return True
179+
# FIXME: This is probably unnecessary
180+
package_config = self.package_config.get_package_config_for(self.job_config)
181+
# The paths that we need to check for files changed
182+
paths = package_config["paths"]
183+
# Early check if the git root was included, in which case we don't need to
184+
# check the files changed
185+
# TODO: refine this check when gitignore-like patterns are supported
186+
if "./" in paths:
187+
return True
188+
for file in self.get_files_changed(previous_git_ref):
189+
# Check if any of the files changed are under the paths that are being tracked
190+
if any(file.is_relative_to(p) for p in paths):
191+
return True
192+
return False

0 commit comments

Comments
 (0)