Commit 72c6be2d authored by Dmitriy Zaporozhets's avatar Dmitriy Zaporozhets

Merge pull request #1183 from riyad/gitlab-flavored-markdown

Gitlab flavored markdown
parents c120457a 5443021a
......@@ -90,6 +90,7 @@ class RefsController < ApplicationController
@repo = project.repo
@commit = project.commit(@ref)
@commit = CommitDecorator.decorate(@commit)
@tree = Tree.new(@commit.tree, project, @ref, params[:path])
@tree = TreeDecorator.new(@tree)
@hex_path = Digest::SHA1.hexdigest(params[:path] || "/")
......
......@@ -42,8 +42,88 @@ module ApplicationHelper
grouped_options_for_select(options, @ref || @project.default_branch)
end
def gfm(text, html_options = {})
return text if text.nil?
raise "@project is not set" if @project.nil?
# Extract pre blocks
# from http://github.github.com/github-flavored-markdown/
extractions = {}
text.gsub!(%r{<pre>.*?</pre>|<code>.*?</code>}m) do |match|
md5 = Digest::MD5.hexdigest(match)
extractions[md5] = match
"{gfm-extraction-#{md5}}"
end
# match 1 2 3 4 5 6
text.gsub!(/(\W)?(@([\w\._]+)|[#!$](\d+)|([\h]{6,40}))(\W)?/) do |match|
prefix = $1
reference = $2
user_name = $3
issue_id = $4
merge_request_id = $4
snippet_id = $4
commit_id = $5
suffix = $6
# TODO: add popups with additional information
ref_link = case reference
# team member: @foo
when /^@/
user = @project.users.where(:name => user_name).first
member = @project.users_projects.where(:user_id => user).first if user
link_to("@#{user_name}", project_team_member_path(@project, member), html_options.merge(:class => "gfm gfm-team_member #{html_options[:class]}")) if member
# issue: #123
when /^#/
# avoid HTML entities
unless prefix.try(:end_with?, "&") && suffix.try(:start_with?, ";")
issue = @project.issues.where(:id => issue_id).first
link_to("##{issue_id}", project_issue_path(@project, issue), html_options.merge(:title => "Issue: #{issue.title}", :class => "gfm gfm-issue #{html_options[:class]}")) if issue
end
# merge request: !123
when /^!/
merge_request = @project.merge_requests.where(:id => merge_request_id).first
link_to("!#{merge_request_id}", project_merge_request_path(@project, merge_request), html_options.merge(:title => "Merge Request: #{merge_request.title}", :class => "gfm gfm-merge_request #{html_options[:class]}")) if merge_request
# snippet: $123
when /^\$/
snippet = @project.snippets.where(:id => snippet_id).first
link_to("$#{snippet_id}", project_snippet_path(@project, snippet), html_options.merge(:title => "Snippet: #{snippet.title}", :class => "gfm gfm-snippet #{html_options[:class]}")) if snippet
# commit: 123456...
when /^\h/
commit = @project.commit(commit_id)
link_to(commit_id, project_commit_path(@project, :id => commit.id), html_options.merge(:title => "Commit: #{commit.author_name} - #{CommitDecorator.new(commit).title}", :class => "gfm gfm-commit #{html_options[:class]}")) if commit
end # case
ref_link.nil? ? match : "#{prefix}#{ref_link}#{suffix}"
end # gsub
# Insert pre block extractions
text.gsub!(/\{gfm-extraction-(\h{32})\}/) do
extractions[$1]
end
text.html_safe
end
# circumvents nesting links, which will behave bad in browsers
def link_to_gfm(body, url, html_options = {})
gfm_body = gfm(body, html_options)
gfm_body.gsub!(%r{<a.*?>.*?</a>}m) do |match|
"</a>#{match}#{link_to("", url, html_options)[0..-5]}" # "</a>".length +1
end
link_to(gfm_body.html_safe, url, html_options)
end
def markdown(text)
@__renderer ||= Redcarpet::Markdown.new(Redcarpet::Render::GitlabHTML.new({ filter_html: true, with_toc_data: true }), {
@__renderer ||= Redcarpet::Markdown.new(Redcarpet::Render::GitlabHTML.new(self, filter_html: true, with_toc_data: true), {
no_intra_emphasis: true,
tables: true,
fenced_code_blocks: true,
......
module CommitsHelper
def commit_msg_with_link_to_issues(project, message)
return '' unless message
out = ''
message.split(/(#[0-9]+)/m).each do |m|
if m =~ /(#([0-9]+))/m
begin
issue = project.issues.find($2)
out += link_to($1, project_issue_path(project, $2))
rescue
out += $1
end
else
out += m
end
end
preserve out
end
def identification_type(line)
if line[0] == "+"
"new"
......
......@@ -16,59 +16,69 @@ class Notify < ActionMailer::Base
def new_issue_email(issue_id)
@issue = Issue.find(issue_id)
mail(:to => @issue.assignee_email, :subject => "gitlab | New Issue was created")
@project = @issue.project
mail(:to => @issue.assignee_email, :subject => "gitlab | new issue ##{@issue.id} | #{@issue.title} | #{@project.name}")
end
def note_wall_email(recipient_id, note_id)
recipient = User.find(recipient_id)
@note = Note.find(note_id)
mail(:to => recipient.email, :subject => "gitlab | #{@note.project_name} ")
@project = @note.project
mail(:to => recipient.email, :subject => "gitlab | #{@project.name}")
end
def note_commit_email(recipient_id, note_id)
recipient = User.find(recipient_id)
@note = Note.find(note_id)
@commit = @note.target
mail(:to => recipient.email, :subject => "gitlab | note for commit | #{@note.project_name} ")
@commit = CommitDecorator.decorate(@commit)
@project = @note.project
mail(:to => recipient.email, :subject => "gitlab | note for commit #{@commit.short_id} | #{@commit.title} | #{@project.name}")
end
def note_merge_request_email(recipient_id, note_id)
recipient = User.find(recipient_id)
@note = Note.find(note_id)
@merge_request = @note.noteable
mail(:to => recipient.email, :subject => "gitlab | note for merge request | #{@note.project_name} ")
@project = @note.project
mail(:to => recipient.email, :subject => "gitlab | note for merge request !#{@merge_request.id} | #{@project.name}")
end
def note_issue_email(recipient_id, note_id)
recipient = User.find(recipient_id)
@note = Note.find(note_id)
@issue = @note.noteable
mail(:to => recipient.email, :subject => "gitlab | note for issue #{@issue.id} | #{@note.project_name} ")
@project = @note.project
mail(:to => recipient.email, :subject => "gitlab | note for issue ##{@issue.id} | #{@project.name}")
end
def note_wiki_email(recipient_id, note_id)
recipient = User.find(recipient_id)
@note = Note.find(note_id)
@wiki = @note.noteable
mail(:to => recipient.email, :subject => "gitlab | note for wiki | #{@note.project_name}")
@project = @note.project
mail(:to => recipient.email, :subject => "gitlab | note for wiki | #{@project.name}")
end
def new_merge_request_email(merge_request_id)
@merge_request = MergeRequest.find(merge_request_id)
mail(:to => @merge_request.assignee_email, :subject => "gitlab | new merge request | #{@merge_request.title} ")
@project = @merge_request.project
mail(:to => @merge_request.assignee_email, :subject => "gitlab | new merge request !#{@merge_request.id} | #{@merge_request.title} | #{@project.name}")
end
def reassigned_merge_request_email(recipient_id, merge_request_id, previous_assignee_id)
recipient = User.find(recipient_id)
@merge_request = MergeRequest.find(merge_request_id)
@previous_assignee ||= User.find(previous_assignee_id)
mail(:to => recipient.email, :subject => "gitlab | merge request changed | #{@merge_request.title} ")
@project = @merge_request.project
mail(:to => recipient.email, :subject => "gitlab | changed merge request !#{@merge_request.id} | #{@merge_request.title} | #{@project.name}")
end
def reassigned_issue_email(recipient_id, issue_id, previous_assignee_id)
recipient = User.find(recipient_id)
@issue = Issue.find(issue_id)
@previous_assignee ||= User.find(previous_assignee_id)
mail(:to => recipient.email, :subject => "gitlab | changed issue | #{@issue.title} ")
@project = @issue.project
mail(:to => recipient.email, :subject => "gitlab | changed issue ##{@issue.id} | #{@issue.title} | #{@project.name}")
end
end
......@@ -7,7 +7,7 @@
%strong.cgray= commit.author_name
&ndash;
= image_tag gravatar_icon(commit.author_email), :class => "avatar", :width => 16
= link_to truncate(commit.title, :length => 50), project_commit_path(@project, :id => commit.id), :class => "row_title"
= link_to_gfm truncate(commit.title, :length => 50), project_commit_path(@project, :id => commit.id), :class => "row_title"
%span.committed_ago
= time_ago_in_words(commit.committed_date)
......
......@@ -11,10 +11,10 @@
= link_to tree_project_ref_path(@project, @commit.id), :class => "browse-button primary grouped" do
%strong Browse Code »
%h3.commit-title.page_title
= commit_msg_with_link_to_issues(@project, @commit.title)
= gfm @commit.title
- if @commit.description.present?
%pre.commit-description
= commit_msg_with_link_to_issues(@project, @commit.description)
= gfm @commit.description
.commit-info
.row
.span4
......
......@@ -17,7 +17,7 @@ xml.feed "xmlns" => "http://www.w3.org/2005/Atom", "xmlns:media" => "http://sear
xml.name commit.author_name
xml.email commit.author_email
end
xml.summary commit.description
xml.summary gfm(commit.description)
end
end
end
......@@ -8,8 +8,8 @@
- if @issues.any?
- @issues.group_by(&:project).each do |group|
%div.ui-box
- project = group[0]
%h5= project.name
- @project = group[0]
%h5= @project.name
%ul.unstyled.issues_table
- group[1].each do |issue|
= render(:partial => 'issues/show', :locals => {:issue => issue})
......
......@@ -7,8 +7,8 @@
- if @merge_requests.any?
- @merge_requests.group_by(&:project).each do |group|
%ul.unstyled.ui-box
- project = group[0]
%h5= project.name
- @project = group[0]
%h5= @project.name
- group[1].each do |merge_request|
= render(:partial => 'merge_requests/merge_request', :locals => {:merge_request => merge_request})
%hr
......
......@@ -5,5 +5,5 @@
%strong.cdark= commit.author_name
&ndash;
= image_tag gravatar_icon(commit.author_email), :class => "avatar", :width => 16
= truncate(commit.title, :length => 50) rescue "--broken encoding"
= gfm truncate(commit.title, :length => 50), project_commit_path(project, :id => commit.id) rescue "--broken encoding"
......@@ -25,7 +25,7 @@
- else
= image_tag "no_avatar.png", :class => "avatar"
%p= link_to truncate(issue.title, :length => 100), project_issue_path(issue.project, issue), :class => "row_title"
%p= link_to_gfm truncate(issue.title, :length => 100), project_issue_path(issue.project, issue), :class => "row_title"
%span.update-author
%small.cdark= "##{issue.id}"
......
......@@ -31,7 +31,7 @@
.alert-message.error.status_info Closed
- else
.alert-message.success.status_info Open
= @issue.title
= gfm @issue.title
.middle_box_content
%cite.cgray Created by
......@@ -46,7 +46,7 @@
- if @issue.milestone
- milestone = @issue.milestone
%cite.cgray and attached to milestone
%strong= link_to truncate(milestone.title, :length => 20), project_milestone_path(milestone.project, milestone)
%strong= link_to_gfm truncate(milestone.title, :length => 20), project_milestone_path(milestone.project, milestone)
.right
- @issue.labels.each do |label|
......
......@@ -16,7 +16,7 @@
= merge_request.target_branch
= image_tag gravatar_icon(merge_request.author_email), :class => "avatar"
%p= link_to truncate(merge_request.title, :length => 80), project_merge_request_path(merge_request.project, merge_request), :class => "row_title"
%p= link_to_gfm truncate(merge_request.title, :length => 80), project_merge_request_path(merge_request.project, merge_request), :class => "row_title"
%span.update-author
%small.cdark= "##{merge_request.id}"
......
......@@ -5,7 +5,7 @@
.alert-message.error.status_info Closed
- else
.alert-message.success.status_info Open
= @merge_request.title
= gfm @merge_request.title
.middle_box_content
%div
......
......@@ -7,7 +7,7 @@
- if can? current_user, :admin_milestone, milestone.project
= link_to 'Edit', edit_project_milestone_path(milestone.project, milestone), :class => "btn small edit-milestone-link grouped"
%h4
= link_to truncate(milestone.title, :length => 100), project_milestone_path(milestone.project, milestone), :class => "row_title"
= link_to_gfm truncate(milestone.title, :length => 100), project_milestone_path(milestone.project, milestone), :class => "row_title"
%small
= milestone.expires_at
%br
......
......@@ -21,7 +21,7 @@
.alert-message.error.status_info Closed
- else
.alert-message.success.status_info Open
= @milestone.title
= gfm @milestone.title
%small.right= @milestone.expires_at
.middle_box_content
......@@ -51,7 +51,7 @@
= link_to [@project, issue] do
%span.badge.badge-info ##{issue.id}
&ndash;
= link_to truncate(issue.title, :length => 60), [@project, issue]
= link_to_gfm truncate(issue.title, :length => 60), [@project, issue]
%br
= paginate @issues, :theme => "gitlab"
......
......@@ -10,7 +10,6 @@
%td{:style => "font-size: 1px; line-height: 1px;", :width => "21"}
%td{:align => "left", :style => "padding: 20px 0 0;"}
%h2{:style => "color:#646464 !important; font-weight: bold; margin: 0; padding: 0; line-height: 26px; font-size: 18px; font-family: Helvetica, Arial, sans-serif; "}
= link_to project_issue_url(@issue.project, @issue), :title => @issue.title do
= "Issue ##{@issue.id.to_s}"
= truncate(@issue.title, :length => 45)
= "Issue ##{@issue.id}"
= link_to_gfm truncate(@issue.title, :length => 45), project_issue_url(@issue.project, @issue), :title => @issue.title
%br
......@@ -4,8 +4,8 @@
%td{:style => "font-size: 1px; line-height: 1px;", :width => "21"}
%td{:align => "left", :style => "padding: 20px 0 0;"}
%h2{:style => "color:#646464; font-weight: bold; margin: 0; padding: 0; line-height: 26px; font-size: 18px; font-family: Helvetica, Arial, sans-serif; "}
New Merge Request
= link_to truncate(@merge_request.title, :length => 16), project_merge_request_url(@merge_request.project, @merge_request)
= "New Merge Request !#{@merge_request.id}"
= link_to_gfm truncate(@merge_request.title, :length => 16), project_merge_request_url(@merge_request.project, @merge_request)
%td{:style => "font-size: 1px; line-height: 1px;", :width => "21"}
%tr
%td{:style => "font-size: 1px; line-height: 1px;", :width => "21"}
......
......@@ -4,8 +4,8 @@
%td{:style => "font-size: 1px; line-height: 1px;", :width => "21"}
%td{:align => "left", :style => "padding: 20px 0 0;"}
%h2{:style => "color:#646464; font-weight: bold; margin: 0; padding: 0; line-height: 26px; font-size: 18px; font-family: Helvetica, Arial, sans-serif; "}
New comment for commit
= link_to truncate(@commit.id.to_s, :length => 16), project_commit_url(@note.project, :id => @commit.id, :anchor => "note_#{@note.id}")
= "New comment for Commit #{@commit.short_id}"
= link_to_gfm truncate(@commit.title, :length => 16), project_commit_url(@note.project, :id => @commit.id, :anchor => "note_#{@note.id}")
%td{:style => "font-size: 1px; line-height: 1px;", :width => "21"}
%tr
%td{:style => "font-size: 1px; line-height: 1px;", :width => "21"}
......
......@@ -4,10 +4,8 @@
%td{:style => "font-size: 1px; line-height: 1px;", :width => "21"}
%td{:align => "left", :style => "padding: 20px 0 0;"}
%h2{:style => "color:#646464 !important; font-weight: bold; margin: 0; padding: 0; line-height: 26px; font-size: 18px; font-family: Helvetica, Arial, sans-serif; "}
New comment -
= link_to project_issue_url(@issue.project, @issue, :anchor => "note_#{@note.id}") do
= "Issue ##{@issue.id.to_s}"
= truncate(@issue.title, :length => 35)
= "New comment for Issue ##{@issue.id}"
= link_to_gfm truncate(@issue.title, :length => 35), project_issue_url(@issue.project, @issue, :anchor => "note_#{@note.id}")
%td{:style => "font-size: 1px; line-height: 1px;", :width => "21"}
%tr
%td{:style => "font-size: 1px; line-height: 1px;", :width => "21"}
......
......@@ -4,8 +4,8 @@
%td{:style => "font-size: 1px; line-height: 1px;", :width => "21"}
%td{:align => "left", :style => "padding: 20px 0 0;"}
%h2{:style => "color:#646464; font-weight: bold; margin: 0; padding: 0; line-height: 26px; font-size: 18px; font-family: Helvetica, Arial, sans-serif; "}
New comment for Merge Request
= link_to truncate(@merge_request.title, :length => 16), project_merge_request_url(@merge_request.project, @merge_request, :anchor => "note_#{@note.id}")
= "New comment for Merge Request !#{@merge_request.id}"
= link_to_gfm truncate(@merge_request.title, :length => 16), project_merge_request_url(@merge_request.project, @merge_request, :anchor => "note_#{@note.id}")
%td{:style => "font-size: 1px; line-height: 1px;", :width => "21"}
%tr
%td{:style => "font-size: 1px; line-height: 1px;", :width => "21"}
......
......@@ -4,9 +4,8 @@
%td{:style => "font-size: 1px; line-height: 1px;", :width => "21"}
%td{:align => "left", :style => "padding: 20px 0 0;"}
%h2{:style => "color:#646464 !important; font-weight: bold; margin: 0; padding: 0; line-height: 26px; font-size: 18px; font-family: Helvetica, Arial, sans-serif; "}
New comment -
= link_to project_issue_url(@wiki.project, @wiki, :anchor => "note_#{@note.id}") do
= "Wiki ##{@wiki.title.to_s}"
New comment for Wiki page
= link_to_gfm @wiki.title, project_issue_url(@wiki.project, @wiki, :anchor => "note_#{@note.id}")
%td{:style => "font-size: 1px; line-height: 1px;", :width => "21"}
%tr
%td{:style => "font-size: 1px; line-height: 1px;", :width => "21"}
......
......@@ -4,8 +4,8 @@
%td{:style => "font-size: 1px; line-height: 1px;", :width => "21"}
%td{:align => "left", :style => "padding: 20px 0 0;"}
%h2{:style => "color:#646464; font-weight: bold; margin: 0; padding: 0; line-height: 26px; font-size: 18px; font-family: Helvetica, Arial, sans-serif; "}
Reassigned Issue
= link_to truncate(@issue.title, :length => 16), project_issue_url(@issue.project, @issue)
= "Reassigned Issue ##{@issue.id}"
= link_to_gfm truncate(@issue.title, :length => 16), project_issue_url(@issue.project, @issue)
%td{:style => "font-size: 1px; line-height: 1px;", :width => "21"}
%tr
%td{:style => "font-size: 1px; line-height: 1px;", :width => "21"}
......
......@@ -4,8 +4,8 @@
%td{:style => "font-size: 1px; line-height: 1px;", :width => "21"}
%td{:align => "left", :style => "padding: 20px 0 0;"}
%h2{:style => "color:#646464; font-weight: bold; margin: 0; padding: 0; line-height: 26px; font-size: 18px; font-family: Helvetica, Arial, sans-serif; "}
Reassigned Merge Request
= link_to truncate(@merge_request.title, :length => 16), project_merge_request_url(@merge_request.project, @merge_request)
= "Reassigned Merge Request !#{@merge_request.id}"
= link_to_gfm truncate(@merge_request.title, :length => 16), project_merge_request_url(@merge_request.project, @merge_request)
%td{:style => "font-size: 1px; line-height: 1px;", :width => "21"}
%tr
%td{:style => "font-size: 1px; line-height: 1px;", :width => "21"}
......
- if tm
%strong= link_to "[#{tm.user_name}]", project_team_member_path(@project, tm)
= link_to truncate(content_commit.title, :length => tm ? 30 : 50), project_commit_path(@project, content_commit.id), :class => "tree-commit-link"
= link_to_gfm truncate(content_commit.title, :length => tm ? 30 : 50), project_commit_path(@project, content_commit.id), :class => "tree-commit-link"
......@@ -33,7 +33,7 @@
%td.blame_commit
&nbsp;
%code= link_to commit.short_id, project_commit_path(@project, :id => commit.id)
= link_to truncate(commit.title, :length => 30), project_commit_path(@project, :id => commit.id), :class => "row_title" rescue "--broken encoding"
= link_to_gfm truncate(commit.title, :length => 30), project_commit_path(@project, :id => commit.id), :class => "row_title" rescue "--broken encoding"
%td.lines
= preserve do
%pre
......
......@@ -11,7 +11,7 @@
%code= commit.short_id
= image_tag gravatar_icon(commit.author_email), :class => "", :width => 16
= truncate(commit.title, :length => 40)
= gfm truncate(commit.title, :length => 40)
%td
%span.update-author.right
= time_ago_in_words(commit.committed_date)
......
......@@ -13,7 +13,7 @@
= link_to project_commits_path(@project, commit.id) do
%code= commit.short_id
= image_tag gravatar_icon(commit.author_email), :class => "", :width => 16
= truncate(commit.title, :length => 40)
= gfm truncate(commit.title, :length => 40)
%td
%span.right.cgray
= time_ago_in_words(commit.committed_date)
......
......@@ -17,7 +17,7 @@
= link_to project_commit_path(@project, commit.id) do
%code= commit.short_id
= image_tag gravatar_icon(commit.author_email), :class => "", :width => 16
= truncate(commit.title, :length => 40)
= gfm truncate(commit.title, :length => 40)
%td
%span.update-author.right
= time_ago_in_words(commit.committed_date)
......
class Redcarpet::Render::GitlabHTML < Redcarpet::Render::HTML
attr_reader :template
alias_method :h, :template
def initialize(template, options = {})
@template = template
@project = @template.instance_variable_get("@project")
super options
end
def block_code(code, language)
if Pygments::Lexer.find(language)
Pygments.highlight(code, :lexer => language, :options => {:encoding => 'utf-8'})
......@@ -6,4 +16,8 @@ class Redcarpet::Render::GitlabHTML < Redcarpet::Render::HTML
Pygments.highlight(code, :options => {:encoding => 'utf-8'})
end
end
def postprocess(full_document)
h.gfm(full_document)
end
end
require "spec_helper"
include Haml::Helpers
describe CommitsHelper do
before do
@project = Factory :project
@other_project = Factory :project, :path => "OtherPath", :code => "OtherCode"
@fake_user = Factory :user
@valid_issue = Factory :issue, :assignee => @fake_user, :author => @fake_user, :project => @project
@invalid_issue = Factory :issue, :assignee => @fake_user, :author => @fake_user, :project => @other_project
end
it "should provides return message untouched if no issue number present" do
message = "Dummy message without issue number"
commit_msg_with_link_to_issues(@project, message).should eql message
end
it "should returns message handled by preserve" do
message = "My brand new
Commit on multiple
lines !"
#\n are converted to &#x000A as specified in preserve_rspec
expected = "My brand new&#x000A; Commit on multiple&#x000A; lines !"
commit_msg_with_link_to_issues(@project, message).should eql expected
end
it "should returns empty string if message undefined" do
commit_msg_with_link_to_issues(@project, nil).should eql ''
end
it "should returns link_to issue for one valid issue in message" do
issue_id = @valid_issue.id
message = "One commit message ##{issue_id}"
expected = "One commit message <a href=\"/#{@project.code}/issues/#{issue_id}\">##{issue_id}</a>"
commit_msg_with_link_to_issues(@project, message).should eql expected
end
it "should returns message untouched for one invalid issue in message" do
issue_id = @invalid_issue.id
message = "One commit message ##{issue_id}"
commit_msg_with_link_to_issues(@project, message).should eql message
end
it "should handle multiple issue references in commit message" do
issue_id = @valid_issue.id
invalid_issue_id = @invalid_issue.id
message = "One big commit message with a valid issue ##{issue_id} and an invalid one ##{invalid_issue_id}.
We reference valid ##{issue_id} multiple times (##{issue_id}) as the invalid ##{invalid_issue_id} is also
referenced another time (##{invalid_issue_id})"
expected = "One big commit message with a valid issue <a href=\"/#{@project.code}/issues/#{issue_id}\">##{issue_id}</a>"+
" and an invalid one ##{invalid_issue_id}.&#x000A; "+
"We reference valid <a href=\"/#{@project.code}/issues/#{issue_id}\">##{issue_id}</a> multiple times "+
"(<a href=\"/#{@project.code}/issues/#{issue_id}\">##{issue_id}</a>) "+
"as the invalid ##{invalid_issue_id} is also&#x000A; referenced another time (##{invalid_issue_id})"
commit_msg_with_link_to_issues(@project, message).should eql expected
end
end
\ No newline at end of file
require "spec_helper"
describe ApplicationHelper do
before do
@project = Project.find_by_path("gitlabhq") || Factory(:project)
@commit = @project.repo.commits.first.parents.first
@commit = CommitDecorator.decorate(Commit.new(@commit))
@other_project = Factory :project, :path => "OtherPath", :code => "OtherCode"
@fake_user = Factory :user, :name => "fred"
end
describe "#gfm" do
it "should raiase an error if @project is not set" do
@project = nil
expect { gfm("foo") }.to raise_error
end
describe "referencing a commit" do
it "should link using a full id" do
gfm("Reverts changes from #{@commit.id}").should == "Reverts changes from #{link_to @commit.id, project_commit_path(@project, :id => @commit.id), :title => "Commit: #{@commit.author_name} - #{@commit.title}", :class => "gfm gfm-commit "}"
end
it "should link using a short id" do
gfm("Backported from #{@commit.id[0, 6]}").should == "Backported from #{link_to @commit.id[0, 6], project_commit_path(@project, :id => @commit.id), :title => "Commit: #{@commit.author_name} - #{@commit.title}", :class => "gfm gfm-commit "}"
end
it "should link with adjecent text" do
gfm("Reverted (see #{@commit.id})").should == "Reverted (see #{link_to @commit.id, project_commit_path(@project, :id => @commit.id), :title => "Commit: #{@commit.author_name} - #{@commit.title}", :class => "gfm gfm-commit "})"
end
it "should not link with an invalid id" do
gfm("What happened in 12345678?").should == "What happened in 12345678?"
end
end
describe "referencing a team member" do
it "should link using a simple name" do
user = Factory :user, name: "barry"
@project.users << user
member = @project.users_projects.where(:user_id => user).first
gfm("@#{user.name} you are right").should == "#{link_to "@#{user.name}", project_team_member_path(@project, member), :class => "gfm gfm-team_member "} you are right"
end
it "should link using a name with dots" do
user = Factory :user, name: "alphA.Beta"
@project.users << user
member = @project.users_projects.where(:user_id => user).first
gfm("@#{user.name} you are right").should == "#{link_to "@#{user.name}", project_team_member_path(@project, member), :class => "gfm gfm-team_member "} you are right"
end
it "should link using name with underscores" do
user = Factory :user, name: "ping_pong_king"
@project.users << user
member = @project.users_projects.where(:user_id => user).first
gfm("@#{user.name} you are right").should == "#{link_to "@#{user.name}", project_team_member_path(@project, member), :class => "gfm gfm-team_member "} you are right"
end
it "should link with adjecent text" do
user = Factory.create(:user, :name => "ace")
@project.users << user
member = @project.users_projects.where(:user_id => user).first
gfm("Mail the Admin (@#{user.name})").should == "Mail the Admin (#{link_to "@#{user.name}", project_team_member_path(@project, member), :class => "gfm gfm-team_member "})"
end
it "should add styles" do
user = Factory :user, name: "barry"
@project.users << user
gfm("@#{user.name} you are right").should have_selector(".gfm.gfm-team_member")
end
it "should not link using a bogus name" do
gfm("What hapened to @foo?").should == "What hapened to @foo?"
end
end
describe "referencing an issue" do
before do
@issue = Factory :issue, :assignee => @fake_user, :author => @fake_user, :project => @project
@invalid_issue = Factory :issue, :assignee => @fake_user, :author => @fake_user, :project => @other_project
end
it "should link using a correct id" do
gfm("Fixes ##{@issue.id}").should == "Fixes #{link_to "##{@issue.id}", project_issue_path(@project, @issue), :title => "Issue: #{@issue.title}", :class => "gfm gfm-issue "}"
end
it "should link with adjecent text" do
gfm("This has already been discussed (see ##{@issue.id})").should == "This has already been discussed (see #{link_to "##{@issue.id}", project_issue_path(@project, @issue), :title => "Issue: #{@issue.title}", :class => "gfm gfm-issue "})"
end
it "should add styles" do
gfm("Fixes ##{@issue.id}").should have_selector(".gfm.gfm-issue")
end
it "should not link using an invalid id" do
gfm("##{@invalid_issue.id} has been marked duplicate of this").should == "##{@invalid_issue.id} has been marked duplicate of this"
end
end
describe "referencing a merge request" do
before do
@merge_request = Factory :merge_request, :assignee => @fake_user, :author => @fake_user, :project => @project
@invalid_merge_request = Factory :merge_request, :assignee => @fake_user, :author => @fake_user, :project => @other_project
end
it "should link using a correct id" do
gfm("Fixed in !#{@merge_request.id}").should == "Fixed in #{link_to "!#{@merge_request.id}", project_merge_request_path(@project, @merge_request), :title => "Merge Request: #{@merge_request.title}", :class => "gfm gfm-merge_request "}"
end
it "should link with adjecent text" do
gfm("This has been fixed already (see !#{@merge_request.id})").should == "This has been fixed already (see #{link_to "!#{@merge_request.id}", project_merge_request_path(@project, @merge_request), :title => "Merge Request: #{@merge_request.title}", :class => "gfm gfm-merge_request "})"
end
it "should add styles" do
gfm("Fixed in !#{@merge_request.id}").should have_selector(".gfm.gfm-merge_request")
end
it "should not link using an invalid id" do
gfm("!#{@invalid_merge_request.id} violates our coding guidelines")
end
end
describe "referencing a snippet" do
before do
@snippet = Factory.create(:snippet,
:title => "Render asset to string",
:author => @fake_user,
:project => @project)
end
it "should link using a correct id" do
gfm("Check out $#{@snippet.id}").should == "Check out #{link_to "$#{@snippet.id}", project_snippet_path(@project, @snippet), :title => "Snippet: #{@snippet.title}", :class => "gfm gfm-snippet "}"
end
it "should link with adjecent text" do
gfm("I have created a snippet for that ($#{@snippet.id})").should == "I have created a snippet for that (#{link_to "$#{@snippet.id}", project_snippet_path(@project, @snippet), :title => "Snippet: #{@snippet.title}", :class => "gfm gfm-snippet "})"
end
it "should add styles" do
gfm("Check out $#{@snippet.id}").should have_selector(".gfm.gfm-snippet")
end
it "should not link using an invalid id" do
gfm("Don't use $1234").should == "Don't use $1234"
end
end
it "should link to multiple things" do
user = Factory :user, name: "barry"
@project.users << user
member = @project.users_projects.where(:user_id => user).first
gfm("Let @#{user.name} fix the *mess* in #{@commit.id}").should == "Let #{link_to "@#{user.name}", project_team_member_path(@project, member), :class => "gfm gfm-team_member "} fix the *mess* in #{link_to @commit.id, project_commit_path(@project, :id => @commit.id), :title => "Commit: #{@commit.author_name} - #{@commit.title}", :class => "gfm gfm-commit "}"
end
it "should not trip over other stuff", :focus => true do
gfm("_Please_ *stop* 'helping' and all the other b*$#%' you do.").should == "_Please_ *stop* 'helping' and all the other b*$#%' you do."
end
it "should not touch HTML entities" do
gfm("We&#39;ll accept good pull requests.").should == "We&#39;ll accept good pull requests."
end
it "should forward HTML options to links" do
gfm("fixed in #{@commit.id}", :class => "foo").should have_selector("a.foo")
end
end
describe "#link_to_gfm" do
let(:issue1) { Factory :issue, :assignee => @fake_user, :author => @fake_user, :project => @project }
let(:issue2) { Factory :issue, :assignee => @fake_user, :author => @fake_user, :project => @project }
it "should handle references nested in links with all the text" do
link_to_gfm("This should finally fix ##{issue1.id} and ##{issue2.id} for real", project_commit_path(@project, :id => @commit.id)).should == "#{link_to "This should finally fix ", project_commit_path(@project, :id => @commit.id)}#{link_to "##{issue1.id}", project_issue_path(@project, issue1), :title => "Issue: #{issue1.title}", :class => "gfm gfm-issue "}#{link_to " and ", project_commit_path(@project, :id => @commit.id)}#{link_to "##{issue2.id}", project_issue_path(@project, issue2), :title => "Issue: #{issue2.title}", :class => "gfm gfm-issue "}#{link_to " for real", project_commit_path(@project, :id => @commit.id)}"
end
it "should forward HTML options" do
link_to_gfm("This should finally fix ##{issue1.id} for real", project_commit_path(@project, :id => @commit.id), :class => "foo").should have_selector(".foo")
end
end
describe "#markdown" do
before do
@issue = Factory :issue, :assignee => @fake_user, :author => @fake_user, :project => @project
@merge_request = Factory :merge_request, :assignee => @fake_user, :author => @fake_user, :project => @project
@note = Factory.create(:note,
:note => "Screenshot of the new feature",
:project => @project,
:noteable_id => @commit.id,
:noteable_type => "Commit",
:attachment => "screenshot123.jpg")
@snippet = Factory.create(:snippet,
:title => "Render asset to string",
:author => @fake_user,
:project => @project)
@other_user = Factory :user, name: "bill"
@project.users << @other_user
@member = @project.users_projects.where(:user_id => @other_user).first
end
it "should handle references in paragraphs" do
markdown("\n\nLorem ipsum dolor sit amet, consectetur adipiscing elit. #{@commit.id} Nam pulvinar sapien eget odio adipiscing at faucibus orci vestibulum.\n").should == "<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. #{link_to @commit.id, project_commit_path(@project, :id => @commit.id), :title => "Commit: #{@commit.author_name} - #{@commit.title}", :class => "gfm gfm-commit "} Nam pulvinar sapien eget odio adipiscing at faucibus orci vestibulum.</p>\n"
end
it "should handle references in headers" do
markdown("\n# Working around ##{@issue.id} for now\n## Apply !#{@merge_request.id}").should == "<h1 id=\"toc_0\">Working around #{link_to "##{@issue.id}", project_issue_path(@project, @issue), :title => "Issue: #{@issue.title}", :class => "gfm gfm-issue "} for now</h1>\n\n<h2 id=\"toc_1\">Apply #{link_to "!#{@merge_request.id}", project_merge_request_path(@project, @merge_request), :title => "Merge Request: #{@merge_request.title}", :class => "gfm gfm-merge_request "}</h2>\n"
end
it "should handle references in lists" do
markdown("\n* dark: ##{@issue.id}\n* light by @#{@other_user.name}\n").should == "<ul>\n<li>dark: #{link_to "##{@issue.id}", project_issue_path(@project, @issue), :title => "Issue: #{@issue.title}", :class => "gfm gfm-issue "}</li>\n<li>light by #{link_to "@#{@other_user.name}", project_team_member_path(@project, @member), :class => "gfm gfm-team_member "}</li>\n</ul>\n"
end
it "should handle references in <em>" do
markdown("Apply _!#{@merge_request.id}_ ASAP").should == "<p>Apply <em>#{link_to "!#{@merge_request.id}", project_merge_request_path(@project, @merge_request), :title => "Merge Request: #{@merge_request.title}", :class => "gfm gfm-merge_request "}</em> ASAP</p>\n"
end
it "should leave code blocks untouched" do
markdown("\n some code from $#{@snippet.id}\n here too\n").should == "<div class=\"highlight\"><pre><span class=\"n\">some</span> <span class=\"n\">code</span> <span class=\"n\">from</span> $#{@snippet.id}\n<span class=\"n\">here</span> <span class=\"n\">too</span>\n</pre>\n</div>\n"
markdown("\n```\nsome code from $#{@snippet.id}\nhere too\n```\n").should == "<div class=\"highlight\"><pre><span class=\"n\">some</span> <span class=\"n\">code</span> <span class=\"n\">from</span> $#{@snippet.id}\n<span class=\"n\">here</span> <span class=\"n\">too</span>\n</pre>\n</div>\n"
end
it "should leave inline code untouched" do
markdown("\nDon't use `$#{@snippet.id}` here.\n").should == "<p>Don&#39;t use <code>$#{@snippet.id}</code> here.</p>\n"
end
end
end
......@@ -60,7 +60,7 @@ describe Notify do
it_behaves_like 'an assignee email'
it 'has the correct subject' do
should have_subject /New Issue was created/
should have_subject /new issue ##{issue.id}/
end
it 'contains a link to the new issue' do
......@@ -102,7 +102,7 @@ describe Notify do
it_behaves_like 'an assignee email'
it 'has the correct subject' do
should have_subject /new merge request/
should have_subject /new merge request !#{merge_request.id}/
end
it 'contains a link to the new merge request' do
......@@ -126,7 +126,7 @@ describe Notify do
it_behaves_like 'a multiple recipients email'
it 'has the correct subject' do
should have_subject /merge request changed/
should have_subject /changed merge request !#{merge_request.id}/
end
it 'contains the name of the previous assignee' do
......@@ -188,6 +188,8 @@ describe Notify do
mock(:commit).tap do |commit|
commit.stub(:id).and_return('fauxsha1')
commit.stub(:project).and_return(project)
commit.stub(:short_id).and_return('fauxsha1')
commit.stub(:safe_message).and_return('some message')
end
end
before(:each) { note.stub(:target).and_return(commit) }
......@@ -197,7 +199,7 @@ describe Notify do
it_behaves_like 'a note email'
it 'has the correct subject' do
should have_subject /note for commit/
should have_subject /note for commit #{commit.short_id}/
end
it 'contains a link to the commit' do
......@@ -215,7 +217,7 @@ describe Notify do
it_behaves_like 'a note email'
it 'has the correct subject' do
should have_subject /note for merge request/
should have_subject /note for merge request !#{merge_request.id}/
end
it 'contains a link to the merge request note' do
......@@ -233,7 +235,7 @@ describe Notify do
it_behaves_like 'a note email'
it 'has the correct subject' do
should have_subject /note for issue #{issue.id}/
should have_subject /note for issue ##{issue.id}/
end
it 'contains a link to the issue note' do
......
require 'spec_helper'
describe "Gitlab Flavored Markdown" do
let(:project) { Factory :project }
let(:issue) { Factory :issue, :project => project }
let(:merge_request) { Factory :merge_request, :project => project }
let(:fred) do
u = Factory :user, :name => "fred"
project.users << u
u
end
before do
# add test branch
@branch_name = "gfm-test"
r = project.repo
i = r.index
# add test file
@test_file = "gfm_test_file"
i.add(@test_file, "foo\nbar\n")
# add commit with gfm
i.commit("fix ##{issue.id}\n\nask @#{fred.name} for details", :head => @branch_name)
# add test tag
@tag_name = "gfm-test-tag"
r.git.native(:tag, {}, @tag_name, commit.id)
end
after do
# delete test branch and tag
project.repo.git.native(:branch, {:D => true}, @branch_name)
project.repo.git.native(:tag, {:d => true}, @tag_name)
project.repo.gc_auto
end
let(:commit) { project.commits(@branch_name).first }
before do
login_as :user
project.add_access(@user, :read, :write)
end
describe "for commits" do
it "should render title in commits#index" do
visit project_commits_path(project, :ref => @branch_name)
page.should have_link("##{issue.id}")
end
it "should render title in commits#show" do
visit project_commit_path(project, :id => commit.id)
page.should have_link("##{issue.id}")
end
it "should render description in commits#show" do
visit project_commit_path(project, :id => commit.id)
page.should have_link("@#{fred.name}")
end
it "should render title in refs#tree", :js => true do
visit tree_project_ref_path(project, :id => @branch_name)
within(".tree_commit") do
page.should have_link("##{issue.id}")
end
end
it "should render title in refs#blame" do
visit blame_file_project_ref_path(project, :id => @branch_name, :path => @test_file)
within(".blame_commit") do
page.should have_link("##{issue.id}")
end
end
it "should render title in repositories#branches" do
visit branches_project_repository_path(project)
page.should have_link("##{issue.id}")
end
it "should render title in repositories#tags" do
visit tags_project_repository_path(project)
page.should have_link("##{issue.id}")
end
end
describe "for issues" do
before do
@other_issue = Factory :issue,
:author => @user,
:assignee => @user,
:project => project
@issue = Factory :issue,
:author => @user,
:assignee => @user,
:project => project,
:title => "fix ##{@other_issue.id}",
:description => "ask @#{fred.name} for details"
end
it "should render subject in issues#index" do
visit project_issues_path(project)
page.should have_link("##{@other_issue.id}")
end
it "should render subject in issues#show" do
visit project_issue_path(project, @issue)
page.should have_link("##{@other_issue.id}")
end
it "should render details in issues#show" do
visit project_issue_path(project, @issue)
page.should have_link("@#{fred.name}")
end
end
describe "for merge requests" do
before do
@merge_request = Factory :merge_request,
:project => project,
:title => "fix ##{issue.id}"
end
it "should render title in merge_requests#index" do
visit project_merge_requests_path(project)
page.should have_link("##{issue.id}")
end
it "should render title in merge_requests#show" do
visit project_merge_request_path(project, @merge_request)
page.should have_link("##{issue.id}")
end
end
describe "for milestones" do
before do
@milestone = Factory :milestone,
:project => project,
:title => "fix ##{issue.id}",
:description => "ask @#{fred.name} for details"
end
it "should render title in milestones#index" do
visit project_milestones_path(project)
page.should have_link("##{issue.id}")
end
it "should render title in milestones#show" do
visit project_milestone_path(project, @milestone)
page.should have_link("##{issue.id}")
end
it "should render description in milestones#show" do
visit project_milestone_path(project, @milestone)
page.should have_link("@#{fred.name}")
end
end
describe "for notes" do
it "should render in commits#show", :js => true do
visit project_commit_path(project, :id => commit.id)
fill_in "note_note", :with => "see ##{issue.id}"
click_button "Add Comment"
page.should have_link("##{issue.id}")
end
it "should render in issue#show", :js => true do
visit project_issue_path(project, issue)
fill_in "note_note", :with => "see ##{issue.id}"
click_button "Add Comment"
page.should have_link("##{issue.id}")
end
it "should render in merge_request#show", :js => true do
visit project_merge_request_path(project, merge_request)
fill_in "note_note", :with => "see ##{issue.id}"
click_button "Add Comment"
page.should have_link("##{issue.id}")
end
it "should render in projects#wall", :js => true do
visit wall_project_path(project)
fill_in "note_note", :with => "see ##{issue.id}"
click_button "Add Comment"
page.should have_link("##{issue.id}")
end
it "should render in wikis#index", :js => true do
visit project_wiki_path(project, :index)
fill_in "Title", :with => 'Test title'
fill_in "Content", :with => '[link test](test)'
click_on "Save"
fill_in "note_note", :with => "see ##{issue.id}"
click_button "Add Comment"
page.should have_link("##{issue.id}")
end
end
describe "for wikis" do
before do
visit project_wiki_path(project, :index)
fill_in "Title", :with => "Circumvent ##{issue.id}"
fill_in "Content", :with => "# Other pages\n\n* [Foo](foo)\n* [Bar](bar)\n\nAlso look at ##{issue.id} :-)"
click_on "Save"
end
it "should NOT render title in wikis#show" do
within(".content h3") do # page title
page.should have_content("Circumvent ##{issue.id}")
page.should_not have_link("##{issue.id}")
end
end
it "should render content in wikis#show" do
page.should have_link("##{issue.id}")
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment