2019-03-22 09:28:50 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2013-05-30 23:16:49 +00:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 18:09:03 +00:00
|
|
|
RSpec.describe Gitlab::ReferenceExtractor do
|
2020-01-30 21:08:47 +00:00
|
|
|
let_it_be(:project) { create(:project) }
|
2016-03-17 20:39:50 +00:00
|
|
|
|
2017-01-24 23:42:12 +00:00
|
|
|
before do
|
2017-12-22 08:18:28 +00:00
|
|
|
project.add_developer(project.creator)
|
2017-01-24 23:42:12 +00:00
|
|
|
end
|
2016-11-01 20:18:51 +00:00
|
|
|
|
2017-07-25 17:09:00 +00:00
|
|
|
subject { described_class.new(project, project.creator) }
|
2015-03-27 11:58:23 +00:00
|
|
|
|
2015-04-03 16:03:34 +00:00
|
|
|
it 'accesses valid user objects' do
|
2015-03-27 11:58:23 +00:00
|
|
|
@u_foo = create(:user, username: 'foo')
|
|
|
|
@u_bar = create(:user, username: 'bar')
|
2015-04-03 16:03:34 +00:00
|
|
|
@u_offteam = create(:user, username: 'offteam')
|
2013-05-30 23:16:49 +00:00
|
|
|
|
2017-12-22 08:18:28 +00:00
|
|
|
project.add_guest(@u_foo)
|
|
|
|
project.add_guest(@u_bar)
|
2013-05-30 23:16:49 +00:00
|
|
|
|
2015-03-27 11:58:23 +00:00
|
|
|
subject.analyze('@foo, @baduser, @bar, and @offteam')
|
2015-10-13 20:53:05 +00:00
|
|
|
expect(subject.users).to match_array([@u_foo, @u_bar, @u_offteam])
|
2015-03-27 11:58:23 +00:00
|
|
|
end
|
2013-05-30 23:16:49 +00:00
|
|
|
|
2015-06-02 11:17:11 +00:00
|
|
|
it 'ignores user mentions inside specific elements' do
|
|
|
|
@u_foo = create(:user, username: 'foo')
|
|
|
|
@u_bar = create(:user, username: 'bar')
|
|
|
|
@u_offteam = create(:user, username: 'offteam')
|
|
|
|
|
2017-12-22 08:18:28 +00:00
|
|
|
project.add_reporter(@u_foo)
|
|
|
|
project.add_reporter(@u_bar)
|
2015-06-02 11:17:11 +00:00
|
|
|
|
|
|
|
subject.analyze(%Q{
|
2015-08-11 12:33:31 +00:00
|
|
|
Inline code: `@foo`
|
2015-06-02 11:17:11 +00:00
|
|
|
|
|
|
|
Code block:
|
|
|
|
|
|
|
|
```
|
|
|
|
@bar
|
|
|
|
```
|
2015-06-02 11:17:21 +00:00
|
|
|
|
2015-08-11 12:33:31 +00:00
|
|
|
Quote:
|
2015-06-02 11:17:21 +00:00
|
|
|
|
|
|
|
> @offteam
|
2015-06-02 11:17:11 +00:00
|
|
|
})
|
2016-12-05 12:12:22 +00:00
|
|
|
|
2015-10-13 20:53:05 +00:00
|
|
|
expect(subject.users).to match_array([])
|
2015-06-02 11:17:11 +00:00
|
|
|
end
|
|
|
|
|
2016-12-05 12:12:22 +00:00
|
|
|
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
|
|
|
|
|
2017-12-08 09:50:22 +00:00
|
|
|
it 'does not include anchors from table of contents in issue references' do
|
|
|
|
issue1 = create(:issue, project: project)
|
|
|
|
issue2 = create(:issue, project: project)
|
|
|
|
|
|
|
|
subject.analyze("not real issue <h4>#{issue1.iid}</h4>, real issue #{issue2.to_reference}")
|
|
|
|
|
|
|
|
expect(subject.issues).to match_array([issue2])
|
|
|
|
end
|
|
|
|
|
2015-03-27 11:58:23 +00:00
|
|
|
it 'accesses valid issue objects' do
|
|
|
|
@i0 = create(:issue, project: project)
|
|
|
|
@i1 = create(:issue, project: project)
|
2013-05-30 23:16:49 +00:00
|
|
|
|
2020-04-01 15:07:45 +00:00
|
|
|
subject.analyze("#{@i0.to_reference}, #{@i1.to_reference}, and #{Issue.reference_prefix}#{non_existing_record_iid}.")
|
2016-12-05 12:12:22 +00:00
|
|
|
|
2015-10-13 20:53:05 +00:00
|
|
|
expect(subject.issues).to match_array([@i0, @i1])
|
2015-03-27 11:58:23 +00:00
|
|
|
end
|
2013-05-30 23:16:49 +00:00
|
|
|
|
2015-03-27 11:58:23 +00:00
|
|
|
it 'accesses valid merge requests' do
|
2015-08-11 12:33:31 +00:00
|
|
|
@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')
|
2013-05-30 23:16:49 +00:00
|
|
|
|
2020-04-01 15:07:45 +00:00
|
|
|
subject.analyze("!#{non_existing_record_iid}, !#{@m1.iid}, and !#{@m0.iid}.")
|
2016-12-05 12:12:22 +00:00
|
|
|
|
2015-10-13 20:53:05 +00:00
|
|
|
expect(subject.merge_requests).to match_array([@m1, @m0])
|
2015-03-27 11:58:23 +00:00
|
|
|
end
|
2013-05-30 23:16:49 +00:00
|
|
|
|
2015-04-15 18:02:36 +00:00
|
|
|
it 'accesses valid labels' do
|
|
|
|
@l0 = create(:label, title: 'one', project: project)
|
|
|
|
@l1 = create(:label, title: 'two', project: project)
|
|
|
|
@l2 = create(:label)
|
|
|
|
|
2020-04-01 15:07:45 +00:00
|
|
|
subject.analyze("~#{@l0.id}, ~#{non_existing_record_id}, ~#{@l2.id}, ~#{@l1.id}")
|
2016-12-05 12:12:22 +00:00
|
|
|
|
2015-10-13 20:53:05 +00:00
|
|
|
expect(subject.labels).to match_array([@l0, @l1])
|
2015-04-15 18:02:36 +00:00
|
|
|
end
|
|
|
|
|
2015-03-27 11:58:23 +00:00
|
|
|
it 'accesses valid snippets' do
|
|
|
|
@s0 = create(:project_snippet, project: project)
|
|
|
|
@s1 = create(:project_snippet, project: project)
|
|
|
|
@s2 = create(:project_snippet)
|
2013-05-30 23:16:49 +00:00
|
|
|
|
2020-04-01 15:07:45 +00:00
|
|
|
subject.analyze("$#{@s0.id}, $#{non_existing_record_id}, $#{@s2.id}, $#{@s1.id}")
|
2016-12-05 12:12:22 +00:00
|
|
|
|
2015-10-13 20:53:05 +00:00
|
|
|
expect(subject.snippets).to match_array([@s0, @s1])
|
2015-03-27 11:58:23 +00:00
|
|
|
end
|
2013-05-30 23:16:49 +00:00
|
|
|
|
2015-03-27 11:58:23 +00:00
|
|
|
it 'accesses valid commits' do
|
2017-01-24 23:42:12 +00:00
|
|
|
project = create(:project, :repository) { |p| p.add_developer(p.creator) }
|
2015-04-21 13:13:40 +00:00
|
|
|
commit = project.commit('master')
|
2013-05-30 23:16:49 +00:00
|
|
|
|
2017-01-24 23:42:12 +00:00
|
|
|
extractor = described_class.new(project, project.creator)
|
|
|
|
extractor.analyze("this references commits #{commit.sha[0..6]} and 012345")
|
|
|
|
extracted = extractor.commits
|
|
|
|
|
2015-03-27 11:58:23 +00:00
|
|
|
expect(extracted.size).to eq(1)
|
|
|
|
expect(extracted[0].sha).to eq(commit.sha)
|
|
|
|
expect(extracted[0].message).to eq(commit.message)
|
|
|
|
end
|
2015-03-06 22:08:28 +00:00
|
|
|
|
2015-03-27 11:58:23 +00:00
|
|
|
it 'accesses valid commit ranges' do
|
2017-01-24 23:42:12 +00:00
|
|
|
project = create(:project, :repository) { |p| p.add_developer(p.creator) }
|
2015-04-21 13:13:40 +00:00
|
|
|
commit = project.commit('master')
|
|
|
|
earlier_commit = project.commit('master~2')
|
2015-03-06 22:08:28 +00:00
|
|
|
|
2017-01-24 23:42:12 +00:00
|
|
|
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-04-25 18:46:06 +00:00
|
|
|
|
2015-03-27 11:58:23 +00:00
|
|
|
expect(extracted.size).to eq(1)
|
2015-04-25 18:46:06 +00:00
|
|
|
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
|
2013-05-30 23:16:49 +00:00
|
|
|
end
|
2015-03-12 16:11:59 +00:00
|
|
|
|
2015-12-17 22:08:14 +00:00
|
|
|
context 'with an external issue tracker' do
|
|
|
|
let(:project) { create(:jira_project) }
|
2017-07-10 07:38:42 +00:00
|
|
|
let(:issue) { create(:issue, project: project) }
|
|
|
|
|
|
|
|
context 'when GitLab issues are enabled' do
|
2019-06-28 13:25:56 +00:00
|
|
|
it 'returns both Jira and internal issues' do
|
2017-07-10 07:38:42 +00:00
|
|
|
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
|
|
|
|
|
2020-05-26 06:08:20 +00:00
|
|
|
it 'returns only Jira issues if the internal one does not exist' do
|
2020-04-01 15:07:45 +00:00
|
|
|
subject.analyze("JIRA-123 and FOOBAR-4567 and ##{non_existing_record_iid}")
|
2017-07-10 07:38:42 +00:00
|
|
|
expect(subject.issues).to eq [ExternalIssue.new('JIRA-123', project),
|
|
|
|
ExternalIssue.new('FOOBAR-4567', project)]
|
|
|
|
end
|
|
|
|
end
|
2015-12-17 22:08:14 +00:00
|
|
|
|
2017-07-10 07:38:42 +00:00
|
|
|
context 'when GitLab issues are disabled' do
|
|
|
|
before do
|
|
|
|
project.issues_enabled = false
|
|
|
|
project.save!
|
|
|
|
end
|
|
|
|
|
2019-06-28 13:25:56 +00:00
|
|
|
it 'returns only Jira issues' do
|
2017-07-10 07:38:42 +00:00
|
|
|
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-17 22:08:14 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-03-13 06:09:37 +00:00
|
|
|
context 'with an inactive external issue tracker' do
|
|
|
|
let(:project) { create(:project) }
|
2021-06-18 15:10:16 +00:00
|
|
|
let!(:jira_integration) { create(:jira_integration, project: project, active: false) }
|
2020-03-13 06:09:37 +00:00
|
|
|
let(:issue) { create(:issue, project: project) }
|
|
|
|
|
|
|
|
context 'when GitLab issues are enabled' do
|
|
|
|
it 'returns only internal issue' do
|
|
|
|
subject.analyze("JIRA-123 and FOOBAR-4567 and #{issue.to_reference}")
|
|
|
|
expect(subject.issues).to eq([issue])
|
|
|
|
end
|
|
|
|
|
2020-05-26 06:08:20 +00:00
|
|
|
it 'does not return any issue if the internal one does not exist' do
|
2020-03-13 06:09:37 +00:00
|
|
|
subject.analyze("JIRA-123 and FOOBAR-4567 and #999")
|
|
|
|
expect(subject.issues).to be_empty
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-03-12 16:11:59 +00:00
|
|
|
context 'with a project with an underscore' do
|
2017-08-02 19:55:11 +00:00
|
|
|
let(:other_project) { create(:project, path: 'test_project') }
|
2015-04-03 16:03:34 +00:00
|
|
|
let(:issue) { create(:issue, project: other_project) }
|
|
|
|
|
|
|
|
before do
|
2017-12-22 08:18:28 +00:00
|
|
|
other_project.add_developer(project.creator)
|
2015-04-03 16:03:34 +00:00
|
|
|
end
|
2015-03-12 16:11:59 +00:00
|
|
|
|
|
|
|
it 'handles project issue references' do
|
2015-05-21 20:35:15 +00:00
|
|
|
subject.analyze("this refers issue #{issue.to_reference(project)}")
|
2016-12-05 12:12:22 +00:00
|
|
|
|
2015-03-27 11:58:23 +00:00
|
|
|
extracted = subject.issues
|
2015-03-12 16:11:59 +00:00
|
|
|
expect(extracted.size).to eq(1)
|
2015-10-13 20:53:05 +00:00
|
|
|
expect(extracted).to match_array([issue])
|
2015-03-12 16:11:59 +00:00
|
|
|
end
|
|
|
|
end
|
2016-03-07 12:09:53 +00:00
|
|
|
|
|
|
|
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}" }
|
|
|
|
|
2016-03-19 17:58:52 +00:00
|
|
|
before do
|
2017-12-22 08:18:28 +00:00
|
|
|
project.add_developer(project.creator)
|
2016-03-19 17:58:52 +00:00
|
|
|
subject.analyze(text)
|
|
|
|
end
|
2016-03-07 12:09:53 +00:00
|
|
|
|
|
|
|
it 'returns all referables' do
|
|
|
|
expect(subject.all).to match_array([issue, label])
|
|
|
|
end
|
|
|
|
end
|
2016-03-18 13:48:55 +00:00
|
|
|
|
|
|
|
describe '.references_pattern' do
|
|
|
|
subject { described_class.references_pattern }
|
2019-10-18 11:11:44 +00:00
|
|
|
|
2016-03-18 13:48:55 +00:00
|
|
|
it { is_expected.to be_kind_of Regexp }
|
|
|
|
end
|
2017-12-01 09:21:05 +00:00
|
|
|
|
|
|
|
describe 'referables prefixes' do
|
|
|
|
def prefixes
|
|
|
|
described_class::REFERABLES.each_with_object({}) do |referable, result|
|
2019-10-17 12:07:33 +00:00
|
|
|
class_name = referable.to_s.camelize
|
|
|
|
klass = class_name.constantize if Object.const_defined?(class_name)
|
2017-12-01 09:21:05 +00:00
|
|
|
|
|
|
|
next unless klass.respond_to?(:reference_prefix)
|
|
|
|
|
|
|
|
prefix = klass.reference_prefix
|
|
|
|
result[prefix] ||= []
|
|
|
|
result[prefix] << referable
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns all supported prefixes' do
|
2020-11-17 21:09:19 +00:00
|
|
|
expect(prefixes.keys.uniq).to match_array(%w(@ # ~ % ! $ & [vulnerability: *iteration:))
|
2017-12-01 09:21:05 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not allow one prefix for multiple referables if not allowed specificly' do
|
|
|
|
# make sure you are not overriding existing prefix before changing this hash
|
|
|
|
multiple_allowed = {
|
|
|
|
'@' => 3
|
|
|
|
}
|
|
|
|
|
|
|
|
prefixes.each do |prefix, referables|
|
|
|
|
expected_count = multiple_allowed[prefix] || 1
|
|
|
|
expect(referables.count).to eq(expected_count)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2020-01-30 21:08:47 +00:00
|
|
|
|
|
|
|
describe '#references' do
|
|
|
|
let_it_be(:user) { create(:user) }
|
|
|
|
let_it_be(:issue) { create(:issue, project: project) }
|
2021-06-29 03:07:32 +00:00
|
|
|
|
2020-01-30 21:08:47 +00:00
|
|
|
let(:text) { "Ref. #{issue.to_reference}" }
|
|
|
|
|
|
|
|
subject { described_class.new(project, user) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
subject.analyze(text)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when references are visible' do
|
|
|
|
before do
|
|
|
|
project.add_developer(user)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns visible references of given type' do
|
|
|
|
expect(subject.references(:issue)).to eq([issue])
|
|
|
|
end
|
2021-08-26 18:10:35 +00:00
|
|
|
end
|
2020-01-30 21:08:47 +00:00
|
|
|
|
2021-08-26 18:10:35 +00:00
|
|
|
it 'does not return any references' do
|
|
|
|
expect(subject.references(:issue)).to be_empty
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#all_visible?' do
|
|
|
|
let_it_be(:user) { create(:user) }
|
|
|
|
let_it_be(:project2) { create(:project) }
|
|
|
|
let_it_be(:issue) { create(:issue, project: project) }
|
|
|
|
let_it_be(:issue2) { create(:issue, project: project2) }
|
|
|
|
|
|
|
|
let(:text) { "Ref. #{issue.to_reference} and #{issue2.to_reference(project)}" }
|
|
|
|
|
|
|
|
subject { described_class.new(project, user) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
subject.analyze(text)
|
2020-01-30 21:08:47 +00:00
|
|
|
end
|
|
|
|
|
2021-08-26 18:10:35 +00:00
|
|
|
it 'returns true if no references were parsed yet' do
|
|
|
|
expect(subject.all_visible?).to be_truthy
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when references was already called' do
|
|
|
|
let(:membership) { [] }
|
|
|
|
|
|
|
|
before do
|
|
|
|
membership.each { |p| p.add_developer(user) }
|
|
|
|
|
|
|
|
subject.references(:issue)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns false' do
|
|
|
|
expect(subject.all_visible?).to be_falsey
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when user can access only some references' do
|
|
|
|
let(:membership) { [project] }
|
|
|
|
|
|
|
|
it 'returns false' do
|
|
|
|
expect(subject.all_visible?).to be_falsey
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when user can access all references' do
|
|
|
|
let(:membership) { [project, project2] }
|
|
|
|
|
|
|
|
it 'returns true' do
|
|
|
|
expect(subject.all_visible?).to be_truthy
|
|
|
|
end
|
|
|
|
end
|
2020-01-30 21:08:47 +00:00
|
|
|
end
|
|
|
|
end
|
2013-05-30 23:16:49 +00:00
|
|
|
end
|