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