All files / libs/vendors/gitlab/src/lib gitlab.ts

90.62% Statements 58/64
85.71% Branches 6/7
100% Functions 3/3
90.62% Lines 58/64

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 651x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 2x 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  
import { type Gitlab as TGitlab } from '@gitbeaker/core';
import * as env from 'env-var';
 
import { commitExists, formatSha } from '@amalia/vendors/git';
 
import { debug } from './debug';
 
export const getMergeRequestBaseSha = () => {
  const CI_MERGE_REQUEST_DIFF_BASE_SHA = formatSha(env.get('CI_MERGE_REQUEST_DIFF_BASE_SHA').default('').asString());
 
  debug('CI_MERGE_REQUEST_DIFF_BASE_SHA: %s', CI_MERGE_REQUEST_DIFF_BASE_SHA);
 
  return CI_MERGE_REQUEST_DIFF_BASE_SHA;
};
 
export const findLastSuccessfulPipelineCommit = async <T extends boolean>(
  client: TGitlab<T>,
  {
    projectId = env.get('CI_PROJECT_ID').required().asString(),
    ref = env.get('CI_COMMIT_REF_NAME').required().asString(),
    head = env.get('CI_COMMIT_SHA').required().asString(),
    depth = 20,
  } = {},
): Promise<string> => {
  debug('projectId: %s, ref: %s, head: %s, depth: %d', projectId, ref, head, depth);
 
  const runs = await client.Pipelines.all(projectId, {
    ref,
    status: 'success',
    /* don't include scheduled renovate pipelines */
    source: 'push',
    orderBy: 'id',
    sort: 'desc',
    // max 100 per page
    perPage: Math.min(depth, 100),
    maxPages: Math.ceil(depth / 100),
  }).catch((error: Error) => {
    debug('Error fetching pipelines: %s', error.message);
    return [];
  });
 
  let shas = runs.map(({ sha }) => sha);
 
  debug('Found %d successful pipeline runs: %s', shas.length, shas.join(', '));
 
  if (shas.includes(head)) {
    debug('Removing HEAD %s from successful pipeline shas', head);
    shas = shas.filter((sha) => sha !== head);
  }
 
  // find the first sha that isn't HEAD and that still exists in the repo.
  return shas.find((sha) => commitExists(sha)) ?? '';
};
 
export const getAllProjectBadges = async <T extends boolean>(
  client: TGitlab<T>,
  { projectId = env.get('CI_PROJECT_ID').required().asString() }: { projectId?: number | string } = {},
) => {
  debug('projectId : %s', projectId);
  return client.ProjectBadges.all(projectId).catch((error: Error) => {
    debug('Error fetching badges: %s', error.message);
    return [];
  });
};