blob: b3734b97199f79271a67e2e7ce1cb037fef48b8f [file] [log] [blame]
Patrick Williamsd0269de2022-01-06 21:04:25 -06001#!/usr/bin/python3
2
3import argparse
4import json
5import os
6import re
7import libvoters.acceptable as acceptable
8from collections import defaultdict
9from libvoters.time import timestamp, TimeOfDay
10from typing import Dict
11
12
13class subcmd:
14 def __init__(self, parser: argparse._SubParsersAction) -> None:
15 p = parser.add_parser(
16 "analyze-reviews", help="Determine points for reviews"
17 )
18
19 p.add_argument(
20 "--before",
21 "-b",
22 help="Before timestamp (YYYY-MM-DD)",
23 required=True,
24 )
25 p.add_argument(
26 "--after",
27 "-a",
28 help="After timestamp (YYYY-MM-DD)",
29 required=True,
30 )
31
32 p.set_defaults(cmd=self)
33
34 def run(self, args: argparse.Namespace) -> int:
35 before = timestamp(args.before, TimeOfDay.AM)
36 after = timestamp(args.after, TimeOfDay.PM)
37
38 changes_per_user: Dict[str, list[int]] = defaultdict(list)
39
40 for f in sorted(os.listdir(args.dir)):
41 path = os.path.join(args.dir, f)
42 if not os.path.isfile(path):
43 continue
44
45 if not re.match("[0-9]*\.json", f):
46 continue
47
48 with open(path, "r") as file:
49 data = json.load(file)
50
51 project = data["project"]
52 id_number = data["number"]
53 author = data["owner"]["username"]
54
55 if not acceptable.project(project):
56 print("Rejected project:", project, id_number)
57
58 comments_per_user: Dict[str, int] = defaultdict(int)
59
60 for patch_set in data["patchSets"]:
61 created_on = data["createdOn"]
62
63 if created_on > before or created_on < after:
64 continue
65
66 if "comments" not in patch_set:
67 continue
68
69 for comment in patch_set["comments"]:
70 reviewer = comment["reviewer"]["username"]
71
72 if reviewer == author:
73 continue
74 if not acceptable.file(project, comment["file"]):
75 continue
76
77 comments_per_user[reviewer] += 1
78
79 print(project, id_number)
80 for (user, count) in comments_per_user.items():
81 if count < 3:
82 continue
83 print(" ", user, count)
84 changes_per_user[user].append(id_number)
85
86 with open(os.path.join(args.dir, "reviews.json"), "w") as outfile:
87 outfile.write(json.dumps(changes_per_user, indent=4))
88
89 return 0