Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include coauthors in GitHub repos #63

Merged
merged 2 commits into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ unless you access private repositories.

For private GitHub repositories, you only need to allow read-only access to `Contents` and `Metadata` on the target repository. This could be done by setting `Read-only` access of `Permissions > Repository permissions > Contents`.

## Counting Contributors

* In GitHub repositories, the commit authors, [committers](https://stackoverflow.com/a/18754896), and [co-authors](https://docs.github.com/en/pull-requests/committing-changes-to-your-project/creating-and-editing-commits/creating-a-commit-with-multiple-authors) are counted as contributors. However, the plugin requires a GitHub token to fetch the list of co-authors. If co-authors exist but no token is provided, the plugin will show a warning and will only display the commit authors and committers.
* In GitLab repositories, only the commit authors are counted as contributors.

## Config

- `enabled` - Disables plugin if set to `False` for e.g. local builds (default: `True`)
Expand Down
56 changes: 56 additions & 0 deletions mkdocs_git_committers_plugin_2/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ def get_contributors_to_file(self, path):
if r.status_code == 200:
# Get login, url and avatar for each author. Ensure no duplicates.
res = r.json()
github_coauthors_exist = False
for commit in res:
if not self.config['gitlab_repository']:
# GitHub
Expand All @@ -121,6 +122,8 @@ def get_contributors_to_file(self, path):
'url': commit['committer']['html_url'],
'avatar': commit['committer']['avatar_url']
})
if commit['commit'] and commit['commit']['message'] and '\nCo-authored-by:' in commit['commit']['message']:
github_coauthors_exist = True
else:
# GitLab
if commit['author_name']:
Expand Down Expand Up @@ -153,6 +156,59 @@ def get_contributors_to_file(self, path):
break
else:
LOG.error("git-committers: " + str(r.status_code) + " " + r.reason)
if github_coauthors_exist:
github_coauthors_count = 0
# Get co-authors info through the GraphQL API, which is not available in the REST API
if self.auth_header is None:
LOG.warning("git-committers: Co-authors exist in commit messages but will not be added, since no GitHub token is provided. Set it under 'token' mkdocs.yml config or MKDOCS_GIT_COMMITTERS_APIKEY environment variable.")
else:
LOG.info("git-committers: fetching contributors for " + path + " using GraphQL API")
# Query GraphQL API, and get a list of unique authors
url = self.githuburl + "/graphql"
query = {
"query": """
{
repository(owner: "%s", name: "%s") {
object(expression: "%s") {
... on Commit {
history(first: 100, path: "%s") {
nodes {
authors(first: 100) {
nodes {
user {
login
name
url
avatarUrl
}
}
}
}
}
}
}
}
}
""" % (self.config['repository'].split('/')[0], self.config['repository'].split('/')[1], self.branch, path)
}
r = requests.post(url=url, json=query, headers=self.auth_header)
res = r.json()
if r.status_code == 200:
if res.get('data'):
if res['data']['repository']['object']['history']['nodes']:
for history_node in res['data']['repository']['object']['history']['nodes']:
for author_node in history_node['authors']['nodes']:
# If user is not None (GitHub user was deleted)
if author_node['user']:
if author_node['user']['login'] not in [author['login'] for author in authors]:
authors.append({'login': author_node['user']['login'],
'name': author_node['user']['name'],
'url': author_node['user']['url'],
'avatar': author_node['user']['avatarUrl']})
github_coauthors_count += 1
else:
LOG.warning("git-committers: Error from GitHub GraphQL call: " + res['errors'][0]['message'])
LOG.info(f"git-committers: added {github_coauthors_count} co-authors")
return authors
else:
LOG.error("git-committers: error fetching contributors for " + path)
Expand Down