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