Commit 571432b1 authored by Peter Leitzen's avatar Peter Leitzen Committed by Bob Van Landuyt

Serialize incidents for the Status Page MVC

This commit enables serializing of GitLab's issues as incidents
for the upcoming Status Page MVC.

Future MRs will utilize this functionality.
parent 4ecc362c
# frozen_string_literal: true
module StatusPage
class IncidentCommentEntity < Grape::Entity
expose :note_html, as: :note
expose :created_at
end
end
# frozen_string_literal: true
module StatusPage
class IncidentEntity < Grape::Entity
expose :iid, as: :id
expose :state
expose :title_html, as: :title
expose :description_html, as: :description
expose :updated_at
expose :created_at
expose :user_notes, as: :comments, using: IncidentCommentEntity
private
def user_notes
Array(options[:user_notes])
end
end
end
# frozen_string_literal: true
module StatusPage
class IncidentSerializer < BaseSerializer
entity IncidentEntity
def represent_list(resource)
represent(resource, except: [:comments])
end
def represent_details(resource, user_notes)
represent(resource, user_notes: user_notes)
end
private :represent
end
end
{
"type": "object",
"required": [
"id",
"state",
"title",
"description",
"updated_at",
"created_at"
],
"properties": {
"id": { "type": "integer" },
"state": {
"type": "string",
"enum": ["opened", "closed"]
},
"title": { "type": "string" },
"description": { "type": "string" },
"updated_at": { "type": "date" },
"created_at": { "type": "date" }
}
}
{
"type": "object",
"allOf": [
{ "$ref": "basic_incident.json" },
{
"required": ["comments"],
"properties": {
"type": "object",
"items": {
"required": ["note", "created_at"],
"properties": {
"note": { "type": "string" },
"created_at": { "type": "date" }
},
"additionalProperties": false
}
}
}
]
}
{
"type": "array",
"items": { "$ref": "basic_incident.json" },
"additionalProperties": false
}
# frozen_string_literal: true
require 'spec_helper'
describe StatusPage::IncidentCommentEntity do
let_it_be(:note) { create(:note, note: ':ok:') }
let(:json) { subject.as_json }
subject { described_class.new(note) }
it 'exposes JSON fields' do
expect(json).to eq(
note: note.note_html,
created_at: note.created_at
)
end
end
# frozen_string_literal: true
require 'spec_helper'
describe StatusPage::IncidentEntity do
let_it_be(:issue) do
create(:issue, title: ':ok:', description: ':tada:')
end
let(:json) { subject.as_json }
subject { described_class.new(issue) }
it 'exposes JSON fields' do
expect(json).to eq(
id: issue.iid,
state: issue.state,
title: issue.title_html,
description: issue.description_html,
updated_at: issue.updated_at,
created_at: issue.created_at,
comments: []
)
end
context 'with user notes' do
let(:user_notes) do
create_list(:note, 1, noteable: issue, project: issue.project)
end
subject { described_class.new(issue, user_notes: user_notes) }
it 'exposes comments' do
expect(json).to include(:comments)
expect(json[:comments].size).to eq(1)
end
end
end
# frozen_string_literal: true
require 'spec_helper'
describe StatusPage::IncidentSerializer do
let_it_be(:issue) { create(:issue) }
shared_examples 'valid JSON schema' do |schema:|
it 'matches JSON schema' do
expect(json_entity).to match_schema(schema, dir: 'ee')
end
end
describe '.represent_list' do
let(:resource) { [issue] }
let(:json_entity) do
subject.represent_list(resource).map(&:with_indifferent_access)
end
it_behaves_like 'valid JSON schema', schema: 'status_page/incident_list'
it 'returns a list with one entity' do
expect(json_entity.size).to eq(1)
end
it 'does not contain comments' do
expect(json_entity).to be_none(include(:comments))
end
end
describe '.represent_details' do
let(:resource) { issue }
let_it_be(:user_notes) do
create_list(:note, 1, noteable: issue, project: issue.project)
end
let(:json_entity) do
subject
.represent_details(resource, user_notes)
.with_indifferent_access
end
it_behaves_like 'valid JSON schema', schema: 'status_page/incident_details'
end
end
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