Repository: PlasmaPy/PlasmaPy

Issue:
Add GitHub Action to verify that contributors are included in `credits.rst`
As a follow-up to #1599, I was thinking that it would be helpful to have a GitHub Action that would check that contributors have added themselves to the author list.  This would probably shorten the code review process in a lot of cases, such as when a PR is straightforward and ready to merge, except for adding the first-time contributor's name to the author list.  

There are a couple of possibilities for performing this check:

 - Verify that the GitHub username of the contributor show up in `docs/about/credits.rst` (most robust, since it would perform the check during every PR)
 - Verify that a first-time contributor has modified `docs/about/credits.rst` (not robust)

It's also possible that someone has implemented a GitHub Action like this already, which would be worth looking into.


Discussion:

Resolving patch:
diff --git a/.github/scripts/authors_in_cff.py b/.github/scripts/authors_in_cff.py
new file mode 100644
--- /dev/null
+++ b/.github/scripts/authors_in_cff.py
@@ -0,0 +1,78 @@
+"""Check that the authors of a PR are included in ``CITATION.cff``."""
+
+import os
+import pathlib
+import requests
+import sys
+
+from typing import Optional
+
+GITHUB_TOKEN = os.getenv("GITHUB_TOKEN")
+PR_NUMBER = os.getenv("PR_NUMBER")
+REPO = os.getenv("GITHUB_REPOSITORY")
+
+EXCLUDED_USERS = ["dependabot", "pre-commit-ci", "sourcery-ai"]
+
+
+def get_pr_authors() -> set[str]:
+    """Get the GitHub usernames of a 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 authors - set(EXCLUDED_USERS)
+
+
+def check_citation_file(authors: set[str]) -> tuple[bool, Optional[str]]:
+    """Verify that all authors of a PR are included in :file:`CITATION.cff`."""
+    with pathlib.Path("CITATION.cff").open() as file:
+        contents = file.read()
+        for author in authors:
+            if f"alias: {author}" not in contents:
+                return False, author
+    return True, None
+
+
+def main():
+    """Check that all authors are included in CITATION.cff."""
+    authors = get_pr_authors()
+    check_passed, missing_github_username = check_citation_file(authors)
+    if not check_passed:
+        branch_name = os.getenv("GITHUB_HEAD_REF")
+
+        error_message = f"""
+To ensure that you get credit for your contribution to PlasmaPy, please
+add {missing_github_username!r} as an author to CITATION.cff.
+
+The entry should be of the form:
+
+- given-names: <given names>
+  family-names: <family names>
+  affiliation: <affiliation>
+  orcid: https://orcid.org/<ORCiD number>
+  alias: {missing_github_username}
+
+This file can be edited directly on GitHub at:
+
+https://github.com/{REPO}/edit/{branch_name}/CITATION.cff
+
+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!
+"""
+
+        print(error_message)
+
+        sys.exit(1)
+
+
+if __name__ == "__main__":
+    main()


Test patch: