File: commits_spec.rb

package info (click to toggle)
ruby-gitlab 5.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,660 kB
  • sloc: ruby: 12,582; makefile: 7; sh: 4
file content (296 lines) | stat: -rw-r--r-- 11,535 bytes parent folder | download
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::Client do
  it { is_expected.to respond_to :repo_commits }
  it { is_expected.to respond_to :repo_commit }
  it { is_expected.to respond_to :repo_commit_diff }
  it { is_expected.to respond_to :repo_commit_comments }
  it { is_expected.to respond_to :repo_create_commit_comment }
  it { is_expected.to respond_to :repo_commit_status }
  it { is_expected.to respond_to :repo_update_commit_status }
  it { is_expected.to respond_to :repo_commit_merge_requests }

  describe '.commits' do
    before do
      stub_get('/projects/3/repository/commits', 'project_commits')
        .with(query: { ref: 'api' })
      @commits = Gitlab.commits(3, ref: 'api')
    end

    it 'gets the correct resource' do
      expect(a_get('/projects/3/repository/commits')
        .with(query: { ref: 'api' })).to have_been_made
    end

    it 'returns a paginated response of repository commits' do
      expect(@commits).to be_a Gitlab::PaginatedResponse
      expect(@commits.first.id).to eq('f7dd067490fe57505f7226c3b54d3127d2f7fd46')
    end
  end

  describe '.commit' do
    before do
      stub_get('/projects/3/repository/commits/6104942438c14ec7bd21c6cd5bd995272b3faff6', 'project_commit')
      @commit = Gitlab.commit(3, '6104942438c14ec7bd21c6cd5bd995272b3faff6')
    end

    it 'gets the correct resource' do
      expect(a_get('/projects/3/repository/commits/6104942438c14ec7bd21c6cd5bd995272b3faff6'))
        .to have_been_made
    end

    it 'returns a repository commit' do
      expect(@commit.id).to eq('6104942438c14ec7bd21c6cd5bd995272b3faff6')
    end
  end

  describe '.commit_refs' do
    before do
      stub_get('/projects/3/repository/commits/0b4cd14ccc6a5c392526df719d29baf4315a4bbb/refs', 'project_commit_refs')
      @refs = Gitlab.commit_refs(3, '0b4cd14ccc6a5c392526df719d29baf4315a4bbb')
    end

    it 'gets the correct resource' do
      expect(a_get('/projects/3/repository/commits/0b4cd14ccc6a5c392526df719d29baf4315a4bbb/refs'))
        .to have_been_made
    end

    it 'returns an Array of refs' do
      expect(@refs.map(&:to_h))
        .to include('type' => 'branch', 'name' => '12-1-stable')
        .and include('type' => 'tag', 'name' => 'v12.1.0')
    end
  end

  describe '.cherry_pick_commit' do
    context 'when success' do
      before do
        stub_post('/projects/3/repository/commits/6104942438c14ec7bd21c6cd5bd995272b3faff6/cherry_pick', 'project_commit').with(body: { branch: 'master' })
        @cherry_pick_commit = Gitlab.cherry_pick_commit(3, '6104942438c14ec7bd21c6cd5bd995272b3faff6', 'master')
      end

      it 'gets the correct resource' do
        expect(a_post('/projects/3/repository/commits/6104942438c14ec7bd21c6cd5bd995272b3faff6/cherry_pick')
          .with(body: { branch: 'master' }))
          .to have_been_made
      end

      it 'returns the correct response' do
        expect(@cherry_pick_commit).to be_a Gitlab::ObjectifiedHash
        expect(@cherry_pick_commit.id).to eq('6104942438c14ec7bd21c6cd5bd995272b3faff6')
      end
    end

    context 'when failure' do
      it 'includes the error_code' do
        stub_post('/projects/3/repository/commits/6104942438c14ec7bd21c6cd5bd995272b3faff6/cherry_pick', 'cherry_pick_commit_failure', 400)

        expect { Gitlab.cherry_pick_commit(3, '6104942438c14ec7bd21c6cd5bd995272b3faff6', 'master') }.to raise_error(Gitlab::Error::BadRequest) do |ex|
          expect(ex.error_code).to eq('conflict')
        end
      end
    end

    context 'with additional options' do
      it 'passes additional options' do
        stub_post('/projects/3/repository/commits/6104942438c14ec7bd21c6cd5bd995272b3faff6/cherry_pick', 'project_commit')
          .with(body: { branch: 'master', dry_run: true })

        Gitlab.cherry_pick_commit(3, '6104942438c14ec7bd21c6cd5bd995272b3faff6', 'master', dry_run: true)

        expect(a_post('/projects/3/repository/commits/6104942438c14ec7bd21c6cd5bd995272b3faff6/cherry_pick')
          .with(body: { branch: 'master', dry_run: true }))
          .to have_been_made
      end
    end
  end

  describe '.revert_commit' do
    context 'when success' do
      before do
        stub_post('/projects/3/repository/commits/6104942438c14ec7bd21c6cd5bd995272b3faff6/revert', 'project_commit').with(body: { branch: 'master' })
        @revert_commit = Gitlab.revert_commit(3, '6104942438c14ec7bd21c6cd5bd995272b3faff6', 'master')
      end

      it 'gets the correct resource' do
        expect(a_post('/projects/3/repository/commits/6104942438c14ec7bd21c6cd5bd995272b3faff6/revert')
          .with(body: { branch: 'master' }))
          .to have_been_made
      end

      it 'returns the correct response' do
        expect(@revert_commit).to be_a Gitlab::ObjectifiedHash
        expect(@revert_commit.id).to eq('6104942438c14ec7bd21c6cd5bd995272b3faff6')
      end
    end

    context 'when failure' do
      it 'includes the error_code' do
        stub_post('/projects/3/repository/commits/6104942438c14ec7bd21c6cd5bd995272b3faff6/revert', 'revert_commit_failure', 400)

        expect { Gitlab.revert_commit(3, '6104942438c14ec7bd21c6cd5bd995272b3faff6', 'master') }.to raise_error(Gitlab::Error::BadRequest) do |ex|
          expect(ex.error_code).to eq('empty')
        end
      end
    end

    context 'with additional options' do
      it 'passes additional options' do
        stub_post('/projects/3/repository/commits/6104942438c14ec7bd21c6cd5bd995272b3faff6/revert', 'project_commit')
          .with(body: { branch: 'master', dry_run: true })

        Gitlab.revert_commit(3, '6104942438c14ec7bd21c6cd5bd995272b3faff6', 'master', dry_run: true)

        expect(a_post('/projects/3/repository/commits/6104942438c14ec7bd21c6cd5bd995272b3faff6/revert')
          .with(body: { branch: 'master', dry_run: true }))
          .to have_been_made
      end
    end
  end

  describe '.commit_diff' do
    before do
      stub_get('/projects/3/repository/commits/6104942438c14ec7bd21c6cd5bd995272b3faff6/diff', 'project_commit_diff')
      @diff = Gitlab.commit_diff(3, '6104942438c14ec7bd21c6cd5bd995272b3faff6')
    end

    it 'gets the correct resource' do
      expect(a_get('/projects/3/repository/commits/6104942438c14ec7bd21c6cd5bd995272b3faff6/diff'))
        .to have_been_made
    end

    it 'returns a diff of a commit' do
      expect(@diff.new_path).to eq('doc/update/5.4-to-6.0.md')
    end
  end

  describe '.commit_comments' do
    before do
      stub_get('/projects/3/repository/commits/6104942438c14ec7bd21c6cd5bd995272b3faff6/comments', 'project_commit_comments')
      @commit_comments = Gitlab.commit_comments(3, '6104942438c14ec7bd21c6cd5bd995272b3faff6')
    end

    it 'gets the correct resource' do
      expect(a_get('/projects/3/repository/commits/6104942438c14ec7bd21c6cd5bd995272b3faff6/comments'))
        .to have_been_made
    end

    it "returns commit's comments" do
      expect(@commit_comments).to be_a Gitlab::PaginatedResponse
      expect(@commit_comments.length).to eq(2)
      expect(@commit_comments[0].note).to eq('this is the 1st comment on commit 6104942438c14ec7bd21c6cd5bd995272b3faff6')
      expect(@commit_comments[0].author.id).to eq(11)
      expect(@commit_comments[1].note).to eq('another discussion point on commit 6104942438c14ec7bd21c6cd5bd995272b3faff6')
      expect(@commit_comments[1].author.id).to eq(12)
    end
  end

  describe '.create_commit_comment' do
    before do
      stub_post('/projects/3/repository/commits/6104942438c14ec7bd21c6cd5bd995272b3faff6/comments', 'project_commit_comment')
      @merge_request = Gitlab.create_commit_comment(3, '6104942438c14ec7bd21c6cd5bd995272b3faff6', 'Nice code!')
    end

    it 'returns information about the newly created comment' do
      expect(@merge_request.note).to eq('Nice code!')
      expect(@merge_request.author.id).to eq(1)
    end
  end

  describe '.commit_status' do
    before do
      stub_get('/projects/6/repository/commits/7d938cb8ac15788d71f4b67c035515a160ea76d8/statuses', 'project_commit_status')
        .with(query: { all: 'true' })
      @statuses = Gitlab.commit_status(6, '7d938cb8ac15788d71f4b67c035515a160ea76d8', all: true)
    end

    it 'gets the correct resource' do
      expect(a_get('/projects/6/repository/commits/7d938cb8ac15788d71f4b67c035515a160ea76d8/statuses')
        .with(query: { all: true })).to have_been_made
    end

    it 'gets statuses of a commit' do
      expect(@statuses).to be_a Gitlab::PaginatedResponse
      expect(@statuses.first.sha).to eq('7d938cb8ac15788d71f4b67c035515a160ea76d8')
      expect(@statuses.first.ref).to eq('decreased-spec')
      expect(@statuses.first.status).to eq('failed')
      expect(@statuses.last.sha).to eq('7d938cb8ac15788d71f4b67c035515a160ea76d8')
      expect(@statuses.last.status).to eq('success')
    end
  end

  describe '.update_commit_status' do
    before do
      stub_post('/projects/6/statuses/7d938cb8ac15788d71f4b67c035515a160ea76d8', 'project_update_commit_status')
        .with(body: { name: 'test', ref: 'decreased-spec', state: 'failed' })
      @status = Gitlab.update_commit_status(6, '7d938cb8ac15788d71f4b67c035515a160ea76d8', 'failed', name: 'test', ref: 'decreased-spec')
    end

    it 'gets the correct resource' do
      expect(a_post('/projects/6/statuses/7d938cb8ac15788d71f4b67c035515a160ea76d8')
        .with(body: { name: 'test', ref: 'decreased-spec', state: 'failed' })).to have_been_made
    end

    it 'returns information about the newly created status' do
      expect(@status).to be_a Gitlab::ObjectifiedHash
      expect(@status.id).to eq(498)
      expect(@status.sha).to eq('7d938cb8ac15788d71f4b67c035515a160ea76d8')
      expect(@status.status).to eq('failed')
      expect(@status.ref).to eq('decreased-spec')
    end
  end

  describe '.create_commit' do
    let(:actions) do
      [
        {
          action: 'create',
          file_path: 'foo/bar',
          content: 'some content'
        }
      ]
    end

    let(:query) do
      {
        branch: 'dev',
        commit_message: 'refactors everything',
        actions: actions,
        author_email: 'joe@sample.org',
        author_name: 'Joe Sample'
      }
    end

    before do
      stub_post('/projects/6/repository/commits', 'project_commit_create').with(body: query)
      @commit = Gitlab.create_commit(6, 'dev', 'refactors everything', actions, author_email: 'joe@sample.org', author_name: 'Joe Sample')
    end

    it 'returns id of a created commit' do
      expect(@commit.id).to eq('ed899a2f4b50b4370feeea94676502b42383c746')
    end
  end

  describe '.repo_commit_merge_requests' do
    before do
      stub_get('/projects/3/repository/commits/6104942438c14ec7bd21c6cd5bd995272b3faff6/merge_requests', 'project_commit_merge_requests')
      @commit_merge_requests = Gitlab.commit_merge_requests(3, '6104942438c14ec7bd21c6cd5bd995272b3faff6')
    end

    it 'gets the correct resource' do
      expect(a_get('/projects/3/repository/commits/6104942438c14ec7bd21c6cd5bd995272b3faff6/merge_requests'))
        .to have_been_made
    end

    it "returns commit's associated merge_requests" do
      expect(@commit_merge_requests).to be_a Gitlab::PaginatedResponse
      expect(@commit_merge_requests.length).to eq(2)
      expect(@commit_merge_requests[0].iid).to eq(1)
      expect(@commit_merge_requests[0].author.id).to eq(1)
      expect(@commit_merge_requests[1].iid).to eq(2)
      expect(@commit_merge_requests[1].author.id).to eq(2)
    end
  end
end