summaryrefslogtreecommitdiff
path: root/spec/services/git_tag_push_service_spec.rb
blob: 0879e3ab4c881e8b77efd601eb8ddbd1c6386221 (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
require 'spec_helper'

describe GitTagPushService, services: true do
  include RepoHelpers

  let(:user) { create :user }
  let(:project) { create :project }
  let(:service) { GitTagPushService.new(project, user, oldrev: oldrev, newrev: newrev, ref: ref) }

  let(:oldrev) { Gitlab::Git::BLANK_SHA }
  let(:newrev) { "8a2a6eb295bb170b34c24c76c49ed0e9b2eaf34b" } # gitlab-test: git rev-parse refs/tags/v1.1.0
  let(:ref) { 'refs/tags/v1.1.0' }

  describe "Push tags" do
    subject do
      service.execute
      service
    end

    it 'flushes general cached data' do
      expect(project.repository).to receive(:expire_cache)

      subject
    end

    it 'flushes the tags cache' do
      expect(project.repository).to receive(:expire_tags_cache)

      subject
    end

    it 'flushes the tag count cache' do
      expect(project.repository).to receive(:expire_tag_count_cache)

      subject
    end
  end

  describe "Git Tag Push Data" do
    subject { @push_data }
    let(:tag) { project.repository.find_tag(tag_name) }
    let(:commit) { tag.dereferenced_target }

    context 'annotated tag' do
      let(:tag_name) { Gitlab::Git.ref_name(ref) }

      before do
        service.execute
        @push_data = service.push_data
      end

      it { is_expected.to include(object_kind: 'tag_push') }
      it { is_expected.to include(ref: ref) }
      it { is_expected.to include(before: oldrev) }
      it { is_expected.to include(after: newrev) }
      it { is_expected.to include(message: tag.message) }
      it { is_expected.to include(user_id: user.id) }
      it { is_expected.to include(user_name: user.name) }
      it { is_expected.to include(project_id: project.id) }

      context "with repository data" do
        subject { @push_data[:repository] }

        it { is_expected.to include(name: project.name) }
        it { is_expected.to include(url: project.url_to_repo) }
        it { is_expected.to include(description: project.description) }
        it { is_expected.to include(homepage: project.web_url) }
      end

      context "with commits" do
        subject { @push_data[:commits] }

        it { is_expected.to be_an(Array) }
        it 'has 1 element' do
          expect(subject.size).to eq(1)
        end

        context "the commit" do
          subject { @push_data[:commits].first }

          it { is_expected.to include(id: commit.id) }
          it { is_expected.to include(message: commit.safe_message) }
          it { is_expected.to include(timestamp: commit.date.xmlschema) }
          it do
            is_expected.to include(
              url: [
               Gitlab.config.gitlab.url,
               project.namespace.to_param,
               project.to_param,
               'commit',
               commit.id
              ].join('/')
            )
          end

          context "with a author" do
            subject { @push_data[:commits].first[:author] }

            it { is_expected.to include(name: commit.author_name) }
            it { is_expected.to include(email: commit.author_email) }
          end
        end
      end
    end

    context 'lightweight tag' do
      let(:tag_name) { 'light-tag' }
      let(:newrev) { '5937ac0a7beb003549fc5fd26fc247adbce4a52e' }
      let(:ref) { "refs/tags/light-tag" }

      before do
        # Create the lightweight tag
        project.repository.raw_repository.rugged.tags.create(tag_name, newrev)

        # Clear tag list cache
        project.repository.expire_tags_cache

        service.execute
        @push_data = service.push_data
      end

      it { is_expected.to include(object_kind: 'tag_push') }
      it { is_expected.to include(ref: ref) }
      it { is_expected.to include(before: oldrev) }
      it { is_expected.to include(after: newrev) }
      it { is_expected.to include(message: tag.message) }
      it { is_expected.to include(user_id: user.id) }
      it { is_expected.to include(user_name: user.name) }
      it { is_expected.to include(project_id: project.id) }

      context "with repository data" do
        subject { @push_data[:repository] }

        it { is_expected.to include(name: project.name) }
        it { is_expected.to include(url: project.url_to_repo) }
        it { is_expected.to include(description: project.description) }
        it { is_expected.to include(homepage: project.web_url) }
      end

      context "with commits" do
        subject { @push_data[:commits] }

        it { is_expected.to be_an(Array) }
        it 'has 1 element' do
          expect(subject.size).to eq(1)
        end

        context "the commit" do
          subject { @push_data[:commits].first }

          it { is_expected.to include(id: commit.id) }
          it { is_expected.to include(message: commit.safe_message) }
          it { is_expected.to include(timestamp: commit.date.xmlschema) }
          it do
            is_expected.to include(
              url: [
               Gitlab.config.gitlab.url,
               project.namespace.to_param,
               project.to_param,
               'commit',
               commit.id
              ].join('/')
            )
          end

          context "with a author" do
            subject { @push_data[:commits].first[:author] }

            it { is_expected.to include(name: commit.author_name) }
            it { is_expected.to include(email: commit.author_email) }
          end
        end
      end
    end
  end

  describe "Webhooks" do
    context "execute webhooks" do
      let(:service) { GitTagPushService.new(project, user, oldrev: 'oldrev', newrev: 'newrev', ref: 'refs/tags/v1.0.0') }

      it "when pushing tags" do
        expect(project).to receive(:execute_hooks)
        service.execute
      end
    end
  end
end