Project

General

Profile

Feature #3346 » cross-project_revision_and_commit_links_v4.diff

Patch v4 - Adding unit tests and wiki syntax help - compatible with r2845 - Babar O'Cap, 2009-08-20 12:31

View differences:

app/helpers/application_helper.rb
29 29
  def_delegators :wiki_helper, :wikitoolbar_for, :heads_for_wiki_formatter
30 30

  
31 31
  # Return true if user is authorized for controller/action, otherwise false
32
  def authorize_for(controller, action)
33
    User.current.allowed_to?({:controller => controller, :action => action}, @project)
32
  def authorize_for(controller, action, project = @project)
33
    User.current.allowed_to?({:controller => controller, :action => action}, project)
34 34
  end
35 35

  
36 36
  # Display a link if user is authorized
......
422 422
    #     #52 -> Link to issue #52
423 423
    #   Changesets:
424 424
    #     r52 -> Link to revision 52
425
    #     r:project:52 -> Link to revision 52 of an other project, using project name or identifier
425 426
    #     commit:a85130f -> Link to scmid starting with a85130f
427
    #     commit:project:a85130f -> Link to scmid starting with a85130f of an other project, using project name or identifier
426 428
    #   Documents:
427 429
    #     document#17 -> Link to document with id 17
428 430
    #     document:Greetings -> Link to the document with title "Greetings"
......
441 443
    #     export:some/file -> Force the download of the file
442 444
    #  Forum messages:
443 445
    #     message#1218 -> Link to message with id 1218
