Skip to content

Commit d4c9bd3

Browse files
committed
check if a contributor has signed latest agreement
1 parent 18decc5 commit d4c9bd3

File tree

1 file changed

+70
-0
lines changed

1 file changed

+70
-0
lines changed
+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
"""
2+
© Ocado Group
3+
Created on 15/07/2024 at 12:52:50(+01:00).
4+
"""
5+
6+
from typing import Dict
7+
8+
import requests
9+
10+
# from codeforlife.request import Request
11+
from codeforlife.response import Response
12+
13+
# from codeforlife.user.models import User
14+
from codeforlife.views import ModelViewSet
15+
16+
# from django.http import HttpResponse
17+
from rest_framework import status
18+
19+
from ..models import AgreementSignature, Contributor
20+
21+
22+
class CheckAgreementSignatureViewSet(ModelViewSet):
23+
"""
24+
An endpoint to check if a contributor has signed latest agreement,
25+
return OKAY if he has otherwise return the latest commit ID.
26+
"""
27+
28+
def get(self, request):
29+
"""
30+
Get the latest commit id and compare with contributor's
31+
agreement signature.
32+
"""
33+
# Repo information
34+
github_id = 118008817 # TODO: Change later
35+
owner = "ocadotechnology"
36+
repo = "codeforlife-workshop"
37+
file_name = "CONTRIBUTING.md"
38+
39+
params: Dict[str, str]
40+
params = {"path": file_name, "per_page": 1}
41+
42+
url = f"https://api.github.com/repos/{owner}/{repo}/commits"
43+
44+
# Send an API request
45+
response = requests.get(url, params=params, timeout=10)
46+
47+
# Check the result of the API call
48+
if response.status_code == 200:
49+
latest_commit_id = response.json()[0]["sha"]
50+
else:
51+
return Response(
52+
status=status.HTTP_451_UNAVAILABLE_FOR_LEGAL_REASONS
53+
)
54+
55+
# Retrieve contributor
56+
contributor = Contributor.objects.get(id=github_id)
57+
if not contributor:
58+
return Response(status=status.HTTP_404_NOT_FOUND)
59+
60+
# Retrieve signature agreement IDs
61+
signatures = AgreementSignature.objects.filter(contributor=contributor)
62+
latest_signature = signatures.order_by("-signed_by").first()
63+
if not latest_signature:
64+
return Response(status=status.HTTP_404_NOT_FOUND)
65+
66+
# Compare agreement IDs
67+
if latest_commit_id == latest_signature.agreement_id:
68+
return Response(status=status.HTTP_200_OK)
69+
70+
return Response(status=status.HTTP_451_UNAVAILABLE_FOR_LEGAL_REASONS)

0 commit comments

Comments
 (0)