Commit da470249 authored by Sean McGivern's avatar Sean McGivern

Fix label serialisation in issue and note hooks

We were not calling hook_attrs on the labels correctly. Specs were
passing because the issues under test did not have any labels!
parent 7277c9b0
---
title: Fix label serialization in issue and note hooks
merge_request: 29850
author:
type: fixed
...@@ -44,7 +44,7 @@ module Gitlab ...@@ -44,7 +44,7 @@ module Gitlab
data[:commit] = build_data_for_commit(project, user, note) data[:commit] = build_data_for_commit(project, user, note)
elsif note.for_issue? elsif note.for_issue?
data[:issue] = note.noteable.hook_attrs data[:issue] = note.noteable.hook_attrs
data[:issue][:labels] = note.noteable.labels(&:hook_attrs) data[:issue][:labels] = note.noteable.labels.map(&:hook_attrs)
elsif note.for_merge_request? elsif note.for_merge_request?
data[:merge_request] = note.noteable.hook_attrs data[:merge_request] = note.noteable.hook_attrs
elsif note.for_snippet? elsif note.for_snippet?
......
...@@ -45,7 +45,7 @@ module Gitlab ...@@ -45,7 +45,7 @@ module Gitlab
human_time_estimate: issue.human_time_estimate, human_time_estimate: issue.human_time_estimate,
assignee_ids: issue.assignee_ids, assignee_ids: issue.assignee_ids,
assignee_id: issue.assignee_ids.first, # This key is deprecated assignee_id: issue.assignee_ids.first, # This key is deprecated
labels: issue.labels labels: issue.labels.map(&:hook_attrs)
} }
issue.attributes.with_indifferent_access.slice(*self.class.safe_hook_attributes) issue.attributes.with_indifferent_access.slice(*self.class.safe_hook_attributes)
......
...@@ -38,9 +38,11 @@ describe Gitlab::DataBuilder::Note do ...@@ -38,9 +38,11 @@ describe Gitlab::DataBuilder::Note do
end end
describe 'When asking for a note on issue' do describe 'When asking for a note on issue' do
let(:label) { create(:label, project: project) }
let(:issue) do let(:issue) do
create(:issue, created_at: fixed_time, updated_at: fixed_time, create(:labeled_issue, created_at: fixed_time, updated_at: fixed_time,
project: project) project: project, labels: [label])
end end
let(:note) do let(:note) do
...@@ -48,13 +50,16 @@ describe Gitlab::DataBuilder::Note do ...@@ -48,13 +50,16 @@ describe Gitlab::DataBuilder::Note do
end end
it 'returns the note and issue-specific data' do it 'returns the note and issue-specific data' do
without_timestamps = lambda { |label| label.except('created_at', 'updated_at') }
hook_attrs = issue.reload.hook_attrs
expect(data).to have_key(:issue) expect(data).to have_key(:issue)
expect(data[:issue].except('updated_at')) expect(data[:issue].except('updated_at', 'labels'))
.to eq(issue.reload.hook_attrs.except('updated_at')) .to eq(hook_attrs.except('updated_at', 'labels'))
expect(data[:issue]['updated_at']) expect(data[:issue]['updated_at'])
.to be >= issue.hook_attrs['updated_at'] .to be >= hook_attrs['updated_at']
expect(data[:issue]['labels']) expect(data[:issue]['labels'].map(&without_timestamps))
.to eq(issue.hook_attrs['labels']) .to eq(hook_attrs['labels'].map(&without_timestamps))
end end
context 'with confidential issue' do context 'with confidential issue' do
......
require 'spec_helper' require 'spec_helper'
describe Gitlab::HookData::IssueBuilder do describe Gitlab::HookData::IssueBuilder do
set(:issue) { create(:issue) } set(:label) { create(:label) }
set(:issue) { create(:labeled_issue, labels: [label], project: label.project) }
let(:builder) { described_class.new(issue) } let(:builder) { described_class.new(issue) }
describe '#build' do describe '#build' do
...@@ -39,6 +40,7 @@ describe Gitlab::HookData::IssueBuilder do ...@@ -39,6 +40,7 @@ describe Gitlab::HookData::IssueBuilder do
expect(data).to include(:human_time_estimate) expect(data).to include(:human_time_estimate)
expect(data).to include(:human_total_time_spent) expect(data).to include(:human_total_time_spent)
expect(data).to include(:assignee_ids) expect(data).to include(:assignee_ids)
expect(data).to include('labels' => [label.hook_attrs])
end end
context 'when the issue has an image in the description' do context 'when the issue has an image in the description' do
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment