From 74b1c699a15126ccd91a710ff315cf4e012f0047 Mon Sep 17 00:00:00 2001
From: Emily Ring <ering@gitlab.com>
Date: Tue, 17 Nov 2020 12:02:15 +0000
Subject: [PATCH] Fixes for missing users for Terraform State

Udated terraform vue to handle missing user info
Updated associated tests and translations
---
 .../terraform/components/states_table.vue     | 24 ++++++++++++-------
 .../unreleased/emilyring-terraform-fixes.yml  |  5 ++++
 locale/gitlab.pot                             |  3 +++
 .../terraform/components/states_table_spec.js | 20 ++++++++++++----
 4 files changed, 39 insertions(+), 13 deletions(-)
 create mode 100644 changelogs/unreleased/emilyring-terraform-fixes.yml

diff --git a/app/assets/javascripts/terraform/components/states_table.vue b/app/assets/javascripts/terraform/components/states_table.vue
index fa9bf15731a..2e4c18c5a5b 100644
--- a/app/assets/javascripts/terraform/components/states_table.vue
+++ b/app/assets/javascripts/terraform/components/states_table.vue
@@ -1,5 +1,6 @@
 <script>
 import { GlBadge, GlIcon, GlSprintf, GlTable, GlTooltip } from '@gitlab/ui';
+import { s__ } from '~/locale';
 import TimeAgoTooltip from '~/vue_shared/components/time_ago_tooltip.vue';
 import timeagoMixin from '~/vue_shared/mixins/timeago';
 
@@ -34,6 +35,17 @@ export default {
       ];
     },
   },
+  methods: {
+    createdByUserName(item) {
+      return item.latestVersion?.createdByUser?.name;
+    },
+    lockedByUserName(item) {
+      return item.lockedByUser?.name || s__('Terraform|Unknown User');
+    },
+    updatedTime(item) {
+      return item.latestVersion?.updatedAt || item.updatedAt;
+    },
+  },
 };
 </script>
 
@@ -58,7 +70,7 @@ export default {
           >
             <gl-sprintf :message="s__('Terraform|Locked by %{user} %{timeAgo}')">
               <template #user>
-                {{ item.lockedByUser.name }}
+                {{ lockedByUserName(item) }}
               </template>
 
               <template #timeAgo>
@@ -75,18 +87,12 @@ export default {
         <gl-sprintf :message="s__('Terraform|%{user} updated %{timeAgo}')">
           <template #user>
             <span v-if="item.latestVersion">
-              {{ item.latestVersion.createdByUser.name }}
+              {{ createdByUserName(item) }}
             </span>
           </template>
 
           <template #timeAgo>
-            <span v-if="item.latestVersion">
-              <time-ago-tooltip :time="item.latestVersion.updatedAt" />
-            </span>
-
-            <span v-else>
-              <time-ago-tooltip :time="item.updatedAt" />
-            </span>
+            <time-ago-tooltip :time="updatedTime(item)" />
           </template>
         </gl-sprintf>
       </p>
diff --git a/changelogs/unreleased/emilyring-terraform-fixes.yml b/changelogs/unreleased/emilyring-terraform-fixes.yml
new file mode 100644
index 00000000000..a733340936b
--- /dev/null
+++ b/changelogs/unreleased/emilyring-terraform-fixes.yml
@@ -0,0 +1,5 @@
+---
+title: Fix for missing user info for Terraform State
+merge_request: 47814
+author:
+type: fixed
diff --git a/locale/gitlab.pot b/locale/gitlab.pot
index 9b14d4951a8..06c3dc5d141 100644
--- a/locale/gitlab.pot
+++ b/locale/gitlab.pot
@@ -26590,6 +26590,9 @@ msgstr ""
 msgid "Terraform|The Terraform report %{name} was generated in your pipelines."
 msgstr ""
 
+msgid "Terraform|Unknown User"
+msgstr ""
+
 msgid "Test"
 msgstr ""
 
diff --git a/spec/frontend/terraform/components/states_table_spec.js b/spec/frontend/terraform/components/states_table_spec.js
index e2c2660b221..7a8cb19971e 100644
--- a/spec/frontend/terraform/components/states_table_spec.js
+++ b/spec/frontend/terraform/components/states_table_spec.js
@@ -39,6 +39,16 @@ describe('StatesTable', () => {
           },
         },
       },
+      {
+        name: 'state-4',
+        lockedAt: '2020-10-10T00:00:00Z',
+        lockedByUser: null,
+        updatedAt: '2020-10-10T00:00:00Z',
+        latestVersion: {
+          updatedAt: '2020-10-09T00:00:00Z',
+          createdByUser: null,
+        },
+      },
     ],
   };
 
@@ -53,10 +63,11 @@ describe('StatesTable', () => {
   });
 
   it.each`
-    name         | toolTipText                      | locked   | lineNumber
-    ${'state-1'} | ${'Locked by user-1 2 days ago'} | ${true}  | ${0}
-    ${'state-2'} | ${null}                          | ${false} | ${1}
-    ${'state-3'} | ${'Locked by user-2 5 days ago'} | ${true}  | ${2}
+    name         | toolTipText                            | locked   | lineNumber
+    ${'state-1'} | ${'Locked by user-1 2 days ago'}       | ${true}  | ${0}
+    ${'state-2'} | ${null}                                | ${false} | ${1}
+    ${'state-3'} | ${'Locked by user-2 5 days ago'}       | ${true}  | ${2}
+    ${'state-4'} | ${'Locked by Unknown User 5 days ago'} | ${true}  | ${3}
   `(
     'displays the name and locked information "$name" for line "$lineNumber"',
     ({ name, toolTipText, locked, lineNumber }) => {
@@ -80,6 +91,7 @@ describe('StatesTable', () => {
     ${'updated 2 days ago'}        | ${0}
     ${'updated 5 days ago'}        | ${1}
     ${'user-3 updated 4 days ago'} | ${2}
+    ${'updated 6 days ago'}        | ${3}
   `('displays the time "$updateTime" for line "$lineNumber"', ({ updateTime, lineNumber }) => {
     const states = wrapper.findAll('[data-testid="terraform-states-table-updated"]');
 
-- 
2.30.9