Projet

Général

Profil

Révision 3a9aefa9

Ajouté par Julien Enselme il y a presque 11 ans

Passe les tests avec succès.

Voir les différences:

scripts_divers/migrer_taches_vers_redmine/goto_redmine.py
9 9
import re
10 10
import os
11 11

  
12
SUBMITERS = {'jenselme': '4a36554437feef722e82a206de1bc1c1250df973'}
12
SUBMITERS = {'jenselme': 'a7630a1244be353424cc0f56a49657c3fa9dbcc6'}
13 13
Headers = {'content-type': 'application/json', 'X-Redmine-API-Key': ''}
14 14
URL = 'http://localhost/redmine/issues'
15 15
LIST_TODO = 'http://localhost/portail/liste-tache'
16 16
BASE_URL = 'http://localhost/portail'
17
PROJECT_ID = 1
17
PROJECT_ID = 2
18 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}
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 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}
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}
22 23
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
#NB : 17 : drupal6, 18 : drupal7
25
DRUPAL_VERSION = {'17': 4, '18': 5}
24 26

  
25 27
def give_api_key(submiter):
26 28
    if submiter in SUBMITERS:
......
30 32

  
31 33

  
32 34
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())
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
    #S’il n’y a pas de commentaire, comments_json est une liste vide et pas un dictionnaire
39
    if comments_json:
40
        comments = list(comments_json.keys())
41
        comments.sort() #ce sont les clés d’un dictionnaire. Pas d’ordre à priori
42
        return comments
43
    else:
44
        return list()
36 45

  
37 46

  
38 47
def give_comments(cids):
39 48
    comments = list()
40 49
    for cid in cids:
41 50
        comment = urllib.request.urlopen(BASE_URL + '/comment/' + cid + '.json').read()
42
        comments.append(json.load(comment))
51
        comments.append(json.loads(comment.decode('utf-8')))
43 52
    return comments
44 53

  
45 54

  
......
67 76
            drupal_status = 'En pause'
68 77
            del elt
69 78
            break
79
    if not drupal_status:
80
        drupal_status = 'En cours'
70 81
    return STATUS[drupal_status]
71 82

  
72 83

  
......
76 87
    issue['tracker_id'] = TRACKER_ID
77 88
    issue['subject'] = tache['title']
78 89
    issue['description'] = format(tache['body']['value'])
79
    issue['priority_id'] = PRIORITY[tache['field_prioritaecute']]
80
    issue['done_ratio'] = DONE_RATIO[tache['field_avancement']]
90
    #de temps en temps, le champ priorité est vide. On met 'Normale' dans ce cas
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']
81 99
    issue['status_id'] = give_redmine_status_id(tache)
82
    tache['custom_fields'] = DRUPAL_VERSION[tache['taxonomy_term_8']]
100
    issue['fixed_version_id'] = DRUPAL_VERSION[tache['taxonomy_vocabulary_8']['id']]
101
    return issue
83 102

  
84 103

  
85 104
nids, urls = url_parser.give_json_urls(LIST_TODO, BASE_URL)
......
88 107

  
89 108
for post_url in urls:
90 109
    nid = nids[urls.index(post_url)]
110
    print(nid)
91 111
    tache_json = urllib.request.urlopen(post_url)
92
    tache_drupal = json.load(tache_json)
112
    tache_drupal = json.loads(tache_json.read().decode('utf-8'))
93 113

  
94 114
    cids = give_comments_ids(nid)
95 115
    comments_drupal = give_comments(cids)
......
106 126
    iid = re.findall(r',"id":([0-9]*),', content.decode('utf-8'))[0]
107 127

  
108 128
    #on a besoin de l’url à laquelle on met les commentaires, pour changer le status
109
    put_url = ''
129
    put_url = URL + '/' + iid + '.json'
110 130
    for index, comment in enumerate(comments_drupal):
111 131
        submiter = comment['name']  #le premier est celui qui a soumis le node
112 132
        Headers['X-Redmine-API-Key'] = give_api_key(submiter)
113 133
        #si la personne n’a pas sa clé, on modifie le commentaire
114 134
        comment_body = format(comment['comment_body']['value'])
115 135
        if not submiter in SUBMITERS:
116
            comment_body = "{} a dit que : {}".format(submiter, comment_body)
136
            comment_body = "_{}_ a dit que :\n\n{}".format(submiter, comment_body)
117 137
        update = {}
118 138
        update['issue'] = {'notes': comment_body}
119 139
        data = json.dumps(update)
120
        put_url = URL + '/' + iid + '.json'
121 140
        h.request(put_url, 'PUT', body=data, headers=Headers)
122 141

  
123 142
    #Les taches sont crées avec le status nouveau peu importe ce qu’il y a dans le json

Formats disponibles : Unified diff