Commit 5d6e79f4b97290829e4c22aecf91e57519107a5c
Exists in
master
and in
4 other branches
Merge pull request #237 from CedricGatay/feature/expand_issues_ref_in_commit
Autolinks to issues in commit message (see #155)
Showing
3 changed files
with
88 additions
and
2 deletions
Show diff stats
app/helpers/commits_helper.rb
... | ... | @@ -23,4 +23,24 @@ module CommitsHelper |
23 | 23 | link_to "More", project_commits_path(@project, :offset => offset.to_i + limit.to_i, :limit => limit), |
24 | 24 | :remote => true, :class => "lite_button vm", :style => "text-align:center; width:930px; ", :id => "more-commits-link" |
25 | 25 | end |
26 | + | |
27 | + def commit_msg_with_link_to_issues(project, message) | |
28 | + return '' unless message | |
29 | + out = '' | |
30 | + message.split(/(#[0-9]+)/m).each do |m| | |
31 | + if m =~ /(#([0-9]+))/m | |
32 | + begin | |
33 | + issue = Issue.find($2) | |
34 | + raise Exception('Issue not belonging to current project, not creating link !') unless issue.project_id == project.id | |
35 | + out += link_to($1, project_issue_path(project, $2)) | |
36 | + rescue | |
37 | + out += $1 | |
38 | + end | |
39 | + else | |
40 | + out += m | |
41 | + end | |
42 | + end | |
43 | + preserve out | |
44 | + end | |
45 | + | |
26 | 46 | end | ... | ... |
app/views/commits/show.html.haml
... | ... | @@ -0,0 +1,67 @@ |
1 | +require "spec_helper" | |
2 | +include Haml::Helpers | |
3 | + | |
4 | +describe CommitsHelper do | |
5 | + | |
6 | + before do | |
7 | + @project = Factory :project | |
8 | + @other_project = Factory :project, :path => "OtherPath", :code => "OtherCode" | |
9 | + @fake_user = Factory :user | |
10 | + @valid_issue = Factory :issue, :assignee => @fake_user, :author => @fake_user, :project => @project | |
11 | + @invalid_issue = Factory :issue, :assignee => @fake_user, :author => @fake_user, :project => @other_project | |
12 | + end | |
13 | + | |
14 | + it "should provides return message untouched if no issue number present" do | |
15 | + message = "Dummy message without issue number" | |
16 | + | |
17 | + commit_msg_with_link_to_issues(@project, message).should eql message | |
18 | + end | |
19 | + | |
20 | + it "should returns message handled by preserve" do | |
21 | + message = "My brand new | |
22 | + Commit on multiple | |
23 | + lines !" | |
24 | + | |
25 | + #\n are converted to 
 as specified in preserve_rspec | |
26 | + expected = "My brand new
 Commit on multiple
 lines !" | |
27 | + | |
28 | + commit_msg_with_link_to_issues(@project, message).should eql expected | |
29 | + end | |
30 | + | |
31 | + it "should returns empty string if message undefined" do | |
32 | + commit_msg_with_link_to_issues(@project, nil).should eql '' | |
33 | + end | |
34 | + | |
35 | + it "should returns link_to issue for one valid issue in message" do | |
36 | + issue_id = @valid_issue.id | |
37 | + message = "One commit message ##{issue_id}" | |
38 | + expected = "One commit message <a href=\"/#{@project.code}/issues/#{issue_id}\">##{issue_id}</a>" | |
39 | + | |
40 | + commit_msg_with_link_to_issues(@project, message).should eql expected | |
41 | + end | |
42 | + | |
43 | + it "should returns message untouched for one invalid issue in message" do | |
44 | + issue_id = @invalid_issue.id | |
45 | + message = "One commit message ##{issue_id}" | |
46 | + | |
47 | + commit_msg_with_link_to_issues(@project, message).should eql message | |
48 | + end | |
49 | + | |
50 | + it "should handle multiple issue references in commit message" do | |
51 | + issue_id = @valid_issue.id | |
52 | + invalid_issue_id = @invalid_issue.id | |
53 | + | |
54 | + message = "One big commit message with a valid issue ##{issue_id} and an invalid one ##{invalid_issue_id}. | |
55 | + We reference valid ##{issue_id} multiple times (##{issue_id}) as the invalid ##{invalid_issue_id} is also | |
56 | + referenced another time (##{invalid_issue_id})" | |
57 | + | |
58 | + expected = "One big commit message with a valid issue <a href=\"/#{@project.code}/issues/#{issue_id}\">##{issue_id}</a>"+ | |
59 | + " and an invalid one ##{invalid_issue_id}.
 "+ | |
60 | + "We reference valid <a href=\"/#{@project.code}/issues/#{issue_id}\">##{issue_id}</a> multiple times "+ | |
61 | + "(<a href=\"/#{@project.code}/issues/#{issue_id}\">##{issue_id}</a>) "+ | |
62 | + "as the invalid ##{invalid_issue_id} is also
 referenced another time (##{invalid_issue_id})" | |
63 | + | |
64 | + commit_msg_with_link_to_issues(@project, message).should eql expected | |
65 | + end | |
66 | + | |
67 | +end | |
0 | 68 | \ No newline at end of file | ... | ... |