Skip to content

Commit 497460a

Browse files
authored
Merge pull request #63 from j3soon/feat/coauthors
Include coauthors in GitHub repos
2 parents 39a2b29 + f09b170 commit 497460a

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@ unless you access private repositories.
5454

5555
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`.
5656

57+
## Counting Contributors
58+
59+
* 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.
60+
* In GitLab repositories, only the commit authors are counted as contributors.
61+
5762
## Config
5863

5964
- `enabled` - Disables plugin if set to `False` for e.g. local builds (default: `True`)

mkdocs_git_committers_plugin_2/plugin.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ def get_contributors_to_file(self, path):
106106
if r.status_code == 200:
107107
# Get login, url and avatar for each author. Ensure no duplicates.
108108
res = r.json()
109+
github_coauthors_exist = False
109110
for commit in res:
110111
if not self.config['gitlab_repository']:
111112
# GitHub
@@ -121,6 +122,8 @@ def get_contributors_to_file(self, path):
121122
'url': commit['committer']['html_url'],
122123
'avatar': commit['committer']['avatar_url']
123124
})
125+
if commit['commit'] and commit['commit']['message'] and '\nCo-authored-by:' in commit['commit']['message']:
126+
github_coauthors_exist = True
124127
else:
125128
# GitLab
126129
if commit['author_name']:
@@ -153,6 +156,59 @@ def get_contributors_to_file(self, path):
153156
break
154157
else:
155158
LOG.error("git-committers: " + str(r.status_code) + " " + r.reason)
159+
if github_coauthors_exist:
160+
github_coauthors_count = 0
161+
# Get co-authors info through the GraphQL API, which is not available in the REST API
162+
if self.auth_header is None:
163+
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.")
164+
else:
165+
LOG.info("git-committers: fetching contributors for " + path + " using GraphQL API")
166+
# Query GraphQL API, and get a list of unique authors
167+
url = self.githuburl + "/graphql"
168+
query = {
169+
"query": """
170+
{
171+
repository(owner: "%s", name: "%s") {
172+
object(expression: "%s") {
173+
... on Commit {
174+
history(first: 100, path: "%s") {
175+
nodes {
176+
authors(first: 100) {
177+
nodes {
178+
user {
179+
login
180+
name
181+
url
182+
avatarUrl
183+
}
184+
}
185+
}
186+
}
187+
}
188+
}
189+
}
190+
}
191+
}
192+
""" % (self.config['repository'].split('/')[0], self.config['repository'].split('/')[1], self.branch, path)
193+
}
194+
r = requests.post(url=url, json=query, headers=self.auth_header)
195+
res = r.json()
196+
if r.status_code == 200:
197+
if res.get('data'):
198+
if res['data']['repository']['object']['history']['nodes']:
199+
for history_node in res['data']['repository']['object']['history']['nodes']:
200+
for author_node in history_node['authors']['nodes']:
201+
# If user is not None (GitHub user was deleted)
202+
if author_node['user']:
203+
if author_node['user']['login'] not in [author['login'] for author in authors]:
204+
authors.append({'login': author_node['user']['login'],
205+
'name': author_node['user']['name'],
206+
'url': author_node['user']['url'],
207+
'avatar': author_node['user']['avatarUrl']})
208+
github_coauthors_count += 1
209+
else:
210+
LOG.warning("git-committers: Error from GitHub GraphQL call: " + res['errors'][0]['message'])
211+
LOG.info(f"git-committers: added {github_coauthors_count} co-authors")
156212
return authors
157213
else:
158214
LOG.error("git-committers: error fetching contributors for " + path)

0 commit comments

Comments
 (0)