Commit 289cc8a8 authored by Vitali Tatarintev's avatar Vitali Tatarintev

Merge branch 'lm-be-dag-graphql' into 'master'

Adds BE graphql for DAG

See merge request gitlab-org/gitlab!36900
parents 160570ab 4451c5a0
# frozen_string_literal: true
module Resolvers
module Ci
class PipelineStagesResolver < BaseResolver
include LooksAhead
alias_method :pipeline, :object
def resolve_with_lookahead
apply_lookahead(pipeline.stages)
end
def preloads
{
statuses: [:needs]
}
end
end
end
end
# frozen_string_literal: true
module Types
module Ci
# rubocop: disable Graphql/AuthorizeTypes
class GroupType < BaseObject
graphql_name 'CiGroup'
field :name, GraphQL::STRING_TYPE, null: true,
description: 'Name of the job group'
field :size, GraphQL::INT_TYPE, null: true,
description: 'Size of the group'
field :jobs, Ci::JobType.connection_type, null: true,
description: 'Jobs in group'
end
end
end
# frozen_string_literal: true
module Types
module Ci
# rubocop: disable Graphql/AuthorizeTypes
class JobType < BaseObject
graphql_name 'CiJob'
field :name, GraphQL::STRING_TYPE, null: true,
description: 'Name of the job'
field :needs, JobType.connection_type, null: true,
description: 'Builds that must complete before the jobs run'
end
end
end
......@@ -37,6 +37,10 @@ module Types
description: "Timestamp of the pipeline's completion"
field :committed_at, Types::TimeType, null: true,
description: "Timestamp of the pipeline's commit"
field :stages, Types::Ci::StageType.connection_type, null: true,
description: 'Stages of the pipeline',
extras: [:lookahead],
resolver: Resolvers::Ci::PipelineStagesResolver
# TODO: Add triggering user as a type
end
......
# frozen_string_literal: true
module Types
module Ci
# rubocop: disable Graphql/AuthorizeTypes
class StageType < BaseObject
graphql_name 'CiStage'
field :name, GraphQL::STRING_TYPE, null: true,
description: 'Name of the stage'
field :groups, Ci::GroupType.connection_type, null: true,
description: 'Group of jobs for the stage'
end
end
end
......@@ -1347,6 +1347,212 @@ type Branch {
name: String!
}
type CiGroup {
"""
Jobs in group
"""
jobs(
"""
Returns the elements in the list that come after the specified cursor.
"""
after: String
"""
Returns the elements in the list that come before the specified cursor.
"""
before: String
"""
Returns the first _n_ elements from the list.
"""
first: Int
"""
Returns the last _n_ elements from the list.
"""
last: Int
): CiJobConnection
"""
Name of the job group
"""
name: String
"""
Size of the group
"""
size: Int
}
"""
The connection type for CiGroup.
"""
type CiGroupConnection {
"""
A list of edges.
"""
edges: [CiGroupEdge]
"""
A list of nodes.
"""
nodes: [CiGroup]
"""
Information to aid in pagination.
"""
pageInfo: PageInfo!
}
"""
An edge in a connection.
"""
type CiGroupEdge {
"""
A cursor for use in pagination.
"""
cursor: String!
"""
The item at the end of the edge.
"""
node: CiGroup
}
type CiJob {
"""
Name of the job
"""
name: String
"""
Builds that must complete before the jobs run
"""
needs(
"""
Returns the elements in the list that come after the specified cursor.
"""
after: String
"""
Returns the elements in the list that come before the specified cursor.
"""
before: String
"""
Returns the first _n_ elements from the list.
"""
first: Int
"""
Returns the last _n_ elements from the list.
"""
last: Int
): CiJobConnection
}
"""
The connection type for CiJob.
"""
type CiJobConnection {
"""
A list of edges.
"""
edges: [CiJobEdge]
"""
A list of nodes.
"""
nodes: [CiJob]
"""
Information to aid in pagination.
"""
pageInfo: PageInfo!
}
"""
An edge in a connection.
"""
type CiJobEdge {
"""
A cursor for use in pagination.
"""
cursor: String!
"""
The item at the end of the edge.
"""
node: CiJob
}
type CiStage {
"""
Group of jobs for the stage
"""
groups(
"""
Returns the elements in the list that come after the specified cursor.
"""
after: String
"""
Returns the elements in the list that come before the specified cursor.
"""
before: String
"""
Returns the first _n_ elements from the list.
"""
first: Int
"""
Returns the last _n_ elements from the list.
"""
last: Int
): CiGroupConnection
"""
Name of the stage
"""
name: String
}
"""
The connection type for CiStage.
"""
type CiStageConnection {
"""
A list of edges.
"""
edges: [CiStageEdge]
"""
A list of nodes.
"""
nodes: [CiStage]
"""
Information to aid in pagination.
"""
pageInfo: PageInfo!
}
"""
An edge in a connection.
"""
type CiStageEdge {
"""
A cursor for use in pagination.
"""
cursor: String!
"""
The item at the end of the edge.
"""
node: CiStage
}
type Commit {
"""
Author of the commit
......@@ -9850,6 +10056,31 @@ type Pipeline {
"""
sha: String!
"""
Stages of the pipeline
"""
stages(
"""
Returns the elements in the list that come after the specified cursor.
"""
after: String
"""
Returns the elements in the list that come before the specified cursor.
"""
before: String
"""
Returns the first _n_ elements from the list.
"""
first: Int
"""
Returns the last _n_ elements from the list.
"""
last: Int
): CiStageConnection
"""
Timestamp when the pipeline was started
"""
......
......@@ -231,6 +231,25 @@ Autogenerated return type of BoardListUpdateLimitMetrics
| `commit` | Commit | Commit for the branch |
| `name` | String! | Name of the branch |
## CiGroup
| Name | Type | Description |
| --- | ---- | ---------- |
| `name` | String | Name of the job group |
| `size` | Int | Size of the group |
## CiJob
| Name | Type | Description |
| --- | ---- | ---------- |
| `name` | String | Name of the job |
## CiStage
| Name | Type | Description |
| --- | ---- | ---------- |
| `name` | String | Name of the stage |
## Commit
| Name | Type | Description |
......
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Types::Ci::GroupType do
specify { expect(described_class.graphql_name).to eq('CiGroup') }
it 'exposes the expected fields' do
expected_fields = %i[
name
size
jobs
]
expect(described_class).to have_graphql_fields(*expected_fields)
end
end
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Types::Ci::JobType do
specify { expect(described_class.graphql_name).to eq('CiJob') }
it 'exposes the expected fields' do
expected_fields = %i[
name
needs
]
expect(described_class).to have_graphql_fields(*expected_fields)
end
end
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Types::Ci::StageType do
specify { expect(described_class.graphql_name).to eq('CiStage') }
it 'exposes the expected fields' do
expected_fields = %i[
name
groups
]
expect(described_class).to have_graphql_fields(*expected_fields)
end
end
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe 'Query.project.pipeline.stages.groups' do
include GraphqlHelpers
let(:project) { create(:project, :repository, :public) }
let(:user) { create(:user) }
let(:pipeline) { create(:ci_pipeline, project: project, user: user) }
let(:group_graphql_data) { graphql_data.dig('project', 'pipeline', 'stages', 'nodes', 0, 'groups', 'nodes') }
let(:params) { {} }
let(:fields) do
<<~QUERY
nodes {
#{all_graphql_fields_for('CiGroup')}
}
QUERY
end
let(:query) do
%(
query {
project(fullPath: "#{project.full_path}") {
pipeline(iid: "#{pipeline.iid}") {
stages {
nodes {
groups {
#{fields}
}
}
}
}
}
}
)
end
before do
create(:commit_status, pipeline: pipeline, name: 'rspec 0 2')
create(:commit_status, pipeline: pipeline, name: 'rspec 0 1')
create(:commit_status, pipeline: pipeline, name: 'spinach 0 1')
post_graphql(query, current_user: user)
end
it_behaves_like 'a working graphql query'
it 'returns a array of jobs belonging to a pipeline' do
expect(group_graphql_data.map { |g| g.slice('name', 'size') }).to eq([
{ 'name' => 'rspec', 'size' => 2 },
{ 'name' => 'spinach', 'size' => 1 }
])
end
end
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe 'Query.project.pipeline.stages.groups.jobs' do
include GraphqlHelpers
let(:project) { create(:project, :repository, :public) }
let(:user) { create(:user) }
let(:pipeline) do
pipeline = create(:ci_pipeline, project: project, user: user)
stage = create(:ci_stage_entity, pipeline: pipeline, name: 'first')
create(:commit_status, stage_id: stage.id, pipeline: pipeline, name: 'my test job')
pipeline
end
def first(field)
[field.pluralize, 'nodes', 0]
end
let(:jobs_graphql_data) { graphql_data.dig(*%w[project pipeline], *first('stage'), *first('group'), 'jobs', 'nodes') }
let(:query) do
%(
query {
project(fullPath: "#{project.full_path}") {
pipeline(iid: "#{pipeline.iid}") {
stages {
nodes {
name
groups {
nodes {
name
jobs {
nodes {
name
}
}
}
}
}
}
}
}
}
)
end
it 'returns the jobs of a pipeline stage' do
post_graphql(query, current_user: user)
expect(jobs_graphql_data).to contain_exactly(a_hash_including('name' => 'my test job'))
end
context 'when fetching jobs from the pipeline' do
it 'avoids N+1 queries' do
control_count = ActiveRecord::QueryRecorder.new do
post_graphql(query, current_user: user)
end
build_stage = create(:ci_stage_entity, name: 'build', pipeline: pipeline)
test_stage = create(:ci_stage_entity, name: 'test', pipeline: pipeline)
create(:commit_status, pipeline: pipeline, stage_id: build_stage.id, name: 'docker 1 2')
create(:commit_status, pipeline: pipeline, stage_id: build_stage.id, name: 'docker 2 2')
create(:commit_status, pipeline: pipeline, stage_id: test_stage.id, name: 'rspec 1 2')
create(:commit_status, pipeline: pipeline, stage_id: test_stage.id, name: 'rspec 2 2')
expect do
post_graphql(query, current_user: user)
end.not_to exceed_query_limit(control_count)
expect(response).to have_gitlab_http_status(:ok)
build_stage = graphql_data.dig('project', 'pipeline', 'stages', 'nodes').find do |stage|
stage['name'] == 'build'
end
test_stage = graphql_data.dig('project', 'pipeline', 'stages', 'nodes').find do |stage|
stage['name'] == 'test'
end
docker_group = build_stage.dig('groups', 'nodes').first
rspec_group = test_stage.dig('groups', 'nodes').first
expect(docker_group['name']).to eq('docker')
expect(rspec_group['name']).to eq('rspec')
docker_jobs = docker_group.dig('jobs', 'nodes')
rspec_jobs = rspec_group.dig('jobs', 'nodes')
expect(docker_jobs).to eq([{ 'name' => 'docker 1 2' }, { 'name' => 'docker 2 2' }])
expect(rspec_jobs).to eq([{ 'name' => 'rspec 1 2' }, { 'name' => 'rspec 2 2' }])
end
end
end
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe 'Query.project.pipeline.stages' do
include GraphqlHelpers
let(:project) { create(:project, :repository, :public) }
let(:user) { create(:user) }
let(:pipeline) { create(:ci_pipeline, project: project, user: user) }
let(:stage_graphql_data) { graphql_data['project']['pipeline']['stages'] }
let(:params) { {} }
let(:fields) do
<<~QUERY
nodes {
#{all_graphql_fields_for('CiStage')}
}
QUERY
end
let(:query) do
%(
query {
project(fullPath: "#{project.full_path}") {
pipeline(iid: "#{pipeline.iid}") {
stages {
#{fields}
}
}
}
}
)
end
before do
create(:ci_stage_entity, pipeline: pipeline, name: 'deploy')
post_graphql(query, current_user: user)
end
it_behaves_like 'a working graphql query'
it 'returns the stage of a pipeline' do
expect(stage_graphql_data['nodes'].first['name']).to eq('deploy')
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