提交 fb300396 编写于 作者: D Dmitriy Zaporozhets

Merge branch 'disable-ref-generation-in-code-blocks' into 'master'

Disable reference generation in preformatted/code blocks

### Summary

If a user adds text in code or preformatted text via Markdown or HTML that contains `#XXX`, the system adds a note that issue `XXX` was mentioned. This is particularly annoying because we often list gdb backtrace dumps into our issues, and many issues get mentioned as a result. For example:

```
(gdb) bt
#0  0x00000000004004c4 in second () at main.cc:6
#1  0x00000000004004d2 in first () at main.cc:11
#2  0x00000000004004dd in main () at main.cc:17
(gdb)
```

### Steps to reproduce

1. In an issue, write the above text using Markdown or HTML tags (e.g. `<code>`, `<pre>`).
2. Observe that [issue 1](https://gitlab.com/gitlab-org/gitlab-ce/issues/1) and [issue 2](https://gitlab.com/gitlab-org/gitlab-ce/issues/2) have a note that says they were mentioned.

### Expected behavior

Everything enclosed in the code blocks should be ignored as references.

### Observed behavior

Issues get referenced unnecessarily.

### Fix

I've made `reference_extractor.rb` strip out HTML and Markdown blocks before processing. I considered running the raw text through the entire Markdown processor, but this seems overkill and perhaps could lead to some unintended side effects.

See merge request !365
......@@ -5,6 +5,7 @@ v 7.10.0 (unreleased)
- Fix dots in Wiki slugs causing errors (Stan Hu)
- Update poltergeist to version 1.6.0 to support PhantomJS 2.0 (Zeger-Jan van de Weg)
- Fix cross references when usernames, milestones, or project names contain underscores (Stan Hu)
- Disable reference creation for comments surrounded by code/preformatted blocks (Stan Hu)
- enable line wrapping per default and remove the checkbox to toggle it (Hannes Rosenögger)
- extend the commit calendar to show the actual commits made on a date (Hannes Rosenögger)
- Fix a link in the patch update guide
......
......@@ -11,7 +11,13 @@ module Gitlab
end
def analyze(string, project)
parse_references(string.dup, project)
text = string.dup
# Remove preformatted/code blocks so that references are not included
text.gsub!(%r{<pre>.*?</pre>|<code>.*?</code>}m) { |match| '' }
text.gsub!(%r{^```.*?^```}m) { |match| '' }
parse_references(text, project)
end
# Given a valid project, resolve the extracted identifiers of the requested type to
......
......@@ -50,6 +50,26 @@ describe Gitlab::ReferenceExtractor do
expect(text).to eq('issue #123 is just the worst, @user')
end
it 'extracts no references for <pre>..</pre> blocks' do
subject.analyze("<pre>def puts '#1 issue'\nend\n</pre>```", nil)
expect(subject.issues).to be_blank
end
it 'extracts no references for <code>..</code> blocks' do
subject.analyze("<code>def puts '!1 request'\nend\n</code>```", nil)
expect(subject.merge_requests).to be_blank
end
it 'extracts no references for code blocks with language' do
subject.analyze("this code:\n```ruby\ndef puts '#1 issue'\nend\n```", nil)
expect(subject.issues).to be_blank
end
it 'extracts issue references for invalid code blocks' do
subject.analyze('test: ```this one talks about issue #1234```', nil)
expect(subject.issues).to eq([{ project: nil, id: '1234' }])
end
it 'handles all possible kinds of references' do
accessors = Gitlab::Markdown::TYPES.map { |t| "#{t}s".to_sym }
expect(subject).to respond_to(*accessors)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册