diff --git a/pgcommitfest/commitfest/apiv1.py b/pgcommitfest/commitfest/apiv1.py index 9d6109ad..1b80c926 100644 --- a/pgcommitfest/commitfest/apiv1.py +++ b/pgcommitfest/commitfest/apiv1.py @@ -1,16 +1,19 @@ -from django.db.models import Prefetch, QuerySet +from django.db.models import Prefetch, Q, QuerySet from django.http import ( + Http404, HttpResponse, ) from django.shortcuts import get_object_or_404 import json from datetime import date, datetime, timedelta, timezone +from urllib.parse import urlencode from .models import ( CommitFest, MailThread, Patch, + PatchHistory, PatchOnCommitFest, ) @@ -41,6 +44,9 @@ def api_response(payload, status=200, content_type="application/json"): return response +HISTORY_PAGE_SIZE = 500 + + def all_commitfests(request): """Return all commitfests, including closed ones.""" return api_response({"commitfests": CommitFest.objects.order_by("id")}) @@ -76,8 +82,8 @@ def commitfest_patches(request, cfid): pocs = ( PatchOnCommitFest.objects.filter(commitfest=cf) - .select_related("patch") - .prefetch_related("patch__authors") + .select_related("patch__committer__user", "patch__targetversion") + .prefetch_related("patch__authors", "patch__reviewers", "patch__tags") .order_by("patch__id") ) @@ -104,6 +110,28 @@ def commitfest_patches(request, cfid): ) +def patch(request, patch_id): + """Return a patch on its current commitfest, with its threads. + + The patch has the same shape as in /commitfests//patches. A missing + patch returns 404 with {"error": ""}. + """ + poc = ( + PatchOnCommitFest.objects.filter(patch_id=patch_id) + .exclude(status=PatchOnCommitFest.STATUS_MOVED) + .select_related("patch__committer__user", "patch__targetversion") + .prefetch_related("patch__authors", "patch__reviewers", "patch__tags") + .first() + ) + if poc is None: + return api_response({"error": "Patch not found"}, 404) + + entry = poc.to_json() + entry["threads"] = poc.patch.mailthread_set.with_has_attachment() + + return api_response({"commitfest_id": poc.commitfest_id, "patch": entry}) + + def patch_threads(request, patch_id): """Return thread information for a patch. @@ -121,3 +149,68 @@ def patch_threads(request, patch_id): "threads": threads, } ) + + +def patch_history(request, patch_id): + """Return the activity history of a patch, oldest first.""" + # Checking existence avoids loading relations. + if not Patch.objects.filter(pk=patch_id).exists(): + raise Http404("Patch not found") + + history = ( + PatchHistory.objects.filter(patch_id=patch_id) + .select_related("by") + .order_by("date", "id") + ) + + return api_response( + { + "patch_id": int(patch_id), + "history": history, + } + ) + + +def history(request): + """Return the activity history of all patches, oldest first. + + ?since= returns entries from that time on; ?after= breaks + ties between entries sharing a timestamp. "next" is the URL of the + following page, or null on the last one. Malformed parameters return + 400 with {"error": ""}. + """ + since = request.GET.get("since") + after = request.GET.get("after") + + entries = PatchHistory.objects.select_related("by").order_by("date", "id") + + if since is not None: + try: + since = datetime.fromisoformat(since) + except ValueError: + return api_response({"error": "since must be an ISO 8601 date"}, 400) + # Dates are stored as naive UTC + if since.tzinfo is not None: + since = since.astimezone(timezone.utc).replace(tzinfo=None) + entries = entries.filter(date__gte=since) + + if after is not None: + if since is None: + return api_response({"error": "after requires since"}, 400) + try: + after = int(after) + except ValueError: + return api_response({"error": "after must be an integer"}, 400) + entries = entries.filter(Q(date__gt=since) | Q(id__gt=after)) + + page = list(entries[: HISTORY_PAGE_SIZE + 1]) + next_url = None + if len(page) > HISTORY_PAGE_SIZE: + page = page[:HISTORY_PAGE_SIZE] + last = page[-1] + next_url = "{}?{}".format( + request.path, + urlencode({"since": datetime_serializer(last.date), "after": last.id}), + ) + + return api_response({"history": page, "next": next_url}) diff --git a/pgcommitfest/commitfest/models.py b/pgcommitfest/commitfest/models.py index c7b8fb4f..eb903470 100644 --- a/pgcommitfest/commitfest/models.py +++ b/pgcommitfest/commitfest/models.py @@ -659,7 +659,23 @@ def to_json(self): "id": self.id, "name": self.name, "authors": [f"{a.first_name} {a.last_name}" for a in self.authors.all()], + "reviewers": [ + f"{r.first_name} {r.last_name}" for r in self.reviewers.all() + ], + "committer": ( + f"{self.committer.user.first_name} {self.committer.user.last_name}" + if self.committer + else None + ), + "target_version": ( + self.targetversion.version if self.targetversion else None + ), + "tags": [t.name for t in self.tags.all()], + "wiki_link": self.wikilink or None, + "git_link": self.gitlink or None, "last_email_time": self.lastmail, + "created": self.created, + "modified": self.modified, } def __str__(self): @@ -796,6 +812,16 @@ def by_string(self): def __str__(self): return "%s - %s" % (self.patch.name, self.date) + def to_json(self): + return { + "id": self.id, + "patch_id": self.patch_id, + "date": self.date, + "by": f"{self.by.first_name} {self.by.last_name}" if self.by else None, + "by_cfbot": self.by_cfbot, + "what": self.what, + } + class Meta: ordering = ("-date",) constraints = [ diff --git a/pgcommitfest/commitfest/tests/test_apiv1.py b/pgcommitfest/commitfest/tests/test_apiv1.py index 539f29e1..ca3c452a 100644 --- a/pgcommitfest/commitfest/tests/test_apiv1.py +++ b/pgcommitfest/commitfest/tests/test_apiv1.py @@ -6,11 +6,16 @@ import pytest +from pgcommitfest.commitfest import apiv1 from pgcommitfest.commitfest.models import ( + Committer, MailThread, MailThreadAttachment, Patch, + PatchHistory, PatchOnCommitFest, + Tag, + TargetVersion, ) pytestmark = pytest.mark.django_db @@ -124,16 +129,27 @@ def test_needs_ci_endpoint(client, commitfests): assert data == expected -def test_commitfest_patches_endpoint(client, open_cf, alice, bob): +def test_commitfest_patches_endpoint(client, open_cf, alice, bob, charlie): """Test the /api/v1/commitfests//patches endpoint.""" # Create test patches patch1 = Patch.objects.create(name="Add feature X") patch1.authors.add(alice) + patch1.reviewers.add(bob) + patch1.targetversion = TargetVersion.objects.create(version="19") + patch1.tags.add( + Tag.objects.create(name="API tag A", color="#ff0000"), + Tag.objects.create(name="API tag B", color="#00ff00"), + ) + patch1.wikilink = "https://wiki.postgresql.org/wiki/Feature_X" + patch1.gitlink = "https://github.com/example/postgres/tree/feature-x" patch1.lastmail = datetime(2025, 1, 15, 10, 30, 0, tzinfo=timezone.utc) + patch1.created = datetime(2024, 12, 20, 9, 0, 0, tzinfo=timezone.utc) + patch1.modified = datetime(2025, 1, 14, 17, 15, 0, tzinfo=timezone.utc) patch1.save() patch2 = Patch.objects.create(name="Fix bug Y") patch2.authors.add(alice, bob) + patch2.committer = Committer.objects.create(user=charlie) patch2.save() # Link patches to commitfest @@ -192,7 +208,15 @@ def test_commitfest_patches_endpoint(client, open_cf, alice, bob): assert p1["name"] == "Add feature X" assert p1["status"] == "Needs review" assert p1["authors"] == ["Alice Anderson"] + assert p1["reviewers"] == ["Bob Brown"] + assert p1["committer"] is None + assert p1["target_version"] == "19" + assert p1["tags"] == ["API tag A", "API tag B"] + assert p1["wiki_link"] == "https://wiki.postgresql.org/wiki/Feature_X" + assert p1["git_link"] == "https://github.com/example/postgres/tree/feature-x" assert p1["last_email_time"] == "2025-01-15T10:30:00+00:00" + assert p1["created"] == "2024-12-20T09:00:00+00:00" + assert p1["modified"] == "2025-01-14T17:15:00+00:00" assert p1["enterdate"] == "2025-01-01T12:00:00+00:00" assert p1["leavedate"] is None @@ -201,6 +225,12 @@ def test_commitfest_patches_endpoint(client, open_cf, alice, bob): assert p2["name"] == "Fix bug Y" assert p2["status"] == "Committed" assert sorted(p2["authors"]) == ["Alice Anderson", "Bob Brown"] + assert p2["reviewers"] == [] + assert p2["committer"] == "Charlie Chen" + assert p2["target_version"] is None + assert p2["tags"] == [] + assert p2["wiki_link"] is None + assert p2["git_link"] is None assert p2["last_email_time"] is None assert p2["enterdate"] == "2025-01-02T08:00:00+00:00" assert p2["leavedate"] == "2025-01-20T16:45:00+00:00" @@ -250,6 +280,86 @@ def test_commitfest_patches_endpoint_not_found(client, commitfests): assert response.status_code == 404 +def test_patch_endpoint(client, commitfests, alice, bob): + """Test the /api/v1/patches/ endpoint.""" + patch = Patch.objects.create(name="Add feature X") + patch.authors.add(alice) + patch.reviewers.add(bob) + patch.tags.add(Tag.objects.create(name="API tag A", color="#ff0000")) + + # An earlier commitfest the patch moved out of + PatchOnCommitFest.objects.create( + patch=patch, + commitfest=commitfests["in_progress"], + enterdate=datetime(2024, 11, 1, 12, 0, 0, tzinfo=timezone.utc), + leavedate=datetime(2024, 11, 30, 12, 0, 0, tzinfo=timezone.utc), + status=PatchOnCommitFest.STATUS_MOVED, + ) + PatchOnCommitFest.objects.create( + patch=patch, + commitfest=commitfests["open"], + enterdate=datetime(2025, 1, 1, 12, 0, 0, tzinfo=timezone.utc), + status=PatchOnCommitFest.STATUS_AUTHOR, + ) + + thread = MailThread.objects.create( + messageid="abc123@example.com", + subject="[PATCH] Add feature X", + firstmessage=datetime(2025, 1, 10, 9, 0, 0, tzinfo=timezone.utc), + firstauthor="alice@example.com", + latestmessage=datetime(2025, 1, 12, 14, 30, 0, tzinfo=timezone.utc), + latestauthor="bob@example.com", + latestsubject="Re: [PATCH] Add feature X", + latestmsgid="def456@example.com", + ) + patch.mailthread_set.add(thread) + MailThreadAttachment.objects.create( + mailthread=thread, + messageid="abc123@example.com", + attachmentid=1, + filename="v1.patch", + date=datetime(2025, 1, 10, 9, 0, 0, tzinfo=timezone.utc), + author="alice@example.com", + ispatch=True, + ) + + response = client.get(f"/api/v1/patches/{patch.id}") + + assert response.status_code == 200 + assert response["Content-Type"] == "application/json" + assert response["Access-Control-Allow-Origin"] == "*" + + data = json.loads(response.content) + + assert data["commitfest_id"] == commitfests["open"].id + p = data["patch"] + assert p["id"] == patch.id + assert p["name"] == "Add feature X" + assert p["status"] == "Waiting on Author" + assert p["authors"] == ["Alice Anderson"] + assert p["reviewers"] == ["Bob Brown"] + assert p["tags"] == ["API tag A"] + assert p["enterdate"] == "2025-01-01T12:00:00+00:00" + assert p["leavedate"] is None + assert p["threads"] == [ + { + "messageid": "abc123@example.com", + "subject": "[PATCH] Add feature X", + "latest_message_id": "def456@example.com", + "latest_message_time": "2025-01-12T14:30:00+00:00", + "has_attachment": True, + } + ] + + +def test_patch_endpoint_not_found(client, commitfests): + """Test the patch endpoint returns a JSON 404 for non-existent patch.""" + response = client.get("/api/v1/patches/99999") + assert response.status_code == 404 + assert response["Content-Type"] == "application/json" + assert json.loads(response.content) == {"error": "Patch not found"} + + def test_patch_threads_endpoint(client, open_cf, alice): """Test the /api/v1/patches//threads endpoint.""" patch = Patch.objects.create(name="Test patch") @@ -298,3 +408,166 @@ def test_patch_threads_endpoint_not_found(client, commitfests): """Test the threads endpoint returns 404 for non-existent patch.""" response = client.get("/api/v1/patches/99999/threads") assert response.status_code == 404 + + +def make_history(patch, date, what, by=None): + """Create a PatchHistory row at a fixed date (date is auto_now_add).""" + entry = PatchHistory.objects.create( + patch=patch, by=by, by_cfbot=by is None, what=what + ) + PatchHistory.objects.filter(pk=entry.pk).update(date=date) + return entry + + +def test_patch_history_endpoint(client, alice): + """Test the /api/v1/patches//history endpoint.""" + patch = Patch.objects.create(name="Test patch") + other = Patch.objects.create(name="Other patch") + + created = make_history( + patch, datetime(2025, 1, 1, 12, 0, 0, tzinfo=timezone.utc), "Created", alice + ) + rebase = make_history( + patch, + datetime(2025, 1, 3, 8, 0, 0, tzinfo=timezone.utc), + "Patch needs rebase", + ) + status = make_history( + patch, + datetime(2025, 1, 2, 9, 30, 0, tzinfo=timezone.utc), + "New status: Ready for Committer", + alice, + ) + make_history( + other, datetime(2025, 1, 2, 10, 0, 0, tzinfo=timezone.utc), "Created", alice + ) + + response = client.get(f"/api/v1/patches/{patch.id}/history") + + assert response.status_code == 200 + assert response["Content-Type"] == "application/json" + assert response["Access-Control-Allow-Origin"] == "*" + + # Oldest first, only this patch, cfbot entries included + assert json.loads(response.content) == { + "patch_id": patch.id, + "history": [ + { + "id": created.id, + "patch_id": patch.id, + "date": "2025-01-01T12:00:00+00:00", + "by": "Alice Anderson", + "by_cfbot": False, + "what": "Created", + }, + { + "id": status.id, + "patch_id": patch.id, + "date": "2025-01-02T09:30:00+00:00", + "by": "Alice Anderson", + "by_cfbot": False, + "what": "New status: Ready for Committer", + }, + { + "id": rebase.id, + "patch_id": patch.id, + "date": "2025-01-03T08:00:00+00:00", + "by": None, + "by_cfbot": True, + "what": "Patch needs rebase", + }, + ], + } + + +def test_patch_history_endpoint_not_found(client, commitfests): + """Test the history endpoint returns 404 for non-existent patch.""" + response = client.get("/api/v1/patches/99999/history") + assert response.status_code == 404 + + +def test_history_endpoint(client, alice): + """Test the /api/v1/history endpoint.""" + patch1 = Patch.objects.create(name="Add feature X") + patch2 = Patch.objects.create(name="Fix bug Y") + + first = make_history( + patch1, datetime(2025, 1, 1, 12, 0, 0, tzinfo=timezone.utc), "Created", alice + ) + rebase = make_history( + patch2, + datetime(2025, 1, 3, 8, 0, 0, tzinfo=timezone.utc), + "Patch needs rebase", + ) + status = make_history( + patch1, + datetime(2025, 1, 2, 9, 30, 0, tzinfo=timezone.utc), + "New status: Ready for Committer", + alice, + ) + + response = client.get("/api/v1/history") + + assert response.status_code == 200 + assert response["Content-Type"] == "application/json" + assert response["Access-Control-Allow-Origin"] == "*" + + # All patches, oldest first, cfbot entries included + data = json.loads(response.content) + assert data["next"] is None + assert [h["id"] for h in data["history"]] == [first.id, status.id, rebase.id] + assert data["history"][2] == { + "id": rebase.id, + "patch_id": patch2.id, + "date": "2025-01-03T08:00:00+00:00", + "by": None, + "by_cfbot": True, + "what": "Patch needs rebase", + } + + # since is inclusive and accepts any timezone + response = client.get("/api/v1/history", {"since": "2025-01-02T10:30:00+01:00"}) + data = json.loads(response.content) + assert [h["id"] for h in data["history"]] == [status.id, rebase.id] + + +def test_history_endpoint_pagination(client, alice, monkeypatch): + """Following next walks every entry once, even across shared timestamps.""" + monkeypatch.setattr(apiv1, "HISTORY_PAGE_SIZE", 2) + patch = Patch.objects.create(name="Add feature X") + + # A bulk move at commitfest close gives many entries the same timestamp + close = datetime(2025, 2, 1, 0, 0, 0, tzinfo=timezone.utc) + entries = [ + make_history(patch, datetime(2025, 1, 1, tzinfo=timezone.utc), "Created", alice) + ] + [make_history(patch, close, f"Moved {i}") for i in range(4)] + + seen = [] + url = "/api/v1/history" + pages = 0 + while url: + response = client.get(url) + assert response.status_code == 200 + data = json.loads(response.content) + assert len(data["history"]) <= 2 + seen.extend(h["id"] for h in data["history"]) + url = data["next"] + pages += 1 + + assert seen == [e.id for e in entries] + assert pages == 3 + + +@pytest.mark.parametrize( + "params", + [ + {"since": "yesterday"}, + {"after": "1"}, + {"since": "2025-01-01T00:00:00+00:00", "after": "x"}, + ], +) +def test_history_endpoint_bad_params(client, params): + """Test the history endpoint rejects malformed cursors.""" + response = client.get("/api/v1/history", params) + assert response.status_code == 400 + assert "error" in json.loads(response.content) diff --git a/pgcommitfest/urls.py b/pgcommitfest/urls.py index de80e32c..3f4666d2 100644 --- a/pgcommitfest/urls.py +++ b/pgcommitfest/urls.py @@ -19,7 +19,10 @@ re_path(r"^api/v1/commitfests$", apiv1.all_commitfests), re_path(r"^api/v1/commitfests/needs_ci$", apiv1.commitfestst_that_need_ci), re_path(r"^api/v1/commitfests/(\d+)/patches$", apiv1.commitfest_patches), + re_path(r"^api/v1/patches/(\d+)$", apiv1.patch), re_path(r"^api/v1/patches/(\d+)/threads$", apiv1.patch_threads), + re_path(r"^api/v1/patches/(\d+)/history$", apiv1.patch_history), + re_path(r"^api/v1/history$", apiv1.history), re_path(r"^help/$", views.help), re_path(r"^commitfest_history/$", views.commitfest_history_legacy_redirect), re_path(r"^me/$", views.me_legacy_redirect),