444
    text = text.gsub(%r{([\s\(,\-\>]|^)(!)?(attachment|document|version|commit|source|export|message)?((#|r)(\d+)|(:)([^"\s<>][^\s<>]*?|"[^"]+?"))(?=(?=[[:punct:]]\W)|,|\s|<|$)}) do |m|
445
      leading, esc, prefix, sep, oid = $1, $2, $3, $5 || $7, $6 || $8
446
    text = text.gsub(%r{([\s\(,\-\>]|^)(!)?(attachment|document|version|commit|source|export|message)?((#|r)(:[^\s<>][^\:\r\n\b]+[^\s<>]:)?(\d+)|(:)([^\s<>][^\:\r\n\b]+[^\s<>]:)?([^"\:\s<>][^\:\s\r\n\b<>]*?|"[^"]+?"))(?=(?=[[:punct:]]\W)|,|\s|<|$)}) do |m|
447
      leading, esc, prefix, sep, revproj, oid = $1, $2, $3, $8 || $5, $9 || $6, $10 || $7
446 448
      link = nil
447 449
      if esc.nil?
448 450
        if prefix.nil? && sep == 'r'
449
          if project && (changeset = project.changesets.find_by_revision(oid))
450
            link = link_to("r#{oid}", {:only_path => only_path, :controller => 'repositories', :action => 'revision', :id => project, :rev => oid},
451
                                      :class => 'changeset',
452
                                      :title => truncate_single_line(changeset.comments, :length => 100))
451
          if revproj.nil?
452
            if project && (changeset = project.changesets.find_by_revision(oid))
453
              link = link_to("r#{oid}", {:only_path => only_path, :controller => 'repositories', :action => 'revision', :id => project, :rev => oid},
454
                                        :class => 'changeset',
455
                                        :title => truncate_single_line(changeset.comments, :length => 100))
456
            end
457
          else
458
            revproj.gsub!(/[\:"]/,'')
459
            link_project = Project.find_by_name(h(revproj)) || Project.find_by_identifier(h(revproj))
460
            if link_project && (changeset = link_project.changesets.find_by_revision(oid))              
461
              title = truncate_single_line(changeset.comments, :length => 100) if authorize_for('repositories', 'revision', link_project)
462
              link = link_to h("#{revproj}:r#{oid}"), {:only_path => only_path, :controller => 'repositories', :action => 'revision', :id => link_project, :rev => oid},
463
                                                      :class => 'changeset',
464
                                                      :title => title
465
            end            
453 466
          end
454 467
        elsif sep == '#'
455 468
          oid = oid.to_i
......
497 510
                                              :class => 'version'
498 511
            end
499 512
          when 'commit'
500
            if project && (changeset = project.changesets.find(:first, :conditions => ["scmid LIKE ?", "#{name}%"]))
501
              link = link_to h("#{name}"), {:only_path => only_path, :controller => 'repositories', :action => 'revision', :id => project, :rev => changeset.revision},
502
                                           :class => 'changeset',
503
                                           :title => truncate_single_line(changeset.comments, :length => 100)
513
            if revproj.nil?
514
              if project && (changeset = project.changesets.find(:first, :conditions => ["scmid LIKE ?", "#{name}%"]))
515
                link = link_to h("#{name}"), {:only_path => only_path, :controller => 'repositories', :action => 'revision', :id => project, :rev => changeset.revision},
516
                                             :class => 'changeset',
517
                                             :title => truncate_single_line(changeset.comments, :length => 100)
518
              end
519
            else
520
              revproj.gsub!(/[\:"]/,'')
521
              link_project = Project.find_by_name(h(revproj)) || Project.find_by_identifier(h(revproj))
522
              if link_project && (changeset = link_project.changesets.find(:first, :conditions => ["scmid LIKE ?", "#{name}%"]))
523
                title = truncate_single_line(changeset.comments, :length => 100) if authorize_for('repositories', 'revision', link_project)
524
                link = link_to h("#{revproj}:#{name}"), {:only_path => only_path, :controller => 'repositories', :action => 'revision', :id => link_project, :rev => changeset.revision},
525
                                                        :class => 'changeset',
526
                                                        :title => title
527
              end
504 528
            end
505 529
          when 'source', 'export'
506 530
            if project && project.repository
......
521 545
          end
522 546
        end
523 547
      end
524
      leading + (link || "#{prefix}#{sep}#{oid}")
548
      leading + (link || (revproj ? "#{revproj}:" : "") + "#{prefix}#{sep}#{oid}")
525 549
    end
526 550

  
527 551
    text
public/help/wiki_syntax_detailed.html
49 49
            <li>Link to a changeset with a non-numeric hash: <strong>commit:c6f4d0fd</strong> (displays c6f4d0fd).  Added in <a href="/repositories/revision/1?rev=1236" class="changeset" title="Merged Git support branch (r1200 to r1226).">r1236</a>.</li>
50 50
        </ul>
51 51

  
52
        <p>You can also link to changesets of an other project repository:</p>
53

  
54
        <ul>
55
            <li>Link to a changeset of SandBox project: <strong>r:sandbox:758</strong> (displays <a href="/repositories/revision/1?rev=758" class="changeset" title="Search engine now only searches objects the user is allowed to view.">sandbox:r758</a>)</li>
56
            <li>Link to a changeset of SandBox project with a non-numeric hash: <strong>commit:sandbox:c6f4d0fd</strong> (displays sandbox:c6f4d0fd).</li>
57
        </ul>
58

  
52 59
        <p>Wiki links:</p>
53 60

  
54 61
        <ul>
test/fixtures/changes.yml
20 20
  path: /test/some/path/in/the/repo
21 21
  from_path:
22 22
  from_revision:
23
changes_009:
24
  id: 9
25
  changeset_id: 108
26
  action: M
27
  path: /test/some/path/in/the/repo
28
  from_path:
29
  from_revision:
30
changes_0010:
31
  id: 10
32
  changeset_id: 109
33
  action: M
34
  path: /test/some/path/in/the/repo
35
  from_path:
36
  from_revision:
23 37
  
test/fixtures/changesets.yml
80 80
  user_id: 3
81 81
  repository_id: 10
82 82
  committer: dlopper
83
changesets_009:
84
  commit_date: 2007-04-11
85
  committed_on: 2007-04-11 15:14:44 +02:00
86
  revision: 1
87
  id: 108
88
  comments: My very first commit
89
  repository_id: 12
90
  committer: dlopper
91
  user_id: 3
92
changesets_010:
93
  commit_date: 2007-04-12
94
  committed_on: 2007-04-12 15:14:44 +02:00
95
  revision: 2
96
  id: 109
97
  comments: 'This commit fixes #5'
98
  repository_id: 12
99
  committer: dlopper
100
  user_id: 3
83 101
  
test/fixtures/repositories.yml
15 15
  password: ""
16 16
  login: ""
17 17
  type: Subversion
18

  
19
repositories_003:
20
  project_id: 3
21
  url: svn://localhost/test
22
  id: 12
23
  root_url: svn://localhost
24
  password: ""
25
  login: ""
26
  type: Subversion
test/unit/helpers/application_helper_test.rb
31 31

  
32 32
  def setup
33 33
    super
34
    @admin = User.find(1)
35
    @jsmith = User.find(2)
36
    @dlopper = User.find(3)
34 37
  end
35 38
  
36 39
  def test_auto_links
......
126 129
                                   :class => 'changeset', :title => 'My very first commit')
127 130
    changeset_link2 = link_to('r2', {:controller => 'repositories', :action => 'revision', :id => 'ecookbook', :rev => 2},
128 131
                                    :class => 'changeset', :title => 'This commit fixes #1, #2 and references #1 & #3')
129
    
132

  
133
    changeset_link_other_project_id = link_to('subproject1:r1', {:controller => 'repositories', :action => 'revision', :id => 'subproject1', :rev => 1},
134
                                    :class => 'changeset', :title => 'My very first commit')
135

  
136
    changeset_link_other_project_name = link_to('eCookbook Subproject 1:r2', {:controller => 'repositories', :action => 'revision', :id => 'subproject1', :rev => 2},
137
                                    :class => 'changeset', :title => 'This commit fixes #5')
138

  
130 139
    document_link = link_to('Test document', {:controller => 'documents', :action => 'show', :id => 1},
131 140
                                             :class => 'document')
132 141
    
......
146 155
      'r1.'                         => "#{changeset_link}.",
147 156
      'r1, r2'                      => "#{changeset_link}, #{changeset_link2}",
148 157
      'r1,r2'                       => "#{changeset_link},#{changeset_link2}",
158
      'r:subproject1:1'                         => "#{changeset_link_other_project_id}",
159
      'r:subproject1:1.'                         => "#{changeset_link_other_project_id}.",
160
      'r:subproject1:1,r:eCookbook Subproject 1:2'                       => "#{changeset_link_other_project_id},#{changeset_link_other_project_name}",
149 161
      # documents
150 162
      'document#1'                  => document_link,
151 163
      'document:"Test document"'    => document_link,
......
184 196
      "http://foo.bar/FAQ#3"       => '<a class="external" href="http://foo.bar/FAQ#3">http://foo.bar/FAQ#3</a>',
185 197
    }
186 198
    @project = Project.find(1)
199
    User.current = @admin
200
    
187 201
    to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text) }
202

  
203
    User.current = nil
188 204
  end
189 205
  
190 206
  def test_wiki_links
......
277 293
              }
278 294
    to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text) }
279 295
  end
280
  
296

  
281 297
  def test_wiki_horizontal_rule
282 298
    assert_equal '<hr />', textilizable('---')
283 299
    assert_equal '<p>Dashes: ---</p>', textilizable('Dashes: ---')
(6-6/7)