|
2 | 2 | # SPDX-License-Identifier: MIT |
3 | 3 |
|
4 | 4 | import logging |
| 5 | +from pathlib import Path |
| 6 | + |
| 7 | +from packit.config import JobConfigTriggerType |
5 | 8 |
|
6 | 9 | from packit_service.constants import ( |
7 | 10 | INTERNAL_TF_BUILDS_AND_TESTS_NOT_ALLOWED, |
@@ -140,12 +143,42 @@ def _pre_check(self) -> bool: |
140 | 143 | return True |
141 | 144 |
|
142 | 145 |
|
143 | | -class AreFilesChanged(Checker): |
| 146 | +class AreFilesChanged(Checker, GetCoprBuildJobHelperForIdMixin): |
144 | 147 | """ |
145 | 148 | Check if any files under the current package's `paths` field is changed. |
146 | 149 | If not, then just skip the current copr build job. |
147 | 150 | """ |
148 | 151 |
|
| 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 | + |
149 | 167 | 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