All files / ee/app/assets/javascripts/dependencies/components dependencies_table.vue

9.09% Statements 2/22
7.69% Branches 1/13
6.25% Functions 1/16
9.09% Lines 2/22

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                                          3x                       3x                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
<script>
import {
  GlBadge,
  GlIcon,
  GlButton,
  GlSkeletonLoader,
  GlTable,
  GlPopover,
  GlLink,
  GlLoadingIcon,
} from '@gitlab/ui';
import { cloneDeep } from 'lodash';
import { DOCS_URL_IN_EE_DIR } from 'jh_else_ce/lib/utils/url_utility';
import glFeatureFlagsMixin from '~/vue_shared/mixins/gl_feature_flags_mixin';
import { NAMESPACE_ORGANIZATION, NAMESPACE_PROJECT, DEPENDENCIES_TABLE_I18N } from '../constants';
import DependencyLicenseLinks from './dependency_license_links.vue';
import DependencyLocation from './dependency_location.vue';
import DependencyLocationCount from './dependency_location_count.vue';
import DependencyProjectCount from './dependency_project_count.vue';
import DependencyVulnerabilities from './dependency_vulnerabilities.vue';
 
const tdClass = (defaultClasses = []) => (value, key, item) => {
  const classes = [...defaultClasses];
 
  // Don't draw a border between a row and its `row-details` slot
  // eslint-disable-next-line no-underscore-dangle
  if (item._showDetails) {
    classes.push('border-bottom-0');
  }
 
  return classes;
};
 
const sharedFields = [
  { key: 'component', label: DEPENDENCIES_TABLE_I18N.component, tdClass: tdClass() },
  { key: 'packager', label: DEPENDENCIES_TABLE_I18N.packager, tdClass: tdClass() },
  {
    key: 'location',
    label: DEPENDENCIES_TABLE_I18N.location,
    tdClass: tdClass(['gl-md-max-w-26']),
  },
  { key: 'license', label: DEPENDENCIES_TABLE_I18N.license, tdClass: tdClass() },
];
 
export default {
  name: 'DependenciesTable',
  components: {
    DependencyLicenseLinks,
    DependencyVulnerabilities,
    DependencyLocation,
    DependencyLocationCount,
    DependencyProjectCount,
    GlBadge,
    GlIcon,
    GlButton,
    GlSkeletonLoader,
    GlTable,
    GlPopover,
    GlLink,
    GlLoadingIcon,
  },
  mixins: [glFeatureFlagsMixin()],
  inject: ['namespaceType'],
  props: {
    dependencies: {
      type: Array,
      required: true,
    },
    vulnerabilityInfo: {
      type: Object,
      required: true,
    },
    vulnerabilityItemsLoading: {
      type: Array,
      required: false,
      default: () => [],
    },
    isLoading: {
      type: Boolean,
      required: true,
    },
  },
  computed: {
    anyDependencyHasVulnerabilities() {
      return this.localDependencies.some((dependency) => this.vulnerabilitiesCount(dependency));
    },
    fields() {
      Iif (this.isOrganizationNamespace) {
        return this.$options.organizationFields;
      }
 
      return this.isProjectNamespace ? this.$options.projectFields : this.$options.groupFields;
    },
    isProjectNamespace() {
      return this.namespaceType === NAMESPACE_PROJECT;
    },
    isOrganizationNamespace() {
      return this.namespaceType === NAMESPACE_ORGANIZATION;
    },
    localDependencies() {
      return this.transformDependenciesForUI(this.dependencies);
    },
    isGroupLevelOrWithSbomOccurrencesEnabled() {
      return !this.isProjectNamespace || this.glFeatures.projectLevelSbomOccurrences;
    },
  },
  methods: {
    // The GlTable component mutates the `_showDetails` property on items
    // passed to it in order to track the visibility of each row's `row-details`
    // slot. So, create a deep clone of them here to avoid mutating the
    // `dependencies` prop.
    // We also make sure that `vulnerabilities` is always defined to prevent rendering
    // errors when the user is allowe to see dependencies but not their vulnerabilities.
    transformDependenciesForUI(dependencies) {
      return dependencies.map(({ vulnerabilities, ...dep }) => ({
        ...cloneDeep(dep),
        vulnerabilities: vulnerabilities ? cloneDeep(vulnerabilities) : [],
      }));
    },
    displayLocation(item) {
      return !item.occurrenceCount || item.occurrenceCount < 2;
    },
    packager(dependency) {
      return dependency.packager || this.$options.i18n.unknown;
    },
    vulnerabilitiesCount(item) {
      Iif (this.isGroupLevelOrWithSbomOccurrencesEnabled) {
        return item.vulnerabilityCount;
      }
      return item.vulnerabilities?.length;
    },
    vulnerabilities(item) {
      Iif (this.isGroupLevelOrWithSbomOccurrencesEnabled) {
        return this.vulnerabilityInfo[item.occurrenceId];
      }
      return item.vulnerabilities;
    },
    rowExpanded(showDetails, item) {
      showDetails();
      Iif (this.isGroupLevelOrWithSbomOccurrencesEnabled) {
        this.$emit('row-click', item);
      }
    },
  },
  organizationFields: [...sharedFields],
  groupFields: [
    ...sharedFields,
    { key: 'projects', label: DEPENDENCIES_TABLE_I18N.projects, tdClass: tdClass() },
    { key: 'isVulnerable', label: '', tdClass: tdClass(['gl-text-right']) },
  ],
  projectFields: [
    ...sharedFields,
    { key: 'isVulnerable', label: '', tdClass: tdClass(['gl-text-right']) },
  ],
  DEPENDENCIES_PER_PAGE: 20,
  DEPENDENCY_PATH_LINK: `${DOCS_URL_IN_EE_DIR}/user/application_security/dependency_list/#dependency-paths`,
  i18n: DEPENDENCIES_TABLE_I18N,
};
</script>
 
