Commit d0835e21 authored by Thomas Lotze's avatar Thomas Lotze

re #1: optionally create csv reports

parent 13c7e332
from gitlabtime.fetch import fetch
from import report
from import report_csv
import click
......@@ -26,14 +27,21 @@ import click
help='Filter for user name',
def cli(url, token, month, project, username):
help='path to CSV output file',
def cli(url, token, month, project, username, csv):
filter = dict(
entries = fetch(url, token, filter)
if csv:
report_csv(entries, csv)
def main():
from collections import defaultdict
from gitlab.v4.objects import ProjectIssue
from gitlab.v4.objects import ProjectMergeRequest
import csv
def report(entries_by_project):
def report_csv(entries_by_project, csvpath):
"""Print a report and create a CSV file.
with open(csvpath, 'w', newline='') as csvfile:
csvwriter = csv.DictWriter(
['Project', 'Issue/MR', 'Author', 'Date', 'Hours'],
def writerow(project, item, author, date, hours):
'Project': project.name_with_namespace,
'Issue/MR': '{}: {}'.format(
item.references['short'], item.title),
'Author': author,
'Date': date,
'Hours': hours,
report(entries_by_project, writerow)
def report(entries_by_project, writerow=lambda *args: None):
"""Aggregate unordered sequences of time tracking entries into a report.
total = sum(report_project(project, entries)
total = sum(report_project(project, entries, writerow)
for project, entries in entries_by_project)
print('total: {:.2f}'.format(total))
def report_project(project, entries):
def report_project(project, entries, writerow):
# When aggregating things by item, we cannot simply use the item as a dict
# key: Both issues and merge requests just use their iid as a hash and
# those number sets do overlap, so mixing them as keys in the same dict
......@@ -49,6 +74,7 @@ def report_project(project, entries):
for date, hours in sorted(time_log):
print('{}{}: {:6.2f} ({})'.format(
8*' ', date, hours, author))
writerow(project, item, author, date, hours)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment