generated from ocadotechnology/codeforlife-template-backend
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/development' into deploy-to-gcp
- Loading branch information
Showing
7 changed files
with
533 additions
and
14 deletions.
There are no files selected for viewing
16 changes: 16 additions & 0 deletions
16
.github/scripts/python/validate-existing-contributors/Pipfile
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
339
.github/scripts/python/validate-existing-contributors/Pipfile.lock
Large diffs are not rendered by default.
Oops, something went wrong.
123 changes: 123 additions & 0 deletions
123
.github/scripts/python/validate-existing-contributors/__main__.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters