Commit e2542994 authored by Bob Van Landuyt's avatar Bob Van Landuyt

Merge branch '26019-evidence-collection-serializer' into 'master'

Evidence collection - add serializers, entities and JSON schemas

See merge request gitlab-org/gitlab!17335
parents 9ebf5671 8dfd9eee
# frozen_string_literal: true
module Evidences
class AuthorEntity < Grape::Entity
expose :id
expose :name
expose :email
end
end
# frozen_string_literal: true
module Evidences
class IssueEntity < Grape::Entity
expose :id
expose :title
expose :description
expose :author, using: AuthorEntity
expose :state
expose :iid
expose :confidential
expose :created_at
expose :due_date
end
end
# frozen_string_literal: true
module Evidences
class MilestoneEntity < Grape::Entity
expose :id
expose :title
expose :description
expose :state
expose :iid
expose :created_at
expose :due_date
expose :issues, using: IssueEntity
end
end
# frozen_string_literal: true
module Evidences
class ProjectEntity < Grape::Entity
expose :id
expose :name
expose :description
expose :created_at
end
end
# frozen_string_literal: true
module Evidences
class ReleaseEntity < Grape::Entity
expose :id
expose :tag, as: :tag_name
expose :name
expose :description
expose :created_at
expose :project, using: ProjectEntity
expose :milestones, using: MilestoneEntity
end
end
# frozen_string_literal: true
module Evidences
class ReleaseSerializer < BaseSerializer
entity ReleaseEntity
end
end
{
"type": "object",
"required": [
"id",
"name",
"email"
],
"properties": {
"id": { "type": "integer" },
"name": { "type": "string" },
"email": { "type": "string" }
},
"additionalProperties": false
}
{
"type": "object",
"required": [
"id",
"title",
"description",
"author",
"state",
"iid",
"confidential",
"created_at",
"due_date"
],
"properties": {
"id": { "type": "integer" },
"title": { "type": "string" },
"description": { "type": "string" },
"author": { "$ref": "author.json" },
"state": { "type": "string" },
"iid": { "type": "integer" },
"confidential": { "type": "boolean" },
"created_at": { "type": "date" },
"due_date": { "type": "date" }
},
"additionalProperties": false
}
{
"type": "object",
"required": [
"id",
"title",
"description",
"state",
"iid",
"created_at",
"due_date",
"issues"
],
"properties": {
"id": { "type": "integer" },
"title": { "type": "string" },
"description": { "type": "string" },
"state": { "type": "string" },
"iid": { "type": "integer" },
"created_at": { "type": "date" },
"due_date": { "type": "date" },
"issues": {
"type": "array",
"items": { "$ref": "issue.json" }
}
},
"additionalProperties": false
}
{
"type": "object",
"required": [
"id",
"name",
"description",
"created_at"
],
"properties": {
"id": { "type": "integer" },
"name": { "type": "string" },
"description": { "type": "string" },
"created_at": { "type": "date" }
},
"additionalProperties": false
}
{
"type": "object",
"required": [
"id",
"tag",
"name",
"description",
"created_at",
"project",
"milestones"
],
"properties": {
"id": { "type": "integer" },
"tag": { "type": "string" },
"name": { "type": "string" },
"description": { "type": "string" },
"created_at": { "type": "date" },
"project": { "$ref": "project.json" },
"milestones": {
"type": "array",
"items": { "$ref": "milestone.json" }
}
},
"additionalProperties": false
}
# frozen_string_literal: true
require 'spec_helper'
describe Evidences::AuthorEntity do
let(:entity) { described_class.new(build(:author)) }
subject { entity.as_json }
it 'exposes the expected fields' do
expect(subject.keys).to contain_exactly(:id, :name, :email)
end
end
# frozen_string_literal: true
require 'spec_helper'
describe Evidences::IssueEntity do
let(:entity) { described_class.new(build(:issue)) }
subject { entity.as_json }
it 'exposes the expected fields' do
expect(subject.keys).to contain_exactly(:id, :title, :description, :author, :state, :iid, :confidential, :created_at, :due_date)
end
end
# frozen_string_literal: true
require 'spec_helper'
describe Evidences::MilestoneEntity do
let(:milestone) { build(:milestone) }
let(:entity) { described_class.new(milestone) }
subject { entity.as_json }
it 'exposes the expected fields' do
expect(subject.keys).to contain_exactly(:id, :title, :description, :state, :iid, :created_at, :due_date, :issues)
end
context 'when there issues linked to this milestone' do
let(:issue_1) { build(:issue) }
let(:issue_2) { build(:issue) }
let(:milestone) { build(:milestone, issues: [issue_1, issue_2]) }
it 'exposes these issues' do
expect(subject[:issues]).to contain_exactly(
Evidences::IssueEntity.new(issue_1).as_json,
Evidences::IssueEntity.new(issue_2).as_json
)
end
end
context 'when the release has no milestone' do
let(:milestone) { build(:milestone, issues: []) }
it 'exposes an empty array for milestones' do
expect(subject[:issues]).to be_empty
end
end
end
# frozen_string_literal: true
require 'spec_helper'
describe Evidences::ProjectEntity do
let(:entity) { described_class.new(build(:project)) }
subject { entity.as_json }
it 'exposes the expected fields' do
expect(subject.keys).to contain_exactly(:id, :name, :description, :created_at)
end
end
# frozen_string_literal: true
require 'spec_helper'
describe Evidences::ReleaseEntity do
let(:release) { build(:release) }
let(:entity) { described_class.new(release) }
subject { entity.as_json }
it 'exposes the expected fields' do
expect(subject.keys).to contain_exactly(:id, :tag_name, :name, :description, :created_at, :project, :milestones)
end
context 'when the release has milestones' do
let(:project) { create(:project) }
let(:milestone_1) { build(:milestone, project: project) }
let(:milestone_2) { build(:milestone, project: project) }
let(:release) { build(:release, project: project, milestones: [milestone_1, milestone_2]) }
it 'exposes these milestones' do
expect(subject[:milestones]).to contain_exactly(
Evidences::MilestoneEntity.new(milestone_1).as_json,
Evidences::MilestoneEntity.new(milestone_2).as_json
)
end
end
context 'when the release has no milestone' do
let(:release) { build(:release, milestones: []) }
it 'exposes an empty array for milestones' do
expect(subject[:milestones]).to be_empty
end
end
end
# frozen_string_literal: true
require 'spec_helper'
describe Evidences::ReleaseSerializer do
it 'represents an Evidence::ReleaseEntity entity' do
expect(described_class.entity_class).to eq(Evidences::ReleaseEntity)
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