Skip to content

Commit 81588de

Browse files
mfosterwclaude
andcommitted
Fix outdated documentation and incorrect docstrings
- Update docs/webiscite.rst to reflect current architecture: GithubWebhookView and PullRequestHandler replace the removed github_hook, process_pull, and pr_opened identifiers - Fix managers.py docstrings: correct parameter names and return descriptions for create_from_github on both managers; add missing docstrings to get_queryset and annotate_user_vote; add module docstring - Fix webhooks.py docstrings: update module docstring to class-based view; fix _validate_header and _validate_signature return type descriptions - Add missing module docstring to activitypub/models.py Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 8fcb425 commit 81588de

4 files changed

Lines changed: 53 additions & 28 deletions

File tree

democrasite/activitypub/models.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
"""Models for the activitypub app."""
2+
13
from django.contrib.auth import get_user_model
24
from django.db import models
35
from django.urls import reverse

democrasite/webiscite/managers.py

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
"""Managers for the webiscite app models."""
2+
13
import json
24
from collections.abc import Iterator
35
from contextlib import contextmanager
@@ -24,16 +26,14 @@
2426

2527
class PullRequestManager[T](models.Manager):
2628
def create_from_github(self, pr: dict[str, Any]) -> T:
27-
"""Create a :class:`~democrasite.webiscite.models.PullRequest` and optionally a
28-
:class:`~democrasite.webiscite.models.Bill` instance from a GitHub pull request
29+
"""Create or update a :class:`~democrasite.webiscite.models.PullRequest` from
30+
a GitHub pull request payload
2931
3032
Args:
31-
pr_args: The parameters for the ``PullRequest``
32-
bill_args: The parameters for the ``Bill`` or None
33+
pr: The pull request data from the GitHub API
3334
3435
Returns:
35-
A tuple containing the new or updated pull request and new bill instance, if
36-
applicable
36+
The new or updated pull request instance
3737
"""
3838
pull_request, created = self.update_or_create(
3939
number=pr["number"],
@@ -54,6 +54,12 @@ def create_from_github(self, pr: dict[str, Any]) -> T:
5454

5555
class BillManager[T](models.Manager):
5656
def get_queryset(self):
57+
"""Return a queryset with pull_request pre-fetched and vote percentages added.
58+
59+
All Bill querysets include ``total_votes``, ``yes_percent``, and
60+
``no_percent``
61+
annotations, and are ordered by creation date.
62+
"""
5763
return (
5864
super()
5965
.get_queryset()
@@ -87,6 +93,16 @@ def get_queryset(self):
8793
def annotate_user_vote(
8894
self, user: User, queryset: models.QuerySet["Bill"] | None = None
8995
):
96+
"""Annotate a queryset with the given user's vote on each bill.
97+
98+
Args:
99+
user: The user whose vote to annotate
100+
queryset: The queryset to annotate; defaults to ``self.get_queryset()``
101+
102+
Returns:
103+
The queryset annotated with a ``user_vote`` field
104+
(``True``/``False``/``None``)
105+
"""
90106
if queryset is None:
91107
queryset = self.get_queryset()
92108

@@ -111,13 +127,15 @@ def create_from_github(
111127
GitHub pull request
112128
113129
Args:
114-
pr: The pull request data from the GitHub API
115-
diff_text: The text of the diff of the pull request
130+
title: The title of the pull request
131+
body: The body/description of the pull request
132+
author: The user who authored the pull request
133+
diff_text: The text of the diff of the pull request, used to determine
134+
whether the bill is constitutional
116135
pull_request: The pull request instance to associate with the bill
117136
118137
Returns:
119-
A tuple containing the new or update pull request and new bill instance, if
120-
applicable
138+
The newly created bill instance
121139
"""
122140
with self._create_submit_task() as submit_task:
123141
self._bill: Bill = self.model(

democrasite/webiscite/webhooks.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""Views for processing webhooks.
22
3-
Each service that sends webhooks should have its own function-based view."""
3+
Each service that sends webhooks should have its own class-based view."""
44

55
import hmac
66
import json
@@ -138,10 +138,10 @@ def _validate_header(headers: request.HttpHeaders) -> HttpResponseBadRequest | N
138138
"""Validate the headers of a request from a webhook
139139
140140
Args:
141-
headers (dict): The headers from the request to validate
141+
headers: The headers from the request to validate
142142
143143
Returns:
144-
str: Error message if the headers are invalid, otherwise an empty string
144+
A bad request response if required headers are missing, otherwise ``None``
145145
"""
146146
header_signature = headers.get("x-hub-signature-256")
147147
if header_signature is None:
@@ -164,11 +164,12 @@ def _validate_signature(
164164
"""Validate the signature of a request from a webhook
165165
166166
Args:
167-
header_signature (str): The signature from the request to validate
168-
request_body (bytes): The body of the request to validate
167+
header_signature: The HMAC signature from the request headers
168+
request_body: The raw body of the request to validate
169169
170170
Returns:
171-
str: Error message if the signature is invalid, otherwise an empty string
171+
An error response if the signature is invalid or uses an unsupported
172+
digest, otherwise ``None``
172173
"""
173174
digest_name, signature = header_signature.split("=")
174175
if digest_name != "sha256":

docs/webiscite.rst

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,27 @@ Pull Requests
1515
Pull Request Processing Pipeline
1616
--------------------------------
1717

18-
When a pull request is created on `GitHub`_, a `webhook`_ makes a request to
19-
the GitHub :func:`webhook view <democrasite.webiscite.webhooks.github_hook>`.
18+
When a pull request is created on `GitHub`_, a `webhook`_ makes a POST request
19+
to the :class:`~democrasite.webiscite.webhooks.GithubWebhookView`, which
20+
validates the request signature and dispatches to the appropriate handler.
2021

21-
This method parses the data from the request and calls
22-
:func:`~democrasite.webiscite.tasks.process_pull`
23-
to handle the request. In the event a pull request was opened or reopened,
24-
:func:`~democrasite.webiscite.tasks.pr_opened` is called.
22+
For pull request events, the request is handled by
23+
:class:`~democrasite.webiscite.webhooks.PullRequestHandler`. When a pull
24+
request is opened or reopened, its
25+
:meth:`~democrasite.webiscite.webhooks.PullRequestHandler.opened` method
26+
creates a :class:`~democrasite.webiscite.models.PullRequest` instance.
2527

2628
If the user who created the pull request has a democrasite account, a new
2729
:class:`~democrasite.webiscite.models.Bill`
2830
is created with the information from the pull request and made visible on the
29-
homepage immediately.
30-
31-
A task to execute :func:`~democrasite.webiscite.tasks.submit_bill`
32-
is also put in the celery queue to execute once the voting period ends. The
33-
function verifies that the pull request is open and unedited and then counts
34-
the votes for and against that Bill.
31+
homepage immediately. A :class:`~django_celery_beat.models.PeriodicTask` is
32+
also scheduled to execute :func:`~democrasite.webiscite.tasks.submit_bill`
33+
once the voting period ends.
34+
35+
:func:`~democrasite.webiscite.tasks.submit_bill` verifies that the pull
36+
request is still open and that its SHA has not changed since the bill was
37+
created (i.e. the pull request has not been edited), then counts the votes for
38+
and against that Bill.
3539

3640
If the votes for the Bill pass the threshold, the pull request is merged into
3741
the master branch on Github and automatically deployed, officially making it

0 commit comments

Comments
 (0)