add dump-option for milestones in JSON format
authorLuke Kenneth Casson Leighton <lkcl@lkcl.net>
Sat, 16 Jul 2022 14:06:06 +0000 (15:06 +0100)
committerLuke Kenneth Casson Leighton <lkcl@lkcl.net>
Sat, 16 Jul 2022 14:06:06 +0000 (15:06 +0100)
src/budget_sync/main.py
src/budget_sync/money.py

index 6b37d03bf2569d56ce84bc5ac36ee528d063b2b6..12ce493cfac90a0b9eebba9ca4d0d6d5e58e9592 100644 (file)
@@ -1,6 +1,7 @@
 import os
 import re
 import sys
+import json
 from typing import Optional
 from budget_sync.ordered_set import OrderedSet
 from budget_sync.write_budget_csv import write_budget_csv
@@ -66,6 +67,7 @@ def main():
         write_budget_markdown(budget_graph, args.output_dir)
         write_budget_csv(budget_graph, args.output_dir)
     summarize_milestones(budget_graph)
+    json_milestones(budget_graph)
 
 
 def print_markdown_for_person(budget_graph: BudgetGraph, config: Config,
@@ -132,5 +134,61 @@ def summarize_milestones(budget_graph: BudgetGraph):
     print ("```") # for using the output as markdown
 
 
+def json_milestones(budget_graph):
+    """reports milestones as json format
+    """
+    for milestone, payments in budget_graph.milestone_payments.items():
+        summary = PaymentSummary(payments)
+        # and one to display people
+        ppl = []
+        for person in budget_graph.milestone_people[milestone]:
+            p = {'name': person.full_name, 'email': person.email}
+            ppl.append(p)
+
+        tasks = []
+        canonical = budget_graph.nodes[milestone.canonical_bug_id]
+        for child in canonical.immediate_children:
+            milestones = []
+            # include the task itself as a milestone
+            for st in [child] + list(child.children()):
+                amount = st.fixed_budget_including_subtasks.int()
+                if amount == 0: # skip anything at zero
+                    continue
+                task = {'description': "%d %s" % (st.bug.id, st.bug.summary),
+                        'intro': [],
+                        'amount': amount,
+                        'url': st.bug_url,
+                       }
+                # add parent and MoU top-level
+                parent_id = st.parent.bug.id,
+                if parent_id != milestone.canonical_bug_id:
+                    task['parent'] = parent_id
+                mou_bug = st.closest_bug_in_mou
+                if mou_bug is not None:
+                    task['mou_task'] = mou_bug.bug.id
+                milestones.append(task)
+            # create MoU task
+            task = {'title': "%d %s" % (child.bug.id, child.bug.summary),
+                    'intro': [],
+                    'amount': child.fixed_budget_including_subtasks.int(),
+                    'url': child.bug_url,
+                    'milestones': milestones
+                   }
+            tasks.append(task)
+
+        d = {'participants': ppl,
+             'preamble': '',
+             'type': 'Group',
+             'plan': { 'intro': [''],
+                       'tasks': tasks,
+                       'rfp_secret': '',
+                     }
+             }
+
+        with open("report.%s.json" % milestone.identifier, "w") as f:
+            json.dump(d, f, indent=2)
+
+
+
 if __name__ == "__main__":
     main()
index 7f62b7a2318eadbf55e5b9832c28207ada006690..772e8ca02d43fcc19061a0a126e10807589e893a 100644 (file)
@@ -81,6 +81,9 @@ class Money:
             cents = -cents
         return Money(cents=cents)
 
+    def int(self):
+        return int(str(self))
+
     def __str__(self):
         retval = "-" if self.cents < 0 else ""
         retval += str(abs(self.cents) // CENTS_PER_EURO)