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        contributions = commits | reviews
35
36        for user in sorted(contributions.keys()):
37            user_commits = len(commits.get(user, {}).get("changes", []))
38            user_reviews = len(reviews.get(user, {}).get("changes", []))
39
40            points = user_commits * 3 + user_reviews
41            print(user, points, user_commits, user_reviews)
42
43            qualified = points >= 15
44
45            results[user] = {
46                "name": contributions[user]["name"],
47                "email": contributions[user]["email"],
48                "qualified": qualified,
49                "points": points,
50                "commits": user_commits,
51                "reviews": user_reviews,
52            }
53
54        with open(os.path.join(args.dir, "report.json"), "w") as outfile:
55            outfile.write(json.dumps(results, indent=4))
56
57        return 0
58