Skip to content

Commit

Permalink
Merge pull request #64 from j3soon/feat/submodules
Browse files Browse the repository at this point in the history
Support non-recursive submodules for GitHub repos
  • Loading branch information
ojacques authored Oct 3, 2024
2 parents 325cf6f + e2b51d0 commit 7aa4f27
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ Note: the plugin configuration in `mkdocs.yml` still uses the original `git-comm
## Limitations
- Getting the contributors relies on what is available on GitHub or GitLab.
- For now, Git submodule is not supported and will report no contributors.
- For now, non-recursive Git submodule is supported for GitHub, while GitLab submodules and recursive submodules will report no contributors.
- GitLab users may not be properly identified. See [issue #50](https://github.com/ojacques/mkdocs-git-committers-plugin-2/issues/50)
## Usage
Expand Down
33 changes: 30 additions & 3 deletions mkdocs_git_committers_plugin_2/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import requests, json
from requests.exceptions import HTTPError
import time
import re

from mkdocs_git_committers_plugin_2.exclude import exclude

Expand Down Expand Up @@ -90,16 +91,18 @@ def on_config(self, config):
return config

# Get unique contributors for a given path
def get_contributors_to_file(self, path):
def get_contributors_to_file(self, path, submodule_repo=None):
# We already got a 401 (unauthorized) or 403 (rate limit) error, so we don't try again
if self.last_request_return_code == 403 or self.last_request_return_code == 401:
return []
if self.config['gitlab_repository']:
# REST endpoint is in the form https://gitlab.com/api/v4/projects/[project ID]/repository/commits?path=[uri-encoded-path]&ref_name=[branch]
url = self.gitlaburl + "/projects/" + str(self.config['gitlab_repository']) + "/repository/commits?path=" + requests.utils.quote(path) + "&ref_name=" + self.branch
else:
# Check git submodule
repository = submodule_repo or self.config['repository']
# REST endpoint is in the form https://api.github.com/repos/[repository]/commits?path=[uri-encoded-path]&sha=[branch]&per_page=100
url = self.githuburl + "/repos/" + self.config['repository'] + "/commits?path=" + requests.utils.quote(path) + "&sha=" + self.branch + "&per_page=100"
url = self.githuburl + "/repos/" + repository + "/commits?path=" + requests.utils.quote(path) + "&sha=" + self.branch + "&per_page=100"
authors = []
LOG.info("git-committers: fetching contributors for " + path)
r = requests.get(url=url, headers=self.auth_header)
Expand Down Expand Up @@ -228,6 +231,25 @@ def list_contributors(self, path):
# Use the last commit and get the date
last_commit_date = time.strftime("%Y-%m-%d", time.gmtime(c.authored_date))

# Check if the file is in a git submodule on GitHub
submodule_repo, path_in_submodule = None, None
if last_commit_date == "" and not self.config['gitlab_repository']:
for submodule in self.localrepo.submodules:
if submodule_repo:
break
if not path.startswith(submodule.path):
continue
match = re.match(r"https:\/\/github\.com\/([^\/]+\/[^\/.]+)", submodule.url)
if not match:
LOG.warning("git-committers: Submodule matched but will not be queried, since it isn't a GitHub repo.")
continue
path_in_submodule = path[len(submodule.path)+1:]
for c in Commit.iter_items(submodule.module(), submodule.module().head, path_in_submodule):
if not last_commit_date:
# Use the last commit and get the date
submodule_repo = match.group(1)
last_commit_date = time.strftime("%Y-%m-%d", time.gmtime(c.authored_date))

# File not committed yet
if last_commit_date == "":
last_commit_date = datetime.now().strftime("%Y-%m-%d")
Expand All @@ -241,7 +263,12 @@ def list_contributors(self, path):
return self.cache_page_authors[path]['authors'], self.cache_page_authors[path]['last_commit_date']

authors=[]
authors = self.get_contributors_to_file(path)
if not submodule_repo:
authors = self.get_contributors_to_file(path)
else:
LOG.info("git-committers: fetching submodule info for " + path + " from repository " + submodule_repo + " with path " + path_in_submodule)
authors = self.get_contributors_to_file(path_in_submodule, submodule_repo=submodule_repo)

if path not in self.cache_page_authors or self.cache_page_authors[path] != {'last_commit_date': last_commit_date, 'authors': authors}:
self.should_save_cache = True
self.cache_page_authors[path] = {'last_commit_date': last_commit_date, 'authors': authors}
Expand Down

0 comments on commit 7aa4f27

Please sign in to comment.