From 89321dc685fbc4f471e3ef77a592bf3ab4817596 Mon Sep 17 00:00:00 2001 From: Christopher Liu Date: Sun, 22 Jan 2023 12:57:45 -0500 Subject: [PATCH 1/6] Add GH API experiment --- experiment.py | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 experiment.py diff --git a/experiment.py b/experiment.py new file mode 100644 index 0000000..3f989e9 --- /dev/null +++ b/experiment.py @@ -0,0 +1,41 @@ +import json +import requests + +PRODUCTS_TO_URL = { + "website": "pennlabs.org/", + "platform": "platform.pennlabs.org/", + "penn-clubs": "pennclubs.com/", +} + + +def get_pulls() -> list: + headers = {"Authorization": "GH_PERSONAL_ACCESS_TOKEN"} + pulls = [] + + for product, product_url in PRODUCTS_TO_URL.items(): + url = f"https://api.github.com/repos/pennlabs/{product}/pulls" + r = requests.get(url, headers=headers) + if r.status_code != 200: + print(f"Error: Request returned status code {r.status_code}") + return + + for pull in r.json(): + if "labels" not in pull: + continue + for label in pull["labels"]: + if "name" in label and label["name"] == "dependencies": + # if "name" in label and label["name"].startswith("feature-branch:"): + pulls.append( + { + "url": f"https://pr-{pull['number']}.{product_url}", + "status": "STATUS" + # "status": label["name"].split(":")[1] + } + ) + break + return pulls + + +if __name__ == "__main__": + for pull in get_pulls(): + print(pull) From d4c5de977a19d48bc10cf82e30ada994a2fa7151 Mon Sep 17 00:00:00 2001 From: Christopher Liu Date: Fri, 27 Jan 2023 18:01:53 -0500 Subject: [PATCH 2/6] Add pulls view --- backend/Platform/settings/base.py | 3 ++ backend/Platform/urls.py | 1 + backend/monitor/__init__.py | 0 backend/monitor/apps.py | 5 ++++ backend/monitor/urls.py | 10 +++++++ backend/monitor/views.py | 46 +++++++++++++++++++++++++++++++ experiment.py | 41 --------------------------- 7 files changed, 65 insertions(+), 41 deletions(-) create mode 100644 backend/monitor/__init__.py create mode 100644 backend/monitor/apps.py create mode 100644 backend/monitor/urls.py create mode 100644 backend/monitor/views.py delete mode 100644 experiment.py diff --git a/backend/Platform/settings/base.py b/backend/Platform/settings/base.py index b9d3fa6..89710d1 100644 --- a/backend/Platform/settings/base.py +++ b/backend/Platform/settings/base.py @@ -208,3 +208,6 @@ # Media Upload Settings MEDIA_ROOT = os.path.join(BASE_DIR, "accounts", "mediafiles") MEDIA_URL = "/media/" + +# GitHub API +GH_PERSONAL_ACCESS_TOKEN = os.environ.get("GH_PERSONAL_ACCESS_TOKEN") diff --git a/backend/Platform/urls.py b/backend/Platform/urls.py index 9d9b3c0..e66c7b6 100644 --- a/backend/Platform/urls.py +++ b/backend/Platform/urls.py @@ -12,6 +12,7 @@ path("accounts/", include("accounts.urls")), path("options/", include("options.urls", namespace="options")), path("identity/", include("identity.urls", namespace="identity")), + path("monitor/", include("monitor.urls", namespace="monitor")), path("s/", include("shortener.urls", namespace="shortener")), path( "openapi/", diff --git a/backend/monitor/__init__.py b/backend/monitor/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/backend/monitor/apps.py b/backend/monitor/apps.py new file mode 100644 index 0000000..3b67f38 --- /dev/null +++ b/backend/monitor/apps.py @@ -0,0 +1,5 @@ +from django.apps import AppConfig + + +class MonitorConfig(AppConfig): + name = "monitor" diff --git a/backend/monitor/urls.py b/backend/monitor/urls.py new file mode 100644 index 0000000..4574632 --- /dev/null +++ b/backend/monitor/urls.py @@ -0,0 +1,10 @@ +from django.urls import path +from monitor.views import PullsView + + +app_name = "monitor" + + +urlpatterns = [ + path("pulls/", PullsView.as_view(), name="pulls"), +] diff --git a/backend/monitor/views.py b/backend/monitor/views.py new file mode 100644 index 0000000..f464fd7 --- /dev/null +++ b/backend/monitor/views.py @@ -0,0 +1,46 @@ +import requests + +from django.conf import settings +from django.http.response import HttpResponse +from django.views.generic.base import View + + +PRODUCTS_TO_URL = { + "website": "pennlabs.org/", + "platform": "platform.pennlabs.org/", + "penn-clubs": "pennclubs.com/", +} + + +class PullsView(View): + """ + Returns a view displaying all PRs that have the feature-branch tag. + + """ + + def get(self, request): + headers = {"Authorization": settings.GH_PERSONAL_ACCESS_TOKEN} + pulls = [] + + for product, product_url in PRODUCTS_TO_URL.items(): + url = f"https://api.github.com/repos/pennlabs/{product}/pulls" + r = requests.get(url, headers=headers) + if r.status_code != 200: + print(f"Error: Request returned status code {r.status_code}") + return + + for pull in r.json(): + if "labels" not in pull: + continue + for label in pull["labels"]: + if "name" in label and label["name"] == "dependencies": + # if "name" in label and label["name"].startswith("feature-branch:"): + pulls.append( + { + "url": f"https://pr-{pull['number']}.{product_url}", + "status": "STATUS" + # "status": label["name"].split(":")[1] + } + ) + break + return HttpResponse("
".join(str(pull) for pull in pulls)) diff --git a/experiment.py b/experiment.py deleted file mode 100644 index 3f989e9..0000000 --- a/experiment.py +++ /dev/null @@ -1,41 +0,0 @@ -import json -import requests - -PRODUCTS_TO_URL = { - "website": "pennlabs.org/", - "platform": "platform.pennlabs.org/", - "penn-clubs": "pennclubs.com/", -} - - -def get_pulls() -> list: - headers = {"Authorization": "GH_PERSONAL_ACCESS_TOKEN"} - pulls = [] - - for product, product_url in PRODUCTS_TO_URL.items(): - url = f"https://api.github.com/repos/pennlabs/{product}/pulls" - r = requests.get(url, headers=headers) - if r.status_code != 200: - print(f"Error: Request returned status code {r.status_code}") - return - - for pull in r.json(): - if "labels" not in pull: - continue - for label in pull["labels"]: - if "name" in label and label["name"] == "dependencies": - # if "name" in label and label["name"].startswith("feature-branch:"): - pulls.append( - { - "url": f"https://pr-{pull['number']}.{product_url}", - "status": "STATUS" - # "status": label["name"].split(":")[1] - } - ) - break - return pulls - - -if __name__ == "__main__": - for pull in get_pulls(): - print(pull) From e817d826964bb9dd7abb57fad471f3de3884d8b2 Mon Sep 17 00:00:00 2001 From: Christopher Liu Date: Fri, 27 Jan 2023 18:04:08 -0500 Subject: [PATCH 3/6] Update description --- backend/monitor/views.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/backend/monitor/views.py b/backend/monitor/views.py index f464fd7..d78998b 100644 --- a/backend/monitor/views.py +++ b/backend/monitor/views.py @@ -14,8 +14,8 @@ class PullsView(View): """ - Returns a view displaying all PRs that have the feature-branch tag. - + Returns a view displaying all PRs that have the feature-branch tag and + their status. """ def get(self, request): @@ -33,8 +33,8 @@ def get(self, request): if "labels" not in pull: continue for label in pull["labels"]: + # if "name" in label and label["name"].startswith("feature-branch:"): if "name" in label and label["name"] == "dependencies": - # if "name" in label and label["name"].startswith("feature-branch:"): pulls.append( { "url": f"https://pr-{pull['number']}.{product_url}", From dc1b48ef7fa50135bf1675b25b1cecdcac762d62 Mon Sep 17 00:00:00 2001 From: Christopher Liu Date: Fri, 27 Jan 2023 18:14:04 -0500 Subject: [PATCH 4/6] Switch to feature-branch tags --- backend/monitor/views.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/backend/monitor/views.py b/backend/monitor/views.py index d78998b..4cdc712 100644 --- a/backend/monitor/views.py +++ b/backend/monitor/views.py @@ -33,13 +33,11 @@ def get(self, request): if "labels" not in pull: continue for label in pull["labels"]: - # if "name" in label and label["name"].startswith("feature-branch:"): - if "name" in label and label["name"] == "dependencies": + if "name" in label and label["name"].startswith("feature-branch:"): pulls.append( { "url": f"https://pr-{pull['number']}.{product_url}", - "status": "STATUS" - # "status": label["name"].split(":")[1] + "status": label["name"].split(":")[1] } ) break From 6044283c5dfbee4c193c0e79e065183a413c3f8d Mon Sep 17 00:00:00 2001 From: Christopher Liu Date: Fri, 3 Feb 2023 16:56:02 -0500 Subject: [PATCH 5/6] Fix styling --- backend/monitor/views.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/backend/monitor/views.py b/backend/monitor/views.py index 4cdc712..5a9fbd6 100644 --- a/backend/monitor/views.py +++ b/backend/monitor/views.py @@ -1,5 +1,4 @@ import requests - from django.conf import settings from django.http.response import HttpResponse from django.views.generic.base import View @@ -37,7 +36,7 @@ def get(self, request): pulls.append( { "url": f"https://pr-{pull['number']}.{product_url}", - "status": label["name"].split(":")[1] + "status": label["name"].split(":")[1], } ) break From c687858e6092afef85a5f015a4b0cad5ddafcb47 Mon Sep 17 00:00:00 2001 From: Christopher Liu Date: Fri, 3 Feb 2023 17:08:35 -0500 Subject: [PATCH 6/6] Add tests --- backend/tests/monitor/__init__.py | 0 backend/tests/monitor/test_views.py | 12 ++++++++++++ 2 files changed, 12 insertions(+) create mode 100644 backend/tests/monitor/__init__.py create mode 100644 backend/tests/monitor/test_views.py diff --git a/backend/tests/monitor/__init__.py b/backend/tests/monitor/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/backend/tests/monitor/test_views.py b/backend/tests/monitor/test_views.py new file mode 100644 index 0000000..aeb4185 --- /dev/null +++ b/backend/tests/monitor/test_views.py @@ -0,0 +1,12 @@ +from django.test import TestCase +from django.urls import reverse +from rest_framework.test import APIClient + + +class PullsViewTestCase(TestCase): + def setUp(self): + self.client = APIClient() + + def test_monitor_pulls(self): + resp = self.client.get(reverse("monitor:pulls")) + self.assertEqual(resp.status_code, 200, resp.content)