2013-05-30 19:16:49 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe Gitlab::ReferenceExtractor do
|
|
|
|
it 'extracts username references' do
|
2014-10-02 14:26:39 -04:00
|
|
|
subject.analyze('this contains a @user reference', nil)
|
2015-02-12 13:17:35 -05:00
|
|
|
expect(subject.users).to eq([{ project: nil, id: 'user' }])
|
2013-05-30 19:16:49 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'extracts issue references' do
|
2014-10-02 14:26:39 -04:00
|
|
|
subject.analyze('this one talks about issue #1234', nil)
|
2015-02-12 13:17:35 -05:00
|
|
|
expect(subject.issues).to eq([{ project: nil, id: '1234' }])
|
2013-05-30 19:16:49 -04:00
|
|
|
end
|
|
|
|
|
2014-05-06 13:51:56 -04:00
|
|
|
it 'extracts JIRA issue references' do
|
2014-10-02 14:26:39 -04:00
|
|
|
subject.analyze('this one talks about issue JIRA-1234', nil)
|
2015-02-12 13:17:35 -05:00
|
|
|
expect(subject.issues).to eq([{ project: nil, id: 'JIRA-1234' }])
|
2014-05-06 13:51:56 -04:00
|
|
|
end
|
|
|
|
|
2013-05-30 19:16:49 -04:00
|
|
|
it 'extracts merge request references' do
|
2014-10-02 14:26:39 -04:00
|
|
|
subject.analyze("and here's !43, a merge request", nil)
|
2015-02-12 13:17:35 -05:00
|
|
|
expect(subject.merge_requests).to eq([{ project: nil, id: '43' }])
|
2013-05-30 19:16:49 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'extracts snippet ids' do
|
2014-10-02 14:26:39 -04:00
|
|
|
subject.analyze('snippets like $12 get extracted as well', nil)
|
2015-02-12 13:17:35 -05:00
|
|
|
expect(subject.snippets).to eq([{ project: nil, id: '12' }])
|
2013-05-30 19:16:49 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'extracts commit shas' do
|
2014-10-02 14:26:39 -04:00
|
|
|
subject.analyze('commit shas 98cf0ae3 are pulled out as Strings', nil)
|
2015-02-12 13:17:35 -05:00
|
|
|
expect(subject.commits).to eq([{ project: nil, id: '98cf0ae3' }])
|
2013-05-30 19:16:49 -04:00
|
|
|
end
|
|
|
|
|
2015-03-06 17:08:28 -05:00
|
|
|
it 'extracts commit ranges' do
|
|
|
|
subject.analyze('here you go, a commit range: 98cf0ae3...98cf0ae4', nil)
|
|
|
|
expect(subject.commit_ranges).to eq([{ project: nil, id: '98cf0ae3...98cf0ae4' }])
|
|
|
|
end
|
|
|
|
|
2013-05-30 19:16:49 -04:00
|
|
|
it 'extracts multiple references and preserves their order' do
|
2014-10-02 14:26:39 -04:00
|
|
|
subject.analyze('@me and @you both care about this', nil)
|
2015-02-12 13:17:35 -05:00
|
|
|
expect(subject.users).to eq([
|
2014-10-02 14:26:39 -04:00
|
|
|
{ project: nil, id: 'me' },
|
|
|
|
{ project: nil, id: 'you' }
|
2015-02-12 13:17:35 -05:00
|
|
|
])
|
2013-05-30 19:16:49 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'leaves the original note unmodified' do
|
2014-10-02 14:26:39 -04:00
|
|
|
text = 'issue #123 is just the worst, @user'
|
|
|
|
subject.analyze(text, nil)
|
2015-02-12 13:17:35 -05:00
|
|
|
expect(text).to eq('issue #123 is just the worst, @user')
|
2013-05-30 19:16:49 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'handles all possible kinds of references' do
|
|
|
|
accessors = Gitlab::Markdown::TYPES.map { |t| "#{t}s".to_sym }
|
2015-02-12 13:17:35 -05:00
|
|
|
expect(subject).to respond_to(*accessors)
|
2013-05-30 19:16:49 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'with a project' do
|
2014-01-22 14:03:52 -05:00
|
|
|
let(:project) { create(:project) }
|
2013-05-30 19:16:49 -04:00
|
|
|
|
|
|
|
it 'accesses valid user objects on the project team' do
|
|
|
|
@u_foo = create(:user, username: 'foo')
|
|
|
|
@u_bar = create(:user, username: 'bar')
|
|
|
|
create(:user, username: 'offteam')
|
|
|
|
|
|
|
|
project.team << [@u_foo, :reporter]
|
|
|
|
project.team << [@u_bar, :guest]
|
|
|
|
|
2014-10-02 14:26:39 -04:00
|
|
|
subject.analyze('@foo, @baduser, @bar, and @offteam', project)
|
2015-02-12 13:17:35 -05:00
|
|
|
expect(subject.users_for(project)).to eq([@u_foo, @u_bar])
|
2013-05-30 19:16:49 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'accesses valid issue objects' do
|
|
|
|
@i0 = create(:issue, project: project)
|
|
|
|
@i1 = create(:issue, project: project)
|
|
|
|
|
2014-10-02 14:26:39 -04:00
|
|
|
subject.analyze("##{@i0.iid}, ##{@i1.iid}, and #999.", project)
|
2015-02-12 13:17:35 -05:00
|
|
|
expect(subject.issues_for(project)).to eq([@i0, @i1])
|
2013-05-30 19:16:49 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'accesses valid merge requests' do
|
|
|
|
@m0 = create(:merge_request, source_project: project, target_project: project, source_branch: 'aaa')
|
|
|
|
@m1 = create(:merge_request, source_project: project, target_project: project, source_branch: 'bbb')
|
|
|
|
|
2014-10-02 14:26:39 -04:00
|
|
|
subject.analyze("!999, !#{@m1.iid}, and !#{@m0.iid}.", project)
|
2015-02-12 13:17:35 -05:00
|
|
|
expect(subject.merge_requests_for(project)).to eq([@m1, @m0])
|
2013-05-30 19:16:49 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'accesses valid snippets' do
|
|
|
|
@s0 = create(:project_snippet, project: project)
|
|
|
|
@s1 = create(:project_snippet, project: project)
|
|
|
|
@s2 = create(:project_snippet)
|
|
|
|
|
2014-10-02 14:26:39 -04:00
|
|
|
subject.analyze("$#{@s0.id}, $999, $#{@s2.id}, $#{@s1.id}", project)
|
2015-02-12 13:17:35 -05:00
|
|
|
expect(subject.snippets_for(project)).to eq([@s0, @s1])
|
2013-05-30 19:16:49 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'accesses valid commits' do
|
2014-10-02 14:26:39 -04:00
|
|
|
commit = project.repository.commit('master')
|
2013-05-30 19:16:49 -04:00
|
|
|
|
2014-10-02 14:26:39 -04:00
|
|
|
subject.analyze("this references commits #{commit.sha[0..6]} and 012345",
|
|
|
|
project)
|
2013-05-30 19:16:49 -04:00
|
|
|
extracted = subject.commits_for(project)
|
2015-02-12 13:17:35 -05:00
|
|
|
expect(extracted.size).to eq(1)
|
|
|
|
expect(extracted[0].sha).to eq(commit.sha)
|
|
|
|
expect(extracted[0].message).to eq(commit.message)
|
2013-05-30 19:16:49 -04:00
|
|
|
end
|
2015-03-06 17:08:28 -05:00
|
|
|
|
|
|
|
it 'accesses valid commit ranges' do
|
|
|
|
commit = project.repository.commit('master')
|
|
|
|
earlier_commit = project.repository.commit('master~2')
|
|
|
|
|
|
|
|
subject.analyze("this references commits #{earlier_commit.sha[0..6]}...#{commit.sha[0..6]}",
|
|
|
|
project)
|
|
|
|
extracted = subject.commit_ranges_for(project)
|
|
|
|
expect(extracted.size).to eq(1)
|
|
|
|
expect(extracted[0][0].sha).to eq(earlier_commit.sha)
|
|
|
|
expect(extracted[0][0].message).to eq(earlier_commit.message)
|
|
|
|
expect(extracted[0][1].sha).to eq(commit.sha)
|
|
|
|
expect(extracted[0][1].message).to eq(commit.message)
|
|
|
|
end
|
2013-05-30 19:16:49 -04:00
|
|
|
end
|
|
|
|
end
|