Skip to content

Commit

Permalink
Early exit when no files are changed. (#456)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
Co-authored-by: IvanZosimov <[email protected]>
  • Loading branch information
3 people authored Jul 7, 2023
1 parent 994304c commit be13bbd
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 0 deletions.
9 changes: 9 additions & 0 deletions __tests__/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 4 additions & 0 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
7 changes: 7 additions & 0 deletions src/labeler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, StringOrMatchConfig[]> =
await getLabelGlobs(client, configPath);

Expand Down

0 comments on commit be13bbd

Please sign in to comment.