2019-06-23 09:44:27 +00:00
#!/bin/env python
import argparse
import logging
import pytea
import sys
import json
import configparser
import marshmallow . validate
2019-08-07 17:38:56 +00:00
import re
2019-08-08 09:08:24 +00:00
import os
2019-06-23 09:44:27 +00:00
from dataclasses import field
from marshmallow_dataclass import dataclass
from typing import List , Optional
from pytodoist import todoist
from pprint import pprint
from types import SimpleNamespace as Namespace
2019-08-07 17:38:56 +00:00
from jira import JIRA
2019-08-08 09:08:24 +00:00
from pprint import pprint
2019-06-23 09:44:27 +00:00
config = configparser . ConfigParser ( )
config . read ( ' ./config.ini ' )
logging . basicConfig ( stream = sys . stderr , level = logging . INFO , format = ' %(asctime)s %(levelname)s %(filename)s : %(message)s ' )
logger = logging . getLogger ( " todoist-im " )
2019-08-08 09:08:24 +00:00
2019-06-23 09:44:27 +00:00
@dataclass
class GiteaUser :
avatar_url : str
full_name : str
id : int
is_admin : bool
language : str
login : str
username : str
@dataclass
class GiteaIssue :
id : int
number : int
assignee : Optional [ GiteaUser ]
body : str
user : Optional [ GiteaUser ]
title : str
url : str
2019-08-07 17:38:56 +00:00
2019-06-23 09:44:27 +00:00
# args = parser.parse_args()
# print(args.accumulate(args.integers))
gitea = pytea . API ( config [ ' auth ' ] [ ' gitea_url ' ] , token = config [ ' auth ' ] [ ' gitea_token ' ] )
todo = todoist . login ( config [ ' auth ' ] [ ' todoist_login ' ] , config [ ' auth ' ] [ ' todoist_pass ' ] )
2019-08-08 09:08:24 +00:00
jiraOptions = { ' server ' : config [ ' auth ' ] [ ' jira_url ' ] }
jira = JIRA ( options = jiraOptions , basic_auth = ( config [ ' auth ' ] [ ' jira_username ' ] , os . environ [ ' JIRA_API_TOKEN ' ] ) )
2019-06-23 09:44:27 +00:00
userJson = gitea . get ( " /user " ) . json ( )
user , _ = GiteaUser . Schema ( ) . load ( userJson )
2019-08-08 09:08:24 +00:00
for jiraProject in config [ ' jira_projects ' ] :
logger . info ( " Jira.... " + jiraProject )
totdoistProject = config [ ' jira_projects ' ] [ jiraProject ]
for issue in jira . search_issues ( ' project= ' + jiraProject . upper ( ) + ' and (assignee = currentUser() or assignee = unassigned) and status = " In Progress " and status = " To Do " ' ) :
addTask = True
#print('{}: {} === {} ==== {} === {}'.format(issue.key, issue.fields.summary, issue.fields.status, totdoistProject, issue.fields.description))
todoProject = todo . get_project ( totdoistProject )
tasks = todoProject . get_tasks ( )
for task in tasks :
if ( " [ " + issue . key + " ] " + issue . fields . summary ) == task . content :
logger . info ( " Task exists in todoist ... %r " , issue . fields . summary )
addTask = False
if addTask :
logger . info ( " Adding ... %r " , issue . fields . summary )
task = todoProject . add_task ( " [ " + issue . key + " ] " + issue . fields . summary )
task . add_note ( config [ ' auth ' ] [ ' jira_url ' ] + ' /browse/ ' + issue . key )
task . add_note ( " Description: " + str ( issue . fields . description ) )
2019-06-23 09:44:27 +00:00
for giteaProject in config [ ' gitea_projects ' ] :
2019-08-08 09:08:24 +00:00
logger . info ( " Scanning ... " + config [ ' auth ' ] [ ' gitea_url ' ] + " / " + giteaProject )
2019-06-23 09:44:27 +00:00
totdoistProject = config [ ' gitea_projects ' ] [ giteaProject ]
issuesJson = json . loads ( gitea . get ( " /repos/ " + giteaProject . strip ( ) + " /issues " , params = { " state " : " open " , " page " : 0 } ) . text )
todoProject = todo . get_project ( totdoistProject )
tasks = todoProject . get_tasks ( )
for issueJson in issuesJson :
addTask = True
issue , _ = GiteaIssue . Schema ( ) . load ( issueJson )
for task in tasks :
if issue . title == task . content :
logger . info ( " Task exists in todoist ... %r " , issue . title )
addTask = False
if addTask :
logger . info ( " Adding ... %r " , issue . title )
task = todoProject . add_task ( issue . title )
2019-08-07 17:43:42 +00:00
task . add_note ( config [ ' auth ' ] [ ' gitea_url ' ] + giteaProject + " /issues/ " + str ( issue . number ) )
2019-08-08 09:08:24 +00:00