All files / ee/app/assets/javascripts/on_demand_scans/components on_demand_scans.vue

96.88% Statements 31/32
75% Branches 6/8
100% Functions 14/14
96.88% Lines 31/32

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  1x 1x 1x 1x       1x 1x             1x 1x 1x 1x 1x 1x                                         8x             16x     8x   40x 40x                           8x           16x               16x               16x                                                 8x 8x 8x   8x     8x       8x 48x 8x         8x                             199x                                                              
<script>
import { GlButton, GlLink, GlSprintf, GlScrollableTabs } from '@gitlab/ui';
import { s__ } from '~/locale';
import ConfigurationPageLayout from 'ee/security_configuration/components/configuration_page_layout.vue';
import {
  getQueryHeaders,
  toggleQueryPollingByVisibility,
} from '~/pipelines/components/graph/utils';
import onDemandScanCounts from '../graphql/on_demand_scan_counts.query.graphql';
import {
  HELP_PAGE_PATH,
  PIPELINE_TABS_KEYS,
  PIPELINES_COUNT_POLL_INTERVAL,
  PIPELINES_SCOPE_RUNNING,
  PIPELINES_SCOPE_FINISHED,
} from '../constants';
import AllTab from './tabs/all.vue';
import RunningTab from './tabs/running.vue';
import FinishedTab from './tabs/finished.vue';
import ScheduledTab from './tabs/scheduled.vue';
import SavedTab from './tabs/saved.vue';
import EmptyState from './empty_state.vue';
 
export default {
  HELP_PAGE_PATH,
  components: {
    GlButton,
    GlLink,
    GlSprintf,
    GlScrollableTabs,
    ConfigurationPageLayout,
    AllTab,
    RunningTab,
    FinishedTab,
    ScheduledTab,
    EmptyState,
  },
  inject: ['newDastScanPath', 'projectPath', 'projectOnDemandScanCountsEtag'],
  apollo: {
    liveOnDemandScanCounts: {
      query: onDemandScanCounts,
      variables() {
        return {
          fullPath: this.projectPath,
          runningScope: PIPELINES_SCOPE_RUNNING,
          finishedScope: PIPELINES_SCOPE_FINISHED,
        };
      },
      context() {
        return getQueryHeaders(this.projectOnDemandScanCountsEtag);
      },
      update(data) {
        return Object.fromEntries(
          PIPELINE_TABS_KEYS.map((key) => {
            const { count } = data[key].pipelines;
            return [key, count];
          }),
        );
      },
      pollInterval: PIPELINES_COUNT_POLL_INTERVAL,
    },
  },
  props: {
    initialOnDemandScanCounts: {
      type: Object,
      required: true,
    },
  },
  data() {
    return {
      activeTabIndex: 0,
    };
  },
  computed: {
    onDemandScanCounts() {
      return this.liveOnDemandScanCounts ?? this.initialOnDemandScanCounts;
    },
    hasData() {
      // Scheduled and saved scans aren't included in the total count because they are
      // dastProfiles, not pipelines.
      // When https://gitlab.com/gitlab-org/gitlab/-/issues/342950 is addressed, we won't need to
      // include scheduled scans in the calculation. We'll still need to include saved scans as
      // those will likely neverr be considered pipelines.
      return (
        this.onDemandScanCounts.all +
          this.onDemandScanCounts.scheduled +
          this.onDemandScanCounts.saved >
        0
      );
    },
    tabs() {
      return {
        all: {
          component: AllTab,
          itemsCount: this.onDemandScanCounts.all,
        },
        running: {
          component: RunningTab,
          itemsCount: this.onDemandScanCounts.running,
        },
        finished: {
          component: FinishedTab,
          itemsCount: this.onDemandScanCounts.finished,
        },
        scheduled: {
          component: ScheduledTab,
          itemsCount: this.onDemandScanCounts.scheduled,
        },
        saved: {
          component: SavedTab,
          itemsCount: this.onDemandScanCounts.saved,
        },
      };
    },
    activeTab: {
      set(newTabIndex) {
        const newTabId = Object.keys(this.tabs)[newTabIndex];
E        if (this.$route.params.tabId !== newTabId) {
          this.$router.push(`/${newTabId}`);
        }
        this.activeTabIndex = newTabIndex;
      },
      get() {
        return this.activeTabIndex;
      },
    },
  },
  created() {
    const tabIndex = Object.keys(this.tabs).findIndex((tab) => tab === this.$route.params.tabId);
I    if (tabIndex !== -1) {
      this.activeTabIndex = tabIndex;
    }
  },
  mounted() {
    toggleQueryPollingByVisibility(
      this.$apollo.queries.liveOnDemandScanCounts,
      PIPELINES_COUNT_POLL_INTERVAL,
    );
  },
  i18n: {
    title: s__('OnDemandScans|On-demand scans'),
    newScanButtonLabel: s__('OnDemandScans|New DAST scan'),
    description: s__(
      'OnDemandScans|On-demand scans run outside of DevOps cycle and find vulnerabilities in your projects. %{learnMoreLinkStart}Learn more%{learnMoreLinkEnd}.',
    ),
  },
};
</script>
 
<template>
  <configuration-page-layout v-if="hasData" no-border>
    <template #heading>
      {{ $options.i18n.title }}
    </template>
    <template #actions>
      <gl-button variant="confirm" :href="newDastScanPath" data-testid="new-scan-link">
        {{ $options.i18n.newScanButtonLabel }}
      </gl-button>
    </template>
    <template #description>
      <gl-sprintf :message="$options.i18n.description">
        <template #learnMoreLink="{ content }">
          <gl-link :href="$options.HELP_PAGE_PATH" data-testid="help-page-link">{{
            content
          }}</gl-link>
        </template>
      </gl-sprintf>
    </template>
    <gl-scrollable-tabs v-model="activeTab" data-testid="on-demand-scans-tabs">
      <component
        :is="tab.component"
        v-for="(tab, key, index) in tabs"
        :key="key"
        :items-count="tab.itemsCount"
        :is-active="activeTab === index"
      />
    </gl-scrollable-tabs>
  </configuration-page-layout>
  <empty-state v-else />
</template>