-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlink.py
118 lines (97 loc) · 2.89 KB
/
link.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
from flask import Flask, request, json, Response
from jira import JIRA
import todoist
import perm
import uuid
import logging
logging.captureWarnings(True)
# setup Jira
jira = JIRA('https://circlelabs.atlassian.net', basic_auth=(perm.USERNAME, perm.PASSWORD))
# setup todoist
api = todoist.TodoistAPI(perm.TODOISTAPI)
app = Flask(__name__)
app.config.from_object(__name__)
@app.route('/', methods=['POST'])
def todoist():
json = request.get_json()
todoId = json['event_data']['id']
print(json)
text = json['event_data']['content']
type = json['event_name']
issue = jira.search_issues('project = TEST AND "Todoist ID" ~ "'+ str(todoId) +'"')
if type == 'item:added':
if len(issue) == 0:
print('Adding Jira Issue')
new_issue = jira.create_issue(project='TEST', summary=text, customfield_10025=str(todoId), issuetype={'name':'Task'})
return ''
if len(issue) == 0:
return 'No jira issue to act on'
issue = issue[0]
if type == 'item:completed':
print('Completing Jira Issue')
jira.transition_issue(issue, '71')
if type == 'item:uncompleted':
print('Reverting Jira Issue')
jira.transition_issue(issue, '101')
if type == 'item:deleted':
print('Deleting Jira Issue')
issue.delete()
return ''
@app.route('/test')
def test():
issue = jira.issue('TODO-40')
trans = jira.transitions(issue)
return Response(json.dumps(trans), 200, {'Content-Type':'application/json'})
# from open
# to backlog = 11 someday
# to To Do = 21 next
# To Start = 31 today
# Unecssary = 81 complete
#
# From next
# to Inprogress = 41 today
# to Done = 61 complete
#
# From today
# to Done = 71
#
# From done to open = 101
@app.route('/jira', methods=['POST'])
def main():
json = request.get_json()
title = json['issue']['fields']['summary']
desc = json['issue']['fields']['description']
todoId = json['issue']['fields']['customfield_10025']
if desc == None:
desc = ''
print(todoId == None)
event = json.get('webhookEvent', None)
if event != None:
if event.find('issue_created') != -1:
# text = title + "\n" + desc
text = title
if (todoId == None):
print('Creating Todoist Issue')
unique = uuid.uuid4()
temp = uuid.uuid4()
item = api.sync(commands=[{'type':'item_add', 'temp_id':str(temp), 'uuid':str(unique), 'args':{'content':text}}])
todoId = str(item['TempIdMapping'][str(temp)])
jira.issue(json['issue']['key']).update(customfield_10025=todoId)
elif event.find('issue_deleted') != -1:
print('Deleting Todoist Issue')
item = api.items.get_by_id(todoId)
item.delete()
trans = json.get('transition', None)
if trans != None:
if trans['to_status'] == 'Completed':
print('Completing Todoist Item')
item = api.items.get_by_id(todoId)
item.complete()
elif trans['to_status'] == 'Inbox':
print('Reverting Todoist Item')
item = api.items.get_by_id(todoId)
item.uncomplete()
api.commit()
return ''
if __name__ == '__main__':
app.run(debug=True)