Skip to content

Commit 0c617a6

Browse files
committed
Save the commit info from the webhook
Signed-off-by: Cristian Le <[email protected]>
1 parent 22eccf6 commit 0c617a6

File tree

4 files changed

+36
-8
lines changed

4 files changed

+36
-8
lines changed

packit_service/events/github/push.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
# Copyright Contributors to the Packit project.
22
# SPDX-License-Identifier: MIT
33

4-
from packit_service.service.db_project_events import AddBranchPushEventToDb
4+
from packit_service.service.db_project_events import AddBranchPushEventToDb, CommitInfo
55

66
from .abstract import GithubEvent
77

88

9+
class GithubCommitInfo(CommitInfo):
10+
pass
11+
12+
913
class Commit(AddBranchPushEventToDb, GithubEvent):
1014
def __init__(
1115
self,
@@ -15,6 +19,7 @@ def __init__(
1519
project_url: str,
1620
commit_sha: str,
1721
commit_sha_before: str,
22+
commits: list[GithubCommitInfo],
1823
):
1924
super().__init__(project_url=project_url)
2025
self.repo_namespace = repo_namespace
@@ -23,6 +28,7 @@ def __init__(
2328
self.commit_sha = commit_sha
2429
self.commit_sha_before = commit_sha_before
2530
self.identifier = git_ref
31+
self.commits = commits
2632

2733
@classmethod
2834
def event_type(cls) -> str:

packit_service/events/gitlab/push.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
# Copyright Contributors to the Packit project.
22
# SPDX-License-Identifier: MIT
33

4-
from packit_service.service.db_project_events import (
5-
AddBranchPushEventToDb,
6-
)
4+
from packit_service.service.db_project_events import AddBranchPushEventToDb, CommitInfo
75

86
from .abstract import GitlabEvent
97

108

9+
class GitlabCommitInfo(CommitInfo):
10+
pass
11+
12+
1113
class Commit(AddBranchPushEventToDb, GitlabEvent):
1214
def __init__(
1315
self,
@@ -17,6 +19,7 @@ def __init__(
1719
project_url: str,
1820
commit_sha: str,
1921
commit_sha_before: str,
22+
commits: list[GitlabCommitInfo],
2023
):
2124
super().__init__(project_url=project_url)
2225
self.repo_namespace = repo_namespace
@@ -25,6 +28,7 @@ def __init__(
2528
self.commit_sha = commit_sha
2629
self.commit_sha_before = commit_sha_before
2730
self.identifier = git_ref
31+
self.commits = commits
2832

2933
@classmethod
3034
def event_type(cls) -> str:

packit_service/service/db_project_events.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
This file contains helper classes for events.
66
"""
77

8-
from typing import Optional
8+
from typing import Optional, TypedDict
99

1010
from ogr.abstract import GitProject
1111

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

6363

64+
class CommitInfo(TypedDict, total=False):
65+
id: str
66+
title: str
67+
message: str
68+
added: list[str]
69+
modified: list[str]
70+
removed: list[str]
71+
72+
6473
class AddBranchPushEventToDb:
6574
git_ref: str
6675
repo_namespace: str
6776
repo_name: str
6877
project_url: str
6978
commit_sha: str
79+
commits: list[CommitInfo]
7080
_branch: GitBranchModel = None
7181
_event: ProjectEventModel = None
7282

packit_service/worker/parser.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from dataclasses import dataclass
1010
from datetime import datetime, timezone
1111
from os import getenv
12-
from typing import Any, Callable, ClassVar, Optional, Union
12+
from typing import Any, Callable, ClassVar, Optional, Union, cast
1313

1414
from ogr.parsing import RepoUrl, parse_git_repo
1515
from packit.config import JobConfigTriggerType
@@ -64,8 +64,9 @@ class _GitlabCommonData:
6464
project_url: str
6565
parsed_url: Optional[RepoUrl]
6666
ref: str
67-
head_commit: dict
67+
head_commit: gitlab.push.GitlabCommitInfo
6868
commit_sha_before: str
69+
commits: list[gitlab.push.GitlabCommitInfo]
6970

7071
@property
7172
def commit_sha(self) -> str:
@@ -420,7 +421,9 @@ def get_gitlab_push_common_data(event) -> _GitlabCommonData:
420421
before = event.get("before")
421422
checkout_sha = event.get("checkout_sha")
422423
actor = event.get("user_username")
423-
commits = event.get("commits", [])
424+
commits = [
425+
cast(gitlab.push.GitlabCommitInfo, commit) for commit in event.get("commits", [])
426+
]
424427
number_of_commits = event.get("total_commits_count")
425428

426429
if not Parser.is_gitlab_push_a_create_event(event):
@@ -455,6 +458,7 @@ def get_gitlab_push_common_data(event) -> _GitlabCommonData:
455458
ref=ref,
456459
head_commit=head_commit,
457460
commit_sha_before=before,
461+
commits=commits,
458462
)
459463

460464
@staticmethod
@@ -515,6 +519,7 @@ def parse_gitlab_push_event(event) -> Optional[gitlab.push.Commit]:
515519
project_url=data.project_url,
516520
commit_sha=data.commit_sha,
517521
commit_sha_before=data.commit_sha_before,
522+
commits=data.commits,
518523
)
519524

520525
@staticmethod
@@ -561,13 +566,16 @@ def parse_github_push_event(event) -> Optional[github.push.Commit]:
561566

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

569+
commits = [cast(github.push.GithubCommitInfo, commit) for commit in event.get("commits")]
570+
564571
return github.push.Commit(
565572
repo_namespace=repo_namespace,
566573
repo_name=repo_name,
567574
git_ref=ref,
568575
project_url=repo_url,
569576
commit_sha=head_commit,
570577
commit_sha_before=before,
578+
commits=commits,
571579
)
572580

573581
@staticmethod

0 commit comments

Comments
 (0)