From 29b4d7ace353ed21aa6967c7d39e3e1f830393d8 Mon Sep 17 00:00:00 2001 From: Renato Vicente Date: Wed, 8 May 2024 20:43:51 +0200 Subject: [PATCH 01/12] feat: changing to use config file --- .github/workflows/auto-label.json | 26 ++++++++++++++++++++++++++ .github/workflows/auto-label.yml | 17 +++++++++++++++++ src/domain/getConfigFile.ts | 16 ++++++++++++++-- src/domain/getJsonFile.ts | 24 ++++++++++++++++++++++++ 4 files changed, 81 insertions(+), 2 deletions(-) create mode 100644 .github/workflows/auto-label.json create mode 100644 .github/workflows/auto-label.yml create mode 100644 src/domain/getJsonFile.ts diff --git a/.github/workflows/auto-label.json b/.github/workflows/auto-label.json new file mode 100644 index 0000000..b927962 --- /dev/null +++ b/.github/workflows/auto-label.json @@ -0,0 +1,26 @@ +{ + "labels-synonyms": { + "bug": [ + "error", + "need fix", + "not working" + ], + "enhancement": [ + "upgrade" + ], + "question": [ + "help", + "how can i" + ] + }, + "labels-not-allowed": [ + "documentation", + "duplicate", + "good first issue", + "help wanted", + "invalid" + ], + "default-labels": [ + "triage" + ] +} \ No newline at end of file diff --git a/.github/workflows/auto-label.yml b/.github/workflows/auto-label.yml new file mode 100644 index 0000000..5e2994f --- /dev/null +++ b/.github/workflows/auto-label.yml @@ -0,0 +1,17 @@ +name: Labeling new issue + +on: + issues: + types: ['opened'] +jobs: + build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + with: + sparse-checkout: | + .github/workflows/auto-label.json + sparse-checkout-cone-mode: false + - uses: Renato66/auto-label@main + with: + repo-token: ${{ secrets.GITHUB_TOKEN }} diff --git a/src/domain/getConfigFile.ts b/src/domain/getConfigFile.ts index dacf40f..0f9fbb5 100644 --- a/src/domain/getConfigFile.ts +++ b/src/domain/getConfigFile.ts @@ -1,6 +1,16 @@ import { getInput } from './getInput' +import { getLabelConfigs } from './getJsonFile' -export const getConfigFile = () => { +export type Config = { + labelsNotAllowed: string[]; + defaultLabels: string[]; + labelsSynonyms: Record; + ignoreComments: boolean; +} + +export const getConfigFile = (): Config => { + + const configPath = getInput('configuration-file', '.github/workflows/auto-label.json') const labelsNotAllowed = getInput('labels-not-allowed', []) const defaultLabels = getInput('default-labels', []) const labelsSynonyms = getInput>( @@ -8,11 +18,13 @@ export const getConfigFile = () => { {} ) const ignoreComments = getInput('ignore-comments', true) + const config = getLabelConfigs(configPath) return { labelsNotAllowed, defaultLabels, labelsSynonyms, - ignoreComments + ignoreComments, + ...config } } diff --git a/src/domain/getJsonFile.ts b/src/domain/getJsonFile.ts new file mode 100644 index 0000000..239c678 --- /dev/null +++ b/src/domain/getJsonFile.ts @@ -0,0 +1,24 @@ +import fs from 'fs' +import type { Config } from './getConfigFile' +import * as core from '@actions/core' + +export const getLabelConfigs = (configurationPath: string): Config | {} => { + if (fs.existsSync(configurationPath)) return {} + const fileContent = fs.readFileSync(configurationPath, { + encoding: 'utf8' + }) + + try { + const config = + JSON.parse(fileContent) + return { + defaultLabels: Array.isArray(config.defaultLabels) ? config.defaultLabels : undefined, + labelsNotAllowed: Array.isArray(config.labelsNotAllowed) ? config.labelsNotAllowed : undefined, + ignoreComments: typeof config.ignoreComments === 'boolean' ? config.ignoreComments : undefined, + labelsSynonyms: typeof config.labelsSynonyms === 'object' && !Array.isArray(config.labelsSynonyms) ? config.labelsSynonyms : undefined + } + } catch { + core.warning('Could not parse configuration file. skipping') + return {} + } +} From 1e52f26f258efea70ce6c7413e70bad67665f0eb Mon Sep 17 00:00:00 2001 From: Renato Vicente Date: Wed, 8 May 2024 20:52:23 +0200 Subject: [PATCH 02/12] feat: using json5 to read manual config files --- .../{auto-label.json => auto-label.json5} | 0 bun.lockb | Bin 14146 -> 14494 bytes package.json | 3 ++- src/domain/getJsonFile.ts | 3 ++- 4 files changed, 4 insertions(+), 2 deletions(-) rename .github/workflows/{auto-label.json => auto-label.json5} (100%) diff --git a/.github/workflows/auto-label.json b/.github/workflows/auto-label.json5 similarity index 100% rename from .github/workflows/auto-label.json rename to .github/workflows/auto-label.json5 diff --git a/bun.lockb b/bun.lockb index a123601565b448fdf854363f126c62570b7e15df..de008c440d1e9f30cfff75ba1f66801ae35bc1e0 100755 GIT binary patch delta 2530 zcmb_eYiv|S6uz_D?e5)j+itsU_tlotc6a;Qwp*6AAbT;fK)Y0^sEZ(#P(@n{SP-Zf zX$g{05*p!vfFS&kXblgewV|ja0!k?;gs7wljlo9(O$ZOg0Eyst_MuHg@dqb4_nU7X zXJ*dKIdcy$*wONaC-1zsIote2-I*PW25!h#dUjTA+E;z^x5^DI#qV6LF)aNlzCsE~ z?)qTnbW?7agug~fYHwP+aDflB0Ga@P4Tw(deLw}6rI92vFcD}1whKv$2gYe7DHV8o z{h_pndqLtrhR~P@+!Yh<{C$M?0h3YxEsUlEyMb(U$%1Da8yY2PDp^Ij(Poe&8MS%? z%@i3GD#}bdBe)94ljyMQ5kP>%JJ|fAZPPE$uURs7(cJp+mfg#`yTlx!djzdsQewu5 zr6g-rO{$<8tzUFf2Z&EY$3a$;9HWX;R2`$rS=ho9STmDJTQ1KB3-DZYT3arTkgQY1 zuT-s5HLe(1tMiNJs2^25B*&`q@7S%1&U&iTmCHVzBvpaMupzOW`qASX)J;O2hEB(p zYizMpqxXw?>d>oV8};LRiev-UsHYl(Upz(~2372!etgf6EUR)hJTo>t9*dg|_9$3* zal5HsR>dI6MpYxrRAcmu$<$#~#b)Zq_XNpts>Wobnz)eVxN&V5F0ryOV)^V)2`9`) zE9s#rP?q6{L*ye52_t+YS)p8SgR%@qwwnQEyCb38KUZ$7CF5(%O-`iF9@f6)sUY=0j@aC)gKT2PD1w^5o`2Pr5Ib>@S~n{fVP(dj>xDm9;KTZA;yAoc6|BXnuS^Xz4(FkX}eo zC_Nz{bkvv-Buk<~UxVq%ni!;GV6PLbWLYkw#lnf;2f* zp+CT^RGk{6n_zoW1Hw+Xz`D{Dnw=I9>GXbDkY-pEQY-g?+$;NrDc!T)e2`zy4fVW6u|Jk9OsYXIaL9-&rzMSU52eP7&Mme3FYG{&kPOK>N zcs)Ma>#Vc?b&`<>NW=RKNjKefT77*e^I>W~m}g#=yV0l-^*&FD2MH+p9`4*a=Ekn? z+@GPP5-m7@YgFs9igr4fXBB<466HSH=CazOFKS|I%ZKk)WmaKCd3eMSokFkMblJ`0 zUUXYEi5kjqTYS+MTt3pX@7&WrpTLkY;UUpCe1j;S(scDw23owB9rK3_h+bOluC+%W z^P}#QE>mz-@863YHktk3X9?N*kM<#2h)v$F*DzPm)x6R6skj*Yzw*go(~69bTQ>39 z!@KGxHNRGapNP(Ui^gW4u6(ah>0G|m9{qz@Up#Z2vuAvzMy$YoOG`avos>{u_5ZUy z`szggIpl>89UZEB>r;&MdAwshqordUNs!4!G_PO+brqC|iFCF=wN1nY#AR)Ny~6jE vXpO(q${_`btsrmV9J#r1ZgE3%qo;{ViyYx(P+U|Tx=M7xrX@j& z(F9CG5D$d5MhuZCnphvT0YQk-gz&KV2NH_`5+jjnF#3ap{s8^X-L5o7@fVYv{qA?> z%$b=pbI$C+&0lYPBVK)O&xYS7cjXMfn|yrPg%evIyXCt!_u9FhDQ`Wsv+k&8d(CH! z!fF+J&9d%-iZn_8dWF~#!>1281dITCfGY616ruol5ts*@3(Nt2XA`0bcnX}cedDS% z?5*>~6`M9~6=IuRh%D#_(4Pxj=SYt~$Q-lp=)s2{*o#3Wz)m1f)Uk1O+p0G7QCKEg zueya$pgj*{>(y&kcC3ZvhAsF(!wA94x5J3-TE=lckf%Qj%m%{7vODl&aR2qSyE+Ds z$>mbcq?BE^T^7`DPs$b=0U47NbR?Y%vArj##6C+B^*MCqI`|^^DWDbhgi?pi8DLHd zI}&myrJTAvN_|e<_N$#PJCm}SG?y;7Qp%+($FXxA#%?;~OekYu^S~VJNG3^h>&gMA z5c8q4QP`cZU3F5wJ1NU)#I4I^qc(&ZjXW$Cs{3iW4Mp0j3k!&CGj$Ss5X{eZ&J&AzSp{7*a{P&oKx z(Ks$vJWB2x4@%{{L=m4s0iQ}Q`wVHPvp$o~fKAOcq?5Mhn$(r6(iJc_Rp*&hm8a6~ zJVPop0`@)F?0iFJ(d+pp?aEi_CYVZ1ev@YSReIZRNFR-Y-2iJYFk~L}6`0gppwb;M zKP@aYX+fb%9~Bz1kjB7ngFRbh$YT1S$fP4hDyhYW)F@SKQfskFC&5Cb1WfV-RN4?Q zWC?u%b_}dUGh`XHX(p}HR2l{glNK~75XAn1hMYoYgR2JTlpdCpYj@;zbhNGJa1FcC zRT0_i@>6z6wxB+sbzr?S%>oMUuh0f2JF&$|1eu-IF5}kCsz! zB+xV!YQD%dkQjt7c@+eS%teh0mxx2ALHNMf18THTX*3dw^5x|;;T%IH7R3&0L;pS7-#+hSGeCPMZs9(bR>%#ejua{8Ag{U>165U+2HKP--Lo}g9L(yrP$ICnZzJ6(EdtX2 { if (fs.existsSync(configurationPath)) return {} @@ -10,7 +11,7 @@ export const getLabelConfigs = (configurationPath: string): Config | {} => { try { const config = - JSON.parse(fileContent) + JSON5.parse(fileContent) return { defaultLabels: Array.isArray(config.defaultLabels) ? config.defaultLabels : undefined, labelsNotAllowed: Array.isArray(config.labelsNotAllowed) ? config.labelsNotAllowed : undefined, From a528db7846f4a9b534b1d2da2fb8848352f0680b Mon Sep 17 00:00:00 2001 From: Renato Vicente Date: Wed, 8 May 2024 20:54:16 +0200 Subject: [PATCH 03/12] chore: updating default name --- .github/workflows/auto-label.yml | 2 +- src/domain/getConfigFile.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/auto-label.yml b/.github/workflows/auto-label.yml index 5e2994f..ee6afef 100644 --- a/.github/workflows/auto-label.yml +++ b/.github/workflows/auto-label.yml @@ -10,7 +10,7 @@ jobs: - uses: actions/checkout@v4 with: sparse-checkout: | - .github/workflows/auto-label.json + .github/workflows/auto-label.json5 sparse-checkout-cone-mode: false - uses: Renato66/auto-label@main with: diff --git a/src/domain/getConfigFile.ts b/src/domain/getConfigFile.ts index 0f9fbb5..b6bd66c 100644 --- a/src/domain/getConfigFile.ts +++ b/src/domain/getConfigFile.ts @@ -10,7 +10,7 @@ export type Config = { export const getConfigFile = (): Config => { - const configPath = getInput('configuration-file', '.github/workflows/auto-label.json') + const configPath = getInput('configuration-file', '.github/workflows/auto-label.json5') const labelsNotAllowed = getInput('labels-not-allowed', []) const defaultLabels = getInput('default-labels', []) const labelsSynonyms = getInput>( From 463a66002a15550bbf6dc3af92d317cceb4a1d17 Mon Sep 17 00:00:00 2001 From: Renato Vicente Date: Wed, 8 May 2024 21:03:19 +0200 Subject: [PATCH 04/12] feat: update json5 to be ignored by prettier --- .github/workflows/auto-label.yml | 2 +- .prettierignore | 1 + src/domain/getConfigFile.ts | 14 ++++++++------ src/domain/getJsonFile.ts | 22 ++++++++++++++++------ 4 files changed, 26 insertions(+), 13 deletions(-) create mode 100644 .prettierignore diff --git a/.github/workflows/auto-label.yml b/.github/workflows/auto-label.yml index ee6afef..6c05670 100644 --- a/.github/workflows/auto-label.yml +++ b/.github/workflows/auto-label.yml @@ -2,7 +2,7 @@ name: Labeling new issue on: issues: - types: ['opened'] + types: ['opened'] jobs: build: runs-on: ubuntu-latest diff --git a/.prettierignore b/.prettierignore new file mode 100644 index 0000000..c5a1cc7 --- /dev/null +++ b/.prettierignore @@ -0,0 +1 @@ +auto-label.json5 diff --git a/src/domain/getConfigFile.ts b/src/domain/getConfigFile.ts index b6bd66c..fac28bb 100644 --- a/src/domain/getConfigFile.ts +++ b/src/domain/getConfigFile.ts @@ -2,15 +2,17 @@ import { getInput } from './getInput' import { getLabelConfigs } from './getJsonFile' export type Config = { - labelsNotAllowed: string[]; - defaultLabels: string[]; - labelsSynonyms: Record; - ignoreComments: boolean; + labelsNotAllowed: string[] + defaultLabels: string[] + labelsSynonyms: Record + ignoreComments: boolean } export const getConfigFile = (): Config => { - - const configPath = getInput('configuration-file', '.github/workflows/auto-label.json5') + const configPath = getInput( + 'configuration-file', + '.github/workflows/auto-label.json5' + ) const labelsNotAllowed = getInput('labels-not-allowed', []) const defaultLabels = getInput('default-labels', []) const labelsSynonyms = getInput>( diff --git a/src/domain/getJsonFile.ts b/src/domain/getJsonFile.ts index 819d146..11d18a5 100644 --- a/src/domain/getJsonFile.ts +++ b/src/domain/getJsonFile.ts @@ -10,13 +10,23 @@ export const getLabelConfigs = (configurationPath: string): Config | {} => { }) try { - const config = - JSON5.parse(fileContent) + const config = JSON5.parse(fileContent) return { - defaultLabels: Array.isArray(config.defaultLabels) ? config.defaultLabels : undefined, - labelsNotAllowed: Array.isArray(config.labelsNotAllowed) ? config.labelsNotAllowed : undefined, - ignoreComments: typeof config.ignoreComments === 'boolean' ? config.ignoreComments : undefined, - labelsSynonyms: typeof config.labelsSynonyms === 'object' && !Array.isArray(config.labelsSynonyms) ? config.labelsSynonyms : undefined + defaultLabels: Array.isArray(config.defaultLabels) + ? config.defaultLabels + : undefined, + labelsNotAllowed: Array.isArray(config.labelsNotAllowed) + ? config.labelsNotAllowed + : undefined, + ignoreComments: + typeof config.ignoreComments === 'boolean' + ? config.ignoreComments + : undefined, + labelsSynonyms: + typeof config.labelsSynonyms === 'object' && + !Array.isArray(config.labelsSynonyms) + ? config.labelsSynonyms + : undefined } } catch { core.warning('Could not parse configuration file. skipping') From 8d2fc176e07d7f6d62158afa01180e726fd59140 Mon Sep 17 00:00:00 2001 From: Renato Vicente Date: Fri, 10 May 2024 20:44:41 +0200 Subject: [PATCH 05/12] test: read config file --- .prettierignore | 2 +- src/__mock__/config/config.json | 14 ++++++ src/__mock__/config/config.json5 | 18 +++++++ src/__mock__/config/config.jsonc | 15 ++++++ src/__mock__/config/invalid/invalid1.json | 6 +++ src/__mock__/config/invalid/invalid2.json | 1 + src/__mock__/config/invalid/invalid3.json | 1 + src/__mock__/config/valid/valid.json | 14 ++++++ src/domain/getConfigFile.ts | 4 +- src/domain/getLabelConfigs.spec.ts | 50 +++++++++++++++++++ .../{getJsonFile.ts => getLabelConfigs.ts} | 23 +++++++-- 11 files changed, 141 insertions(+), 7 deletions(-) create mode 100644 src/__mock__/config/config.json create mode 100644 src/__mock__/config/config.json5 create mode 100644 src/__mock__/config/config.jsonc create mode 100644 src/__mock__/config/invalid/invalid1.json create mode 100644 src/__mock__/config/invalid/invalid2.json create mode 100644 src/__mock__/config/invalid/invalid3.json create mode 100644 src/__mock__/config/valid/valid.json create mode 100644 src/domain/getLabelConfigs.spec.ts rename src/domain/{getJsonFile.ts => getLabelConfigs.ts} (51%) diff --git a/.prettierignore b/.prettierignore index c5a1cc7..38838f9 100644 --- a/.prettierignore +++ b/.prettierignore @@ -1 +1 @@ -auto-label.json5 +*.json5 diff --git a/src/__mock__/config/config.json b/src/__mock__/config/config.json new file mode 100644 index 0000000..d433cbd --- /dev/null +++ b/src/__mock__/config/config.json @@ -0,0 +1,14 @@ +{ + "labels-synonyms": { + "bug": [ + "error" + ] + }, + "labels-not-allowed": [ + "documentation" + ], + "default-labels": [ + "triage" + ], + "ignoreComments": true +} \ No newline at end of file diff --git a/src/__mock__/config/config.json5 b/src/__mock__/config/config.json5 new file mode 100644 index 0000000..617631a --- /dev/null +++ b/src/__mock__/config/config.json5 @@ -0,0 +1,18 @@ +{ + // comment test + "labels-synonyms": { + "bug": [ + "error" + // trailing comma + + ], + }, + // single quote + 'labels-not-allowed': [ + "documentation" + ], + "default-labels": [ + "triage" + ], + "ignoreComments": true +} \ No newline at end of file diff --git a/src/__mock__/config/config.jsonc b/src/__mock__/config/config.jsonc new file mode 100644 index 0000000..31cf928 --- /dev/null +++ b/src/__mock__/config/config.jsonc @@ -0,0 +1,15 @@ +{ + // comment test + "labels-synonyms": { + "bug": [ + "error" + ] + }, + "labels-not-allowed": [ + "documentation" + ], + "default-labels": [ + "triage" + ], + "ignoreComments": true +} \ No newline at end of file diff --git a/src/__mock__/config/invalid/invalid1.json b/src/__mock__/config/invalid/invalid1.json new file mode 100644 index 0000000..3782d9a --- /dev/null +++ b/src/__mock__/config/invalid/invalid1.json @@ -0,0 +1,6 @@ +{ + "labels-synonyms": "expected: Record", + "labels-not-allowed": "expected: string[]", + "default-labels": "expected: string[]", + "ignoreComments": "expected: boolean" +} \ No newline at end of file diff --git a/src/__mock__/config/invalid/invalid2.json b/src/__mock__/config/invalid/invalid2.json new file mode 100644 index 0000000..cf5e7e4 --- /dev/null +++ b/src/__mock__/config/invalid/invalid2.json @@ -0,0 +1 @@ +"invalid json" \ No newline at end of file diff --git a/src/__mock__/config/invalid/invalid3.json b/src/__mock__/config/invalid/invalid3.json new file mode 100644 index 0000000..3ade052 --- /dev/null +++ b/src/__mock__/config/invalid/invalid3.json @@ -0,0 +1 @@ +not-valid-json \ No newline at end of file diff --git a/src/__mock__/config/valid/valid.json b/src/__mock__/config/valid/valid.json new file mode 100644 index 0000000..d433cbd --- /dev/null +++ b/src/__mock__/config/valid/valid.json @@ -0,0 +1,14 @@ +{ + "labels-synonyms": { + "bug": [ + "error" + ] + }, + "labels-not-allowed": [ + "documentation" + ], + "default-labels": [ + "triage" + ], + "ignoreComments": true +} \ No newline at end of file diff --git a/src/domain/getConfigFile.ts b/src/domain/getConfigFile.ts index fac28bb..2d80936 100644 --- a/src/domain/getConfigFile.ts +++ b/src/domain/getConfigFile.ts @@ -1,5 +1,5 @@ import { getInput } from './getInput' -import { getLabelConfigs } from './getJsonFile' +import { getLabelConfigs } from './getLabelConfigs' export type Config = { labelsNotAllowed: string[] @@ -11,7 +11,7 @@ export type Config = { export const getConfigFile = (): Config => { const configPath = getInput( 'configuration-file', - '.github/workflows/auto-label.json5' + '.github/workflows/auto-label' ) const labelsNotAllowed = getInput('labels-not-allowed', []) const defaultLabels = getInput('default-labels', []) diff --git a/src/domain/getLabelConfigs.spec.ts b/src/domain/getLabelConfigs.spec.ts new file mode 100644 index 0000000..ff4ac14 --- /dev/null +++ b/src/domain/getLabelConfigs.spec.ts @@ -0,0 +1,50 @@ +import { expect, describe, test, mock, jest } from 'bun:test' +import { getLabelConfigs } from './getLabelConfigs' +import * as core from '@actions/core' + +const configurationPath = 'src/__mock__/config' +const defaultConfig = { + 'labels-synonyms': { + bug: ['error'] + }, + 'labels-not-allowed': ['documentation'], + 'default-labels': ['triage'], + ignoreComments: true +} +describe('getLabelConfigs', () => { + test('should return label configurations from a valid JSON folder path', () => { + const options = [`${configurationPath}/valid/`, `${configurationPath}/valid`] + options.forEach(elem => { + const result = getLabelConfigs(elem) + expect(result).toEqual(defaultConfig) + }) + }) + + + test('should return label configurations from a valid JSONC file path', () => { + const result = getLabelConfigs(`${configurationPath}/config.jsonc`) + expect(result).toEqual(defaultConfig) + }) + + test('should return label configurations from a valid JSON5 file path', () => { + const result = getLabelConfigs(`${configurationPath}/config.json5`) + expect(result).toEqual(defaultConfig) + }) + + test('should return an empty object if the configuration file is not valid', () => { + const options = [`${configurationPath}/invalid/invalid1.json`, `${configurationPath}/invalid/invalid2.json`] + options.forEach(elem => { + const result = getLabelConfigs(elem) + expect(result).toEqual({}) + }) + }) + + test('should send an warning if file is not readable', () => { + mock.module('@actions/core', () => ({ + warning: jest.fn(), + })) + const result = getLabelConfigs(`${configurationPath}/invalid/invalid3.json`) + expect(result).toEqual({}) + mock.module('@actions/core', () => core) + }) +}) diff --git a/src/domain/getJsonFile.ts b/src/domain/getLabelConfigs.ts similarity index 51% rename from src/domain/getJsonFile.ts rename to src/domain/getLabelConfigs.ts index 11d18a5..f106199 100644 --- a/src/domain/getJsonFile.ts +++ b/src/domain/getLabelConfigs.ts @@ -2,14 +2,29 @@ import fs from 'fs' import type { Config } from './getConfigFile' import * as core from '@actions/core' import JSON5 from 'json5' +const jsonTypes = ['json', 'jsonc', 'json5'] + +const getFilePath = (configurationPath: string): string | undefined => { + const repoPath = `./${configurationPath}`.replace('//', '/').replace('././', './') + if (configurationPath.includes('.json') && fs.existsSync(repoPath)) return repoPath + if (!configurationPath.includes('.json')) { + const files = fs.readdirSync(repoPath) + files.filter(elem => jsonTypes.includes(elem)) + if (!files.length) return + return `${repoPath}/${files[0]}`.replace('//', '/') + } +} export const getLabelConfigs = (configurationPath: string): Config | {} => { - if (fs.existsSync(configurationPath)) return {} - const fileContent = fs.readFileSync(configurationPath, { + const filePath = getFilePath(configurationPath) + if (!filePath) return {} + + const fileContent = fs.readFileSync(filePath, { encoding: 'utf8' }) try { + console.log(fileContent) const config = JSON5.parse(fileContent) return { defaultLabels: Array.isArray(config.defaultLabels) @@ -28,8 +43,8 @@ export const getLabelConfigs = (configurationPath: string): Config | {} => { ? config.labelsSynonyms : undefined } - } catch { - core.warning('Could not parse configuration file. skipping') + } catch (error: any) { + core.warning(`Could not parse configuration file at ${filePath}: ${error.message}. Skipping.`) return {} } } From 94cad46f23adffbc4f9fbc0d372d7ac5a3428915 Mon Sep 17 00:00:00 2001 From: Renato Vicente Date: Fri, 10 May 2024 20:48:37 +0200 Subject: [PATCH 06/12] chore: lint fix --- .github/workflows/auto-label.json5 | 35 ++++++++--------------- .prettierignore | 2 +- src/__mock__/config/config.json | 14 +++------ src/__mock__/config/config.json5 | 21 ++++++-------- src/__mock__/config/config.jsonc | 14 +++------ src/__mock__/config/invalid/invalid1.json | 2 +- src/__mock__/config/invalid/invalid2.json | 2 +- src/__mock__/config/valid/valid.json | 14 +++------ src/domain/getLabelConfigs.spec.ts | 17 +++++++---- src/domain/getLabelConfigs.ts | 13 ++++++--- 10 files changed, 55 insertions(+), 79 deletions(-) diff --git a/.github/workflows/auto-label.json5 b/.github/workflows/auto-label.json5 index b927962..10cb96f 100644 --- a/.github/workflows/auto-label.json5 +++ b/.github/workflows/auto-label.json5 @@ -1,26 +1,15 @@ { - "labels-synonyms": { - "bug": [ - "error", - "need fix", - "not working" - ], - "enhancement": [ - "upgrade" - ], - "question": [ - "help", - "how can i" - ] + 'labels-synonyms': { + bug: ['error', 'need fix', 'not working'], + enhancement: ['upgrade'], + question: ['help', 'how can i'] }, - "labels-not-allowed": [ - "documentation", - "duplicate", - "good first issue", - "help wanted", - "invalid" + 'labels-not-allowed': [ + 'documentation', + 'duplicate', + 'good first issue', + 'help wanted', + 'invalid' ], - "default-labels": [ - "triage" - ] -} \ No newline at end of file + 'default-labels': ['triage'] +} diff --git a/.prettierignore b/.prettierignore index 38838f9..c5dcfb0 100644 --- a/.prettierignore +++ b/.prettierignore @@ -1 +1 @@ -*.json5 +src/__mock__/config/invalid/invalid3.json \ No newline at end of file diff --git a/src/__mock__/config/config.json b/src/__mock__/config/config.json index d433cbd..f71cb85 100644 --- a/src/__mock__/config/config.json +++ b/src/__mock__/config/config.json @@ -1,14 +1,8 @@ { "labels-synonyms": { - "bug": [ - "error" - ] + "bug": ["error"] }, - "labels-not-allowed": [ - "documentation" - ], - "default-labels": [ - "triage" - ], + "labels-not-allowed": ["documentation"], + "default-labels": ["triage"], "ignoreComments": true -} \ No newline at end of file +} diff --git a/src/__mock__/config/config.json5 b/src/__mock__/config/config.json5 index 617631a..1866f3c 100644 --- a/src/__mock__/config/config.json5 +++ b/src/__mock__/config/config.json5 @@ -1,18 +1,13 @@ { // comment test - "labels-synonyms": { - "bug": [ - "error" + 'labels-synonyms': { + bug: [ + 'error' // trailing comma - - ], + ] }, // single quote - 'labels-not-allowed': [ - "documentation" - ], - "default-labels": [ - "triage" - ], - "ignoreComments": true -} \ No newline at end of file + 'labels-not-allowed': ['documentation'], + 'default-labels': ['triage'], + ignoreComments: true +} diff --git a/src/__mock__/config/config.jsonc b/src/__mock__/config/config.jsonc index 31cf928..ef67ff4 100644 --- a/src/__mock__/config/config.jsonc +++ b/src/__mock__/config/config.jsonc @@ -1,15 +1,9 @@ { // comment test "labels-synonyms": { - "bug": [ - "error" - ] + "bug": ["error"] }, - "labels-not-allowed": [ - "documentation" - ], - "default-labels": [ - "triage" - ], + "labels-not-allowed": ["documentation"], + "default-labels": ["triage"], "ignoreComments": true -} \ No newline at end of file +} diff --git a/src/__mock__/config/invalid/invalid1.json b/src/__mock__/config/invalid/invalid1.json index 3782d9a..683c115 100644 --- a/src/__mock__/config/invalid/invalid1.json +++ b/src/__mock__/config/invalid/invalid1.json @@ -3,4 +3,4 @@ "labels-not-allowed": "expected: string[]", "default-labels": "expected: string[]", "ignoreComments": "expected: boolean" -} \ No newline at end of file +} diff --git a/src/__mock__/config/invalid/invalid2.json b/src/__mock__/config/invalid/invalid2.json index cf5e7e4..466d656 100644 --- a/src/__mock__/config/invalid/invalid2.json +++ b/src/__mock__/config/invalid/invalid2.json @@ -1 +1 @@ -"invalid json" \ No newline at end of file +"invalid json" diff --git a/src/__mock__/config/valid/valid.json b/src/__mock__/config/valid/valid.json index d433cbd..f71cb85 100644 --- a/src/__mock__/config/valid/valid.json +++ b/src/__mock__/config/valid/valid.json @@ -1,14 +1,8 @@ { "labels-synonyms": { - "bug": [ - "error" - ] + "bug": ["error"] }, - "labels-not-allowed": [ - "documentation" - ], - "default-labels": [ - "triage" - ], + "labels-not-allowed": ["documentation"], + "default-labels": ["triage"], "ignoreComments": true -} \ No newline at end of file +} diff --git a/src/domain/getLabelConfigs.spec.ts b/src/domain/getLabelConfigs.spec.ts index ff4ac14..4b66f1d 100644 --- a/src/domain/getLabelConfigs.spec.ts +++ b/src/domain/getLabelConfigs.spec.ts @@ -13,14 +13,16 @@ const defaultConfig = { } describe('getLabelConfigs', () => { test('should return label configurations from a valid JSON folder path', () => { - const options = [`${configurationPath}/valid/`, `${configurationPath}/valid`] - options.forEach(elem => { + const options = [ + `${configurationPath}/valid/`, + `${configurationPath}/valid` + ] + options.forEach((elem) => { const result = getLabelConfigs(elem) expect(result).toEqual(defaultConfig) }) }) - test('should return label configurations from a valid JSONC file path', () => { const result = getLabelConfigs(`${configurationPath}/config.jsonc`) expect(result).toEqual(defaultConfig) @@ -32,8 +34,11 @@ describe('getLabelConfigs', () => { }) test('should return an empty object if the configuration file is not valid', () => { - const options = [`${configurationPath}/invalid/invalid1.json`, `${configurationPath}/invalid/invalid2.json`] - options.forEach(elem => { + const options = [ + `${configurationPath}/invalid/invalid1.json`, + `${configurationPath}/invalid/invalid2.json` + ] + options.forEach((elem) => { const result = getLabelConfigs(elem) expect(result).toEqual({}) }) @@ -41,7 +46,7 @@ describe('getLabelConfigs', () => { test('should send an warning if file is not readable', () => { mock.module('@actions/core', () => ({ - warning: jest.fn(), + warning: jest.fn() })) const result = getLabelConfigs(`${configurationPath}/invalid/invalid3.json`) expect(result).toEqual({}) diff --git a/src/domain/getLabelConfigs.ts b/src/domain/getLabelConfigs.ts index f106199..6122e81 100644 --- a/src/domain/getLabelConfigs.ts +++ b/src/domain/getLabelConfigs.ts @@ -5,11 +5,14 @@ import JSON5 from 'json5' const jsonTypes = ['json', 'jsonc', 'json5'] const getFilePath = (configurationPath: string): string | undefined => { - const repoPath = `./${configurationPath}`.replace('//', '/').replace('././', './') - if (configurationPath.includes('.json') && fs.existsSync(repoPath)) return repoPath + const repoPath = `./${configurationPath}` + .replace('//', '/') + .replace('././', './') + if (configurationPath.includes('.json') && fs.existsSync(repoPath)) + return repoPath if (!configurationPath.includes('.json')) { const files = fs.readdirSync(repoPath) - files.filter(elem => jsonTypes.includes(elem)) + files.filter((elem) => jsonTypes.includes(elem)) if (!files.length) return return `${repoPath}/${files[0]}`.replace('//', '/') } @@ -44,7 +47,9 @@ export const getLabelConfigs = (configurationPath: string): Config | {} => { : undefined } } catch (error: any) { - core.warning(`Could not parse configuration file at ${filePath}: ${error.message}. Skipping.`) + core.warning( + `Could not parse configuration file at ${filePath}: ${error.message}. Skipping.` + ) return {} } } From 062519c19bb28c6b32d34734189b51db70712206 Mon Sep 17 00:00:00 2001 From: Renato Vicente Date: Fri, 10 May 2024 22:00:41 +0200 Subject: [PATCH 07/12] test: fix mocks --- .github/workflows/auto-label.json5 | 6 +++--- src/__mock__/config/config.json | 6 +++--- src/__mock__/config/config.json5 | 6 +++--- src/__mock__/config/config.jsonc | 6 +++--- src/__mock__/config/empty.json | 1 + src/__mock__/config/valid/valid.json | 6 +++--- src/domain/getConfigFile.spec.ts | 28 ++++++++++++++++++++++++---- src/domain/getConfigFile.ts | 2 +- src/domain/getInput.ts | 2 +- src/domain/getLabelConfigs.spec.ts | 6 +++--- src/domain/getLabelConfigs.ts | 8 ++++++-- src/runner.spec.ts | 14 ++++++++++++-- 12 files changed, 63 insertions(+), 28 deletions(-) create mode 100644 src/__mock__/config/empty.json diff --git a/.github/workflows/auto-label.json5 b/.github/workflows/auto-label.json5 index 10cb96f..b741ea0 100644 --- a/.github/workflows/auto-label.json5 +++ b/.github/workflows/auto-label.json5 @@ -1,15 +1,15 @@ { - 'labels-synonyms': { + labelsSynonyms: { bug: ['error', 'need fix', 'not working'], enhancement: ['upgrade'], question: ['help', 'how can i'] }, - 'labels-not-allowed': [ + labelsNotAllowed: [ 'documentation', 'duplicate', 'good first issue', 'help wanted', 'invalid' ], - 'default-labels': ['triage'] + defaultLabels: ['triage'] } diff --git a/src/__mock__/config/config.json b/src/__mock__/config/config.json index f71cb85..00730b3 100644 --- a/src/__mock__/config/config.json +++ b/src/__mock__/config/config.json @@ -1,8 +1,8 @@ { - "labels-synonyms": { + "labelsSynonyms": { "bug": ["error"] }, - "labels-not-allowed": ["documentation"], - "default-labels": ["triage"], + "labelsNotAllowed": ["documentation"], + "defaultLabels": ["triage"], "ignoreComments": true } diff --git a/src/__mock__/config/config.json5 b/src/__mock__/config/config.json5 index 1866f3c..131ad95 100644 --- a/src/__mock__/config/config.json5 +++ b/src/__mock__/config/config.json5 @@ -1,13 +1,13 @@ { // comment test - 'labels-synonyms': { + labelsSynonyms: { bug: [ 'error' // trailing comma ] }, // single quote - 'labels-not-allowed': ['documentation'], - 'default-labels': ['triage'], + labelsNotAllowed: ['documentation'], + defaultLabels: ['triage'], ignoreComments: true } diff --git a/src/__mock__/config/config.jsonc b/src/__mock__/config/config.jsonc index ef67ff4..8d37341 100644 --- a/src/__mock__/config/config.jsonc +++ b/src/__mock__/config/config.jsonc @@ -1,9 +1,9 @@ { // comment test - "labels-synonyms": { + "labelsSynonyms": { "bug": ["error"] }, - "labels-not-allowed": ["documentation"], - "default-labels": ["triage"], + "labelsNotAllowed": ["documentation"], + "defaultLabels": ["triage"], "ignoreComments": true } diff --git a/src/__mock__/config/empty.json b/src/__mock__/config/empty.json new file mode 100644 index 0000000..0967ef4 --- /dev/null +++ b/src/__mock__/config/empty.json @@ -0,0 +1 @@ +{} diff --git a/src/__mock__/config/valid/valid.json b/src/__mock__/config/valid/valid.json index f71cb85..00730b3 100644 --- a/src/__mock__/config/valid/valid.json +++ b/src/__mock__/config/valid/valid.json @@ -1,8 +1,8 @@ { - "labels-synonyms": { + "labelsSynonyms": { "bug": ["error"] }, - "labels-not-allowed": ["documentation"], - "default-labels": ["triage"], + "labelsNotAllowed": ["documentation"], + "defaultLabels": ["triage"], "ignoreComments": true } diff --git a/src/domain/getConfigFile.spec.ts b/src/domain/getConfigFile.spec.ts index 3bfa80e..28edf7a 100644 --- a/src/domain/getConfigFile.spec.ts +++ b/src/domain/getConfigFile.spec.ts @@ -8,13 +8,26 @@ describe('getConfigFile', () => { }) test('returns empty array when labels-not-allowed input is empty', () => { mock.module('@actions/core', () => ({ - getInput: jest.fn(() => undefined), - getBooleanInput: jest.fn(() => undefined) + getInput: jest.fn((input: string) => { + const options: Record = { + 'repo-token': 'mockedToken', + 'configuration-file': 'src/__mock__/config/empty.json', + 'labels-not-allowed': '' + } + return options[input] || undefined + }) })) const result1 = getConfigFile() expect(result1.labelsNotAllowed).toEqual([]) mock.module('@actions/core', () => ({ - getInput: jest.fn(() => '') + getInput: jest.fn((input: string) => { + const options: Record = { + 'repo-token': 'mockedToken', + 'configuration-file': 'src/__mock__/config/empty.json', + 'labels-not-allowed': undefined + } + return options[input] || undefined + }) })) const result2 = getConfigFile() expect(result2.labelsNotAllowed).toEqual([]) @@ -23,7 +36,14 @@ describe('getConfigFile', () => { test('returns parsed array from labels-not-allowed input', () => { const labels = ['label1', 'label2'] mock.module('@actions/core', () => ({ - getInput: jest.fn(() => JSON.stringify(labels)) + getInput: jest.fn((input: string) => { + const options: Record = { + 'repo-token': 'mockedToken', + 'configuration-file': 'src/__mock__/config/empty.json', + 'labels-not-allowed': JSON.stringify(labels) + } + return options[input] || undefined + }) })) const result = getConfigFile() expect(result.labelsNotAllowed).toEqual(labels) diff --git a/src/domain/getConfigFile.ts b/src/domain/getConfigFile.ts index 2d80936..6f13de4 100644 --- a/src/domain/getConfigFile.ts +++ b/src/domain/getConfigFile.ts @@ -11,7 +11,7 @@ export type Config = { export const getConfigFile = (): Config => { const configPath = getInput( 'configuration-file', - '.github/workflows/auto-label' + '.github/workflows/auto-label.json5' ) const labelsNotAllowed = getInput('labels-not-allowed', []) const defaultLabels = getInput('default-labels', []) diff --git a/src/domain/getInput.ts b/src/domain/getInput.ts index fd0306a..1be81ef 100644 --- a/src/domain/getInput.ts +++ b/src/domain/getInput.ts @@ -22,6 +22,6 @@ export const getInput = (field: string, fallback: T): T => { case 'boolean': return getBooleanInput(field, fallback) as T default: - return core.getInput(field) as T + return (core.getInput(field) as T) || fallback } } diff --git a/src/domain/getLabelConfigs.spec.ts b/src/domain/getLabelConfigs.spec.ts index 4b66f1d..d433000 100644 --- a/src/domain/getLabelConfigs.spec.ts +++ b/src/domain/getLabelConfigs.spec.ts @@ -4,11 +4,11 @@ import * as core from '@actions/core' const configurationPath = 'src/__mock__/config' const defaultConfig = { - 'labels-synonyms': { + labelsSynonyms: { bug: ['error'] }, - 'labels-not-allowed': ['documentation'], - 'default-labels': ['triage'], + labelsNotAllowed: ['documentation'], + defaultLabels: ['triage'], ignoreComments: true } describe('getLabelConfigs', () => { diff --git a/src/domain/getLabelConfigs.ts b/src/domain/getLabelConfigs.ts index 6122e81..22d3dc2 100644 --- a/src/domain/getLabelConfigs.ts +++ b/src/domain/getLabelConfigs.ts @@ -27,9 +27,8 @@ export const getLabelConfigs = (configurationPath: string): Config | {} => { }) try { - console.log(fileContent) const config = JSON5.parse(fileContent) - return { + const configObject = { defaultLabels: Array.isArray(config.defaultLabels) ? config.defaultLabels : undefined, @@ -46,6 +45,11 @@ export const getLabelConfigs = (configurationPath: string): Config | {} => { ? config.labelsSynonyms : undefined } + return Object.fromEntries( + Object.entries(configObject).filter( + ([_key, value]) => value !== undefined + ) + ) } catch (error: any) { core.warning( `Could not parse configuration file at ${filePath}: ${error.message}. Skipping.` diff --git a/src/runner.spec.ts b/src/runner.spec.ts index c7d1e2c..4cdb3a5 100644 --- a/src/runner.spec.ts +++ b/src/runner.spec.ts @@ -35,11 +35,21 @@ mock.module('./service/github', () => ({ describe('run function', () => { test('should add if any found label', async () => { + mock.module('@actions/core', () => ({ + getInput: jest.fn((input: string) => { + const options: Record = { + 'repo-token': 'mockedToken', + 'configuration-file': 'src/__mock__/config/empty.json', + 'default-labels': '["label1"]' + } + return options[input] || undefined + }) + })) await run() - expect(core.setFailed).not.toHaveBeenCalled() + // expect(core.setFailed).not.toHaveBeenCalled() expect(addLabelsSpy).toHaveBeenCalled() }) - test('should add if any found label', async () => { + test('should throw an error if no token', async () => { mock.module('@actions/core', () => ({ getInput: jest.fn(() => undefined), info: jest.fn(), From 3fa37a956237887db88ab4c6c36054adc3259458 Mon Sep 17 00:00:00 2001 From: Renato Vicente Date: Sat, 11 May 2024 00:31:04 +0200 Subject: [PATCH 08/12] fix: filter to get all json options --- src/domain/getLabelConfigs.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/domain/getLabelConfigs.ts b/src/domain/getLabelConfigs.ts index 22d3dc2..4218a0e 100644 --- a/src/domain/getLabelConfigs.ts +++ b/src/domain/getLabelConfigs.ts @@ -11,8 +11,8 @@ const getFilePath = (configurationPath: string): string | undefined => { if (configurationPath.includes('.json') && fs.existsSync(repoPath)) return repoPath if (!configurationPath.includes('.json')) { - const files = fs.readdirSync(repoPath) - files.filter((elem) => jsonTypes.includes(elem)) + const allFiles = fs.readdirSync(repoPath) + const files = allFiles.filter((elem) => jsonTypes.includes(elem)) if (!files.length) return return `${repoPath}/${files[0]}`.replace('//', '/') } From 58fca986b2b475854566edf75be501adf7367f56 Mon Sep 17 00:00:00 2001 From: Renato Vicente Date: Sat, 11 May 2024 00:57:31 +0200 Subject: [PATCH 09/12] docs: create configuration file input in readme --- README.md | 96 +++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 75 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index ab34e50..1b50b6d 100644 --- a/README.md +++ b/README.md @@ -2,22 +2,14 @@ ![Unit test](https://github.com/Renato66/auto-label/workflows/Unit%20test/badge.svg) [![Auto Label](https://github.com/Renato66/auto-label/workflows/Labeling%20new%20issue/badge.svg)](https://github.com/Renato66/auto-label) -[![codecov](https://codecov.io/gh/Renato66/auto-label/branch/master/graph/badge.svg)](https://codecov.io/gh/Renato66/auto-label) ![image](https://user-images.githubusercontent.com/9284273/79672530-57c1db80-81a9-11ea-900c-3b4f73984e0a.png) The Auto label action will check for every new issue and automatically adds a label based on the body of the issue. This means that finding specific issues will be much easier. -> [!WARNING] -> The main branch is being refactored, use the stable one at [master](https://github.com/Renato66/auto-label/tree/master) subscribe to [v3](https://github.com/Renato66/auto-label/issues/75) to get the latest version when it's released - ## Creating -Check out the app to make a YAML file [here](https://renato66.github.io/auto-label/). - -or - -add a file to `.github/workflows/issue.yml` +Add a file to `.github/workflows/auto-label.yml` ```yml name: Labeling new issue @@ -28,24 +20,49 @@ jobs: build: runs-on: ubuntu-latest steps: - - uses: Renato66/auto-label@v2 + - uses: actions/checkout@v4 + with: + sparse-checkout: | + .github/workflows + sparse-checkout-cone-mode: false + - uses: Renato66/auto-label@v3 with: repo-token: ${{ secrets.GITHUB_TOKEN }} - ignore-comments: true - labels-synonyms: '{"bug":["error","need fix","not working"],"enhancement":["upgrade"],"question":["help"]}' - labels-not-allowed: '["good first issue"]' - default-labels: '["help wanted"]' ``` +Add a config file to `.github/workflows/auto-label.json5` + +```json5 +// see inputs for more examples +{ + labelsSynonyms: { + bug: ['error', 'need fix', 'not working'], + enhancement: ['upgrade'], + question: ['help', 'how can i'] + }, + labelsNotAllowed: [ + 'documentation', + 'duplicate', + 'good first issue', + 'help wanted', + 'invalid' + ], + defaultLabels: ['triage'], + ignoreComments: true +} +``` + + ## Inputs -| Name | Description | Required | Default | Examples | -| ------------------ | ----------------------------------- | -------- | ------- | :------------------------: | -| repo-token | GitHub token for the repository | true | - | [...](#repo-token) | -| ignore-comments | Ignore labels inside issue comments | false | true | [...](#ignore-comments) | -| labels-synonyms | Text synonyms for labels | false | - | [...](#labels-synonyms) | -| labels-not-allowed | Labels to ignore | false | - | [...](#labels-not-allowed) | -| default-labels | Labels that will always be set | false | - | [...](#default-labels) | +| Name | Description | Required | Default | Examples | +| ------------------ | ----------------------------------- | -------- | ---------------------------------- | :------------------------: | +| repo-token | GitHub token for the repository | true | - | [...](#repo-token) | +| configuration-file | Configuration file path | true | .github/workflows/auto-label.json5 | [...](#configuration-file) | +| ignore-comments | Ignore labels inside issue comments | false | true | [...](#ignore-comments) | +| labels-synonyms | Text synonyms for labels | false | - | [...](#labels-synonyms) | +| labels-not-allowed | Labels to ignore | false | - | [...](#labels-not-allowed) | +| default-labels | Labels that will always be set | false | - | [...](#default-labels) | ### Repo Token @@ -55,6 +72,43 @@ Repo token is provided automatically by GitHub; just need to add repo-token: ${{ secrets.GITHUB_TOKEN }} ``` +### Configuration File + +Configuration file can be created at any place at your repository, it will need another action to get the file like: + +```yml + - uses: actions/checkout@v4 + with: + sparse-checkout: | + .github/workflows/ + sparse-checkout-cone-mode: false +``` + +and it will look for any file named auto-label with the extension `JSON` or `JSON5` or `JSONC` but you can also define a specific extension + +```yml + - uses: actions/checkout@v4 + with: + sparse-checkout: | + .github/workflows/auto-label.json5 + sparse-checkout-cone-mode: false +``` + +to set another place to store your configuration file, you should checkout and point with `configuration-file` input: + +```yml + - uses: actions/checkout@v4 + with: + sparse-checkout: | + src/actions/configuration.json + sparse-checkout-cone-mode: false + - uses: Renato66/auto-label@v3 + with: + configuration-file: 'src/actions/configuration.json' +``` + + + #### Change bot appearance If you want to change who added the labels, you can provide a user token From 7f3ac23bfc6b5ef5849a9f52011f2610661f9d19 Mon Sep 17 00:00:00 2001 From: Renato Vicente Date: Sat, 11 May 2024 00:57:44 +0200 Subject: [PATCH 10/12] feat: update naming for config file --- .github/workflows/auto-label.yml | 5 ++++- src/__mock__/config/{config.json => auto-label.json} | 0 src/__mock__/config/{config.json5 => auto-label.json5} | 0 src/__mock__/config/{config.jsonc => auto-label.jsonc} | 0 src/domain/getConfigFile.ts | 2 +- src/domain/getLabelConfigs.spec.ts | 8 ++++---- src/domain/getLabelConfigs.ts | 2 +- 7 files changed, 10 insertions(+), 7 deletions(-) rename src/__mock__/config/{config.json => auto-label.json} (100%) rename src/__mock__/config/{config.json5 => auto-label.json5} (100%) rename src/__mock__/config/{config.jsonc => auto-label.jsonc} (100%) diff --git a/.github/workflows/auto-label.yml b/.github/workflows/auto-label.yml index 6c05670..623482e 100644 --- a/.github/workflows/auto-label.yml +++ b/.github/workflows/auto-label.yml @@ -10,7 +10,10 @@ jobs: - uses: actions/checkout@v4 with: sparse-checkout: | - .github/workflows/auto-label.json5 + .github/workflows/ + # .github/workflows/auto-label.json5 + # .github/workflows/auto-label.jsonc + # .github/workflows/auto-label.json sparse-checkout-cone-mode: false - uses: Renato66/auto-label@main with: diff --git a/src/__mock__/config/config.json b/src/__mock__/config/auto-label.json similarity index 100% rename from src/__mock__/config/config.json rename to src/__mock__/config/auto-label.json diff --git a/src/__mock__/config/config.json5 b/src/__mock__/config/auto-label.json5 similarity index 100% rename from src/__mock__/config/config.json5 rename to src/__mock__/config/auto-label.json5 diff --git a/src/__mock__/config/config.jsonc b/src/__mock__/config/auto-label.jsonc similarity index 100% rename from src/__mock__/config/config.jsonc rename to src/__mock__/config/auto-label.jsonc diff --git a/src/domain/getConfigFile.ts b/src/domain/getConfigFile.ts index 6f13de4..b1ae52c 100644 --- a/src/domain/getConfigFile.ts +++ b/src/domain/getConfigFile.ts @@ -11,7 +11,7 @@ export type Config = { export const getConfigFile = (): Config => { const configPath = getInput( 'configuration-file', - '.github/workflows/auto-label.json5' + '.github/workflows/' ) const labelsNotAllowed = getInput('labels-not-allowed', []) const defaultLabels = getInput('default-labels', []) diff --git a/src/domain/getLabelConfigs.spec.ts b/src/domain/getLabelConfigs.spec.ts index d433000..ab951f6 100644 --- a/src/domain/getLabelConfigs.spec.ts +++ b/src/domain/getLabelConfigs.spec.ts @@ -14,8 +14,8 @@ const defaultConfig = { describe('getLabelConfigs', () => { test('should return label configurations from a valid JSON folder path', () => { const options = [ - `${configurationPath}/valid/`, - `${configurationPath}/valid` + `${configurationPath}/`, + `${configurationPath}` ] options.forEach((elem) => { const result = getLabelConfigs(elem) @@ -24,12 +24,12 @@ describe('getLabelConfigs', () => { }) test('should return label configurations from a valid JSONC file path', () => { - const result = getLabelConfigs(`${configurationPath}/config.jsonc`) + const result = getLabelConfigs(`${configurationPath}/auto-label.jsonc`) expect(result).toEqual(defaultConfig) }) test('should return label configurations from a valid JSON5 file path', () => { - const result = getLabelConfigs(`${configurationPath}/config.json5`) + const result = getLabelConfigs(`${configurationPath}/auto-label.json5`) expect(result).toEqual(defaultConfig) }) diff --git a/src/domain/getLabelConfigs.ts b/src/domain/getLabelConfigs.ts index 4218a0e..ab19199 100644 --- a/src/domain/getLabelConfigs.ts +++ b/src/domain/getLabelConfigs.ts @@ -12,7 +12,7 @@ const getFilePath = (configurationPath: string): string | undefined => { return repoPath if (!configurationPath.includes('.json')) { const allFiles = fs.readdirSync(repoPath) - const files = allFiles.filter((elem) => jsonTypes.includes(elem)) + const files = allFiles.filter((elem) => jsonTypes.map(elem => `auto-label.${elem}`).includes(elem)) if (!files.length) return return `${repoPath}/${files[0]}`.replace('//', '/') } From a71cd62e17dd5f43d6d92a9460c61e96a7cbbd2b Mon Sep 17 00:00:00 2001 From: Renato Vicente Date: Sat, 11 May 2024 00:58:53 +0200 Subject: [PATCH 11/12] chore: lint fix --- .prettierignore | 3 ++- src/domain/getLabelConfigs.spec.ts | 5 +---- src/domain/getLabelConfigs.ts | 4 +++- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/.prettierignore b/.prettierignore index c5dcfb0..7ac8741 100644 --- a/.prettierignore +++ b/.prettierignore @@ -1 +1,2 @@ -src/__mock__/config/invalid/invalid3.json \ No newline at end of file +src/__mock__/config/invalid/invalid3.json +readme.md \ No newline at end of file diff --git a/src/domain/getLabelConfigs.spec.ts b/src/domain/getLabelConfigs.spec.ts index ab951f6..4f4ab1c 100644 --- a/src/domain/getLabelConfigs.spec.ts +++ b/src/domain/getLabelConfigs.spec.ts @@ -13,10 +13,7 @@ const defaultConfig = { } describe('getLabelConfigs', () => { test('should return label configurations from a valid JSON folder path', () => { - const options = [ - `${configurationPath}/`, - `${configurationPath}` - ] + const options = [`${configurationPath}/`, `${configurationPath}`] options.forEach((elem) => { const result = getLabelConfigs(elem) expect(result).toEqual(defaultConfig) diff --git a/src/domain/getLabelConfigs.ts b/src/domain/getLabelConfigs.ts index ab19199..751e884 100644 --- a/src/domain/getLabelConfigs.ts +++ b/src/domain/getLabelConfigs.ts @@ -12,7 +12,9 @@ const getFilePath = (configurationPath: string): string | undefined => { return repoPath if (!configurationPath.includes('.json')) { const allFiles = fs.readdirSync(repoPath) - const files = allFiles.filter((elem) => jsonTypes.map(elem => `auto-label.${elem}`).includes(elem)) + const files = allFiles.filter((elem) => + jsonTypes.map((elem) => `auto-label.${elem}`).includes(elem) + ) if (!files.length) return return `${repoPath}/${files[0]}`.replace('//', '/') } From 7cb2c295b4089bce31d625855004fcfb24604ac3 Mon Sep 17 00:00:00 2001 From: Renato Vicente Date: Sat, 11 May 2024 01:04:58 +0200 Subject: [PATCH 12/12] chore: code rabbit suggest Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> --- README.md | 3 --- 1 file changed, 3 deletions(-) diff --git a/README.md b/README.md index 1b50b6d..3d8498d 100644 --- a/README.md +++ b/README.md @@ -52,7 +52,6 @@ Add a config file to `.github/workflows/auto-label.json5` } ``` - ## Inputs | Name | Description | Required | Default | Examples | @@ -107,8 +106,6 @@ to set another place to store your configuration file, you should checkout and p configuration-file: 'src/actions/configuration.json' ``` - - #### Change bot appearance If you want to change who added the labels, you can provide a user token