Skip to content

Commit

Permalink
Add stat format option
Browse files Browse the repository at this point in the history
  • Loading branch information
lykahb committed Mar 11, 2022
1 parent afc1a84 commit 62660e2
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 28 deletions.
46 changes: 36 additions & 10 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4732,18 +4732,22 @@ async function run() {
const base = core.getInput('base', { required: false });
const filtersInput = core.getInput('filters', { required: true });
const filtersYaml = isPathInput(filtersInput) ? getConfigFileContent(filtersInput) : filtersInput;
const listFiles = core.getInput('list-files', { required: false }).toLowerCase() || 'none';
const stat = core.getInput('stat', { required: false }).toLowerCase() || 'none';
const listFilesFormat = core.getInput('list-files', { required: false }).toLowerCase() || 'none';
const statFormat = core.getInput('stat', { required: false }).toLowerCase() || 'none';
const initialFetchDepth = parseInt(core.getInput('initial-fetch-depth', { required: false })) || 10;
if (!isExportFormat(listFiles)) {
core.setFailed(`Input parameter 'list-files' is set to invalid value '${listFiles}'`);
if (!isFilesExportFormat(listFilesFormat)) {
core.setFailed(`Input parameter 'list-files' is set to invalid value '${listFilesFormat}'`);
return;
}
if (!isStatExportFormat(statFormat)) {
core.setFailed(`Input parameter 'stat' is set to invalid value '${statFormat}'`);
return;
}
const filter = new filter_1.Filter(filtersYaml);
const files = await getChangedFiles(token, base, ref, initialFetchDepth);
core.info(`Detected ${files.length} changed files`);
const results = filter.match(files);
exportResults(results, listFiles);
exportResults(results, listFilesFormat, statFormat);
}
catch (error) {
core.setFailed(error.message);
Expand Down Expand Up @@ -4904,9 +4908,10 @@ async function getChangedFilesFromApi(token, prNumber) {
core.endGroup();
}
}
function exportResults(results, format) {
function exportResults(results, filesFormat, statFormat) {
core.info('Results:');
const changes = [];
const changeStats = {};
for (const [key, files] of Object.entries(results)) {
const hasMatchingFiles = files.length > 0;
core.startGroup(`Filter ${key} = ${hasMatchingFiles}`);
Expand All @@ -4922,15 +4927,18 @@ function exportResults(results, format) {
}
core.setOutput(key, hasMatchingFiles);
core.setOutput(`${key}_count`, files.length);
if (format !== 'none') {
const filesValue = serializeExport(files, format);
if (filesFormat !== 'none') {
const filesValue = serializeExportChangedFiles(files, filesFormat);
core.setOutput(`${key}_files`, filesValue);
}
const additionCount = files.reduce((sum, f) => sum + f.additions, 0);
const deletionCount = files.reduce((sum, f) => sum + f.deletions, 0);
core.setOutput(`${key}_addition_count`, additionCount);
core.setOutput(`${key}_deletion_count`, deletionCount);
core.setOutput(`${key}_change_count`, additionCount + deletionCount);
changeStats[key] = {
additionCount, deletionCount, fileCount: files.length
};
core.endGroup();
}
if (results['changes'] === undefined) {
Expand All @@ -4941,8 +4949,12 @@ function exportResults(results, format) {
else {
core.info('Cannot set changes output variable - name already used by filter output');
}
if (statFormat !== 'none') {
const statValue = serializeExportStat(changeStats, statFormat);
core.setOutput(`stat`, statValue);
}
}
function serializeExport(files, format) {
function serializeExportChangedFiles(files, format) {
const fileNames = files.map(file => file.filename);
switch (format) {
case 'csv':
Expand All @@ -4957,7 +4969,21 @@ function serializeExport(files, format) {
return '';
}
}
function isExportFormat(value) {
function serializeExportStat(stat, format) {
switch (format) {
case 'csv':
return Object.keys(stat).sort().map(k => [csv_escape_1.csvEscape(k), stat[k].additionCount, stat[k].deletionCount, stat[k].fileCount]
.join(',')).join('\n');
case 'json':
return JSON.stringify(stat);
default:
return '';
}
}
function isFilesExportFormat(value) {
return ['none', 'csv', 'shell', 'json', 'escape'].includes(value);
}
function isStatExportFormat(value) {
return ['none', 'csv', 'shell', 'json', 'escape'].includes(value);
}
run();
Expand Down
42 changes: 24 additions & 18 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import * as git from './git'
import {backslashEscape, shellEscape} from './list-format/shell-escape'
import {csvEscape} from './list-format/csv-escape'

type ExportFormat = 'none' | 'csv' | 'json' | 'shell' | 'escape'
type FilesExportFormat = 'none' | 'csv' | 'json' | 'shell' | 'escape'
type StatExportFormat = 'none' | 'csv' | 'json'

async function run(): Promise<void> {
try {
Expand All @@ -23,20 +24,25 @@ async function run(): Promise<void> {
const base = core.getInput('base', {required: false})
const filtersInput = core.getInput('filters', {required: true})
const filtersYaml = isPathInput(filtersInput) ? getConfigFileContent(filtersInput) : filtersInput
const listFiles = core.getInput('list-files', {required: false}).toLowerCase() || 'none'
const stat = core.getInput('stat', {required: false}).toLowerCase() || 'none'
const listFilesFormat = core.getInput('list-files', {required: false}).toLowerCase() || 'none'
const statFormat = core.getInput('stat', {required: false}).toLowerCase() || 'none'
const initialFetchDepth = parseInt(core.getInput('initial-fetch-depth', {required: false})) || 10

if (!isExportFormat(listFiles)) {
core.setFailed(`Input parameter 'list-files' is set to invalid value '${listFiles}'`)
if (!isFilesExportFormat(listFilesFormat)) {
core.setFailed(`Input parameter 'list-files' is set to invalid value '${listFilesFormat}'`)
return
}

if (!isStatExportFormat(statFormat)) {
core.setFailed(`Input parameter 'stat' is set to invalid value '${statFormat}'`)
return
}

const filter = new Filter(filtersYaml)
const files = await getChangedFiles(token, base, ref, initialFetchDepth)
core.info(`Detected ${files.length} changed files`)
const results = filter.match(files)
exportResults(results, listFiles)
exportResults(results, listFilesFormat, statFormat)
} catch (error) {
core.setFailed(error.message)
}
Expand Down Expand Up @@ -231,7 +237,7 @@ interface Stat {
fileCount: number
}

function exportResults(results: FilterResults, format: ExportFormat): void {
function exportResults(results: FilterResults, filesFormat: FilesExportFormat, statFormat: StatExportFormat): void {
core.info('Results:')
const changes: string[] = []
const changeStats: {[key: string]: Stat} = {}
Expand All @@ -251,8 +257,8 @@ function exportResults(results: FilterResults, format: ExportFormat): void {

core.setOutput(key, hasMatchingFiles)
core.setOutput(`${key}_count`, files.length)
if (format !== 'none') {
const filesValue = serializeExportChangedFiles(files, format)
if (filesFormat !== 'none') {
const filesValue = serializeExportChangedFiles(files, filesFormat)
core.setOutput(`${key}_files`, filesValue)
}

Expand All @@ -276,13 +282,13 @@ function exportResults(results: FilterResults, format: ExportFormat): void {
core.info('Cannot set changes output variable - name already used by filter output')
}

if (format !== 'none') {
const statValue = serializeExportStat(changeStats, format)
if (statFormat !== 'none') {
const statValue = serializeExportStat(changeStats, statFormat)
core.setOutput(`stat`, statValue)
}
}

function serializeExportChangedFiles(files: File[], format: ExportFormat): string {
function serializeExportChangedFiles(files: File[], format: FilesExportFormat): string {
const fileNames = files.map(file => file.filename)
switch (format) {
case 'csv':
Expand All @@ -298,7 +304,7 @@ function serializeExportChangedFiles(files: File[], format: ExportFormat): strin
}
}

function serializeExportStat(stat: {[key: string]: Stat}, format: ExportFormat): string {
function serializeExportStat(stat: {[key: string]: Stat}, format: StatExportFormat): string {
switch (format) {
case 'csv':
return Object.keys(stat).sort().map(k =>
Expand All @@ -307,17 +313,17 @@ function serializeExportStat(stat: {[key: string]: Stat}, format: ExportFormat):
).join('\n')
case 'json':
return JSON.stringify(stat)
case 'escape':
return ''
case 'shell':
return ''
default:
return ''
}
}

function isExportFormat(value: string): value is ExportFormat {
function isFilesExportFormat(value: string): value is FilesExportFormat {
return ['none', 'csv', 'shell', 'json', 'escape'].includes(value)
}

function isStatExportFormat(value: string): value is StatExportFormat {
return ['none', 'csv', 'json'].includes(value)
}

run()

0 comments on commit 62660e2

Please sign in to comment.