Skip to content

Commit

Permalink
Skip autofix on pending Renovate Branches (#1226)
Browse files Browse the repository at this point in the history
* skip autofix when renovate branch does not have a PR

* Create mighty-buttons-fry.md

* tweak syntax

* Run `skuba format`

* Update mighty-buttons-fry.md

* Add buildkite annotation

* Apply suggestions from code review

Co-authored-by: Ryan Ling <[email protected]>

* default to just renovate

* fix test name

---------

Co-authored-by: skuba <[email protected]>
Co-authored-by: Ryan Ling <[email protected]>
  • Loading branch information
3 people authored Jul 25, 2023
1 parent 884f3f6 commit 249525b
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
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 branches when there is no open pull request

This prevents an issue where a Renovate branch can get stuck in the `Edited/Blocked` state without a pull request being raised.
28 changes: 28 additions & 0 deletions src/cli/lint/autofix.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import simpleGit from 'simple-git';

import * as Buildkite from '../../api/buildkite';
import * as Git from '../../api/git';
import * as GitHub from '../../api/github';
import { runESLint } from '../adapter/eslint';
Expand All @@ -10,6 +11,7 @@ import { AUTOFIX_IGNORE_FILES, autofix } from './autofix';
jest.mock('simple-git');
jest.mock('../../api/git');
jest.mock('../../api/github');
jest.mock('../../api/buildkite');
jest.mock('../adapter/eslint');
jest.mock('../adapter/prettier');

Expand Down Expand Up @@ -105,6 +107,32 @@ 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();

expect(Buildkite.annotate).toHaveBeenCalled();

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
18 changes: 18 additions & 0 deletions src/cli/lint/autofix.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { inspect } from 'util';
import fs from 'fs-extra';
import simpleGit from 'simple-git';

import * as Buildkite from '../../api/buildkite';
import * as Git from '../../api/git';
import * as GitHub from '../../api/github';
import { isCiEnv } from '../../utils/env';
Expand All @@ -18,6 +19,8 @@ import { REFRESHABLE_IGNORE_FILES } from '../configure/refreshIgnoreFiles';

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

const RENOVATE_DEFAULT_PREFIX = 'renovate';

const AUTOFIX_COMMIT_MESSAGE = 'Run `skuba format`';

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

if (currentBranch?.startsWith(RENOVATE_DEFAULT_PREFIX)) {
try {
await GitHub.getPullRequestNumber();
} catch (error) {
const warning =
'An autofix is available, but it was not pushed because an open pull request for this Renovate branch could not be found. If a pull request has since been created, retry the lint step to push the fix.';
log.warn(warning);
try {
await Buildkite.annotate(Buildkite.md.terminal(warning));
} catch {}

return false;
}
}

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

0 comments on commit 249525b

Please sign in to comment.