2014-09-02 18:07:02 +05:30
|
|
|
require 'spec_helper'
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
describe Gitlab::ReferenceExtractor do
|
|
|
|
let(:project) { create(:project) }
|
2016-06-02 11:05:42 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
before do
|
|
|
|
project.team << [project.creator, :developer]
|
|
|
|
end
|
2016-11-24 13:41:30 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
subject { described_class.new(project, project.creator) }
|
2015-04-26 12:48:37 +05:30
|
|
|
|
2015-09-11 14:41:01 +05:30
|
|
|
it 'accesses valid user objects' do
|
|
|
|
@u_foo = create(:user, username: 'foo')
|
|
|
|
@u_bar = create(:user, username: 'bar')
|
|
|
|
@u_offteam = create(:user, username: 'offteam')
|
2015-04-26 12:48:37 +05:30
|
|
|
|
2015-09-11 14:41:01 +05:30
|
|
|
project.team << [@u_foo, :reporter]
|
|
|
|
project.team << [@u_bar, :guest]
|
2014-09-02 18:07:02 +05:30
|
|
|
|
2015-09-11 14:41:01 +05:30
|
|
|
subject.analyze('@foo, @baduser, @bar, and @offteam')
|
2015-10-24 18:46:33 +05:30
|
|
|
expect(subject.users).to match_array([@u_foo, @u_bar, @u_offteam])
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
|
2015-09-11 14:41:01 +05:30
|
|
|
it 'ignores user mentions inside specific elements' do
|
2015-04-26 12:48:37 +05:30
|
|
|
@u_foo = create(:user, username: 'foo')
|
|
|
|
@u_bar = create(:user, username: 'bar')
|
|
|
|
@u_offteam = create(:user, username: 'offteam')
|
2014-09-02 18:07:02 +05:30
|
|
|
|
2015-04-26 12:48:37 +05:30
|
|
|
project.team << [@u_foo, :reporter]
|
|
|
|
project.team << [@u_bar, :guest]
|
2014-09-02 18:07:02 +05:30
|
|
|
|
2015-09-11 14:41:01 +05:30
|
|
|
subject.analyze(%Q{
|
2015-09-25 12:07:36 +05:30
|
|
|
Inline code: `@foo`
|
2015-09-11 14:41:01 +05:30
|
|
|
|
|
|
|
Code block:
|
|
|
|
|
|
|
|
```
|
|
|
|
@bar
|
|
|
|
```
|
|
|
|
|
2015-09-25 12:07:36 +05:30
|
|
|
Quote:
|
2015-09-11 14:41:01 +05:30
|
|
|
|
|
|
|
> @offteam
|
|
|
|
})
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2015-10-24 18:46:33 +05:30
|
|
|
expect(subject.users).to match_array([])
|
2015-04-26 12:48:37 +05:30
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
describe 'directly addressed users' do
|
|
|
|
before do
|
|
|
|
@u_foo = create(:user, username: 'foo')
|
|
|
|
@u_foo2 = create(:user, username: 'foo2')
|
|
|
|
@u_foo3 = create(:user, username: 'foo3')
|
|
|
|
@u_foo4 = create(:user, username: 'foo4')
|
|
|
|
@u_foo5 = create(:user, username: 'foo5')
|
|
|
|
|
|
|
|
@u_bar = create(:user, username: 'bar')
|
|
|
|
@u_bar2 = create(:user, username: 'bar2')
|
|
|
|
@u_bar3 = create(:user, username: 'bar3')
|
|
|
|
@u_bar4 = create(:user, username: 'bar4')
|
|
|
|
|
|
|
|
@u_tom = create(:user, username: 'tom')
|
|
|
|
@u_tom2 = create(:user, username: 'tom2')
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when a user is directly addressed' do
|
|
|
|
it 'accesses the user object which is mentioned in the beginning of the line' do
|
|
|
|
subject.analyze('@foo What do you think? cc: @bar, @tom')
|
|
|
|
|
|
|
|
expect(subject.directly_addressed_users).to match_array([@u_foo])
|
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't access the user object if it's not mentioned in the beginning of the line" do
|
|
|
|
subject.analyze('What do you think? cc: @bar')
|
|
|
|
|
|
|
|
expect(subject.directly_addressed_users).to be_empty
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when multiple users are addressed' do
|
|
|
|
it 'accesses the user objects which are mentioned in the beginning of the line' do
|
|
|
|
subject.analyze('@foo @bar What do you think? cc: @tom')
|
|
|
|
|
|
|
|
expect(subject.directly_addressed_users).to match_array([@u_foo, @u_bar])
|
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't access the user objects if they are not mentioned in the beginning of the line" do
|
|
|
|
subject.analyze('What do you think? cc: @foo @bar @tom')
|
|
|
|
|
|
|
|
expect(subject.directly_addressed_users).to be_empty
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when multiple users are addressed in different paragraphs' do
|
|
|
|
it 'accesses user objects which are mentioned in the beginning of each paragraph' do
|
|
|
|
subject.analyze <<-NOTE.strip_heredoc
|
|
|
|
@foo What do you think? cc: @tom
|
|
|
|
|
|
|
|
- @bar can you please have a look?
|
|
|
|
|
|
|
|
>>>
|
|
|
|
@foo2 what do you think? cc: @bar2
|
|
|
|
>>>
|
|
|
|
|
|
|
|
@foo3 @foo4 thank you!
|
|
|
|
|
|
|
|
> @foo5 well done!
|
|
|
|
|
|
|
|
1. @bar3 Can you please check? cc: @tom2
|
|
|
|
2. @bar4 What do you this of this MR?
|
|
|
|
NOTE
|
|
|
|
|
|
|
|
expect(subject.directly_addressed_users).to match_array([@u_foo, @u_foo3, @u_foo4])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-04-26 12:48:37 +05:30
|
|
|
it 'accesses valid issue objects' do
|
|
|
|
@i0 = create(:issue, project: project)
|
|
|
|
@i1 = create(:issue, project: project)
|
2014-09-02 18:07:02 +05:30
|
|
|
|
2015-09-11 14:41:01 +05:30
|
|
|
subject.analyze("#{@i0.to_reference}, #{@i1.to_reference}, and #{Issue.reference_prefix}999.")
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2015-10-24 18:46:33 +05:30
|
|
|
expect(subject.issues).to match_array([@i0, @i1])
|
2015-04-26 12:48:37 +05:30
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
|
2015-04-26 12:48:37 +05:30
|
|
|
it 'accesses valid merge requests' do
|
2015-09-25 12:07:36 +05:30
|
|
|
@m0 = create(:merge_request, source_project: project, target_project: project, source_branch: 'markdown')
|
|
|
|
@m1 = create(:merge_request, source_project: project, target_project: project, source_branch: 'feature_conflict')
|
2014-09-02 18:07:02 +05:30
|
|
|
|
2015-04-26 12:48:37 +05:30
|
|
|
subject.analyze("!999, !#{@m1.iid}, and !#{@m0.iid}.")
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2015-10-24 18:46:33 +05:30
|
|
|
expect(subject.merge_requests).to match_array([@m1, @m0])
|
2015-04-26 12:48:37 +05:30
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
|
2015-09-11 14:41:01 +05:30
|
|
|
it 'accesses valid labels' do
|
|
|
|
@l0 = create(:label, title: 'one', project: project)
|
|
|
|
@l1 = create(:label, title: 'two', project: project)
|
|
|
|
@l2 = create(:label)
|
|
|
|
|
|
|
|
subject.analyze("~#{@l0.id}, ~999, ~#{@l2.id}, ~#{@l1.id}")
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2015-10-24 18:46:33 +05:30
|
|
|
expect(subject.labels).to match_array([@l0, @l1])
|
2015-09-11 14:41:01 +05:30
|
|
|
end
|
|
|
|
|
2015-04-26 12:48:37 +05:30
|
|
|
it 'accesses valid snippets' do
|
|
|
|
@s0 = create(:project_snippet, project: project)
|
|
|
|
@s1 = create(:project_snippet, project: project)
|
|
|
|
@s2 = create(:project_snippet)
|
2014-09-02 18:07:02 +05:30
|
|
|
|
2015-04-26 12:48:37 +05:30
|
|
|
subject.analyze("$#{@s0.id}, $999, $#{@s2.id}, $#{@s1.id}")
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2015-10-24 18:46:33 +05:30
|
|
|
expect(subject.snippets).to match_array([@s0, @s1])
|
2015-04-26 12:48:37 +05:30
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
|
2015-04-26 12:48:37 +05:30
|
|
|
it 'accesses valid commits' do
|
2017-08-17 22:00:37 +05:30
|
|
|
project = create(:project, :repository) { |p| p.add_developer(p.creator) }
|
2015-09-11 14:41:01 +05:30
|
|
|
commit = project.commit('master')
|
2014-09-02 18:07:02 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
extractor = described_class.new(project, project.creator)
|
|
|
|
extractor.analyze("this references commits #{commit.sha[0..6]} and 012345")
|
|
|
|
extracted = extractor.commits
|
|
|
|
|
2015-04-26 12:48:37 +05:30
|
|
|
expect(extracted.size).to eq(1)
|
|
|
|
expect(extracted[0].sha).to eq(commit.sha)
|
|
|
|
expect(extracted[0].message).to eq(commit.message)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'accesses valid commit ranges' do
|
2017-08-17 22:00:37 +05:30
|
|
|
project = create(:project, :repository) { |p| p.add_developer(p.creator) }
|
2015-09-11 14:41:01 +05:30
|
|
|
commit = project.commit('master')
|
|
|
|
earlier_commit = project.commit('master~2')
|
2015-04-26 12:48:37 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
extractor = described_class.new(project, project.creator)
|
|
|
|
extractor.analyze("this references commits #{earlier_commit.sha[0..6]}...#{commit.sha[0..6]}")
|
|
|
|
extracted = extractor.commit_ranges
|
2015-09-11 14:41:01 +05:30
|
|
|
|
2015-04-26 12:48:37 +05:30
|
|
|
expect(extracted.size).to eq(1)
|
2015-09-11 14:41:01 +05:30
|
|
|
expect(extracted.first).to be_kind_of(CommitRange)
|
|
|
|
expect(extracted.first.commit_from).to eq earlier_commit
|
|
|
|
expect(extracted.first.commit_to).to eq commit
|
2015-04-26 12:48:37 +05:30
|
|
|
end
|
|
|
|
|
2015-12-23 02:04:40 +05:30
|
|
|
context 'with an external issue tracker' do
|
|
|
|
let(:project) { create(:jira_project) }
|
2017-09-10 17:25:29 +05:30
|
|
|
let(:issue) { create(:issue, project: project) }
|
|
|
|
|
|
|
|
context 'when GitLab issues are enabled' do
|
|
|
|
it 'returns both JIRA and internal issues' do
|
|
|
|
subject.analyze("JIRA-123 and FOOBAR-4567 and #{issue.to_reference}")
|
|
|
|
expect(subject.issues).to eq [ExternalIssue.new('JIRA-123', project),
|
|
|
|
ExternalIssue.new('FOOBAR-4567', project),
|
|
|
|
issue]
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns only JIRA issues if the internal one does not exists' do
|
|
|
|
subject.analyze("JIRA-123 and FOOBAR-4567 and #999")
|
|
|
|
expect(subject.issues).to eq [ExternalIssue.new('JIRA-123', project),
|
|
|
|
ExternalIssue.new('FOOBAR-4567', project)]
|
|
|
|
end
|
|
|
|
end
|
2015-12-23 02:04:40 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
context 'when GitLab issues are disabled' do
|
|
|
|
before do
|
|
|
|
project.issues_enabled = false
|
|
|
|
project.save!
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns only JIRA issues' do
|
|
|
|
subject.analyze("JIRA-123 and FOOBAR-4567 and #{issue.to_reference}")
|
|
|
|
expect(subject.issues).to eq [ExternalIssue.new('JIRA-123', project),
|
|
|
|
ExternalIssue.new('FOOBAR-4567', project)]
|
|
|
|
end
|
2015-12-23 02:04:40 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-04-26 12:48:37 +05:30
|
|
|
context 'with a project with an underscore' do
|
2017-09-10 17:25:29 +05:30
|
|
|
let(:other_project) { create(:project, path: 'test_project') }
|
2015-04-26 12:48:37 +05:30
|
|
|
let(:issue) { create(:issue, project: other_project) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
other_project.team << [project.creator, :developer]
|
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
|
2015-04-26 12:48:37 +05:30
|
|
|
it 'handles project issue references' do
|
2015-09-11 14:41:01 +05:30
|
|
|
subject.analyze("this refers issue #{issue.to_reference(project)}")
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2015-04-26 12:48:37 +05:30
|
|
|
extracted = subject.issues
|
|
|
|
expect(extracted.size).to eq(1)
|
2015-10-24 18:46:33 +05:30
|
|
|
expect(extracted).to match_array([issue])
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
end
|
2016-06-02 11:05:42 +05:30
|
|
|
|
|
|
|
describe '#all' do
|
|
|
|
let(:issue) { create(:issue, project: project) }
|
|
|
|
let(:label) { create(:label, project: project) }
|
|
|
|
let(:text) { "Ref. #{issue.to_reference} and #{label.to_reference}" }
|
|
|
|
|
|
|
|
before do
|
|
|
|
project.team << [project.creator, :developer]
|
|
|
|
subject.analyze(text)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns all referables' do
|
|
|
|
expect(subject.all).to match_array([issue, label])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '.references_pattern' do
|
|
|
|
subject { described_class.references_pattern }
|
|
|
|
it { is_expected.to be_kind_of Regexp }
|
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|