All files / plugins gitlab-ci-badges.js

45.45% Statements 50/110
100% Branches 1/1
0% Functions 0/4
45.45% Lines 50/110

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 1111x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x       1x 1x 1x 1x 1x 1x 1x 1x                                         1x 1x 1x 1x 1x 1x 1x                                                     1x 1x 1x 1x 1x 1x 1x                       1x 1x  
//@ts-check
import { clone, find, get, has, some, startsWith, upperFirst } from 'lodash-es';
import * as utils from '../src/utils.js';
import axios from 'axios';
import ciFetch from '../src/ci-fetch.js';
 
/**
 * @typedef {{ link_url: string, image_url: string }} CIBadge
 * @typedef {import('@gitbeaker/core').ProjectBadgeSchema} ProjectBadgeSchema
 */
 
const serviceName = upperFirst('gitlab-ci-badges');
 
const pipeline_badge = {
  image_url: 'https://gitlab.cern.ch/%{project_path}/badges/%{default_branch}/pipeline.svg',
  link_url: 'https://gitlab.cern.ch/%{project_path}/pipelines'
};
const coverage_badge = {
  link_url: 'https://apc-dev.web.cern.ch/ci/%{project_path}/coverage',
  image_url: 'https://gitlab.cern.ch/%{project_path}/badges/%{default_branch}/coverage.svg'
};
 
/**
 * @param  {string} url
 * @param  {any} placeholders
 */
async function checkLink(url, placeholders /*: {} */) {
  return axios.get(utils.format(url, placeholders, false));
}
 
/**
 * @param  {GitConfig.Env} env
 * @param  {GitConfig.$ProjectSchema} project
 * @param  {GitConfig.$BadgeSchema[]} badges
 * @param  {CIBadge} ci_badge
 * @param  {string} name
  */
async function update_badge(env, project, badges, ci_badge, name) {
  const current = find(badges, { image_url: ci_badge.image_url });
  if (current && (current.link_url === ci_badge.link_url)) {
    env.spin.debug('%s %s already displayed: %s', serviceName, name, project.name);
  }
  else if (env.opts.dryRun) {
    env.spin.warn('%s %s update required: %s', serviceName, name, project.name);
  }
  else {
    if (current) {
      await env.gitlab.ProjectBadges.edit(project.id, current.id,
        { linkUrl: ci_badge.link_url, imageUrl: ci_badge.image_url, name });
    }
    else {
      await env.gitlab.ProjectBadges.add(project.id, ci_badge.link_url,
        ci_badge.image_url, { name });
    }
    env.spin.succeed('%s %s updated: %s', serviceName, name, project.name);
  }
}
 
/**
 * @param {GitConfig.Env} env
 * @param {GitConfig.$ProjectSchema} project
 * @param  {GitConfig.Settings} settings
 * @param {{[key:string]: any}} gitlabCi
 */
async function checkBadges(env, project, settings, gitlabCi) {
  env.spin.start(`Fetching pipeline-badge config: ${project.name}`);
  const badges = /** @type {ProjectBadgeSchema[]} */ (await env.gitlab.ProjectBadges.all(project.id));
  await update_badge(env, project, badges, pipeline_badge, 'pipeline-badge');

  if (some(gitlabCi, (/** @type {string} */ job, /** @type {any} */ name) => (!startsWith(name, '.') && has(job, 'coverage')))) {
    const badge = clone(coverage_badge);
    const placeholders = {
      project_path: project.path_with_namespace,
      project_id: project.id,
      commit_sha: project.default_branch,
      default_branch: project.default_branch
    };
    badge.link_url = get(settings, [ 'config', 'coverage_link_url' ], badge.link_url);

    await checkLink(badge.link_url, placeholders)
    .catch(() => {
      env.spin.debug('%s using default coverage url: %s', serviceName, project.name);
      badge.link_url = 'https://gitlab.cern.ch/%{project_path}';
    });
    await update_badge(env, project, badges, badge, 'coverage-badge');
  }
  else {
    env.spin.debug(`${serviceName} no coverage info: ${project.name}`);
  }
}
 
/**
 * @param  {GitConfig.Env} env
 * @param  {GitConfig.$ProjectSchema} project
 * @param  {GitConfig.Settings} settings
 */
export async function update(env, project, settings) {
  if (utils.isIgnored(env, project, settings, serviceName)) {
    return;
  }

  env.spin.start(`Fetching gitlab-ci file: ${project.name}`);

  await ciFetch(env, project)
  .then(
    (gitlabCi) => checkBadges(env, project, settings, gitlabCi),
    () => env.spin.debug('%s no gitlab-ci file: %s', serviceName, project.name));
}
 
export default update;