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