summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/gfm/reference_rewriter_spec.rb
blob: 7d4a3655be6f59cef0a589eae4dfdd2caa303d78 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::Gfm::ReferenceRewriter do
  let_it_be(:group) { create(:group) }
  let_it_be(:user) { create(:user) }

  let(:new_project) { create(:project, name: 'new-project', group: group) }
  let(:old_project) { create(:project, name: 'old-project', group: group) }
  let(:old_project_ref) { old_project.to_reference_base(new_project) }
  let(:text) { 'some text' }

  before do
    old_project.add_reporter(user)
  end

  describe '#rewrite' do
    subject do
      described_class.new(text, old_project, user).rewrite(new_project)
    end

    context 'multiple issues and merge requests referenced' do
      let!(:issue_first) { create(:issue, project: old_project) }
      let!(:issue_second) { create(:issue, project: old_project) }
      let!(:merge_request) { create(:merge_request, source_project: old_project) }

      context 'plain text description' do
        let(:text) { 'Description that references #1, #2 and !1' }

        it { is_expected.to include issue_first.to_reference(new_project) }
        it { is_expected.to include issue_second.to_reference(new_project) }
        it { is_expected.to include merge_request.to_reference(new_project) }
      end

      context 'description with ignored elements' do
        let(:text) do
          "Hi. This references #1, but not `#2`\n" \
            '<pre>and not !1</pre>'
        end

        it { is_expected.to include issue_first.to_reference(new_project) }
        it { is_expected.not_to include issue_second.to_reference(new_project) }
        it { is_expected.not_to include merge_request.to_reference(new_project) }
      end

      context 'rewrite ambigous references' do
        context 'url' do
          let(:url) { 'http://gitlab.com/#1' }
          let(:text) { "This references #1, but not #{url}" }

          it { is_expected.to include url }
        end

        context 'code' do
          let(:text) { "#1, but not `[#1]`" }

          it { is_expected.to eq "#{issue_first.to_reference(new_project)}, but not `[#1]`" }
        end

        context 'code reverse' do
          let(:text) { "not `#1`, but #1" }

          it { is_expected.to eq "not `#1`, but #{issue_first.to_reference(new_project)}" }
        end

        context 'code in random order' do
          let(:text) { "#1, `#1`, #1, `#1`" }
          let(:ref) { issue_first.to_reference(new_project) }

          it { is_expected.to eq "#{ref}, `#1`, #{ref}, `#1`" }
        end

        context 'description with project labels' do
          let!(:label) { create(:label, id: 123, name: 'test', project: old_project) }

          context 'label referenced by id' do
            let(:text) { '#1 and ~123' }

            it { is_expected.to eq %Q{#{old_project_ref}#1 and #{old_project_ref}~123} }
          end

          context 'label referenced by text' do
            let(:text) { '#1 and ~"test"' }

            it { is_expected.to eq %Q{#{old_project_ref}#1 and #{old_project_ref}~123} }
          end
        end

        context 'description with group labels' do
          let(:old_group) { create(:group) }
          let!(:group_label) { create(:group_label, id: 321, name: 'group label', group: old_group) }

          before do
            old_project.update!(namespace: old_group)
          end

          context 'label referenced by id' do
            let(:text) { '#1 and ~321' }

            it { is_expected.to eq %Q{#{old_project_ref}#1 and #{old_project_ref}~321} }
          end

          context 'label referenced by text' do
            let(:text) { '#1 and ~"group label"' }

            it { is_expected.to eq %Q{#{old_project_ref}#1 and #{old_project_ref}~321} }
          end
        end
      end
    end

    context 'when description contains a local reference' do
      let(:local_issue) { create(:issue, project: old_project) }
      let(:text) { "See ##{local_issue.iid}" }

      it { is_expected.to eq("See #{old_project.path}##{local_issue.iid}") }
    end

    context 'when description contains a cross reference' do
      let(:merge_request) { create(:merge_request) }
      let(:text) { "See #{merge_request.project.full_path}!#{merge_request.iid}" }

      it { is_expected.to eq(text) }
    end

    context 'with a commit' do
      let(:old_project) { create(:project, :repository, name: 'old-project', group: group) }
      let(:commit) { old_project.commit }

      context 'reference to an absolute URL to a commit' do
        let(:text) { Gitlab::UrlBuilder.build(commit) }

        it { is_expected.to eq(text) }
      end

      context 'reference to a commit' do
        let(:text) { commit.id }

        it { is_expected.to eq("#{old_project_ref}@#{text}") }
      end
    end

    context 'reference contains project milestone' do
      let!(:milestone) do
        create(:milestone, title: '9.0', project: old_project)
      end

      let(:text) { 'milestone: %"9.0"' }

      it { is_expected.to eq %Q[milestone: #{old_project_ref}%"9.0"] }
    end

    context 'when referring to group milestone' do
      let!(:milestone) do
        create(:milestone, title: '10.0', group: group)
      end

      let(:text) { 'milestone %"10.0"' }

      it { is_expected.to eq text }
    end

    context 'when referring to a group' do
      let(:text) { "group @#{group.full_path}" }

      it { is_expected.to eq text }
    end

    context 'when referring to a user' do
      let(:text) { "user @#{user.full_path}" }

      it { is_expected.to eq text }
    end

    context 'when referable has a nil reference' do
      before do
        create(:milestone, title: '9.0', project: old_project)

        allow_any_instance_of(Milestone)
          .to receive(:to_reference)
          .and_return(nil)
      end

      let(:text) { 'milestone: %"9.0"' }

      it 'raises an error that should be fixed' do
        expect { subject }.to raise_error(
          described_class::RewriteError,
          'Unspecified reference detected for Milestone'
        )
      end
    end
  end
end