File size: 1,124 Bytes
2274b3a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
"""Append-only human reviews, stored separately from immutable model measurements."""

import json

PREFIX = "DEFECTRAG_REVIEW_V1:"
STATUSES = ("Needs review", "In review", "Reviewed")
DECISIONS = ("Pending", "Hold", "Accept", "Reject")


def validate_review(status, decision, note=""):
    if (
        status not in STATUSES
        or decision not in DECISIONS
        or (status == "Reviewed") == (decision == "Pending")
        or not isinstance(note, str)
        or len(note) > 1000
    ):
        raise ValueError("Choose a review status and a compatible decision.")
    return {"status": status, "decision": decision, "note": note.strip()}


def review_body(status, decision, note=""):
    return PREFIX + json.dumps(validate_review(status, decision, note))


def parse_review(body):
    if not isinstance(body, str) or not body.startswith(PREFIX):
        return None
    try:
        value = json.loads(body[len(PREFIX) :])
        return validate_review(value["status"], value["decision"], value.get("note", ""))
    except (ValueError, KeyError, TypeError):
        return None