2020-01-01 13:55:28 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
require 'spec_helper'
|
|
|
|
|
2022-07-16 23:28:13 +05:30
|
|
|
RSpec.describe Gitlab::DataBuilder::Issuable do
|
2020-04-08 14:13:33 +05:30
|
|
|
let_it_be(:user) { create(:user) }
|
2018-03-17 18:26:18 +05:30
|
|
|
|
|
|
|
# This shared example requires a `builder` and `user` variable
|
2022-06-21 17:19:12 +05:30
|
|
|
shared_examples 'issuable hook data' do |kind, hook_data_issuable_builder_class|
|
2018-03-17 18:26:18 +05:30
|
|
|
let(:data) { builder.build(user: user) }
|
|
|
|
|
|
|
|
include_examples 'project hook data' do
|
|
|
|
let(:project) { builder.issuable.project }
|
|
|
|
end
|
2020-05-24 23:13:21 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
include_examples 'deprecated repository hook data'
|
|
|
|
|
|
|
|
context "with a #{kind}" do
|
|
|
|
it 'contains issuable data' do
|
|
|
|
expect(data[:object_kind]).to eq(kind)
|
|
|
|
expect(data[:user]).to eq(user.hook_attrs)
|
|
|
|
expect(data[:project]).to eq(builder.issuable.project.hook_attrs)
|
2022-06-21 17:19:12 +05:30
|
|
|
expect(data[:object_attributes]).to eq(hook_data_issuable_builder_class.new(issuable).build)
|
2018-03-17 18:26:18 +05:30
|
|
|
expect(data[:changes]).to eq({})
|
|
|
|
expect(data[:repository]).to eq(builder.issuable.project.hook_attrs.slice(:name, :url, :description, :homepage))
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not contain certain keys' do
|
|
|
|
expect(data).not_to have_key(:assignees)
|
|
|
|
expect(data).not_to have_key(:assignee)
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'changes are given' do
|
|
|
|
let(:changes) do
|
|
|
|
{
|
|
|
|
cached_markdown_version: %w[foo bar],
|
|
|
|
description: ['A description', 'A cool description'],
|
|
|
|
description_html: %w[foo bar],
|
|
|
|
in_progress_merge_commit_sha: %w[foo bar],
|
|
|
|
lock_version: %w[foo bar],
|
|
|
|
merge_jid: %w[foo bar],
|
|
|
|
title: ['A title', 'Hello World'],
|
|
|
|
title_html: %w[foo bar],
|
|
|
|
labels: [
|
|
|
|
[{ id: 1, title: 'foo' }],
|
|
|
|
[{ id: 1, title: 'foo' }, { id: 2, title: 'bar' }]
|
|
|
|
],
|
2019-12-04 20:38:33 +05:30
|
|
|
total_time_spent: [1, 2],
|
|
|
|
assignees: [
|
|
|
|
[],
|
|
|
|
[{
|
|
|
|
name: "Foo Bar",
|
|
|
|
username: "foobar",
|
|
|
|
avatar_url: "http://www.example.com/my-avatar.jpg"
|
|
|
|
}]
|
|
|
|
]
|
2018-03-17 18:26:18 +05:30
|
|
|
}
|
|
|
|
end
|
2020-10-24 23:57:45 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
let(:data) { builder.build(user: user, changes: changes) }
|
|
|
|
|
|
|
|
it 'populates the :changes hash' do
|
|
|
|
expect(data[:changes]).to match(hash_including({
|
|
|
|
title: { previous: 'A title', current: 'Hello World' },
|
|
|
|
description: { previous: 'A description', current: 'A cool description' },
|
|
|
|
labels: {
|
|
|
|
previous: [{ id: 1, title: 'foo' }],
|
|
|
|
current: [{ id: 1, title: 'foo' }, { id: 2, title: 'bar' }]
|
|
|
|
},
|
|
|
|
total_time_spent: {
|
|
|
|
previous: 1,
|
|
|
|
current: 2
|
2019-12-04 20:38:33 +05:30
|
|
|
},
|
|
|
|
assignees: {
|
|
|
|
previous: [],
|
2022-10-11 01:57:18 +05:30
|
|
|
current: [{
|
2019-12-04 20:38:33 +05:30
|
|
|
name: "Foo Bar",
|
|
|
|
username: "foobar",
|
|
|
|
avatar_url: "http://www.example.com/my-avatar.jpg"
|
|
|
|
}]
|
2018-03-17 18:26:18 +05:30
|
|
|
}
|
|
|
|
}))
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not contain certain keys' do
|
|
|
|
expect(data[:changes]).not_to have_key('cached_markdown_version')
|
|
|
|
expect(data[:changes]).not_to have_key('description_html')
|
|
|
|
expect(data[:changes]).not_to have_key('lock_version')
|
|
|
|
expect(data[:changes]).not_to have_key('title_html')
|
|
|
|
expect(data[:changes]).not_to have_key('in_progress_merge_commit_sha')
|
|
|
|
expect(data[:changes]).not_to have_key('merge_jid')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#build' do
|
2022-06-21 17:19:12 +05:30
|
|
|
it_behaves_like 'issuable hook data', 'issue', Gitlab::HookData::IssueBuilder do
|
2018-03-17 18:26:18 +05:30
|
|
|
let(:issuable) { create(:issue, description: 'A description') }
|
|
|
|
let(:builder) { described_class.new(issuable) }
|
|
|
|
end
|
|
|
|
|
2022-06-21 17:19:12 +05:30
|
|
|
it_behaves_like 'issuable hook data', 'merge_request', Gitlab::HookData::MergeRequestBuilder do
|
2018-03-17 18:26:18 +05:30
|
|
|
let(:issuable) { create(:merge_request, description: 'A description') }
|
|
|
|
let(:builder) { described_class.new(issuable) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'issue is assigned' do
|
|
|
|
let(:issue) { create(:issue, assignees: [user]) }
|
|
|
|
let(:data) { described_class.new(issue).build(user: user) }
|
|
|
|
|
|
|
|
it 'returns correct hook data' do
|
|
|
|
expect(data[:object_attributes]['assignee_id']).to eq(user.id)
|
|
|
|
expect(data[:assignees].first).to eq(user.hook_attrs)
|
|
|
|
expect(data).not_to have_key(:assignee)
|
2022-08-27 11:52:29 +05:30
|
|
|
expect(data).not_to have_key(:reviewers)
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'merge_request is assigned' do
|
2019-07-31 22:56:46 +05:30
|
|
|
let(:merge_request) { create(:merge_request, assignees: [user]) }
|
2018-03-17 18:26:18 +05:30
|
|
|
let(:data) { described_class.new(merge_request).build(user: user) }
|
|
|
|
|
|
|
|
it 'returns correct hook data' do
|
|
|
|
expect(data[:object_attributes]['assignee_id']).to eq(user.id)
|
2019-07-31 22:56:46 +05:30
|
|
|
expect(data[:assignees].first).to eq(user.hook_attrs)
|
|
|
|
expect(data).not_to have_key(:assignee)
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
end
|
2022-08-27 11:52:29 +05:30
|
|
|
|
|
|
|
context 'merge_request is assigned reviewers' do
|
|
|
|
let(:merge_request) { create(:merge_request, reviewers: [user]) }
|
|
|
|
let(:data) { described_class.new(merge_request).build(user: user) }
|
|
|
|
|
|
|
|
it 'returns correct hook data' do
|
|
|
|
expect(data[:object_attributes]['reviewer_ids']).to match_array([user.id])
|
|
|
|
expect(data[:reviewers].first).to eq(user.hook_attrs)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when merge_request does not have reviewers and assignees' do
|
|
|
|
let(:merge_request) { create(:merge_request) }
|
|
|
|
let(:data) { described_class.new(merge_request).build(user: user) }
|
|
|
|
|
|
|
|
it 'returns correct hook data' do
|
|
|
|
expect(data).not_to have_key(:assignees)
|
|
|
|
expect(data).not_to have_key(:reviewers)
|
|
|
|
end
|
|
|
|
end
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
end
|