Skip to content
Merged
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
1 change: 1 addition & 0 deletions .TRACFREEZE.txt
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ tracdjangoplugin.plugins.customwikimodule
tracdjangoplugin.plugins.githubbrowserwithsvnchangesets
tracdjangoplugin.plugins.plainlogincomponent
tracdjangoplugin.plugins.reservedusernamescomponent
tracdjangoplugin.plugins.ticketchangevalidator
tracdjangoplugin.plugins.timelineticketcomponentfilter
tracdragdrop.web_ui.tracdragdropmodule
tracext.github.githubloginmodule
Expand Down
134 changes: 134 additions & 0 deletions DjangoPlugin/tracdjangoplugin/plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

from trac.config import ListOption
from trac.core import Component, implements
from trac.perm import PermissionSystem
from trac.ticket.api import ITicketManipulator
from trac.ticket.query import Query
from trac.web.chrome import INavigationContributor
from trac.web.api import IRequestFilter, IRequestHandler, RequestDone
from trac.web.auth import LoginModule
Expand Down Expand Up @@ -304,3 +307,134 @@ def force_logout_and_redirect(self, req):

def post_process_request(self, req, template, data, metadata):
return template, data, metadata # required by Trac to exist


class TicketChangeValidator(Component):
implements(ITicketManipulator)

exempt_groups = {"admins", "fellows", "supertriagers"}
assignment_limit = 3

def prepare_ticket(self, req, ticket, fields, actions):
pass

def validate_ticket(self, req, ticket):
if not ticket.exists:
# New tickets must start unreviewed.
if ticket["stage"] != "Unreviewed":
return [
(
"stage",
"New tickets must have the Unreviewed triage stage.",
)
]
# Creators may assign themselves regardless of the ticket limit.
return []

for validate in (
self.validate_reporter_change,
self.validate_triage_stage_change,
self.validate_owner_change,
):
errors = validate(req, ticket)
if errors:
return errors
return []

def is_user_member_of_exempt_group(self, username):
groups = PermissionSystem(self.env).get_groups_dict()
return any(username in groups.get(group, ()) for group in self.exempt_groups)

def validate_reporter_change(self, req, ticket):
username = req.authname
reporter = ticket["reporter"]
previous_reporter = ticket._old.get("reporter", reporter)
if reporter != previous_reporter and not self.is_user_member_of_exempt_group(
username
):
# We allow members of exempt groups to update the reporter in case
# the reporter is something against the CoC.
return [("reporter", "You cannot update the ticket reporter.")]
return []

def validate_triage_stage_change(self, req, ticket):
username = req.authname
stage = ticket["stage"]
previous_stage = ticket._old.get("stage", stage)

if stage == previous_stage:
return []

if stage == "Accepted" and ticket["reporter"] == username:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you're game, we could also validate that reporter is truthy to make this harder to evade by deleting. No worries if not, since there are other ways to evade by free-texting the owner.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have added a restriction on updating the reporter unless you are an "admin" (in case a reporter is something offensive).

return [
(
"stage",
"You cannot accept your own ticket. "
"Please leave it for another contributor to triage.",
)
]

owner = ticket["owner"]
previous_owner = ticket._old.get("owner", owner)

if (
stage == "Ready for checkin"
and username in {previous_owner, owner}
and not self.is_user_member_of_exempt_group(username)
):
# We allow members of exempt groups to mark their own ticket as
# "Ready for checkin" as sometimes they receive an approval but the
# reviewer forgot to update the Triage stage in Trac.
return [
(
"stage",
"You cannot mark a ticket assigned to you as ready for checkin. "
"Please leave it for another contributor to review.",
)
]

return []

def validate_owner_change(self, req, ticket):
username = req.authname
owner = ticket["owner"]
previous_owner = ticket._old.get("owner", owner)

# Only restrict claiming a ticket for yourself.
# Assigning to others and retaining ownership are allowed.
if owner != username or previous_owner == username:
return []

stage = ticket["stage"]
if stage == "Unreviewed" and ticket["reporter"] != username:
return [
(
"owner",
"Only the ticket's reporter may assign themselves before the "
"ticket is accepted.",
)
]

if stage == "Accepted" and not self.is_user_member_of_exempt_group(username):
count = Query(
self.env,
constraints=[
{
"owner": [username],
"stage": ["Accepted"],
"status": ["!closed"],
}
],
).count(req)
if count >= self.assignment_limit:
return [
(
"owner",
f"You already have {self.assignment_limit} or more open, "
"accepted tickets assigned to you. Please finish or "
"release an existing assignment before taking another "
"ticket.",
)
]

return []
Loading
Loading