All files / ts/actions/codeowners codeowners.ts

0% Statements 0/24
0% Branches 0/4
0% Functions 0/5
0% Lines 0/22

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                                                                                                     
import { setFailed, setOutput, summary } from '@actions/core';
import { readFileSync } from 'fs';
 
import { logger } from '../../utils/logger';
 
const codeownersConfig = `${process.cwd()}/.github/CODEOWNERS`;
 
const codeownersFileContent = readFileSync(codeownersConfig, 'utf8') as NodeJS.ErrnoException | string;
if (typeof codeownersFileContent !== 'string') {
  logger.printError(codeownersFileContent);
 
  process.exit(1);
}
 
const getCodeowners = (config: string) => {
  const owners = config.match(/@[a-z0-9-]+/g);
  return (owners ?? []).map(item => item.replace(/^@/, ''));
};
 
const codeowners = getCodeowners(codeownersFileContent);
 
const codeownersSummary: Array<[{ data: string }, { data: string }]> = codeowners.reduce(
  (accumulator: Array<[{ data: string }, { data: string }]>, item) => {
    const changeSummary: [{ data: string }, { data: string }] = [{ data: item }, { data: `https://github.com/${item}` }];
    accumulator.unshift(changeSummary);
    return accumulator;
  },
  [],
);
 
logger.printInfo(codeowners, 'codeowners');
 
(async () => {
  const headingLevel = 3;
  summary.addHeading('📋 Changes', headingLevel);
  summary.addTable([
    [
      { data: 'Username', header: true },
      { data: 'Link', header: true },
    ],
    ...codeownersSummary,
  ]);
  summary.addBreak();
  await summary.write();
 
  setOutput('matrix', codeowners);
})().catch(error => {
  logger.printError(error, 'Error writing action output');
  setFailed('Something went wrong. Failed writing GitHub action summary.');
});