Skip to content
Snippets Groups Projects
Select Git revision
  • 0bd7d27b33c893c3da6334c4110c6144a51fa451
  • main default protected
  • renovate/main-zod-3.x
  • renovate/main-ghcr.io-renovatebot-base-image-10.x
  • renovate/main-ghcr.io-containerbase-devcontainer-13.x
  • next
  • revert-31645-feat/rename-gradle-wrapper-validation-action
  • renovate/main-redis-5.x
  • fix/36615b-branch-reuse-no-cache
  • chore/punycode
  • fix/36615-branch-reuse-bug
  • refactor/pin-new-value
  • feat/36219--git-x509-signing
  • feat/structured-logger
  • hotfix/39.264.1
  • feat/skip-dangling
  • gh-readonly-queue/next/pr-36034-7a061c4ca1024a19e2c295d773d9642625d1c2be
  • hotfix/39.238.3
  • refactor/gitlab-auto-approve
  • feat/template-strings
  • gh-readonly-queue/next/pr-35654-137d934242c784e0c45d4b957362214f0eade1d7
  • 41.28.2
  • 41.28.1
  • 41.28.0
  • 41.27.1
  • 41.27.0
  • 41.26.2
  • 41.26.1
  • 41.26.0
  • 41.25.1
  • 41.25.0
  • 41.24.0
  • 41.23.5
  • 41.23.4
  • 41.23.3
  • 41.23.2
  • 41.23.1
  • 41.23.0
  • 41.22.0
  • 41.21.4
  • 41.21.3
41 results

schedule.ts

Blame
  • reuse.ts 2.95 KiB
    import { RenovateConfig } from '../../config';
    import { logger } from '../../logger';
    import { platform } from '../../platform';
    import { branchExists, isBranchModified, isBranchStale } from '../../util/git';
    
    type ParentBranch = {
      reuseExistingBranch: boolean;
      isModified?: boolean;
    };
    
    export async function shouldReuseExistingBranch(
      config: RenovateConfig
    ): Promise<ParentBranch> {
      const { branchName } = config;
      // Check if branch exists
      if (!branchExists(branchName)) {
        logger.debug(`Branch needs creating`);
        return { reuseExistingBranch: false };
      }
      logger.debug(`Branch already exists`);
    
      // Check for existing PR
      const pr = await platform.getBranchPr(branchName);
    
      if (pr) {
        if (pr.title?.startsWith('rebase!')) {
          logger.debug(`Manual rebase requested via PR title for #${pr.number}`);
          return { reuseExistingBranch: false };
        }
        if (pr.body?.includes(`- [x] <!-- rebase-check -->`)) {
          logger.debug(`Manual rebase requested via PR checkbox for #${pr.number}`);
          return { reuseExistingBranch: false };
        }
        if (pr.labels?.includes(config.rebaseLabel)) {
          logger.debug(`Manual rebase requested via PR labels for #${pr.number}`);
          // istanbul ignore if
          if (config.dryRun) {
            logger.info(
              `DRY-RUN: Would delete label ${config.rebaseLabel} from #${pr.number}`
            );
          } else {
            await platform.deleteLabel(pr.number, config.rebaseLabel);
          }
          return { reuseExistingBranch: false };
        }
      }
    
      if (
        config.rebaseWhen === 'behind-base-branch' ||
        (config.rebaseWhen === 'auto' && (await platform.getRepoForceRebase())) ||
        (config.automerge && config.automergeType === 'branch')
      ) {
        if (await isBranchStale(branchName)) {
          logger.debug(`Branch is stale and needs rebasing`);
          // We can rebase the branch only if no PR or PR can be rebased
          if (await isBranchModified(branchName)) {
            // TODO: Warn here so that it appears in PR body
            logger.debug('Cannot rebase branch as it has been modified');
            return { reuseExistingBranch: true, isModified: true };
          }
          return { reuseExistingBranch: false };
        }
      }
    
      // Now check if PR is unmergeable. If so then we also rebase
      if (pr?.isConflicted) {
        logger.debug('PR is conflicted');
    
        if ((await isBranchModified(branchName)) === false) {
          logger.debug(`Branch is not mergeable and needs rebasing`);
          if (config.rebaseWhen === 'never') {
            logger.debug('Rebasing disabled by config');
            return { reuseExistingBranch: true, isModified: false };
          }
          // Setting reuseExistingBranch back to undefined means that we'll use the default branch
          return { reuseExistingBranch: false };
        }
        // Don't do anything different, but warn
        // TODO: Add warning to PR
        logger.debug(`Branch is not mergeable but can't be rebased`);
      }
      logger.debug(`Branch does not need rebasing`);
      return { reuseExistingBranch: true, isModified: false };
    }