All files / app/assets/javascripts/pages/import/bulk_imports/history/components bulk_imports_history_app.vue

7.14% Statements 3/42
0% Branches 0/16
4.54% Functions 1/22
7.14% Lines 3/42

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303                                                          1x   1x   1x                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          
<script>
import {
  GlEmptyState,
  GlIcon,
  GlLink,
  GlLoadingIcon,
  GlTableLite,
  GlTooltipDirective as GlTooltip,
} from '@gitlab/ui';
import { isEmpty, isEqual } from 'lodash';
 
import { s__, __ } from '~/locale';
import { createAlert } from '~/alert';
import { parseIntPagination, normalizeHeaders } from '~/lib/utils/common_utils';
import { joinPaths } from '~/lib/utils/url_utility';
import { getBulkImportHistory, getBulkImportsHistory } from '~/rest_api';
import { BULK_IMPORT_STATIC_ITEMS } from '~/import/constants';
import ImportStats from '~/import_entities/components/import_stats.vue';
import ImportStatus from '~/import_entities/import_groups/components/import_status.vue';
import { StatusPoller } from '~/import_entities/import_groups/services/status_poller';
 
import { WORKSPACE_GROUP, WORKSPACE_PROJECT } from '~/issues/constants';
import PaginationBar from '~/vue_shared/components/pagination_bar/pagination_bar.vue';
import TimeAgo from '~/vue_shared/components/time_ago_tooltip.vue';
import LocalStorageSync from '~/vue_shared/components/local_storage_sync.vue';
 
import { isImporting } from '../utils';
import { DEFAULT_ERROR } from '../utils/error_messages';
 
const DEFAULT_PER_PAGE = 20;
 
const HISTORY_PAGINATION_SIZE_PERSIST_KEY = 'gl-bulk-imports-history-per-page';
 
const tableCell = (config) => ({
  tdClass: (value, key, item) => {
    return {
      // eslint-disable-next-line no-underscore-dangle
      'gl-border-b-0!': item._showDetails,
    };
  },
  ...config,
});
 
export default {
  components: {
    GlEmptyState,
    GlIcon,
    GlLink,
    GlLoadingIcon,
    GlTableLite,
    PaginationBar,
    ImportStats,
    ImportStatus,
    TimeAgo,
    LocalStorageSync,
  },
 
  directives: {
    GlTooltip,
  },
 
  inject: ['realtimeChangesPath'],
 
  props: {
    id: {
      type: String,
      required: false,
      default: null,
    },
  },
 
  data() {
    return {
      loading: true,
      historyItems: [],
      paginationConfig: {
        page: 1,
        perPage: DEFAULT_PER_PAGE,
      },
      pageInfo: {},
    };
  },
 
  fields: [
    tableCell({
      key: 'source_full_path',
      label: s__('BulkImport|Source'),
      thClass: `gl-w-30p`,
    }),
    tableCell({
      key: 'destination_name',
      label: s__('BulkImport|Destination'),
      thClass: `gl-w-30p`,
    }),
    tableCell({
      key: 'created_at',
      label: __('Start date'),
    }),
    tableCell({
      key: 'status',
      label: __('Status'),
      thClass: `gl-w-quarter`,
    }),
  ],
 
  computed: {
    hasHistoryItems() {
      return this.historyItems.length > 0;
    },
 
    importingHistoryItemIds() {
      return this.historyItems
        .filter((item) => isImporting(item.status))
        .map((item) => item.bulk_import_id);
    },
 
    paginationConfigCopy() {
      return { ...this.paginationConfig };
    },
  },
 
  watch: {
    paginationConfigCopy: {
      handler(newValue, oldValue) {
        Iif (!isEqual(newValue, oldValue)) {
          this.loadHistoryItems();
        }
      },
      deep: true,
    },
 
    importingHistoryItemIds(value) {
      if (value.length > 0) {
        this.statusPoller.startPolling();
      } else {
        this.statusPoller.stopPolling();
      }
    },
  },
 
  mounted() {
    this.loadHistoryItems();
 
    this.statusPoller = new StatusPoller({
      pollPath: this.realtimeChangesPath,
      updateImportStatus: (update) => {
        Iif (!this.importingHistoryItemIds.includes(update.id)) {
          return;
        }
 
        const updateItemIndex = this.historyItems.findIndex(
          (item) => item.bulk_import_id === update.id,
        );
        const updateItem = this.historyItems[updateItemIndex];
 
        Iif (updateItem.status !== update.status_name) {
          this.$set(this.historyItems, updateItemIndex, {
            ...updateItem,
            status: update.status_name,
          });
        }
      },
    });
  },
 
  beforeDestroy() {
    this.statusPoller.stopPolling();
  },
 
  methods: {
    fetchFn(params) {
      return this.id ? getBulkImportHistory(this.id, params) : getBulkImportsHistory(params);
    },
 
    async loadHistoryItems() {
      try {
        this.loading = true;
 
        const { data: historyItems, headers } = await this.fetchFn({
          page: this.paginationConfig.page,
          per_page: this.paginationConfig.perPage,
        });
        this.pageInfo = parseIntPagination(normalizeHeaders(headers));
        this.historyItems = historyItems;
      } catch (e) {
        createAlert({ message: e.message || DEFAULT_ERROR, captureError: true, error: e });
      } finally {
        this.loading = false;
      }
    },
 
    destinationLinkHref(params) {
      return joinPaths(gon.relative_url_root || '', '/', params.destination_full_path);
    },
 
    pathWithSuffix(path, item) {
      const suffix = item.entity_type === WORKSPACE_GROUP ? '/' : '';
      return `${path}${suffix}`;
    },
 
    destinationLinkText(item) {
      return this.pathWithSuffix(item.destination_full_path, item);
    },
 
    destinationText(item) {
      const fullPath = joinPaths(item.destination_namespace, item.destination_slug);
      return this.pathWithSuffix(fullPath, item);
    },
 
    hasStats(item) {
      return !isEmpty(item.stats);
    },
 
    getEntityTooltip(item) {
      switch (item.entity_type) {
        case WORKSPACE_PROJECT:
          return __('Project');
        case WORKSPACE_GROUP:
          return __('Group');
        default:
          return '';
      }
    },
 
    setPageSize(size) {
      this.paginationConfig.perPage = size;
      this.paginationConfig.page = 1;
    },
  },
 
  gitlabLogo: window.gon.gitlab_logo,
  historyPaginationSizePersistKey: HISTORY_PAGINATION_SIZE_PERSIST_KEY,
  BULK_IMPORT_STATIC_ITEMS,
};
</script>
 
