From be13bbd1b71393037c1fca23f8dd890c37314acb Mon Sep 17 00:00:00 2001 From: Nathan Hammond Date: Fri, 7 Jul 2023 22:35:16 +0800 Subject: [PATCH] Early exit when no files are changed. (#456) * Early exit when no files are changed. As a consequence of the `checkAll` function call returning `true` if the length of `changedFiles` is 0, this must early-exit in order to avoid labeling empty PRs. * Update dist. * Update for new code styles. * review changes requested * Update dist. * chore: prettify code --------- Co-authored-by: MaksimZhukov Co-authored-by: IvanZosimov --- __tests__/main.test.ts | 9 +++++++++ dist/index.js | 4 ++++ src/labeler.ts | 7 +++++++ 3 files changed, 20 insertions(+) diff --git a/__tests__/main.test.ts b/__tests__/main.test.ts index bd4f6cd1a..6b10904cb 100644 --- a/__tests__/main.test.ts +++ b/__tests__/main.test.ts @@ -313,6 +313,15 @@ describe('run', () => { ); }); + it('does not add labels to PRs that have no changed files', async () => { + usingLabelerConfigYaml('only_pdfs.yml'); + mockGitHubResponseChangedFiles(); + + await run(); + + expect(setLabelsMock).toHaveBeenCalledTimes(0); + }); + it('should use local configuration file if it exists', async () => { const configFile = 'only_pdfs.yml'; const configFilePath = path.join(__dirname, 'fixtures', configFile); diff --git a/dist/index.js b/dist/index.js index 50da5bd83..5cb19babf 100644 --- a/dist/index.js +++ b/dist/index.js @@ -81,6 +81,10 @@ function run() { } core.debug(`fetching changed files for pr #${prNumber}`); const changedFiles = yield getChangedFiles(client, prNumber); + if (!changedFiles.length) { + core.warning(`Pull request #${prNumber} has no changed files, skipping`); + continue; + } const labelGlobs = yield getLabelGlobs(client, configPath); const preexistingLabels = pullRequest.labels.map(l => l.name); const allLabels = new Set(preexistingLabels); diff --git a/src/labeler.ts b/src/labeler.ts index 8893a81e5..995abaa35 100644 --- a/src/labeler.ts +++ b/src/labeler.ts @@ -48,6 +48,13 @@ export async function run() { core.debug(`fetching changed files for pr #${prNumber}`); const changedFiles: string[] = await getChangedFiles(client, prNumber); + if (!changedFiles.length) { + core.warning( + `Pull request #${prNumber} has no changed files, skipping` + ); + continue; + } + const labelGlobs: Map = await getLabelGlobs(client, configPath);