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

Skip autofix on pending Renovate Branches #1226

Merged
merged 11 commits into from
Jul 25, 2023
7 changes: 7 additions & 0 deletions .changeset/mighty-buttons-fry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'skuba': minor
---

lint/format: Skip autofixing on `renovate-skuba-` branches when there is no open pull request for the associated branch.
samchungy marked this conversation as resolved.
Show resolved Hide resolved

This prevents an issue where a Renovate branch can get stuck in the `Edited/Blocked` state without a pull request being raised.
24 changes: 24 additions & 0 deletions src/cli/lint/autofix.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,30 @@ describe('autofix', () => {
expectNoAutofix();
});

it('bails on a renovate- branch when there is no open pull request', async () => {
jest.mocked(Git.currentBranch).mockResolvedValue('renovate-skuba-7.x');
jest
.mocked(GitHub.getPullRequestNumber)
.mockRejectedValue(
new Error(
`Commit cdd1520 is not associated with an open GitHub pull request`,
),
);

await expect(autofix(params)).resolves.toBeUndefined();

expectNoAutofix();
});

it('suceeds on a renovate- branch when there is an open pull request associated with the commit', async () => {
jest.mocked(Git.currentBranch).mockResolvedValue('renovate-skuba-7.x');
jest.mocked(GitHub.getPullRequestNumber).mockResolvedValue(6);

await expect(autofix(params)).resolves.toBeUndefined();

expectAutofixCommit();
});

it('bails on a GitHub protected branch', async () => {
process.env.GITHUB_REF_PROTECTED = 'true';

Expand Down
13 changes: 13 additions & 0 deletions src/cli/lint/autofix.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import { REFRESHABLE_IGNORE_FILES } from '../configure/refreshIgnoreFiles';

import type { Input } from './types';

const RENOVATE_DEFAULT_PREFIX = 'renovate-skuba-';
samchungy marked this conversation as resolved.
Show resolved Hide resolved

const AUTOFIX_COMMIT_MESSAGE = 'Run `skuba format`';

const AUTOFIX_DELETE_FILES = [
Expand Down Expand Up @@ -79,6 +81,17 @@ const shouldPush = async ({
return false;
}

if (currentBranch?.startsWith(RENOVATE_DEFAULT_PREFIX)) {
try {
await GitHub.getPullRequestNumber();
} catch (error) {
log.warn(
'Could not find an open pull request for this branch. The autofix commit will not be applied to allow Renovate to open a pull request successfully.',
);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll review this proper in a bit but seems perfectly reasonable. Could we also write a Buildkite warning and share next steps (i.e. fix is available but we did not push it, if the PR has since been created then you can manually rerun the step to push).

return false;
}
}

let headCommitMessage;
try {
headCommitMessage = await Git.getHeadCommitMessage({ dir });
Expand Down