<template>
  <div>
    <h1 class="gl-font-size-h1 gl-my-0 gl-py-4 gl-display-flex gl-align-items-center gl-gap-3">
      <img :src="$options.gitlabLogo" :alt="__('GitLab Logo')" class="gl-w-6 gl-h-6" />
      <span>{{ s__('BulkImport|Direct transfer history') }}</span>
    </h1>
 
    <gl-loading-icon v-if="loading" size="lg" class="gl-mt-5" />
    <gl-empty-state
      v-else-if="!hasHistoryItems"
      :title="s__('BulkImport|No history is available')"
      :description="s__('BulkImport|Your imported groups and projects will appear here.')"
    />
    <template v-else>
      <gl-table-lite :fields="$options.fields" :items="historyItems" class="gl-w-full">
        <template #cell(destination_name)="{ item }">
          <gl-icon
            v-gl-tooltip
            :name="item.entity_type"
            :title="getEntityTooltip(item)"
            :aria-label="getEntityTooltip(item)"
            class="gl-text-gray-500"
          />
          <gl-link
            v-if="item.destination_full_path"
            :href="destinationLinkHref(item)"
            target="_blank"
          >
            {{ destinationLinkText(item) }}
          </gl-link>
          <span v-else>{{ destinationText(item) }}</span>
        </template>
        <template #cell(created_at)="{ value }">
          <time-ago :time="value" />
        </template>
        <template #cell(status)="{ value, item }">
          <div>
            <import-status
              :id="item.bulk_import_id"
              :entity-id="item.id"
              :has-failures="item.has_failures"
              :status="value"
            />
            <import-stats
              v-if="hasStats(item)"
              :stats="item.stats"
              :stats-mapping="$options.BULK_IMPORT_STATIC_ITEMS"
              :status="value"
              class="gl-mt-2"
            />
          </div>
        </template>
      </gl-table-lite>
      <pagination-bar
        :page-info="pageInfo"
        class="gl-m-0 gl-mt-3"
        @set-page="paginationConfig.page = $event"
        @set-page-size="setPageSize"
      />
    </template>
    <local-storage-sync
      v-model="paginationConfig.perPage"
      :storage-key="$options.historyPaginationSizePersistKey"
    />
  </div>
</template>