|
| 1 | +""" |
| 2 | +Helper functions related to continuous integration (CI). |
| 3 | +
|
| 4 | +This is because often CI runners do not have access to full git history. |
| 5 | +
|
| 6 | +Taken from https://github.com/timvink/mkdocs-git-revision-date-localized-plugin/blob/master/mkdocs_git_revision_date_localized_plugin/ci.py |
| 7 | +""" |
| 8 | + |
| 9 | +import os |
| 10 | +import logging |
| 11 | + |
| 12 | +from pathlib import Path |
| 13 | +from mkdocs_git_authors_plugin.git.command import GitCommand |
| 14 | + |
| 15 | + |
| 16 | +def raise_ci_warnings(path: str) -> None: |
| 17 | + """ |
| 18 | + Raise warnings when users use plugin on CI build runners. |
| 19 | +
|
| 20 | + Args: |
| 21 | + path (str): path to the root of the git repo |
| 22 | + """ |
| 23 | + with Path(path): |
| 24 | + if not is_shallow_clone(): |
| 25 | + return None |
| 26 | + |
| 27 | + n_commits = commit_count() |
| 28 | + |
| 29 | + |
| 30 | + # Gitlab Runners |
| 31 | + if os.getenv("GITLAB_CI") is not None and n_commits < 50: |
| 32 | + # Default is GIT_DEPTH of 50 for gitlab |
| 33 | + logging.warning( |
| 34 | + """ |
| 35 | + [git-authors] Running on a GitLab runner might lead to wrong |
| 36 | + Git revision dates due to a shallow git fetch depth. |
| 37 | +
|
| 38 | + Make sure to set GIT_DEPTH to 1000 in your .gitlab-ci.yml file |
| 39 | + (see https://docs.gitlab.com/ee/user/project/pipelines/settings.html#git-shallow-clone). |
| 40 | + """ |
| 41 | + ) |
| 42 | + |
| 43 | + # Github Actions |
| 44 | + if os.getenv("GITHUB_ACTIONS") is not None and n_commits == 1: |
| 45 | + # Default is fetch-depth of 1 for github actions |
| 46 | + logging.warning( |
| 47 | + """ |
| 48 | + [git-authors] Running on GitHub Actions might lead to wrong |
| 49 | + Git revision dates due to a shallow git fetch depth. |
| 50 | +
|
| 51 | + Try setting fetch-depth to 0 in your GitHub Action |
| 52 | + (see https://github.com/actions/checkout). |
| 53 | + """ |
| 54 | + ) |
| 55 | + |
| 56 | + # Bitbucket pipelines |
| 57 | + if os.getenv("CI") is not None and n_commits < 50: |
| 58 | + # Default is fetch-depth of 50 for bitbucket pipelines |
| 59 | + logging.warning( |
| 60 | + """ |
| 61 | + [git-authors] Running on bitbucket pipelines might lead to wrong |
| 62 | + Git revision dates due to a shallow git fetch depth. |
| 63 | +
|
| 64 | + Try setting "clone: depth" to "full" in your pipeline |
| 65 | + (see https://support.atlassian.com/bitbucket-cloud/docs/configure-bitbucket-pipelinesyml/ |
| 66 | + and search 'depth'). |
| 67 | + """ |
| 68 | + ) |
| 69 | + |
| 70 | + # Azure Devops Pipeline |
| 71 | + # Does not limit fetch-depth by default |
| 72 | + if int(os.getenv("Agent.Source.Git.ShallowFetchDepth", 10e99)) < n_commits: |
| 73 | + logging.warning( |
| 74 | + """ |
| 75 | + [git-authors] Running on Azure pipelines with limited |
| 76 | + fetch-depth might lead to wrong git revision dates due to a shallow git fetch depth. |
| 77 | +
|
| 78 | + Remove any Shallow Fetch settings |
| 79 | + (see https://docs.microsoft.com/en-us/azure/devops/pipelines/repos/pipeline-options-for-git?view=azure-devops#shallow-fetch). |
| 80 | + """ |
| 81 | + ) |
| 82 | + |
| 83 | + |
| 84 | +def commit_count() -> int: |
| 85 | + """ |
| 86 | + Determine the number of commits in a repository. |
| 87 | +
|
| 88 | + Returns: |
| 89 | + count (int): Number of commits. |
| 90 | + """ |
| 91 | + gc = GitCommand('rev-list',['--count','HEAD']) |
| 92 | + gc.run() |
| 93 | + n_commits = int(gc._stdout[0]) |
| 94 | + assert n_commits >= 0 |
| 95 | + return n_commits |
| 96 | + |
| 97 | + |
| 98 | +def is_shallow_clone() -> bool: |
| 99 | + """ |
| 100 | + Determine if repository is a shallow clone. |
| 101 | +
|
| 102 | + References & Context: |
| 103 | + https://github.com/timvink/mkdocs-git-revision-date-localized-plugin/issues/10 |
| 104 | + https://stackoverflow.com/a/37203240/5525118 |
| 105 | +
|
| 106 | + Returns: |
| 107 | + bool: If a repo is shallow clone |
| 108 | + """ |
| 109 | + return os.path.exists(".git/shallow") |
0 commit comments