All files / ee/app/assets/javascripts/codequality_report codequality_report_graphql.vue

88.57% Statements 31/35
91.67% Branches 11/12
81.25% Functions 13/16
88.57% Lines 31/35

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  1x   1x 1x 1x 1x 1x 1x 1x 1x 1x 1x                                   6x                     6x               2x     12x 6x           6x                       11x     12x     6x 6x       12x 12x   12x 9x 3x 3x               11x                   1x 1x                                             66x                                                                                                        
<script>
import { GlSkeletonLoader, GlInfiniteScroll, GlLoadingIcon, GlSprintf } from '@gitlab/ui';
import { once } from 'lodash';
import produce from 'immer';
import api from '~/api';
import { componentNames } from 'ee/reports/components/issue_body';
import reportsMixin from 'ee/vue_shared/security_reports/mixins/reports_mixin';
import { n__, s__, sprintf } from '~/locale';
import ReportSection from '~/reports/components/report_section.vue';
import CodequalityIssueBody from '~/reports/codequality_report/components/codequality_issue_body.vue';
import { parseCodeclimateMetrics } from '~/reports/codequality_report/store/utils/codequality_parser';
import getCodeQualityViolations from './graphql/queries/get_code_quality_violations.query.graphql';
import { PAGE_SIZE, VIEW_EVENT_NAME } from './store/constants';
 
export default {
  components: {
    ReportSection,
    CodequalityIssueBody,
    GlSkeletonLoader,
    GlInfiniteScroll,
    GlLoadingIcon,
    GlSprintf,
  },
  mixins: [reportsMixin],
  componentNames,
  inject: ['projectPath', 'pipelineIid', 'blobPath'],
  apollo: {
    codequalityViolations: {
      query: getCodeQualityViolations,
      variables() {
        return {
          projectPath: this.projectPath,
          iid: this.pipelineIid,
          first: PAGE_SIZE,
        };
      },
      update({
        project: {
          pipeline: { codeQualityReports: { nodes = [], pageInfo = {}, count = 0 } = {} } = {},
        } = {},
      }) {
        return {
          nodes,
          parsedList: parseCodeclimateMetrics(nodes, this.blobPath),
          count,
          pageInfo,
        };
      },
      error() {
        this.errored = true;
      },
      watchLoading(isLoading) {
        if (isLoading) {
          this.trackViewEvent();
        }
      },
    },
  },
  data() {
    return {
      codequalityViolations: {
        nodes: [],
        parsedList: [],
        count: 0,
        pageInfo: {},
      },
      errored: false,
    };
  },
  computed: {
    isLoading() {
      return this.$apollo.queries.codequalityViolations.loading;
    },
    hasCodequalityViolations() {
      return this.codequalityViolations.count > 0;
    },
    trackViewEvent() {
      return once(() => {
        api.trackRedisHllUserEvent(VIEW_EVENT_NAME);
      });
    },
    codequalityText() {
      const text = [];
      const { count } = this.codequalityViolations;
 
      if (count === 0) {
        return s__('ciReport|No code quality issues found');
E      } else if (count > 0) {
        return sprintf(s__('ciReport|Found %{issuesWithCount}'), {
          issuesWithCount: n__('%d code quality issue', '%d code quality issues', count),
        });
      }
 
      return text.join('');
    },
    codequalityStatus() {
      return this.checkReportStatus(this.isLoading && !this.hasCodequalityViolations, this.errored);
    },
  },
  i18n: {
    subHeading: s__('ciReport|This report contains all Code Quality issues in the source branch.'),
    loadingText: s__('ciReport|Loading Code Quality report'),
    errorText: s__('ciReport|Failed to load Code Quality report'),
    showingCount: s__('ciReport|Showing %{fetchedItems} of %{totalItems} items'),
  },
  methods: {
    fetchMoreViolations() {
      this.$apollo.queries.codequalityViolations
        .fetchMore({
          variables: {
            first: PAGE_SIZE,
            after: this.codequalityViolations.pageInfo.endCursor,
          },
          updateQuery: (previousResult, { fetchMoreResult }) => {
            return produce(fetchMoreResult, (draftData) => {
              draftData.project.pipeline.codeQualityReports.nodes = [
                ...previousResult.project.pipeline.codeQualityReports.nodes,
                ...draftData.project.pipeline.codeQualityReports.nodes,
              ];
            });
          },
        })
        .catch(() => {
          this.errored = true;
        });
    },
  },
};
</script>
 
<template>
  <div>
    <report-section
      always-open
      :status="codequalityStatus"
      :loading-text="$options.i18n.loadingText"
      :error-text="$options.i18n.errorText"
      :success-text="codequalityText"
      :unresolved-issues="codequalityViolations.parsedList"
      :resolved-issues="[]"
      :has-issues="hasCodequalityViolations"
      :component="$options.componentNames.CodequalityIssueBody"
      class="codequality-report"
    >
      <template v-if="hasCodequalityViolations" #sub-heading>{{
        $options.i18n.subHeading
      }}</template>
      <template #body>
        <gl-infinite-scroll
          :max-list-height="500"
          :fetched-items="codequalityViolations.parsedList.length"
          :total-items="codequalityViolations.count"
          @bottomReached="fetchMoreViolations"
        >
          <template #items>
            <div class="report-block-container">
              <template v-for="(issue, index) in codequalityViolations.parsedList">
                <codequality-issue-body
                  :key="index"
                  :issue="issue"
                  class="report-block-list-issue"
                />
              </template>
            </div>
          </template>
          <template #default>
            <div class="gl-mt-3">
              <gl-loading-icon v-if="isLoading" />
              <gl-sprintf v-else :message="$options.i18n.showingCount"
                ><template #fetchedItems>{{ codequalityViolations.parsedList.length }}</template
                ><template #totalItems>{{ codequalityViolations.count }}</template></gl-sprintf
              >
            </div>
          </template>
        </gl-infinite-scroll>
      </template>
    </report-section>
    <div v-if="isLoading && !hasCodequalityViolations" class="report-block-container">
      <gl-skeleton-loader :lines="36" />
    </div>
  </div>
</template>