diff --git a/lib/workers/repository/finalise/prune.ts b/lib/workers/repository/finalise/prune.ts index 3546ec41b746da..88115d6daf5040 100644 --- a/lib/workers/repository/finalise/prune.ts +++ b/lib/workers/repository/finalise/prune.ts @@ -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 { - 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({ diff --git a/lib/workers/repository/index.ts b/lib/workers/repository/index.ts index 5352b8123ed482..0ecd80d1fba290 100644 --- a/lib/workers/repository/index.ts +++ b/lib/workers/repository/index.ts @@ -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'; @@ -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'; @@ -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) {