Skip to content

Commit ce60e69

Browse files
move latest commit logic to Repository model property
1 parent 2c38bf0 commit ce60e69

File tree

3 files changed

+13
-3
lines changed

3 files changed

+13
-3
lines changed

backend/apps/github/common.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,9 @@ def sync_repository(
7474
year_ago = timezone.now() - td(days=365)
7575

7676
# GitHub repository commits.
77-
latest_commit = (
78-
Commit.objects.filter(repository=repository).order_by("-created_at").first()
77+
since = (
78+
latest_commit.created_at if (latest_commit := repository.latest_commit) else year_ago
7979
)
80-
since = latest_commit.created_at if latest_commit else year_ago
8180

8281
commits_to_save = []
8382
for gh_commit in gh_repository.get_commits(since=since):

backend/apps/github/models/repository.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,16 @@ def __str__(self):
113113
"""
114114
return self.path
115115

116+
@property
117+
def latest_commit(self):
118+
"""Get the latest commit for the repository.
119+
120+
Returns
121+
Commit: The most recently created commit.
122+
123+
"""
124+
return self.commits.order_by("-created_at").first()
125+
116126
@property
117127
def latest_pull_request(self):
118128
"""Get the latest pull request for the repository.

backend/tests/apps/github/common_test.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ def mock_common_deps(mocker):
3535
mock_repository.latest_updated_milestone = None
3636
mock_repository.latest_updated_issue = None
3737
mock_repository.latest_updated_pull_request = None
38+
mock_repository.latest_commit = None
3839
mock_repository.id = 1
3940

4041
return mocks

0 commit comments

Comments
 (0)