1#!/usr/bin/python3
2
3import argparse
4import json
5import os
6
7
8class subcmd:
9    def __init__(self, parser: argparse._SubParsersAction) -> None:
10        p = parser.add_parser("report", help="Create final report")
11
12        p.set_defaults(cmd=self)
13
14    def run(self, args: argparse.Namespace) -> int:
15        commits_fp = os.path.join(args.dir, "commits.json")
16        reviews_fp = os.path.join(args.dir, "reviews.json")
17
18        results = {}
19
20        if not os.path.isfile(commits_fp):
21            print("Missing commits.json; run analyze-commits?")
22            return 1
23
24        if not os.path.isfile(reviews_fp):
25            print("Missing reviews.json; run analyze-reviews?")
26            return 1
27
28        with open(commits_fp, "r") as commits_file:
29            commits = json.load(commits_file)
30
31        with open(reviews_fp, "r") as reviews_file:
32            reviews = json.load(reviews_file)
33
34        for user in sorted(set(commits.keys()).union(reviews.keys())):
35            user_commits = len(commits.get(user, []))
36            user_reviews = len(reviews.get(user, []))
37
38            points = user_commits * 3 + user_reviews
39            print(user, points, user_commits, user_reviews)
40
41            qualified = points >= 15
42
43            results[user] = {
44                "qualified": qualified,
45                "points": points,
46                "commits": user_commits,
47                "reviews": user_reviews,
48            }
49
50        with open(os.path.join(args.dir, "report.json"), "w") as outfile:
51            outfile.write(json.dumps(results, indent=4))
52
53        return 0
54