Projet

Général

Profil

Révision 63ffb014

Ajouté par Julien Enselme il y a plus de 10 ans

fix-cas.rb: amélioration pour la correction des liens.

Voir les différences:

other-scripts/migrate-issues-to-redmine/fix-db.rb
16 16
end
17 17

  
18 18

  
19
def fix_issues_link(text, name2iid)
19
def fix_issues_comment_link(name, cid2iid, cid2post_nb)
20
  # Fix links like : task-name#comment-789
21
  scan_regex = /.*#comment-(\d+)/
22
  cid = name.scan(scan_regex).flatten[0]
23
  post_nb = custom_chomp(cid2post_nb[cid])
24
  return "##{cid2iid[cid]}#note-#{post_nb}"
25
end
26

  
27

  
28
def fix_issues_link(text, name2iid, cid2iid, cid2post_nb)
20 29
  # Find all links in text
21
  scan_regex =  /:http:\/\/assos\.centrale-marseille\.fr(?:\/|\/lessive\/)(?:content(?:\/t%C3%A2che)?|node)\/([^ \n)]*)/
30
  scan_regex =  /:http:\/\/assos\.centrale-marseille\.fr(?:\/|\/lessive\/)(?:content(?:\/t(?:%C3%A2|â)che)?|node)\/([^ \n.,\/)]*)/
22 31

  
23 32
  # Non flattened output is [['some'], ['text']]
24 33
  # flattened output is ['some', 'text']
......
27 36
  if not names.empty?
28 37
    names.each do |name|
29 38
      name = custom_chomp(name)
39
      if name.include? 'é' or name.include? 'è' or name.include? 'ê' or name.include? 'à' or name.include? 'ô' or name.include? 'â' or name.include? 'ù' or name.include? 'û'
40
        name = URI.escape(name)
41
      end
30 42
      unescaped_name = URI.unescape(name)
31
      gsub_regex = /"http:\/\/assos\.centrale-marseille\.fr(?:\/|\/lessive\/)(?:content(?:\/t%C3%A2che)?|node)\/[^ ]*":http:\/\/assos\.centrale-marseille\.fr(?:\/|\/lessive\/)(?:content(?:\/t%C3%A2che)?|node)\/#{name}/
32
      remplace_regex = "#{unescaped_name} : ##{name2iid[name]}"
33
      text.gsub!(gsub_regex, remplace_regex)
43
      gsub_regex = /"http:\/\/assos\.centrale-marseille\.fr(?:\/|\/lessive\/)(?:content(?:\/t(?:%C3%A2|â)che)?|node)\/[^ ]*":http:\/\/assos\.centrale-marseille\.fr(?:\/|\/lessive\/)(?:content(?:\/t(?:%C3%A2|â)che)?|node)\/#{name}/
44
      # Some link may have a description between "…"
45
      if (text =~ gsub_regex)
46
        # Some comments are like this : task-name#comment-nb.
47
        # We fix them here
48
        if name.include? '#comment-'
49
          remplace_regex = fix_issues_comment_link(name, cid2iid, cid2post_nb)
50
        else
51
          remplace_regex = "#{unescaped_name} : ##{name2iid[name]}"
52
        end
53
        text.gsub!(gsub_regex, remplace_regex)
54
      else
55
        scan_regex = /"([^"]*)":http:\/\/assos\.centrale-marseille\.fr(?:\/|\/lessive\/)(?:content(?:\/t(?:%C3%A2|â)che)?|node)\/(?:[^ \n)]*)/
56
        description = text.scan(scan_regex).flatten[0]
57
        gsub_regex = /"#{description}":http:\/\/assos\.centrale-marseille\.fr(?:\/|\/lessive\/)(?:content(?:\/t(?:%C3%A2|â)che)?|node)\/#{name}/
58
        if name.include? '#comment-'
59
          remplace_regex = fix_issues_comment_link(name, cid2iid, cid2post_nb)
60
          remplace_regex = "#{description} (" + remplace_regex + ')'
61
        else
62
          remplace_regex = "#{description} (##{name2iid[name]})"
63
        end
64
        text.gsub!(gsub_regex, remplace_regex)
65
      end
34 66
    end
35 67
  end
36 68
  return text
......
47 79
      cid = custom_chomp(cid)
48 80
      gsub_regex = /"http:\/\/assos\.centrale-marseille\.fr(?:\/|\/lessive\/)comment\/.*":http:\/\/assos\.centrale-marseille\.fr(?:\/|\/lessive\/)comment\/#{cid}#comment-\d+/
49 81
      post_nb = custom_chomp(cid2post_nb[cid])
50
      remplace_regex = "##{cid2iid[cid]}#note-#{post_nb}"
51
      text.gsub!(gsub_regex, remplace_regex)
82
      # Some link may have a description between "…"
83
      if (text =~ gsub_regex)
84
        remplace_regex = "##{cid2iid[cid]}#note-#{post_nb}"
85
        text.gsub!(gsub_regex, remplace_regex)
86
      else
87
        scan_regex = /"([^"]*)":http:\/\/assos\.centrale-marseille\.fr(?:\/|\/lessive\/|\/portail\/)comment\/(?:\d+)/
88
        description = text.scan(scan_regex).flatten[0]
89
        gsub_regex = /"#{description}":http:\/\/assos\.centrale-marseille\.fr(?:\/|\/lessive\/)comment\/#{cid}#comment-\d+/
90
        remplace_regex = "#{description} (##{cid2iid[cid]}#note-#{post_nb})"
91
        text.gsub!(gsub_regex, remplace_regex)
92
      end
52 93
    end
53 94
  end
54 95
  return text
......
68 109

  
69 110
  ###### Initialisation of hashes needed to fix URL
70 111
  name2iid = Hash.new
71
  nid2iid = Hash.new
72 112
  cid2iid = Hash.new
73 113
  cid2post_nb = Hash.new
74 114

  
......
76 116
  fix_url_issues_csv.each do |line|
77 117
    line = line.chomp
78 118
    nid, name, iid = line.split(',')
79
    name2iid[name] = nid
80
    nid2iid[nid] = iid
119
    name2iid[name] = iid
120
    name2iid[nid] = iid
81 121
  end
82 122
  fix_url_issues_csv.close
83 123

  
......
113 153
    req_select_description.execute(iid)
114 154

  
115 155
    description = req_select_description.fetch[0]
116
    description = fix_issues_link(description, name2iid)
156
    description = fix_issues_link(description, name2iid, cid2iid, cid2post_nb)
117 157

  
118 158
    # Comments
119 159
    description = fix_comments_link(description, cid2iid, cid2post_nb)
......
168 208
    # issues
169 209
    req_select_notes.execute(id)
170 210
    notes = req_select_notes.fetch[0]
171
    notes = fix_issues_link(notes, name2iid)
211
    notes = fix_issues_link(notes, name2iid, cid2iid, cid2post_nb)
172 212

  
173 213
    # Comments
174 214
    notes = fix_comments_link(notes, cid2iid, cid2post_nb)

Formats disponibles : Unified diff