From 131cc74f48d4a50f02f1c7d94e8b53b2d5eec539 Mon Sep 17 00:00:00 2001
From: David Barr <38654497+davebarrau@users.noreply.github.com>
Date: Mon, 6 Jun 2022 20:47:33 +1000
Subject: [PATCH] graphql: Add merge_request_event_type field to pipeline_type

Expose pipeline.merge_request_event_type to the GraphQL object
pipeline_type. Returns "DETACHED", "MERGED_RESULT", or
"MERGE_TRAIN".

Changelog: added
---
 .../pipeline_merge_request_event_type_enum.rb | 19 +++++++++++++++++++
 app/graphql/types/ci/pipeline_type.rb         |  3 +++
 doc/api/graphql/reference/index.md            | 11 +++++++++++
 .../pipeline_merge_request_event_type_enum.rb | 15 +++++++++++++++
 .../pipeline_merge_request_type_enum_spec.rb  | 11 +++++++++++
 ...line_merge_request_event_type_enum_spec.rb | 14 ++++++++++++++
 spec/graphql/types/ci/pipeline_type_spec.rb   |  2 +-
 7 files changed, 74 insertions(+), 1 deletion(-)
 create mode 100644 app/graphql/types/ci/pipeline_merge_request_event_type_enum.rb
 create mode 100644 ee/app/graphql/ee/types/ci/pipeline_merge_request_event_type_enum.rb
 create mode 100644 ee/spec/graphql/ee/types/ci/pipeline_merge_request_type_enum_spec.rb
 create mode 100644 spec/graphql/types/ci/pipeline_merge_request_event_type_enum_spec.rb

diff --git a/app/graphql/types/ci/pipeline_merge_request_event_type_enum.rb b/app/graphql/types/ci/pipeline_merge_request_event_type_enum.rb
new file mode 100644
index 0000000000000..a1236b8f2c194
--- /dev/null
+++ b/app/graphql/types/ci/pipeline_merge_request_event_type_enum.rb
@@ -0,0 +1,19 @@
+# frozen_string_literal: true
+
+module Types
+  module Ci
+    class PipelineMergeRequestEventTypeEnum < BaseEnum
+      graphql_name 'PipelineMergeRequestEventType'
+      description 'Event type of the pipeline associated with a merge request'
+
+      value 'MERGED_RESULT',
+            'Pipeline run on the changes from the source branch combined with the target branch.',
+            value: :merged_result
+      value 'DETACHED',
+            'Pipeline run on the changes in the merge request source branch.',
+            value: :detached
+    end
+  end
+end
+
+Types::Ci::PipelineMergeRequestEventTypeEnum.prepend_mod
diff --git a/app/graphql/types/ci/pipeline_type.rb b/app/graphql/types/ci/pipeline_type.rb
index 81afc7f0f4210..60418fec6c565 100644
--- a/app/graphql/types/ci/pipeline_type.rb
+++ b/app/graphql/types/ci/pipeline_type.rb
@@ -175,6 +175,9 @@ class PipelineType < BaseObject
       field :warning_messages, [Types::Ci::PipelineMessageType], null: true,
             description: 'Pipeline warning messages.'
 
+      field :merge_request_event_type, Types::Ci::PipelineMergeRequestEventTypeEnum, null: true,
+            description: "Event type of the pipeline associated with a merge request."
+
       def detailed_status
         object.detailed_status(current_user)
       end
