From b591dcbe55f14bca411c49d55411f9b1e63ea943 Mon Sep 17 00:00:00 2001
From: Phil Hughes <me@iamphill.com>
Date: Mon, 15 Feb 2021 16:49:08 +0000
Subject: [PATCH] Fixes GraphQL pipeline status notification with warnings

Fixes a bug where if a pipeline succeeds with warnings
it will always send notifications to the users browser.
---
 .../queries/get_state.query.graphql            |  1 +
 .../stores/mr_widget_store.js                  |  5 +++++
 app/graphql/types/ci/pipeline_type.rb          |  3 +++
 .../ph-ph-fixWidgetGraphqlPipelineWarnings.yml |  5 +++++
 .../graphql/reference/gitlab_schema.graphql    |  5 +++++
 doc/api/graphql/reference/gitlab_schema.json   | 18 ++++++++++++++++++
 doc/api/graphql/reference/index.md             |  1 +
 spec/graphql/types/ci/pipeline_type_spec.rb    |  2 +-
 8 files changed, 39 insertions(+), 1 deletion(-)
 create mode 100644 changelogs/unreleased/ph-ph-fixWidgetGraphqlPipelineWarnings.yml

diff --git a/app/assets/javascripts/vue_merge_request_widget/queries/get_state.query.graphql b/app/assets/javascripts/vue_merge_request_widget/queries/get_state.query.graphql
index b284bb23969..13ea07884b1 100644
--- a/app/assets/javascripts/vue_merge_request_widget/queries/get_state.query.graphql
+++ b/app/assets/javascripts/vue_merge_request_widget/queries/get_state.query.graphql
@@ -14,6 +14,7 @@ query getState($projectPath: ID!, $iid: String!) {
       pipelines(first: 1) {
         nodes {
           status
+          warnings
         }
       }
       shouldBeRebased
diff --git a/app/assets/javascripts/vue_merge_request_widget/stores/mr_widget_store.js b/app/assets/javascripts/vue_merge_request_widget/stores/mr_widget_store.js
index 78a17493d31..a0f14f558d2 100644
--- a/app/assets/javascripts/vue_merge_request_widget/stores/mr_widget_store.js
+++ b/app/assets/javascripts/vue_merge_request_widget/stores/mr_widget_store.js
@@ -172,6 +172,11 @@ export default class MergeRequestStore {
     this.canBeMerged = mergeRequest.mergeStatus === 'can_be_merged';
     this.canMerge = mergeRequest.userPermissions.canMerge;
     this.ciStatus = pipeline?.status.toLowerCase();
+
+    if (pipeline?.warnings && this.ciStatus === 'success') {
+      this.ciStatus = `${this.ciStatus}-with-warnings`;
+    }
+
     this.commitsCount = mergeRequest.commitCount || 10;
     this.branchMissing = !mergeRequest.sourceBranchExists || !mergeRequest.targetBranchExists;
     this.hasConflicts = mergeRequest.conflicts;
diff --git a/app/graphql/types/ci/pipeline_type.rb b/app/graphql/types/ci/pipeline_type.rb
index af7e0fa224f..2c386c9b564 100644
--- a/app/graphql/types/ci/pipeline_type.rb
+++ b/app/graphql/types/ci/pipeline_type.rb
@@ -27,6 +27,9 @@ module Types
       field :status, PipelineStatusEnum, null: false,
             description: "Status of the pipeline (#{::Ci::Pipeline.all_state_names.compact.join(', ').upcase})"
 
+      field :warnings, GraphQL::BOOLEAN_TYPE, null: false, method: :has_warnings?,
+            description: "Indicates if a pipeline has warnings."
+
       field :detailed_status, Types::Ci::DetailedStatusType, null: false,
             description: 'Detailed status of the pipeline.'
 
diff --git a/changelogs/unreleased/ph-ph-fixWidgetGraphqlPipelineWarnings.yml b/changelogs/unreleased/ph-ph-fixWidgetGraphqlPipelineWarnings.yml
new file mode 100644
index 00000000000..d0cb9490a9e
--- /dev/null
+++ b/changelogs/unreleased/ph-ph-fixWidgetGraphqlPipelineWarnings.yml
@@ -0,0 +1,5 @@
+---
+title: Added warnings field to the pipelines GraphQL type
+merge_request: 54089
+author:
+type: added
diff --git a/doc/api/graphql/reference/gitlab_schema.graphql b/doc/api/graphql/reference/gitlab_schema.graphql
index 8e8e91ec6e8..d93662969fc 100644
--- a/doc/api/graphql/reference/gitlab_schema.graphql
+++ b/doc/api/graphql/reference/gitlab_schema.graphql
@@ -18566,6 +18566,11 @@ type Pipeline {
   Permissions for the current user on the resource
   """
   userPermissions: PipelinePermissions!
+
+  """
+  Indicates if a pipeline has warnings.
+  """
+  warnings: Boolean!
 }
 
 type PipelineAnalytics {
diff --git a/doc/api/graphql/reference/gitlab_schema.json b/doc/api/graphql/reference/gitlab_schema.json
index d161a4a5e17..a6276e172d5 100644
--- a/doc/api/graphql/reference/gitlab_schema.json
+++ b/doc/api/graphql/reference/gitlab_schema.json
@@ -54399,6 +54399,24 @@
               },
               "isDeprecated": false,
               "deprecationReason": null
+            },
+            {
+              "name": "warnings",
+              "description": "Indicates if a pipeline has warnings.",
+              "args": [
+
+              ],
+              "type": {
+                "kind": "NON_NULL",
+                "name": null,
+                "ofType": {
+                  "kind": "SCALAR",
+                  "name": "Boolean",
+                  "ofType": null
+                }
+              },
+              "isDeprecated": false,
+              "deprecationReason": null
             }
           ],
           "inputFields": null,
diff --git a/doc/api/graphql/reference/index.md b/doc/api/graphql/reference/index.md
index 030ae25c22d..df31b6f1dd0 100644
--- a/doc/api/graphql/reference/index.md
+++ b/doc/api/graphql/reference/index.md
@@ -2807,6 +2807,7 @@ Information about pagination in a connection..
 | `upstream` | Pipeline | Pipeline that triggered the pipeline. |
 | `user` | User | Pipeline user. |
 | `userPermissions` | PipelinePermissions! | Permissions for the current user on the resource |
+| `warnings` | Boolean! | Indicates if a pipeline has warnings. |
 
 ### PipelineAnalytics
 
diff --git a/spec/graphql/types/ci/pipeline_type_spec.rb b/spec/graphql/types/ci/pipeline_type_spec.rb
index d435e337ad7..2a1e030480d 100644
--- a/spec/graphql/types/ci/pipeline_type_spec.rb
+++ b/spec/graphql/types/ci/pipeline_type_spec.rb
@@ -12,7 +12,7 @@ RSpec.describe Types::Ci::PipelineType do
       id iid sha before_sha status detailed_status config_source duration
       coverage created_at updated_at started_at finished_at committed_at
       stages user retryable cancelable jobs source_job downstream
-      upstream path project active user_permissions
+      upstream path project active user_permissions warnings
     ]
 
     if Gitlab.ee?
-- 
2.30.9