Skip to content

Commit

Permalink
feat: autoclose PRs when repo disabled (#21263)
Browse files Browse the repository at this point in the history
Co-authored-by: HonkingGoose <[email protected]>
  • Loading branch information
rarkins and HonkingGoose authored Apr 1, 2023
1 parent fd133c1 commit 03618ee
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
8 changes: 5 additions & 3 deletions lib/workers/repository/finalise/prune.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,18 @@ import { logger } from '../../../logger';
import { platform } from '../../../modules/platform';
import { ensureComment } from '../../../modules/platform/comment';
import { scm } from '../../../modules/platform/scm';
import { getBranchList } from '../../../util/git';
import { getBranchList, setUserRepoConfig } from '../../../util/git';

async function cleanUpBranches(
{ pruneStaleBranches: enabled }: RenovateConfig,
config: RenovateConfig,
remainingBranches: string[]
): Promise<void> {
if (enabled === false) {
if (!config.pruneStaleBranches) {
logger.debug('Branch/PR pruning is disabled - skipping');
return;
}
// set Git author in case the repository is not initialized yet
setUserRepoConfig(config);
for (const branchName of remainingBranches) {
try {
const pr = await platform.findPr({
Expand Down
14 changes: 14 additions & 0 deletions lib/workers/repository/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ import fs from 'fs-extra';
import { GlobalConfig } from '../../config/global';
import { applySecretsToConfig } from '../../config/secrets';
import type { RenovateConfig } from '../../config/types';
import {
REPOSITORY_DISABLED_BY_CONFIG,
REPOSITORY_FORKED,
REPOSITORY_NO_CONFIG,
} from '../../constants/error-messages';
import { pkg } from '../../expose.cjs';
import { instrument } from '../../instrumentation';
import { logger, setMeta } from '../../logger';
Expand All @@ -18,6 +23,7 @@ import { setBranchCache } from './cache';
import { ensureDependencyDashboard } from './dependency-dashboard';
import handleError from './error';
import { finaliseRepo } from './finalise';
import { pruneStaleBranches } from './finalise/prune';
import { initRepo } from './init';
import { OnboardingState } from './onboarding/common';
import { ensureOnboardingPr } from './onboarding/pr';
Expand Down Expand Up @@ -92,6 +98,14 @@ export async function renovateRepository(
} catch (err) /* istanbul ignore next */ {
setMeta({ repository: config.repository });
const errorRes = await handleError(config, err);
const pruneWhenErrors = [
REPOSITORY_DISABLED_BY_CONFIG,
REPOSITORY_FORKED,
REPOSITORY_NO_CONFIG,
];
if (pruneWhenErrors.includes(errorRes)) {
await pruneStaleBranches(config, []);
}
repoResult = processResult(config, errorRes);
}
if (localDir && !repoConfig.persistRepoData) {
Expand Down

0 comments on commit 03618ee

Please sign in to comment.