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