<template>
  <gl-table
    :fields="fields"
    :items="localDependencies"
    :busy="isLoading"
    data-testid="dependencies-table-content"
    details-td-class="pt-0"
    stacked="md"
    show-empty
  >
    <template #head(location)="data">
      {{ data.label }}
      <gl-icon id="location-info" name="information-o" class="gl-text-blue-600" />
      <gl-popover
        target="location-info"
        placement="top"
        :title="$options.i18n.locationDependencyTitle"
      >
        {{ $options.i18n.tooltipText }}
        <div class="gl-mt-4">
          <gl-link :href="$options.DEPENDENCY_PATH_LINK" target="_blank">{{
            $options.i18n.tooltipMoreText
          }}</gl-link>
        </div>
      </gl-popover>
    </template>
 
    <!-- toggleDetails and detailsShowing are scoped slot props provided by
      GlTable; they mutate/read the item's _showDetails property, which GlTable
      uses to show/hide the row-details slot -->
    <template #cell(component)="{ item, toggleDetails, detailsShowing }">
      <gl-button
        v-if="anyDependencyHasVulnerabilities"
        class="d-none d-md-inline"
        :class="{ invisible: !vulnerabilitiesCount(item) }"
        category="tertiary"
        size="small"
        :aria-label="$options.i18n.toggleVulnerabilityList"
        :icon="detailsShowing ? 'chevron-up' : 'chevron-down'"
        @click="rowExpanded(toggleDetails, item)"
      />
      <span class="bold">{{ item.name }}</span
      >&nbsp;{{ item.version }}
    </template>
 
    <template #cell(packager)="{ item }">
      <span>{{ packager(item) }}</span>
    </template>
 
    <template #cell(location)="{ item }">
      <dependency-location v-if="displayLocation(item)" :location="item.location" />
      <dependency-location-count
        v-else
        :location-count="item.occurrenceCount"
        :component-id="item.componentId"
      />
    </template>
 
    <template #cell(license)="{ item }">
      <dependency-license-links :licenses="item.licenses" :title="item.name" />
    </template>
 
    <template #cell(projects)="{ item }">
      <dependency-project-count
        v-if="!isProjectNamespace"
        :project="item.project"
        :project-count="item.projectCount"
        :component-id="item.componentId"
      />
    </template>
 
    <template #cell(isVulnerable)="{ item, toggleDetails }">
      <gl-badge
        v-if="vulnerabilitiesCount(item)"
        variant="warning"
        href="#"
        @click.native="rowExpanded(toggleDetails, item)"
      >
        <gl-icon name="warning" class="gl-text-orange-500 mr-1" />
        {{
          n__(
            'Dependencies|%d vulnerability detected',
            'Dependencies|%d vulnerabilities detected',
            vulnerabilitiesCount(item),
          )
        }}
      </gl-badge>
    </template>
 
    <template #row-details="{ item }">
      <gl-loading-icon v-if="vulnerabilityItemsLoading.includes(item)" size="md" />
      <dependency-vulnerabilities v-else class="ml-4" :vulnerabilities="vulnerabilities(item)" />
    </template>
 
    <template #table-busy>
      <div class="mt-2">
        <gl-skeleton-loader v-for="n in $options.DEPENDENCIES_PER_PAGE" :key="n" :lines="1" />
      </div>
    </template>
  </gl-table>
</template>