|
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, |
8 | 11 | ) |
9 | | -from packit_service.events import gitlab |
| 12 | +from packit_service.events import github, gitlab |
10 | 13 | from packit_service.worker.checker.abstract import ( |
11 | 14 | ActorChecker, |
12 | 15 | Checker, |
@@ -140,12 +143,51 @@ def _pre_check(self) -> bool: |
140 | 143 | return True |
141 | 144 |
|
142 | 145 |
|
143 | | -class AreFilesChanged(Checker): |
| 146 | +class AreFilesChanged(Checker, GetCoprBuildJobHelperForIdMixin, ConfigFromEventMixin): |
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 | + 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 | + |
149 | 176 | 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