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 all 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.8"
339 changes: 339 additions & 0 deletions .github/scripts/python/validate-existing-contributors/Pipfile.lock

Large diffs are not rendered by default.

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.

46 changes: 46 additions & 0 deletions .github/workflows/validate-existing-contributors.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
name: Validate Existing Contributors

on:
pull_request:
workflow_call:

env:
PYTHON_VERSION: 3.11
WORKING_DIR: codeforlife-contributor-backend/.github/scripts/python/validate-existing-contributors

jobs:
validate-existing-contributors:
runs-on: ubuntu-latest
steps:
- name: 🛫 Checkout Pull Request
uses: actions/checkout@v4

- name: 🔎 View Pull Request's Commits
id: view-pr
run: echo "PULL_REQUEST=$(gh pr view ${{ github.event.pull_request.number }} --json commits)" >> $GITHUB_OUTPUT
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: 🛫 Checkout Contributor Backend
uses: actions/checkout@v4
with:
repository: ocadotechnology/codeforlife-contributor-backend
ref: development
path: codeforlife-contributor-backend

- name: 🐍 Set up Python ${{ env.PYTHON_VERSION }} Environment
uses: ocadotechnology/codeforlife-workspace/.github/actions/python/setup-environment@main
with:
checkout: "false"
python-version: ${{ env.PYTHON_VERSION }}
working-directory: ${{ env.WORKING_DIR }}

- name: 📦 Install Dependencies with Pipenv
working-directory: ${{ env.WORKING_DIR }}
run: pipenv install

- name: 🕵️ Validate Existing Contributors
working-directory: ${{ env.WORKING_DIR }}
run: pipenv run python .
env:
PULL_REQUEST: ${{ steps.view-pr.outputs.PULL_REQUEST }}
12 changes: 8 additions & 4 deletions api/fixtures/contributors.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"name": "contributor1",
"location": "Hatfield",
"html_url": "https://github.com/contributor1",
"avatar_url": "https://contributor1.github.io/gravatar-url-generator/#/"
"avatar_url": "https://contributor1.github.io/gravatar-url-generator/#/",
"last_login": "2024-02-02T12:00:00Z"
}
},
{
Expand All @@ -18,7 +19,8 @@
"name": "contributor2",
"location": "Hatfield",
"html_url": "https://github.com/contributor2",
"avatar_url": "https://contributor2.github.io/gravatar-url-generator/#/"
"avatar_url": "https://contributor2.github.io/gravatar-url-generator/#/",
"last_login": "2024-02-02T12:00:00Z"
}
},
{
Expand All @@ -29,7 +31,8 @@
"name": "contributor3",
"location": "Hatfield",
"html_url": "https://github.com/contributor3",
"avatar_url": "https://contributor3.github.io/gravatar-url-generator/#/"
"avatar_url": "https://contributor3.github.io/gravatar-url-generator/#/",
"last_login": "2024-02-02T12:00:00Z"
}
},
{
Expand All @@ -40,7 +43,8 @@
"name": "contributor4",
"location": "Hatfield",
"html_url": "https://github.com/contributor4",
"avatar_url": "https://contributor4.github.io/gravatar-url-generator/#/"
"avatar_url": "https://contributor4.github.io/gravatar-url-generator/#/",
"last_login": "2024-02-02T12:00:00Z"
}
}
]
3 changes: 1 addition & 2 deletions api/views/agreement_signature.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@
from codeforlife.types import DataDict
from codeforlife.user.models import User
from codeforlife.views import ModelViewSet, action
from django.conf import settings
from rest_framework import status

import settings

from ..models import AgreementSignature
from ..serializers import AgreementSignatureSerializer

Expand Down