All files / ee/app/assets/javascripts/api dora_api.js

100% Statements 11/11
100% Branches 4/4
100% Functions 3/3
100% Lines 11/11

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      4x 4x 4x         4x 4x     70x 2x     68x   68x                                           53x                                 17x    
import { buildApiUrl } from '~/api/api_utils';
import axios from '~/lib/utils/axios_utils';
 
export const DEPLOYMENT_FREQUENCY_METRIC_TYPE = 'deployment_frequency';
export const LEAD_TIME_FOR_CHANGES = 'lead_time_for_changes';
export const ALL_METRIC_TYPES = Object.freeze([
  DEPLOYMENT_FREQUENCY_METRIC_TYPE,
  LEAD_TIME_FOR_CHANGES,
]);
 
export const PROJECTS_DORA_METRICS_PATH = '/api/:version/projects/:id/dora/metrics';
export const GROUPS_DORA_METRICS_PATH = '/api/:version/groups/:id/dora/metrics';
 
function getDoraMetrics(apiUrl, projectOrGroupId, metric, params) {
  if (!ALL_METRIC_TYPES.includes(metric)) {
    throw new Error(`Unsupported metric type: "${metric}"`);
  }
 
  const url = buildApiUrl(apiUrl).replace(':id', encodeURIComponent(projectOrGroupId));
 
  return axios.get(url, {
    params: {
      metric,
      ...params,
    },
  });
}
 
/**
 * Gets DORA 4 metrics data from a project
 * See https://docs.gitlab.com/ee/api/dora/metrics.html#get-project-level-dora-metrics
 *
 * @param {String|Number} projectId The ID or path of the project
 * @param {String} metric The name of the metric to fetch. Must be one of:
 * `["deployment_frequency", "lead_time_for_changes"]`
 * @param {Object} params Any additional query parameters that should be
 * included with the request. These parameters are optional. See
 * https://docs.gitlab.com/ee/api/dora/metrics.html for a list of available options.
 *
 * @returns {Promise} A `Promise` that resolves to an array of data points.
 */
export function getProjectDoraMetrics(projectId, metric, params = {}) {
  return getDoraMetrics(PROJECTS_DORA_METRICS_PATH, projectId, metric, params);
}
 
/**
 * Gets DORA 4 metrics data from a group
 * See https://docs.gitlab.com/ee/api/dora/metrics.html#get-group-level-dora-metrics
 *
 * @param {String|Number} groupId The ID or path of the group
 * @param {String} metric The name of the metric to fetch. Must be one of:
 * `["deployment_frequency", "lead_time_for_changes"]`
 * @param {Object} params Any additional query parameters that should be
 * included with the request. These parameters are optional. See
 * https://docs.gitlab.com/ee/api/dora/metrics.html for a list of available options.
 *
 * @returns {Promise} A `Promise` that resolves to an array of data points.
 */
export function getGroupDoraMetrics(groupId, metric, params = {}) {
  return getDoraMetrics(GROUPS_DORA_METRICS_PATH, groupId, metric, params);
}