-
Notifications
You must be signed in to change notification settings - Fork 364
Expand file tree
/
Copy pathauthors_in_cff.py
More file actions
97 lines (69 loc) · 2.71 KB
/
authors_in_cff.py
File metadata and controls
97 lines (69 loc) · 2.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
"""Check that the authors of a PR are included in ``CITATION.cff``."""
# /// script
# requires-python = ">=3.14"
# dependencies = ["requests>=2.32"]
# ///
import logging
import os
import pathlib
import sys
import requests
logging.basicConfig(level="INFO")
GITHUB_TOKEN = os.getenv("GITHUB_TOKEN")
PR_NUMBER = os.getenv("PR_NUMBER")
REPO = os.getenv("GITHUB_REPOSITORY")
excluded_authors = {"dependabot[bot]", "pre-commit-ci[bot]", "sourcery-ai"}
def get_pr_authors() -> list[str]:
"""Get the GitHub usernames of the pull request."""
url = f"https://api.github.com/repos/{REPO}/pulls/{PR_NUMBER}/commits"
headers = {"Authorization": f"Bearer {GITHUB_TOKEN}"}
response = requests.get(url, headers=headers, timeout=15)
response.raise_for_status()
authors = {
commit["author"]["login"] for commit in response.json() if commit["author"]
}
return sorted(authors - excluded_authors)
def find_missing_github_usernames(authors: list[str]) -> list[str]:
"""Verify that all authors of a PR are included in :file:`CITATION.cff`."""
with pathlib.Path("CITATION.cff").open() as file:
lines = file.read()
return [
author
for author in authors
if f"alias: {author}" not in lines and f"- alias: {author}" not in lines
]
def main() -> None:
"""Check that all authors are included in CITATION.cff."""
authors = get_pr_authors()
missing_github_usernames = find_missing_github_usernames(authors)
if not missing_github_usernames:
msg = (
f"The authors of pull request {PR_NUMBER} for {REPO} are: "
f"{', '.join(sorted(authors))}. All authors are included "
"in CITATION.cff. ✅️"
)
logging.info(msg)
sys.exit(0)
branch = os.getenv("GITHUB_HEAD_REF")
username = missing_github_usernames[0]
instructions_to_add_author = f"""
⚠️ To ensure that you get credit for your contribution, please add the
following authors to CITATION.cff: {", ".join(missing_github_usernames)!r}
This file can be edited at:
🔗️ https://github.com/{username}/PlasmaPy/edit/{branch}/CITATION.cff
Each entry should be of the form:
- given-names: <given names>
family-names: <family names>
affiliation: <affiliation>
orcid: https://orcid.org/<ORCiD number>
alias: {username}
All fields are optional except "alias", which is the GitHub username.
🏷️ We encourage all contributors to sign up for an ORCID iD: a unique,
persistent identifier used by researchers, authors, and open source
contributors. Sign up at: https://orcid.org/register
Thank you for contributing! 🌱️
"""
logging.info(instructions_to_add_author)
sys.exit(1)
if __name__ == "__main__":
main()