Révision 8a509595
Ajouté par Julien Enselme il y a presque 11 ans
scripts_divers/migrer_taches_vers_redmine/goto_redmine.py | ||
---|---|---|
1 |
import node_parser |
|
2 |
import comments_parser |
|
3 |
import submiters_parser |
|
4 |
import url_parser |
|
1 |
""" |
|
2 |
Pandoc est requis pour convertir le html en textile ! |
|
3 |
""" |
|
4 |
|
|
5 |
import url_parser #permet de connaître les id des taches |
|
6 |
import urllib.request |
|
5 | 7 |
import httplib2 |
6 | 8 |
import json |
7 | 9 |
import re |
10 |
import os |
|
8 | 11 |
|
9 |
SUBMITERS = {'jenselme': 'auieeui'}
|
|
12 |
SUBMITERS = {'jenselme': '4a36554437feef722e82a206de1bc1c1250df973'}
|
|
10 | 13 |
Headers = {'content-type': 'application/json', 'X-Redmine-API-Key': ''} |
11 |
MY_API_KEY = '4a36554437feef722e82a206de1bc1c1250df973' |
|
12 | 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"}]} |
|
13 | 24 |
|
14 | 25 |
def give_api_key(submiter): |
15 | 26 |
if submiter in SUBMITERS: |
... | ... | |
17 | 28 |
else: |
18 | 29 |
return SUBMITERS['jenselme'] |
19 | 30 |
|
20 |
nids = url_parser.give_nids() |
|
21 |
urls = url_parser.give_urls(nids) |
|
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) |
|
22 | 86 |
|
23 | 87 |
h = httplib2.Http() |
24 | 88 |
|
25 | 89 |
for post_url in urls: |
26 | 90 |
nid = nids[urls.index(post_url)] |
27 |
resp, page = h.request(post_url, 'GET') |
|
28 |
page = page.decode('utf-8') |
|
29 |
txt = page.read() |
|
30 |
#tout les scripts fonctionnent en lisant ligne à ligne le fichier. |
|
31 |
#la liste input permet de mimer ce comportement |
|
32 |
input = txt.split('\n') |
|
33 |
|
|
34 |
submiters = submiters_parser.parse_submiters(input) |
|
35 |
node = node_parser.parse_node(input) |
|
36 |
comments = comments_parser.parse_comment(input) |
|
91 |
tache_json = urllib.request.urlopen(post_url) |
|
92 |
tache_drupal = json.load(tache_json) |
|
37 | 93 |
|
38 |
#on a besoin de drupal_done_ratio pour fermer/rejeter une tache
|
|
39 |
drupal_done_ratio = node['done_ratio']
|
|
94 |
cids = give_comments_ids(nid)
|
|
95 |
comments_drupal = give_comments(cids)
|
|
40 | 96 |
|
41 | 97 |
issue = {} |
42 |
issue['issue'] = node
|
|
98 |
issue['issue'] = give_redmine_issue(tache_drupal)
|
|
43 | 99 |
data = json.dumps(issue) |
44 | 100 |
|
45 |
Headers['X-Redmine-API-Key'] = give_api_key(submiters[0])
|
|
101 |
Headers['X-Redmine-API-Key'] = SUBMITERS['jenselme']
|
|
46 | 102 |
|
47 | 103 |
resp, content = h.request(URL + '.json', 'POST', body=data, headers=Headers) |
48 | 104 |
|
105 |
#on récupère l’issue id pour savoir où poster les commentaires |
|
49 | 106 |
iid = re.findall(r',"id":([0-9]*),', content.decode('utf-8'))[0] |
50 | 107 |
|
51 | 108 |
#on a besoin de l’url à laquelle on met les commentaires, pour changer le status |
52 | 109 |
put_url = '' |
53 |
for index, comment in enumerate(comments): |
|
54 |
submiter = submiters[index + 1] #le premier est celui qui a soumis le node
|
|
110 |
for index, comment in enumerate(comments_drupal):
|
|
111 |
submiter = comment['name'] #le premier est celui qui a soumis le node
|
|
55 | 112 |
Headers['X-Redmine-API-Key'] = give_api_key(submiter) |
56 | 113 |
#si la personne n’a pas sa clé, on modifie le commentaire |
114 |
comment_body = format(comment['comment_body']['value']) |
|
57 | 115 |
if not submiter in SUBMITERS: |
58 |
comment = "{} a dit que {}".format(submiter, comment)
|
|
116 |
comment_body = "{} a dit que : {}".format(submiter, comment_body)
|
|
59 | 117 |
update = {} |
60 |
update['issue'] = {'notes': comment} |
|
118 |
update['issue'] = {'notes': comment_body}
|
|
61 | 119 |
data = json.dumps(update) |
62 | 120 |
put_url = URL + '/' + iid + '.json' |
63 | 121 |
h.request(put_url, 'PUT', body=data, headers=Headers) |
64 | 122 |
|
65 | 123 |
#Les taches sont crées avec le status nouveau peu importe ce qu’il y a dans le json |
66 | 124 |
#on modifie le status après coup |
67 |
update_status = {'issue': {'status_id': node['status_id']}}
|
|
125 |
update_status = {'issue': {'status_id': issue['issue']['status_id']}}
|
|
68 | 126 |
data = json.dumps(update_status) |
69 |
h.request(put_url, 'PUT', body=data, headers=Headers) |
|
127 |
h.request(put_url, 'PUT', body=data, headers=Headers) |
Formats disponibles : Unified diff
On utilise désormais les modules drupal entity_json (pour la liste des commentaires)
et restws (pour récupérer le contenu des nodes et des commentaires)