All files / libs/tools/plugin/src/executors/support-weather gitlabApiCalls.ts

0% Statements 0/63
0% Branches 0/1
0% Functions 0/1
0% Lines 0/63

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                                                                                                                               
import { assert } from '@amalia/ext/typescript';

export enum JobsToWatch {
  e2e = 'e2e:mocked',
  sonar = 'sonar',
  build = 'build',
}

const JobsToWatchList = Object.values(JobsToWatch);

const nightlyPipelineScheduleId = 139_896;
const gitlabProjectId = 11_689_534;

export const getLastPipelineStatus = async () => {
  const token = process.env['GITLAB_API_TOKEN'];
  assert(token, 'Gitlab API Token missing');

  const nightlyPipelineSchedule = (await fetch(
    `https://gitlab.com/api/v4/projects/${gitlabProjectId}/pipeline_schedules/${nightlyPipelineScheduleId}`,
    {
      headers: { Authorization: `Bearer ${token}` },
    },
  ).then((response) => response.json())) as {
    last_pipeline: {
      id: string;
    };
  };

  const lastNightlyPipelineId = nightlyPipelineSchedule.last_pipeline.id;

  const lastNightlyPipeline = (await fetch(
    `https://gitlab.com/api/v4/projects/${gitlabProjectId}/pipelines/${lastNightlyPipelineId}/jobs`,
    {
      headers: { Authorization: `Bearer ${token}` },
    },
  ).then((response) => response.json())) as { name: string; status: string }[];

  const jobsStatus = lastNightlyPipeline
    .filter((job): job is { name: JobsToWatch; status: string } => JobsToWatchList.includes(job.name))
    .map((job) => ({
      name: job.name,
      status: job.status,
    }));

  return jobsStatus.reduce(
    (acc, job) => {
      acc[job.name] = job.status;
      return acc;
    },
    {} as Record<JobsToWatch, string>,
  );
};

export const getPipelineStatusEmoji = (status: string) => {
  switch (status) {
    case 'success':
      return ':pipeline-success:';
    case 'failed':
      return ':pipeline-failed:';
    default:
      return '';
  }
};