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

PR monitoring for feature branch deploy #152

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 3 additions & 0 deletions backend/Platform/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
1 change: 1 addition & 0 deletions backend/Platform/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -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/",
Expand Down
Empty file added backend/monitor/__init__.py
Empty file.
5 changes: 5 additions & 0 deletions backend/monitor/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from django.apps import AppConfig


class MonitorConfig(AppConfig):
name = "monitor"
10 changes: 10 additions & 0 deletions backend/monitor/urls.py
Original file line number Diff line number Diff line change
@@ -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"),
]
43 changes: 43 additions & 0 deletions backend/monitor/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
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 and
their status.
"""

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"].startswith("feature-branch:"):
pulls.append(
{
"url": f"https://pr-{pull['number']}.{product_url}",
"status": label["name"].split(":")[1],
}
)
break
return HttpResponse("<br>".join(str(pull) for pull in pulls))
Empty file.
12 changes: 12 additions & 0 deletions backend/tests/monitor/test_views.py
Original file line number Diff line number Diff line change
@@ -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)