Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Early exit when no files are changed. #456

Merged
merged 7 commits into from
Jul 7, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
41 changes: 41 additions & 0 deletions __tests__/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,47 @@ describe('run', () => {
expect(addLabelsMock).toHaveBeenCalledTimes(0);
});

it('does not add labels to PRs that have no changed files', async () => {
usingLabelerConfigYaml('only_pdfs.yml');
mockGitHubResponseChangedFiles();

await run();

expect(removeLabelMock).toHaveBeenCalledTimes(0);
expect(addLabelsMock).toHaveBeenCalledTimes(0);
});

it('(with sync-labels: true) removes all labels from PRs that have no changed files', async () => {
let mockInput = {
'repo-token': 'foo',
'configuration-path': 'bar',
'sync-labels': true
};

jest
.spyOn(core, 'getInput')
.mockImplementation((name: string, ...opts) => mockInput[name]);

usingLabelerConfigYaml('only_pdfs.yml');
mockGitHubResponseChangedFiles();
getPullMock.mockResolvedValue(<any>{
data: {
labels: [{name: 'touched-a-pdf-file'}]
}
});

await run();

expect(removeLabelMock).toHaveBeenCalledTimes(1);
expect(addLabelsMock).toHaveBeenCalledTimes(0);
expect(removeLabelMock).toHaveBeenCalledWith({
owner: 'monalisa',
repo: 'helloworld',
issue_number: 123,
name: 'touched-a-pdf-file'
});
});

it('(with sync-labels: true) it deletes preexisting PR labels that no longer match the glob pattern', async () => {
let mockInput = {
'repo-token': 'foo',
Expand Down
12 changes: 12 additions & 0 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,18 @@ function run() {
const labelGlobs = yield getLabelGlobs(client, configPath);
const labels = [];
const labelsToRemove = [];
if (!changedFiles.length) {
core.debug(`exiting early because pr #${prNumber} has no changed files.`);
if (syncLabels) {
for (let label of pullRequest.labels) {
if (label.name) {
labelsToRemove.push(label.name);
}
}
yield removeLabels(client, prNumber, labelsToRemove);
}
return;
}
for (const [label, globs] of labelGlobs.entries()) {
core.debug(`processing ${label}`);
if (checkGlobs(changedFiles, globs)) {
Expand Down
17 changes: 17 additions & 0 deletions src/labeler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,23 @@ export async function run() {

const labels: string[] = [];
const labelsToRemove: string[] = [];

MaksimZhukov marked this conversation as resolved.
Show resolved Hide resolved
if (!changedFiles.length) {
core.debug(`exiting early because pr #${prNumber} has no changed files.`);
MaksimZhukov marked this conversation as resolved.
Show resolved Hide resolved

if (syncLabels) {
MaksimZhukov marked this conversation as resolved.
Show resolved Hide resolved
for (let label of pullRequest.labels) {
if (label.name) {
labelsToRemove.push(label.name);
}
}

await removeLabels(client, prNumber, labelsToRemove);
}

return;
}

for (const [label, globs] of labelGlobs.entries()) {
core.debug(`processing ${label}`);
if (checkGlobs(changedFiles, globs)) {
Expand Down