Skip to content

Commit fd28dfc

Browse files
committed
[WIP] Implement AreFilesChanged
Signed-off-by: Cristian Le <[email protected]>
1 parent 0c617a6 commit fd28dfc

File tree

1 file changed

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

1 file changed

+46
-4
lines changed

packit_service/worker/checker/copr.py

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@
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,
811
)
9-
from packit_service.events import gitlab
12+
from packit_service.events import github, gitlab
1013
from packit_service.worker.checker.abstract import (
1114
ActorChecker,
1215
Checker,
@@ -140,12 +143,51 @@ def _pre_check(self) -> bool:
140143
return True
141144

142145

143-
class AreFilesChanged(Checker):
146+
class AreFilesChanged(Checker, GetCoprBuildJobHelperForIdMixin, ConfigFromEventMixin):
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+
pr_event = self.data.to_event()
159+
if not isinstance(pr_event, (github.pr.Action, gitlab.mr.Action)):
160+
# TODO: What about comments?
161+
raise NotImplementedError()
162+
# TODO: How to handle PRs?
163+
raise NotImplementedError()
164+
if self.job_config.trigger == JobConfigTriggerType.commit:
165+
push_event = self.data.to_event()
166+
if not isinstance(push_event, (github.push.Commit, gitlab.push.Commit)):
167+
raise NotImplementedError()
168+
files = set()
169+
for commit in push_event.commits:
170+
files |= set(commit["modified"])
171+
files |= set(commit["added"])
172+
# TODO: Check what the path is relative to
173+
return [Path(file) for file in files]
174+
raise NotImplementedError(f"Trigger not supported: {self.job_config.trigger}")
175+
149176
def pre_check(self) -> bool:
150-
# TODO: Implement the logic to check if relevant files are changed
151-
return True
177+
if self.job_config.trigger == JobConfigTriggerType.release:
178+
# For releases we don't do any checks
179+
return True
180+
# FIXME: This is probably unnecessary
181+
package_config = self.package_config.get_package_config_for(self.job_config)
182+
# The paths that we need to check for files changed
183+
paths = package_config["paths"]
184+
# Early check if the git root was included, in which case we don't need to
185+
# check the files changed
186+
# TODO: refine this check when gitignore-like patterns are supported
187+
if "./" in paths:
188+
return True
189+
for changed_file in self.get_files_changed():
190+
# Check if any of the files changed are under the paths that are being tracked
191+
if any(changed_file.is_relative_to(p) for p in paths):
192+
return True
193+
return False

0 commit comments

Comments
 (0)