Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion packit_service/events/github/push.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
# Copyright Contributors to the Packit project.
# SPDX-License-Identifier: MIT

from packit_service.service.db_project_events import AddBranchPushEventToDb
from packit_service.service.db_project_events import AddBranchPushEventToDb, CommitInfo

from .abstract import GithubEvent


class GithubCommitInfo(CommitInfo):
pass


class Commit(AddBranchPushEventToDb, GithubEvent):
def __init__(
self,
Expand All @@ -15,6 +19,7 @@ def __init__(
project_url: str,
commit_sha: str,
commit_sha_before: str,
commits: list[GithubCommitInfo],
):
super().__init__(project_url=project_url)
self.repo_namespace = repo_namespace
Expand All @@ -23,6 +28,7 @@ def __init__(
self.commit_sha = commit_sha
self.commit_sha_before = commit_sha_before
self.identifier = git_ref
self.commits = commits

@classmethod
def event_type(cls) -> str:
Expand Down
10 changes: 7 additions & 3 deletions packit_service/events/gitlab/push.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
# Copyright Contributors to the Packit project.
# SPDX-License-Identifier: MIT

from packit_service.service.db_project_events import (
AddBranchPushEventToDb,
)
from packit_service.service.db_project_events import AddBranchPushEventToDb, CommitInfo

from .abstract import GitlabEvent


class GitlabCommitInfo(CommitInfo):
pass


class Commit(AddBranchPushEventToDb, GitlabEvent):
def __init__(
self,
Expand All @@ -17,6 +19,7 @@ def __init__(
project_url: str,
commit_sha: str,
commit_sha_before: str,
commits: list[GitlabCommitInfo],
):
super().__init__(project_url=project_url)
self.repo_namespace = repo_namespace
Expand All @@ -25,6 +28,7 @@ def __init__(
self.commit_sha = commit_sha
self.commit_sha_before = commit_sha_before
self.identifier = git_ref
self.commits = commits

@classmethod
def event_type(cls) -> str:
Expand Down
12 changes: 11 additions & 1 deletion packit_service/service/db_project_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
This file contains helper classes for events.
"""

from typing import Optional
from typing import Optional, TypedDict

from ogr.abstract import GitProject

Expand Down Expand Up @@ -61,12 +61,22 @@ def get_dict(self, default_dict: Optional[dict] = None) -> dict:
return result


class CommitInfo(TypedDict, total=False):
id: str
title: str
message: str
added: list[str]
modified: list[str]
removed: list[str]


class AddBranchPushEventToDb:
git_ref: str
repo_namespace: str
repo_name: str
project_url: str
commit_sha: str
commits: list[CommitInfo]
_branch: GitBranchModel = None
_event: ProjectEventModel = None

Expand Down
55 changes: 55 additions & 0 deletions packit_service/worker/checker/copr.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,17 @@
# SPDX-License-Identifier: MIT

import logging
from collections.abc import Iterable
from pathlib import Path

from packit.config import JobConfigTriggerType

from packit_service.constants import (
INTERNAL_TF_BUILDS_AND_TESTS_NOT_ALLOWED,
)
from packit_service.events import gitlab
from packit_service.events.abstract.comment import PullRequest as PRCommentEvent
from packit_service.service.db_project_events import AddBranchPushEventToDb as PushEvent
from packit_service.worker.checker.abstract import (
ActorChecker,
Checker,
Expand Down Expand Up @@ -141,3 +147,52 @@ def _pre_check(self) -> bool:
)
return False
return True


class AreFilesChanged(Checker, GetCoprBuildJobHelperForIdMixin, ConfigFromEventMixin):
"""
Check if any files under the current package's `paths` field is changed.
If not, then just skip the current copr build job.
"""

def get_files_changed(self) -> Iterable[Path]:
"""
Get the list of files changed in the current commit or the current pullrequest
"""
if self.job_config.trigger == JobConfigTriggerType.pull_request:
pr = self.project.get_pr(self.data.pr_id)
for file in pr.changes.files:
yield Path(file)
if self.job_config.trigger == JobConfigTriggerType.commit:
push_event = self.data.to_event()
assert isinstance(push_event, PushEvent)
files = set()
for commit in push_event.commits:
files |= set(commit["modified"])
files |= set(commit["added"])
yield from [Path(file) for file in files]
return
# Unexpected case
raise NotImplementedError(f"Trigger not supported: {self.job_config.trigger}")

def pre_check(self) -> bool:
if self.job_config.trigger == JobConfigTriggerType.release:
# For releases we don't do any checks
return True
if isinstance(self.data.to_event(), PRCommentEvent):
# For PR comments don't do any checks
return True
package_config = self.package_config
# The paths that we need to check for files changed
paths = package_config["paths"]
if any(root_path in paths for root_path in (".", "./")):
# Early exit if the git root was included, in which case we don't need to
# check the files changed
return True
for changed_file in self.get_files_changed():
# Main check
if any(changed_file.is_relative_to(p) for p in paths):
return True
# Should only get here if there are specific `paths` and none of the `changed_file`
# are relative to the paths there -> No relevant files changed for the package.
return False
Comment on lines +192 to +198
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, what do we do if we have an empty commit, like the Fedora rebuild commits? Should it be a special case to skip this check?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

like the Fedora rebuild commits?

those wouldn't be happening in upstream, so I don't think we have to think about that, or am I missing something?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean if they want to have that behaviour. I occasionally do that 1, but if it's documented that it is or it is not supported, than that's fine as well.

Footnotes

  1. https://github.com/LecrisUT/FedoraRPM-atuin/commit/99555ccec9f6ed94fe474767e3b88f18aa835387

2 changes: 2 additions & 0 deletions packit_service/worker/handlers/copr.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
)
from packit_service.worker.checker.abstract import Checker
from packit_service.worker.checker.copr import (
AreFilesChanged,
AreOwnerAndProjectMatchingJob,
BuildNotAlreadyStarted,
CanActorRunTestsJob,
Expand Down Expand Up @@ -115,6 +116,7 @@ def get_checkers() -> tuple[type[Checker], ...]:
IsJobConfigTriggerMatching,
IsGitForgeProjectAndEventOk,
CanActorRunTestsJob,
AreFilesChanged,
)

def _run(self) -> TaskResults:
Expand Down
14 changes: 11 additions & 3 deletions packit_service/worker/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from dataclasses import dataclass
from datetime import datetime, timezone
from os import getenv
from typing import Any, Callable, ClassVar, Optional, Union
from typing import Any, Callable, ClassVar, Optional, Union, cast

from ogr.parsing import RepoUrl, parse_git_repo
from packit.config import JobConfigTriggerType
Expand Down Expand Up @@ -68,8 +68,9 @@ class _GitlabCommonData:
project_url: str
parsed_url: Optional[RepoUrl]
ref: str
head_commit: dict
head_commit: gitlab.push.GitlabCommitInfo
commit_sha_before: str
commits: list[gitlab.push.GitlabCommitInfo]

@property
def commit_sha(self) -> str:
Expand Down Expand Up @@ -426,7 +427,9 @@ def get_gitlab_push_common_data(event) -> _GitlabCommonData:
before = event.get("before")
checkout_sha = event.get("checkout_sha")
actor = event.get("user_username")
commits = event.get("commits", [])
commits = [
cast(gitlab.push.GitlabCommitInfo, commit) for commit in event.get("commits", [])
]
number_of_commits = event.get("total_commits_count")

if not Parser.is_gitlab_push_a_create_event(event):
Expand Down Expand Up @@ -461,6 +464,7 @@ def get_gitlab_push_common_data(event) -> _GitlabCommonData:
ref=ref,
head_commit=head_commit,
commit_sha_before=before,
commits=commits,
)

@staticmethod
Expand Down Expand Up @@ -521,6 +525,7 @@ def parse_gitlab_push_event(event) -> Optional[gitlab.push.Commit]:
project_url=data.project_url,
commit_sha=data.commit_sha,
commit_sha_before=data.commit_sha_before,
commits=data.commits,
)

@staticmethod
Expand Down Expand Up @@ -567,13 +572,16 @@ def parse_github_push_event(event) -> Optional[github.push.Commit]:

repo_url = nested_get(event, "repository", "html_url")

commits = [cast(github.push.GithubCommitInfo, commit) for commit in event.get("commits")]

return github.push.Commit(
repo_namespace=repo_namespace,
repo_name=repo_name,
git_ref=ref,
project_url=repo_url,
commit_sha=head_commit,
commit_sha_before=before,
commits=commits,
)

@staticmethod
Expand Down
Loading