#!/usr/bin/env python

# Hook to modify tasks added from BugWarrior in order
# to standarise URL fields coming from different "services"
# See:
# https://taskwarrior.org/docs/hooks.html
# https://bugwarrior.readthedocs.io/en/latest/

import json

url_attrs = [
  'githuburl', 'gitlaburl', 'redmineurl', 'phabricatorurl',
]

def process_task(task):
    """Output a tuple with modified task and a message or None)"""
    msg = None
    for url_attr in url_attrs:
        if url_attr in task:
            task['task_url'] = task[url_attr]
            msg = 'Added task_url from {}'.format(url_attr)
    return (task, msg)

def main():
    import sys
    for line in sys.stdin:
        task = json.loads(line)
        task, msg = process_task(task)
        # Output modified task
        sys.stdout.write(json.dumps(task))
        # Output msg if any was returned
        if msg:
            sys.stdout.write('\n{}'.format(msg))

if __name__ == '__main__':
    main()
