Skip to content

Commit fe3e722

Browse files
committed
Extend CI warnings with bitbucket and azure
Also refactor into separate module
1 parent 08e535b commit fe3e722

File tree

2 files changed

+106
-63
lines changed

2 files changed

+106
-63
lines changed
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
import os
2+
import logging
3+
4+
5+
def raise_ci_warnings(repo):
6+
"""
7+
raise warnings when users use mkdocs-git-revision-date-localized-plugin
8+
on CI build runners
9+
10+
Args:
11+
repo (GitPython.git.repo): The Git repo object
12+
"""
13+
14+
if not is_shallow_clone(repo):
15+
return None
16+
17+
n_commits = commit_count(repo)
18+
19+
# Gitlab Runners
20+
if os.environ.get("GITLAB_CI") and n_commits < 50:
21+
# Default is GIT_DEPTH of 50 for gitlab
22+
logging.warning(
23+
"""
24+
[git-revision-date-localized-plugin] Running on a gitlab runner might lead to wrong git revision dates
25+
due to a shallow git fetch depth.
26+
Make sure to set GIT_DEPTH to 1000 in your .gitlab-ci.yml file.
27+
(see https://docs.gitlab.com/ee/user/project/pipelines/settings.html#git-shallow-clone).
28+
"""
29+
)
30+
31+
# Github Actions
32+
if os.environ.get("GITHUB_ACTIONS") and n_commits == 1:
33+
# Default is fetch-depth of 1 for github actions
34+
logging.warning(
35+
"""
36+
[git-revision-date-localized-plugin] Running on github actions might lead to wrong git revision dates
37+
due to a shallow git fetch depth.
38+
Try setting fetch-depth to 0 in your github action
39+
(see https://github.com/actions/checkout).
40+
"""
41+
)
42+
43+
# Bitbucket pipelines
44+
if os.environ.get("CI") and n_commits < 50:
45+
# Default is fetch-depth of 50 for bitbucket pipelines
46+
logging.warning(
47+
"""
48+
[git-revision-date-localized-plugin] Running on bitbucket pipelines might lead to wrong git revision dates
49+
due to a shallow git fetch depth.
50+
Try setting "clone: depth" to "full" in your pipeline
51+
(see https://support.atlassian.com/bitbucket-cloud/docs/configure-bitbucket-pipelinesyml/
52+
and search 'depth').
53+
"""
54+
)
55+
56+
# Azure Devops Pipeline
57+
# Does not limit fetch-depth by default
58+
if os.environ.get("Agent.Source.Git.ShallowFetchDepth", 10e99) < n_commits:
59+
logging.warning(
60+
"""
61+
[git-revision-date-localized-plugin] Running on Azure pipelines with limited fetch-depth might lead to wrong git revision dates
62+
due to a shallow git fetch depth.
63+
Remove any Shallow Fetch settings
64+
(see https://docs.microsoft.com/en-us/azure/devops/pipelines/repos/pipeline-options-for-git?view=azure-devops#shallow-fetch).
65+
"""
66+
)
67+
68+
69+
def commit_count(repo) -> bool:
70+
"""
71+
Helper function to determine the number of commits in a repository
72+
73+
Args:
74+
repo (GitPython.Repo.git): Repository
75+
76+
Returns:
77+
count (int): Number of commits
78+
"""
79+
refs = repo.for_each_ref().split("\n")
80+
refs = [x.split()[0] for x in refs]
81+
82+
counts = [int(repo.rev_list(x, count=True, first_parent=True)) for x in refs]
83+
return max(counts)
84+
85+
86+
def is_shallow_clone(repo) -> bool:
87+
"""
88+
Helper function to determine if repository
89+
is a shallow clone.
90+
91+
References & Context:
92+
https://github.com/timvink/mkdocs-git-revision-date-localized-plugin/issues/10
93+
https://stackoverflow.com/a/37203240/5525118
94+
95+
Args:
96+
repo (GitPython.Repo.git): Repository
97+
98+
Returns:
99+
bool: If a repo is shallow clone
100+
"""
101+
return os.path.exists(".git/shallow")

mkdocs_git_revision_date_localized_plugin/util.py

Lines changed: 5 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
# standard library
22
import logging
3-
import os
43
import time
54
from datetime import datetime
65

6+
from mkdocs_git_revision_date_localized_plugin.ci import raise_ci_warnings
7+
78
# 3rd party
89
from babel.dates import format_date, get_timezone
9-
from git import Repo, Git, GitCommandError, GitCommandNotFound
10+
from git import Repo, GitCommandError, GitCommandNotFound
1011

1112

1213
class Util:
@@ -33,32 +34,8 @@ def __init__(self, path: str = ".", config={}):
3334
raise
3435

3536
# Checks if user is running builds on CI
36-
# See https://github.com/timvink/mkdocs-git-revision-date-localized-plugin/issues/10
37-
if is_shallow_clone(self.repo):
38-
n_commits = commit_count(self.repo)
39-
40-
if os.environ.get("GITLAB_CI") and n_commits < 50:
41-
# Default is GIT_DEPTH of 50 for gitlab
42-
logging.warning(
43-
"""
44-
Running on a gitlab runner might lead to wrong git revision dates
45-
due to a shallow git fetch depth.
46-
Make sure to set GIT_DEPTH to 1000 in your .gitlab-ci.yml file.
47-
(see https://docs.gitlab.com/ee/user/project/pipelines/settings.html#git-shallow-clone).
48-
"""
49-
)
50-
if os.environ.get("GITHUB_ACTIONS") and n_commits == 1:
51-
# Default is fetch-depth of 1 for github actions
52-
logging.warning(
53-
"""
54-
Running on github actions might lead to wrong git revision dates
55-
due to a shallow git fetch depth.
56-
Try setting fetch-depth to 0 in your github action
57-
(see https://github.com/actions/checkout).
58-
"""
59-
)
60-
61-
# TODO add bitbucket
37+
# and raise appropriate warnings
38+
raise_ci_warnings(self.repo)
6239

6340
@staticmethod
6441
def _date_formats(
@@ -158,38 +135,3 @@ def get_revision_date_for_file(
158135
return self._date_formats(
159136
unix_timestamp=unix_timestamp, time_zone=time_zone, locale=locale
160137
)
161-
162-
163-
def is_shallow_clone(repo: Git) -> bool:
164-
"""
165-
Helper function to determine if repository
166-
is a shallow clone.
167-
168-
References:
169-
https://github.com/timvink/mkdocs-git-revision-date-localized-plugin/issues/10
170-
https://stackoverflow.com/a/37203240/5525118
171-
172-
Args:
173-
repo (git.Repo): Repository
174-
175-
Returns:
176-
bool: If a repo is shallow clone
177-
"""
178-
return os.path.exists(".git/shallow")
179-
180-
181-
def commit_count(repo: Git) -> bool:
182-
"""
183-
Helper function to determine the number of commits in a repository
184-
185-
Args:
186-
repo (git.Repo): Repository
187-
188-
Returns:
189-
count (int): Number of commits
190-
"""
191-
refs = repo.for_each_ref().split("\n")
192-
refs = [x.split()[0] for x in refs]
193-
194-
counts = [int(repo.rev_list(x, count=True, first_parent=True)) for x in refs]
195-
return max(counts)

0 commit comments

Comments
 (0)