All files / app/assets/javascripts commit_merge_requests.js

81.48% Statements 22/27
100% Branches 4/4
70% Functions 7/10
81.48% Lines 22/27

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            7x 4x   3x       3x   3x             4x               4x           3x 3x 3x 3x 3x   3x       2x   2x 1x   1x 2x 2x 2x 2x       2x                                      
import $ from 'jquery';
import { createAlert } from '~/alert';
import axios from './lib/utils/axios_utils';
import { n__, s__ } from './locale';
 
export function getHeaderText(childElementCount, mergeRequestCount) {
  if (childElementCount === 0) {
    return `${mergeRequestCount} ${n__('merge request', 'merge requests', mergeRequestCount)}`;
  }
  return ',';
}
 
export function createHeader(childElementCount, mergeRequestCount) {
  const headerText = getHeaderText(childElementCount, mergeRequestCount);
 
  return $('<span />', {
    class: 'gl-mr-2',
    text: headerText,
  });
}
 
export function createLink(mergeRequest) {
  return $('<a />', {
    class: 'gl-mr-2',
    href: mergeRequest.path,
    text: `!${mergeRequest.iid}`,
  });
}
 
export function createTitle(mergeRequest) {
  return $('<span />', {
    text: mergeRequest.title,
  });
}
 
export function createItem(mergeRequest) {
  const $item = $('<span />');
  const $link = createLink(mergeRequest);
  const $title = createTitle(mergeRequest);
  $item.append($link);
  $item.append($title);
 
  return $item;
}
 
export function createContent(mergeRequests) {
  const $content = $('<span />');
 
  if (mergeRequests.length === 0) {
    $content.text(s__('Commits|No related merge requests found'));
  } else {
    mergeRequests.forEach((mergeRequest) => {
      const $header = createHeader($content.children().length, mergeRequests.length);
      const $item = createItem(mergeRequest);
      $content.append($header);
      $content.append($item);
    });
  }
 
  return $content;
}
 
export function fetchCommitMergeRequests() {
  const $container = $('.merge-requests');
 
  axios
    .get($container.data('projectCommitPath'))
    .then((response) => {
      const $content = createContent(response.data);
 
      $container.html($content);
    })
    .catch(() =>
      createAlert({
        message: s__('Commits|An error occurred while fetching merge requests data.'),
      }),
    );
}