Skip to content

Commit 1d8e795

Browse files
committed
Add the logic for checking affected files
Signed-off-by: Cristian Le <[email protected]>
1 parent 0ee9868 commit 1d8e795

File tree

1 file changed

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

1 file changed

+47
-3
lines changed

packit_service/worker/checker/copr.py

+47-3
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,
@@ -140,12 +144,52 @@ def _pre_check(self) -> bool:
140144
return True
141145

142146

143-
class AreFilesChanged(Checker):
147+
class AreFilesChanged(Checker, GetCoprBuildJobHelperForIdMixin):
144148
"""
145149
Check if any files under the current package's `paths` field is changed.
146150
If not, then just skip the current copr build job.
147151
"""
148152

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

0 commit comments

Comments
 (0)