All files / src utils.js

43.66% Statements 62/142
100% Branches 1/1
0% Functions 0/7
43.66% Lines 62/142

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 1441x 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 1x                     1x 1x 1x 1x 1x 1x                                           1x 1x 1x 1x 1x                          
//@ts-check
 
import { get, isMatchWith, isNil, isObject, noop, partial, replace, some } from 'lodash-es';
import { AccessLevel } from './Consts.js';
import path from 'node:path';
import fs from 'node:fs/promises';
import yaml from 'js-yaml';
 
/**
 * @typedef {import('@gitbeaker/rest')['Gitlab']} Gitlab
 */
 
/**
 * @param  {any} object
 * @param  {any} source
 * @return {boolean}
 */
function deepMatch(object, source) {
  return isMatchWith(object, source, function(objValue, srcValue) {
    if (isObject(objValue) && isObject(srcValue)) {
      return deepMatch(objValue, srcValue);
    }
  });
}
 
/**
 * format a string
 * @details replaces any "%{words}" from it's value repl map.
 * @param {string} string
 * @param {{ [key:string]: any }} repl
 * @param  {boolean} keep  activate to strip unknown "{words}"
 */
export function format(string, repl, keep) {
  return replace(string, /%{([^\}]*)}/g, function(match, key) {
    return get(repl, key, keep ? match : '');
  });
}
 
/**
 * @param  {GitConfig.Env}  env
 * @param  {GitConfig.$ProjectSchema}  project
 * @param  {GitConfig.Filter}  config
 * @param  {string}  name
 */
export function isIgnored(env, project, config, name) {
  const excludeAll = get(config, 'excludeAll', false);
  const include = some(config.include, partial(deepMatch, project));
  const exclude = some(config.exclude, partial(deepMatch, project));

  if (excludeAll) {
    if (include && !exclude) {
      env.spin.debug(`${name} force-include: ${project.name}`);
      return false;
    }
    env.spin.debug(`${name} skipping: ${project.name}`);
    return true;
  }
  else {
    if (include) {
      env.spin.debug(`${name} force-include: ${project.name}`);
      return false;
    }
    else if (exclude) {
      env.spin.debug(`${name} skipping: ${project.name}`);
      return true;
    }
    return false;
  }
}
 
/**
 * @template [T=GitConfig.$AccessLevel]
 * @param  {string|T} name
 * @return {T}
 */
export function getAccessLevel(name) {
  // @ts-ignore
  return get(AccessLevel, name, name);
}
 
/** @type {{ [key:string]: number }} */
const userMap = {};
 
/**
 * @param {InstanceType<Gitlab>} gitlab
 * @param {string} name
 * @return {Promise<number>}
 */
export async function fetchUserId(gitlab, name) {
  if (isNil(name)) {
    /* $FlowIgnore */
    return name;
  }
  if (!userMap[name]) {
    const user = await gitlab.Users.all({ username: name });
    userMap[name] = get(user, [ 0, 'id' ]);
  }
  return userMap[name];
}
 
/**
 *
 * @param {string} filename
 * @returns {Promise<string|null>}
 */
async function resolveFile(filename) {
  try {
    await fs.access(filename, fs.constants.R_OK);
    return filename;
  }
  catch {
    noop();
  }

  for (const ext of [ '.js', '.json', '.yaml', '.yml' ]) {
    try {
      const out = filename + ext;
      await fs.access(out, fs.constants.R_OK);
      return out;
    }
    catch {
      continue;
    }
  }
  return null;
}
 
/**
 * @param {string} filename
 */
export async function loadFile(filename) {
  const file = await resolveFile(path.resolve(filename));
  if (!file) {
    throw new Error(`No such file: ${filename}`);
  }
  else if (file.endsWith('.js')) {
    return (await import(file))?.default;
  }
  else {
    return yaml.load(await fs.readFile(file, 'utf8'));
  }
}