Skip to content

Commit 9f90d2e

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 c9ce2c9 commit 9f90d2e

4 files changed

Lines changed: 47 additions & 24 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: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
"""Managers for the webiscite app models."""
2+
13
from logging import WARNING
24
from typing import TYPE_CHECKING
35
from typing import Any
@@ -16,16 +18,14 @@
1618

1719
class PullRequestManager[T](models.Manager):
1820
def create_from_github(self, pr: dict[str, Any]) -> T:
19-
"""Create a :class:`~democrasite.webiscite.models.PullRequest` and optionally a
20-
:class:`~democrasite.webiscite.models.Bill` instance from a GitHub pull request
21+
"""Create or update a :class:`~democrasite.webiscite.models.PullRequest` from
22+
a GitHub pull request payload
2123
2224
Args:
23-
pr_args: The parameters for the ``PullRequest``
24-
bill_args: The parameters for the ``Bill`` or None
25+
pr: The pull request data from the GitHub API
2526
2627
Returns:
27-
A tuple containing the new or updated pull request and new bill instance, if
28-
applicable
28+
The new or updated pull request instance
2929
"""
3030
pull_request, created = self.update_or_create(
3131
number=pr["number"],
@@ -47,6 +47,12 @@ def create_from_github(self, pr: dict[str, Any]) -> T:
4747

4848
class BillManager[T](models.Manager):
4949
def get_queryset(self):
50+
"""Return a queryset with pull_request pre-fetched and vote percentages added.
51+
52+
All Bill querysets include ``total_votes``, ``yes_percent``, and
53+
``no_percent``
54+
annotations, and are ordered by creation date.
55+
"""
5056
return (
5157
super()
5258
.get_queryset()
@@ -80,6 +86,16 @@ def get_queryset(self):
8086
def annotate_user_vote(
8187
self, user: User, queryset: models.QuerySet["Bill"] | None = None
8288
):
89+
"""Annotate a queryset with the given user's vote on each bill.
90+
91+
Args:
92+
user: The user whose vote to annotate
93+
queryset: The queryset to annotate; defaults to ``self.get_queryset()``
94+
95+
Returns:
96+
The queryset annotated with a ``user_vote`` field
97+
(``True``/``False``/``None``)
98+
"""
8399
if queryset is None:
84100
queryset = self.get_queryset()
85101

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
@@ -151,10 +151,10 @@ def _validate_header(headers: request.HttpHeaders) -> HttpResponseBadRequest | N
151151
"""Validate the headers of a request from a webhook
152152
153153
Args:
154-
headers (dict): The headers from the request to validate
154+
headers: The headers from the request to validate
155155
156156
Returns:
157-
str: Error message if the headers are invalid, otherwise an empty string
157+
A bad request response if required headers are missing, otherwise ``None``
158158
"""
159159
header_signature = headers.get("x-hub-signature-256")
160160
if header_signature is None:
@@ -177,11 +177,12 @@ def _validate_signature(
177177
"""Validate the signature of a request from a webhook
178178
179179
Args:
180-
header_signature (str): The signature from the request to validate
181-
request_body (bytes): The body of the request to validate
180+
header_signature: The HMAC signature from the request headers
181+
request_body: The raw body of the request to validate
182182
183183
Returns:
184-
str: Error message if the signature is invalid, otherwise an empty string
184+
An error response if the signature is invalid or uses an unsupported
185+
digest, otherwise ``None``
185186
"""
186187
digest_name, signature = header_signature.split("=")
187188
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)