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

Validate contributors #19

Merged
merged 29 commits into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from 28 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
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
16 changes: 16 additions & 0 deletions .github/scripts/python/validate-existing-contributors/Pipfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[[source]]
url = "https://pypi.org/simple"
verify_ssl = true
name = "pypi"

[packages]
requests = "*"

[dev-packages]
black = "==23.1.0"
mypy = "==1.6.1"
pylint = "==3.0.2"
types-requests = "==2.31.0.10"

[requires]
python_version = "3.11"
229 changes: 229 additions & 0 deletions .github/scripts/python/validate-existing-contributors/Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

123 changes: 123 additions & 0 deletions .github/scripts/python/validate-existing-contributors/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
"""
© Ocado Group
Created on 08/01/2024 at 09:47:25(+00:00).

Validate all contributors have signed the contribution agreement.
"""

import json
import os
import typing as t

import requests

# TODO: figure out the right import later
# from .....api.models import AgreementSignature # type: ignore


PullRequest = t.Dict[str, t.Any]
Contributors = t.Set[str]

BOTS = {
"49699333+dependabot[bot]@users.noreply.github.com",
"[email protected]",
}

# Information about the repo
GH_ORG = "ocadotechnology"
GH_REPO = "codeforlife-workspace"
GH_FILE = "CONTRIBUTING.md"


def get_inputs():
"""Get script's inputs.

Returns:
A JSON object of the pull request.
"""

pull_request: PullRequest = json.loads(os.environ["PULL_REQUEST"])

return pull_request


def get_signed_contributors() -> Contributors:
"""Get the latest commit hash/ID of the contributor agreement,
and return the contributors that have signed that contribution agreement.

Returns:
A set of the contributors' email addresses.
"""

# Get the latest commit hash/ID of the contributor agreement,
param: t.Dict[str, t.Any] = {"path": GH_FILE, "per_page": 1}
response = requests.get(
url=f"https://api.github.com/repos/{GH_ORG}/{GH_REPO}/commits",
headers={"X-GitHub-Api-Version": "2022-11-28"},
params=param,
timeout=5,
)
if not response.ok:
response.raise_for_status()
data = response.json()
print("ERROR: ", data)
return set()

latest_commit_id = response.json()[0]["sha"]

# TODO: Uncomment this when database is created
# signed_contributors = AgreementSignature.objects.filter(
# latest_commit_id=latest_commit_id
# )

# contributors_emails = {
# contributor.contributor.email.lower()
# for contributor in signed_contributors
# }
# return contributors_emails

# TODO: remove dummy data once database is set up
dummy_signed_contributors = {"[email protected]"}
return dummy_signed_contributors


def assert_contributors(
pull_request: PullRequest,
signed_contributors: Contributors,
):
"""Assert that all contributors have signed the contribution agreement.

Args:
pull_request: The JSON object of the pull request.
signed_contributors: The contributors that have signed the contribution
agreement.
"""

contributors: Contributors = {
author["email"].lower()
for commit in pull_request["commits"]
for author in commit["authors"]
}

unsigned_contributors = contributors.difference(
signed_contributors.union(BOTS),
)

assert not unsigned_contributors, (
"The following contributors have not signed the agreement:"
f" {', '.join(unsigned_contributors)}."
)


def main():
"""Entry point."""

pull_request = get_inputs()

signed_contributors = get_signed_contributors()

assert_contributors(pull_request, signed_contributors)


if __name__ == "__main__":
main()
8 changes: 0 additions & 8 deletions .github/workflows/contributing.yaml

This file was deleted.

Loading
Loading