1
|
"""
|
2
|
Pandoc est requis pour convertir le html en textile !
|
3
|
"""
|
4
|
|
5
|
import url_parser
|
6
|
import urllib.request
|
7
|
import httplib2
|
8
|
import json
|
9
|
import re
|
10
|
import os
|
11
|
|
12
|
SUBMITERS = {'jenselme': '4a36554437feef722e82a206de1bc1c1250df973'}
|
13
|
Headers = {'content-type': 'application/json', 'X-Redmine-API-Key': ''}
|
14
|
URL = 'http://localhost/redmine/issues'
|
15
|
LIST_TODO = 'http://localhost/portail/liste-tache'
|
16
|
BASE_URL = 'http://localhost/portail'
|
17
|
PROJECT_ID = 1
|
18
|
TRACKER_ID = 2
|
19
|
DONE_RATIO = {'À commencer': 0, 'Entamée': 20, 'Bien avancé': 80, 'Terminée (success)': 100, 'Fermée (won\'t fix)': 100}
|
20
|
PRIORITY = {'5 - Très basse': 3, '4 - Basse': 3, '3 - Moyenne': 4, '2 - Haute': 5, '1 - Très haute': 6,\
|
21
|
'0': 3, '1': 3, '2': 4, '3': 5, '4': 6}
|
22
|
STATUS = {'En cours': 2, 'Fermée': 5, 'Rejetée': 6, 'En pause': 7}
|
23
|
DRUPAL_VERSION = {'17': [{"id":1, "value": "1"}], '18': [{"id":1, "value": "2"}]}
|
24
|
|
25
|
def give_api_key(submiter):
|
26
|
if submiter in SUBMITERS:
|
27
|
return SUBMITERS[submiter]
|
28
|
else:
|
29
|
return SUBMITERS['jenselme']
|
30
|
|
31
|
|
32
|
def give_comments_ids(nid):
|
33
|
page = urllib.request.urlopen(BASE_URL + 'entity_json/node/' + nid)
|
34
|
page_json = json.load(page)
|
35
|
return list(page_json['comments'].keys())
|
36
|
|
37
|
|
38
|
def give_comments(cids):
|
39
|
comments = list()
|
40
|
for cid in cids:
|
41
|
comment = urllib.request.urlopen(BASE_URL + '/comment/' + cid + '.json').read()
|
42
|
comments.append(json.load(comment))
|
43
|
return comments
|
44
|
|
45
|
|
46
|
def format(txt):
|
47
|
txt.replace('\n', '')
|
48
|
txt.replace('\t', '')
|
49
|
with open('tmp.html', 'w') as f:
|
50
|
f.write(txt)
|
51
|
os.system('pandoc -f html tmp.html -t textile -o tmp.textile')
|
52
|
with open('tmp.textile', 'r') as f:
|
53
|
txt = f.read()
|
54
|
return txt
|
55
|
|
56
|
|
57
|
def give_redmine_status_id(tache):
|
58
|
drupal_status = ''
|
59
|
for elt in tache['field_avancement']:
|
60
|
if "Terminée" in elt:
|
61
|
drupal_status = 'Fermée'
|
62
|
break
|
63
|
elif "Fermée" in elt:
|
64
|
drupal_status = 'Rejetée'
|
65
|
break
|
66
|
elif "pause" in elt:
|
67
|
drupal_status = 'En pause'
|
68
|
del elt
|
69
|
break
|
70
|
return STATUS[drupal_status]
|
71
|
|
72
|
|
73
|
def give_redmine_issue(tache):
|
74
|
issue = dict()
|
75
|
issue['project_id'] = PROJECT_ID
|
76
|
issue['tracker_id'] = TRACKER_ID
|
77
|
issue['subject'] = tache['title']
|
78
|
issue['description'] = format(tache['body']['value'])
|
79
|
issue['priority_id'] = PRIORITY[tache['field_prioritaecute']]
|
80
|
issue['done_ratio'] = DONE_RATIO[tache['field_avancement']]
|
81
|
issue['status_id'] = give_redmine_status_id(tache)
|
82
|
tache['custom_fields'] = DRUPAL_VERSION[tache['taxonomy_term_8']]
|
83
|
|
84
|
|
85
|
nids, urls = url_parser.give_json_urls(LIST_TODO, BASE_URL)
|
86
|
|
87
|
h = httplib2.Http()
|
88
|
|
89
|
for post_url in urls:
|
90
|
nid = nids[urls.index(post_url)]
|
91
|
tache_json = urllib.request.urlopen(post_url)
|
92
|
tache_drupal = json.load(tache_json)
|
93
|
|
94
|
cids = give_comments_ids(nid)
|
95
|
comments_drupal = give_comments(cids)
|
96
|
|
97
|
issue = {}
|
98
|
issue['issue'] = give_redmine_issue(tache_drupal)
|
99
|
data = json.dumps(issue)
|
100
|
|
101
|
Headers['X-Redmine-API-Key'] = SUBMITERS['jenselme']
|
102
|
|
103
|
resp, content = h.request(URL + '.json', 'POST', body=data, headers=Headers)
|
104
|
|
105
|
|
106
|
iid = re.findall(r',"id":([0-9]*),', content.decode('utf-8'))[0]
|
107
|
|
108
|
|
109
|
put_url = ''
|
110
|
for index, comment in enumerate(comments_drupal):
|
111
|
submiter = comment['name']
|
112
|
Headers['X-Redmine-API-Key'] = give_api_key(submiter)
|
113
|
|
114
|
comment_body = format(comment['comment_body']['value'])
|
115
|
if not submiter in SUBMITERS:
|
116
|
comment_body = "{} a dit que : {}".format(submiter, comment_body)
|
117
|
update = {}
|
118
|
update['issue'] = {'notes': comment_body}
|
119
|
data = json.dumps(update)
|
120
|
put_url = URL + '/' + iid + '.json'
|
121
|
h.request(put_url, 'PUT', body=data, headers=Headers)
|
122
|
|
123
|
|
124
|
|
125
|
update_status = {'issue': {'status_id': issue['issue']['status_id']}}
|
126
|
data = json.dumps(update_status)
|
127
|
h.request(put_url, 'PUT', body=data, headers=Headers)
|