diff --git a/doc/api/graphql/reference/index.md b/doc/api/graphql/reference/index.md
index ec2cd6ba2242a..ee4b5fd2abe5b 100644
--- a/doc/api/graphql/reference/index.md
+++ b/doc/api/graphql/reference/index.md
@@ -14449,6 +14449,7 @@ Represents a file or directory in the project repository that has been locked.
 | <a id="pipelineid"></a>`id` | [`ID!`](#id) | ID of the pipeline. |
 | <a id="pipelineiid"></a>`iid` | [`String!`](#string) | Internal ID of the pipeline. |
 | <a id="pipelinejobartifacts"></a>`jobArtifacts` | [`[CiJobArtifact!]`](#cijobartifact) | Job artifacts of the pipeline. |
+| <a id="pipelinemergerequesteventtype"></a>`mergeRequestEventType` | [`PipelineMergeRequestEventType`](#pipelinemergerequesteventtype) | Event type of the pipeline associated with a merge request. |
 | <a id="pipelinepath"></a>`path` | [`String`](#string) | Relative path to the pipeline's page. |
 | <a id="pipelineproject"></a>`project` | [`Project`](#project) | Project the pipeline belongs to. |
 | <a id="pipelinequeuedduration"></a>`queuedDuration` | [`Duration`](#duration) | How long the pipeline was queued before starting. |
@@ -19188,6 +19189,16 @@ Values for sorting package.
 | <a id="pipelineconfigsourceenumunknown_source"></a>`UNKNOWN_SOURCE` | Unknown source. |
 | <a id="pipelineconfigsourceenumwebide_source"></a>`WEBIDE_SOURCE` | Webide source. |
 
+### `PipelineMergeRequestEventType`
+
+Event type of the pipeline associated with a merge request.
+
+| Value | Description |
+| ----- | ----------- |
+| <a id="pipelinemergerequesteventtypedetached"></a>`DETACHED` | Pipeline run on the changes in the merge request source branch. |
+| <a id="pipelinemergerequesteventtypemerged_result"></a>`MERGED_RESULT` | Pipeline run on the changes from the source branch combined with the target branch. |
+| <a id="pipelinemergerequesteventtypemerge_train"></a>`MERGE_TRAIN` | Pipeline ran as part of a merge train. |
+
 ### `PipelineScopeEnum`
 
 | Value | Description |
diff --git a/ee/app/graphql/ee/types/ci/pipeline_merge_request_event_type_enum.rb b/ee/app/graphql/ee/types/ci/pipeline_merge_request_event_type_enum.rb
new file mode 100644
index 0000000000000..b06a45cbe8103
--- /dev/null
+++ b/ee/app/graphql/ee/types/ci/pipeline_merge_request_event_type_enum.rb
@@ -0,0 +1,15 @@
+# frozen_string_literal: true
+
+module EE
+  module Types
+    module Ci
+      module PipelineMergeRequestEventTypeEnum
+        extend ActiveSupport::Concern
+
+        prepended do
+          value 'MERGE_TRAIN', 'Pipeline ran as part of a merge train.', value: :merge_train
+        end
+      end
+    end
+  end
+end
diff --git a/ee/spec/graphql/ee/types/ci/pipeline_merge_request_type_enum_spec.rb b/ee/spec/graphql/ee/types/ci/pipeline_merge_request_type_enum_spec.rb
new file mode 100644
index 0000000000000..8d838daecbf5f
--- /dev/null
+++ b/ee/spec/graphql/ee/types/ci/pipeline_merge_request_type_enum_spec.rb
@@ -0,0 +1,11 @@
+# frozen_string_literal: true
+
+require 'spec_helper'
+
+RSpec.describe GitlabSchema.types['PipelineMergeRequestEventType'] do
+  it 'has specific values' do
+    expect(described_class.values).to match a_hash_including(
+      'MERGE_TRAIN' => have_attributes(value: :merge_train)
+    )
+  end
+end
diff --git a/spec/graphql/types/ci/pipeline_merge_request_event_type_enum_spec.rb b/spec/graphql/types/ci/pipeline_merge_request_event_type_enum_spec.rb
new file mode 100644
index 0000000000000..3a90e4f1fd9b1
--- /dev/null
+++ b/spec/graphql/types/ci/pipeline_merge_request_event_type_enum_spec.rb
@@ -0,0 +1,14 @@
+# frozen_string_literal: true
+
+require 'spec_helper'
+
+RSpec.describe GitlabSchema.types['PipelineMergeRequestEventType'] do
+  specify { expect(described_class.graphql_name).to eq('PipelineMergeRequestEventType') }
+
+  it 'has specific values' do
+    expect(described_class.values).to match a_hash_including(
+      'MERGED_RESULT' => have_attributes(value: :merged_result),
+      'DETACHED' => have_attributes(value: :detached)
+    )
+  end
+end
diff --git a/spec/graphql/types/ci/pipeline_type_spec.rb b/spec/graphql/types/ci/pipeline_type_spec.rb
index 94d1b42da37b4..9dee834d05f91 100644
--- a/spec/graphql/types/ci/pipeline_type_spec.rb
+++ b/spec/graphql/types/ci/pipeline_type_spec.rb
@@ -14,7 +14,7 @@
       coverage created_at updated_at started_at finished_at committed_at
       stages user retryable cancelable jobs source_job job job_artifacts downstream
       upstream path project active user_permissions warnings commit commit_path uses_needs
-      test_report_summary test_suite ref ref_path warning_messages
+      test_report_summary test_suite ref ref_path warning_messages merge_request_event_type
     ]
 
     if Gitlab.ee?
-- 
GitLab