Skip to content

Commit 6ca13cb

Browse files
committed
Throw usefull error messages when using CI runners, see #10
1 parent ff534a1 commit 6ca13cb

File tree

2 files changed

+56
-12
lines changed

2 files changed

+56
-12
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,13 @@ plugins:
3030
3131
> If you have no `plugins` entry in your config file yet, you'll likely also want to add the `search` plugin. MkDocs enables it by default if there is no `plugins` entry set.
3232

33+
### When using CI runners
34+
35+
The plugin needs access to the last commit that touched a file to be able to retrieve the date. If you build your docs using CI then you might need to change the default settings:
36+
37+
- github actions: set `fetch_depth` to `0` ([docs](https://github.com/actions/checkout))
38+
- gitlab runners: set `GIT_DEPTH` to `1000` ([docs](https://docs.gitlab.com/ee/user/project/pipelines/settings.html#git-shallow-clone))
39+
3340
## Usage
3441

3542
### In supported themes
Lines changed: 49 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import os
2+
import logging
13
from git import Git
24
from datetime import datetime
35
from babel.dates import format_date
@@ -7,24 +9,20 @@ class Util:
79
def __init__(self):
810
self.g = Git()
911

10-
def get_revision_date_for_file(self, path: str, locale: str = 'en'):
12+
@staticmethod
13+
def _date_formats(unix_timestamp, locale = 'en'):
1114
"""
12-
Determine localized date variants for a given file
15+
Returns different date formats / types.
1316
1417
Args:
15-
path (str): Location of a file that is part of a GIT repository
16-
locale (str, optional): Locale code of language to use. Defaults to 'en'.
17-
18+
unix_timestamp (datetiment): a timestamp in seconds since 1970
19+
locale (str): Locale code of language to use. Defaults to 'en'.
20+
1821
Returns:
19-
dict: localized date variants
22+
dict: different date formats
2023
"""
2124

22-
unix_timestamp = self.g.log(path, n=1, date='short', format='%at')
23-
24-
if not unix_timestamp:
25-
unix_timestamp = datetime.utcnow().timestamp()
26-
print('WARNING - %s has no git logs, using current timestamp' % path)
27-
25+
# Convert to millisecond timestamp
2826
unix_timestamp = int(unix_timestamp)
2927
timestamp_in_ms = unix_timestamp * 1000
3028

@@ -37,3 +35,42 @@ def get_revision_date_for_file(self, path: str, locale: str = 'en'):
3735
'iso_datetime' : revision_date.strftime("%Y-%m-%d %H:%M:%S"),
3836
'timeago' : "<span class='timeago' datetime='%s' locale='%s'></span>" % (timestamp_in_ms, locale)
3937
}
38+
39+
40+
def get_revision_date_for_file(self, path, locale = 'en'):
41+
"""
42+
Determine localized date variants for a given file
43+
44+
Args:
45+
path (str): Location of a file that is part of a GIT repository
46+
locale (str, optional): Locale code of language to use. Defaults to 'en'.
47+
48+
Returns:
49+
dict: localized date variants
50+
"""
51+
52+
unix_timestamp = self.g.log(path, n=1, date='short', format='%at')
53+
54+
55+
if not unix_timestamp:
56+
57+
if os.environ.get('GITLAB_CI'):
58+
raise EnvironmentError("""Cannot access git commit for '%s'.
59+
Try setting GIT_DEPTH to 1000 in your .gitlab-ci.yml file.
60+
(see https://docs.gitlab.com/ee/user/project/pipelines/settings.html#git-shallow-clone).
61+
Or disable mkdocs-git-revision-date-localized-plugin when using gitlab runners.
62+
""" % path)
63+
64+
if os.environ.get('GITHUB_ACTIONS'):
65+
raise EnvironmentError("""Cannot access git commit for '%s'.
66+
Try setting fetch-depth to 1 in your github action
67+
(see https://github.com/actions/checkout).
68+
Or disable mkdocs-git-revision-date-localized-plugin when using github actions.
69+
""" % path)
70+
71+
unix_timestamp = datetime.utcnow().timestamp()
72+
logging.warning('%s has no git logs, using current timestamp' % path)
73+
74+
75+
76+
return self._date_formats(unix_timestamp)

0 commit comments

Comments
 (0)