summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/reports/codequality_report/store/utils/codequality_comparison.js
blob: eba9e340c4e18a7ac2dc06f9ce069670883ba1de (plain)
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
import CodeQualityComparisonWorker from '../../workers/codequality_comparison_worker';

export const parseCodeclimateMetrics = (issues = [], path = '') => {
  return issues.map(issue => {
    const parsedIssue = {
      ...issue,
      name: issue.description,
    };

    if (issue?.location?.path) {
      let parseCodeQualityUrl = `${path}/${issue.location.path}`;
      parsedIssue.path = issue.location.path;

      if (issue?.location?.lines?.begin) {
        parsedIssue.line = issue.location.lines.begin;
        parseCodeQualityUrl += `#L${issue.location.lines.begin}`;
      } else if (issue?.location?.positions?.begin?.line) {
        parsedIssue.line = issue.location.positions.begin.line;
        parseCodeQualityUrl += `#L${issue.location.positions.begin.line}`;
      }

      parsedIssue.urlPath = parseCodeQualityUrl;
    }

    return parsedIssue;
  });
};

export const doCodeClimateComparison = (headIssues, baseIssues) => {
  // Do these comparisons in worker threads to avoid blocking the main thread
  return new Promise((resolve, reject) => {
    const worker = new CodeQualityComparisonWorker();
    worker.addEventListener('message', ({ data }) =>
      data.newIssues && data.resolvedIssues ? resolve(data) : reject(data),
    );
    worker.postMessage({
      headIssues,
      baseIssues,
    });
  });
};