From 71c94e62f065ede48eda276664594dd89fecef4e Mon Sep 17 00:00:00 2001 From: Technote Date: Tue, 16 Mar 2021 18:53:08 +0900 Subject: [PATCH 1/2] chore: tweaks --- __tests__/utils/command.test.ts | 62 ++++++++++++++++++--------------- 1 file changed, 34 insertions(+), 28 deletions(-) diff --git a/__tests__/utils/command.test.ts b/__tests__/utils/command.test.ts index 3bcfaf23..0bd9baec 100644 --- a/__tests__/utils/command.test.ts +++ b/__tests__/utils/command.test.ts @@ -1,6 +1,6 @@ /* eslint-disable no-magic-numbers */ import nock from 'nock'; -import path, {resolve} from 'path'; +import path, { resolve } from 'path'; import { generateContext, testEnv, @@ -12,8 +12,8 @@ import { disableNetConnect, getApiFixture, } from '@technote-space/github-action-test-helper'; -import {Logger} from '@technote-space/github-action-log-helper'; -import {getGitDiff, getFileDiff, getDiffFiles, sumResults} from '../../src/utils/command'; +import { Logger } from '@technote-space/github-action-log-helper'; +import { getGitDiff, getFileDiff, getDiffFiles, sumResults } from '../../src/utils/command'; const rootDir = path.resolve(__dirname, '../..'); const fixtureRootDir = resolve(__dirname, '..', 'fixtures'); @@ -612,42 +612,48 @@ describe('getFileDiff', () => { describe('getDiffFiles', () => { testEnv(rootDir); - it('get git diff output 1', () => { - expect(getDiffFiles([], false)).toEqual(''); - expect(getDiffFiles([{file: 'test1', ...defaultFileResult}], false)).toEqual('test1'); - expect(getDiffFiles([{file: 'test1', ...defaultFileResult}, {file: 'test2', ...defaultFileResult}], false)).toEqual('test1 test2'); - expect(getDiffFiles([{file: 'test1', ...defaultFileResult}, {file: 'test2 test3', ...defaultFileResult}], false)).toEqual('test1 \'test2 test3\''); - expect(getDiffFiles([{file: 'test1/test2.txt', ...defaultFileResult}], false)).toEqual('\'test1/test2.txt\''); + it('should get git diff output 1', () => { + expect(getDiffFiles([], false)).toBe(''); + expect(getDiffFiles([{file: 'test1', ...defaultFileResult}], false)).toBe('test1'); + expect(getDiffFiles([{file: 'test1', ...defaultFileResult}, {file: 'test2', ...defaultFileResult}], false)).toBe('test1 test2'); + expect(getDiffFiles([{file: 'test1', ...defaultFileResult}, {file: 'test2 test3', ...defaultFileResult}], false)).toBe('test1 \'test2 test3\''); + expect(getDiffFiles([{file: 'test1/test2.txt', ...defaultFileResult}], false)).toBe('\'test1/test2.txt\''); }); - it('get git diff output 2', () => { + it('should get git diff output 2', () => { process.env.INPUT_SEPARATOR = '\n'; - expect(getDiffFiles([], false)).toEqual(''); - expect(getDiffFiles([{file: 'test1', ...defaultFileResult}], false)).toEqual('test1'); - expect(getDiffFiles([{file: 'test1', ...defaultFileResult}, {file: 'test2', ...defaultFileResult}], false)).toEqual('test1\ntest2'); - expect(getDiffFiles([{file: 'test1', ...defaultFileResult}, {file: 'test2 test3', ...defaultFileResult}], false)).toEqual('test1\n\'test2 test3\''); - expect(getDiffFiles([{file: 'test1/test2.txt', ...defaultFileResult}], false)).toEqual('\'test1/test2.txt\''); + expect(getDiffFiles([], false)).toBe(''); + expect(getDiffFiles([{file: 'test1', ...defaultFileResult}], false)).toBe('test1'); + expect(getDiffFiles([{file: 'test1', ...defaultFileResult}, {file: 'test2', ...defaultFileResult}], false)).toBe('test1\ntest2'); + expect(getDiffFiles([{file: 'test1', ...defaultFileResult}, {file: 'test2 test3', ...defaultFileResult}], false)).toBe('test1\n\'test2 test3\''); + expect(getDiffFiles([{file: 'test1/test2.txt', ...defaultFileResult}], false)).toBe('\'test1/test2.txt\''); }); - it('get git diff output 3', () => { + it('should get git diff output 3', () => { delete process.env.INPUT_SEPARATOR; process.env.INPUT_TEST = ''; - expect(getDiffFiles([], false)).toEqual(''); + expect(getDiffFiles([], false)).toBe(''); }); - it('get git diff output 4', () => { - expect(getDiffFiles([], true)).toEqual(''); - expect(getDiffFiles([{file: 'test1', ...defaultFileResult, isMatched: false}], true)).toEqual(''); - expect(getDiffFiles([{ - file: 'test1', ...defaultFileResult, - isMatched: false, - }, {file: 'test2', ...defaultFileResult}], true)).toEqual('test2'); - expect(getDiffFiles([{ - file: 'test1', ...defaultFileResult, - isMatched: false, - }, {file: 'test2 test3', ...defaultFileResult}], true)).toEqual('\'test2 test3\''); + it('should get git diff output 4', () => { + expect(getDiffFiles([], true)).toBe(''); + expect(getDiffFiles([{file: 'test1', ...defaultFileResult, isMatched: false}], true)).toBe(''); + expect(getDiffFiles([ + { + file: 'test1', ...defaultFileResult, + isMatched: false, + }, + {file: 'test2', ...defaultFileResult}, + ], true)).toBe('test2'); + expect(getDiffFiles([ + { + file: 'test1', ...defaultFileResult, + isMatched: false, + }, + {file: 'test2 test3', ...defaultFileResult}, + ], true)).toBe('\'test2 test3\''); }); }); From 76678df3fa4c6315edd20465be8591c3ebcc5e32 Mon Sep 17 00:00:00 2001 From: Technote Date: Tue, 16 Mar 2021 19:03:05 +0900 Subject: [PATCH 2/2] feat: add json format (#137) --- __tests__/utils/command.test.ts | 21 +++++++++++++++++++++ action.yml | 7 +++++++ src/utils/command.ts | 23 +++++++++++++---------- 3 files changed, 41 insertions(+), 10 deletions(-) diff --git a/__tests__/utils/command.test.ts b/__tests__/utils/command.test.ts index 0bd9baec..6c1d3de6 100644 --- a/__tests__/utils/command.test.ts +++ b/__tests__/utils/command.test.ts @@ -655,6 +655,27 @@ describe('getDiffFiles', () => { {file: 'test2 test3', ...defaultFileResult}, ], true)).toBe('\'test2 test3\''); }); + + it('should get git diff output (json format)', () => { + process.env.INPUT_FORMAT = 'json'; + + expect(getDiffFiles([], true)).toBe('[]'); + expect(getDiffFiles([{file: 'test1', ...defaultFileResult}], false)).toBe('["test1"]'); + expect(getDiffFiles([{file: 'test1', ...defaultFileResult}, {file: 'test2', ...defaultFileResult}], false)).toBe('["test1","test2"]'); + expect(getDiffFiles([{file: 'test1', ...defaultFileResult}, {file: 'test2 test3', ...defaultFileResult}], false)).toBe('["test1","test2 test3"]'); + expect(getDiffFiles([{file: 'test1/test2.txt', ...defaultFileResult}], false)).toBe('["test1/test2.txt"]'); + }); + + it('should get git diff output (escaped json format)', () => { + process.env.INPUT_FORMAT = 'json'; + process.env.INPUT_ESCAPE_JSON = '1'; + + expect(getDiffFiles([], true)).toBe('[]'); + expect(getDiffFiles([{file: 'test1', ...defaultFileResult}], false)).toBe('["test1"]'); + expect(getDiffFiles([{file: 'test1', ...defaultFileResult}, {file: 'test2', ...defaultFileResult}], false)).toBe('["test1","test2"]'); + expect(getDiffFiles([{file: 'test1', ...defaultFileResult}, {file: 'test2 test3', ...defaultFileResult}], false)).toBe('["test1","\'test2 test3\'"]'); + expect(getDiffFiles([{file: 'test1/test2.txt', ...defaultFileResult}], false)).toBe('["\'test1/test2.txt\'"]'); + }); }); describe('sumResults', () => { diff --git a/action.yml b/action.yml index 2d0f2ed7..3814d1bb 100644 --- a/action.yml +++ b/action.yml @@ -18,6 +18,13 @@ inputs: description: Diff filter. default: 'AMRC' required: true + FORMAT: + description: Output format (text or json) + default: text + required: true + ESCAPE_JSON: + description: Whether to escape when json format. + required: false SEPARATOR: description: Separator of diffs. default: ' ' diff --git a/src/utils/command.ts b/src/utils/command.ts index d1c7ffd3..b1b0bdd8 100644 --- a/src/utils/command.ts +++ b/src/utils/command.ts @@ -1,17 +1,19 @@ import path from 'path'; -import {getInput} from '@actions/core' ; -import {Context} from '@actions/github/lib/context'; -import multimatch, {Options} from 'multimatch'; -import {Command, Utils, GitHelper} from '@technote-space/github-action-helper'; -import {Logger} from '@technote-space/github-action-log-helper'; -import {escape, getDiffInfo} from './misc'; -import {FileDiffResult, FileResult, DiffResult, DiffInfo} from '../types'; -import {REMOTE_NAME} from '../constant'; +import { getInput } from '@actions/core' ; +import { Context } from '@actions/github/lib/context'; +import multimatch, { Options } from 'multimatch'; +import { Command, Utils, GitHelper } from '@technote-space/github-action-helper'; +import { Logger } from '@technote-space/github-action-log-helper'; +import { escape, getDiffInfo } from './misc'; +import { FileDiffResult, FileResult, DiffResult, DiffInfo } from '../types'; +import { REMOTE_NAME } from '../constant'; const command = new Command(new Logger()); const getRawInput = (name: string): string => process.env[`INPUT_${name.replace(/ /g, '_').toUpperCase()}`] || ''; const getDot = (): string => getInput('DOT', {required: true}); const getFilter = (): string => getInput('DIFF_FILTER', {required: true}); +const getOutputFormatType = (): string => getRawInput('FORMAT'); +const escapeWhenJsonFormat = (): boolean => Utils.getBoolValue(getRawInput('ESCAPE_JSON')); const getSeparator = (): string => getRawInput('SEPARATOR'); const getPatterns = (): string[] => Utils.getArrayInput('PATTERNS', undefined, ''); const getFiles = (): string[] => Utils.getArrayInput('FILES', undefined, ''); @@ -110,8 +112,9 @@ export const getGitDiff = async(logger: Logger, context: Context): Promise ({...item, file: toAbsolute(item.file, workspace)})); }; -export const getDiffFiles = (diffs: FileResult[], filter: boolean): string => escape(diffs.filter(item => !filter || item.isMatched).map(item => item.file)).join(getSeparator()); -export const getMatchedFiles = (diffs: FileResult[]): string => escape(diffs.filter(item => item.filterIgnored).map(item => item.file)).join(getSeparator()); +const format = (items: string[]): string => getOutputFormatType() !== 'text' ? JSON.stringify(escapeWhenJsonFormat() ? escape(items) : items) : escape(items).join(getSeparator()); +export const getDiffFiles = (diffs: FileResult[], filter: boolean): string => format(diffs.filter(item => !filter || item.isMatched).map(item => item.file)); +export const getMatchedFiles = (diffs: FileResult[]): string => format(diffs.filter(item => item.filterIgnored).map(item => item.file)); export const sumResults = (diffs: DiffResult[], map: (item: DiffResult) => number): number => getSummaryIncludeFilesFlag() ? diffs.map(map).reduce((acc, val) => acc + val, 0) : // eslint-disable-line no-magic-numbers diffs.filter(item => !item.filterIgnored).map(map).reduce((acc, val) => acc + val, 0); // eslint-disable-line no-magic-numbers