#! /usr/bin/env python3

import argparse
import json
import time

from perceval.backends.core.github import GitHub

# token = ["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"]
token = ["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", "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", "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", "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", "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", "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", "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", "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", "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", "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"]

#gregorio robles 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
#francisco servant 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",
#dmorenobi - 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 - dmoreno.bitergia@gmail.com
#jabapipes - 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 - jabapipes@gmail.com
#dmhtmail - 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 - davidmorenolumb@hotmail.com
#Johan Linaker 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
#Míchel Maes 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
#Micael Gallego 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
#Patxi Gortázar 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
#Alex Echaniz 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

# Parse command line arguments
parser = argparse.ArgumentParser(
    description = "Simple parser for GitHub issues and pull requests"
    )
# parser.add_argument("-t", "--token",
#                     '--nargs', nargs='+',
#                     help = "GitHub token")
parser.add_argument("-r", "--repo",
                    help = "GitHub repository, as 'owner/repo'")
args = parser.parse_args()

# Owner and repository names
(owner, repo) = args.repo.split('/')

# create a Git object, pointing to repo_url, using repo_dir for cloning
repo = GitHub(owner=owner, repository=repo, api_token=token)

# fetch all issues/pull requests as an iterator, and iterate it printing
# their number, and whether they are issues or pull requests

try:
    timeinfo = time.gmtime()
    timevalue = "{:04d}{:02d}{:02d}".format(timeinfo.tm_year, timeinfo.tm_mon, timeinfo.tm_mday)
    with open(args.repo.replace("/", "_") + "-" + timevalue + "-issues.json", 'a') as ioutput:
        with open(args.repo.replace("/", "_") + "-" + timevalue + "-pulls.json", 'a') as poutput:
            for item in repo.fetch():
                if 'pull_request' in item['data']:
                    kind = 'Pull request'
                    json.dump(item['data'], poutput, indent=4)
                else:
                    kind = 'Issue'
                    json.dump(item['data'], ioutput, indent=4)


# should anything go wrong, log it in an error file
except Exception as Argument:
    timevalue = "{:04d}{:02d}{:02d}".format(timeinfo.tm_year, timeinfo.tm_mon, timeinfo.tm_mday)
    with open(args.repo.replace("/", "_") + "-" + timevalue + ".errors.txt", 'a') as errorsOutput:
        errorsOutput.write(str(Argument))

