Skip to content
Snippets Groups Projects
Select Git revision
  • 1fb493ee7ac4767aaf2139abb1e668a2741fcc1f
  • main default protected
  • renovate/main-ghcr.io-renovatebot-base-image-11.x
  • refactor/pin-new-value
  • fix/user-agent
  • feat/37517-base64-private-key
  • next
  • feat/gnupg
  • fix/36615b-branch-reuse-no-cache
  • chore/punycode
  • 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
  • fix/32307-global-extends-merging
  • fix/32307-global-extends-repositories
  • 41.93.3
  • 41.93.2
  • 41.93.1
  • 41.93.0
  • 41.92.1
  • 41.92.0
  • 41.91.4
  • 41.91.3
  • 41.91.2
  • 41.91.1
  • 41.91.0
  • 41.90.1
  • 41.90.0
  • 41.89.3
  • 41.89.2
  • 41.89.1
  • 41.89.0
  • 41.88.2
  • 41.88.1
  • 41.88.0
41 results

behind-base-branch-cache.ts

Blame
  • index.spec.js 10.69 KiB
    const branchWorker = require('../../../lib/workers/branch');
    const defaultConfig = require('../../../lib/config/defaults').getConfig();
    
    const schedule = require('../../../lib/workers/branch/schedule');
    const checkExisting = require('../../../lib/workers/branch/check-existing');
    const parent = require('../../../lib/workers/branch/parent');
    const npmPostExtract = require('../../../lib/manager/npm/post-update');
    const commit = require('../../../lib/workers/branch/commit');
    const statusChecks = require('../../../lib/workers/branch/status-checks');
    const automerge = require('../../../lib/workers/branch/automerge');
    const prWorker = require('../../../lib/workers/pr');
    const getUpdated = require('../../../lib/workers/branch/get-updated');
    
    jest.mock('../../../lib/workers/branch/get-updated');
    jest.mock('../../../lib/workers/branch/schedule');
    jest.mock('../../../lib/workers/branch/check-existing');
    jest.mock('../../../lib/workers/branch/parent');
    jest.mock('../../../lib/manager/npm/post-update');
    jest.mock('../../../lib/workers/branch/status-checks');
    jest.mock('../../../lib/workers/branch/automerge');
    jest.mock('../../../lib/workers/pr');
    
    describe('workers/branch', () => {
      describe('processBranch', () => {
        let config;
        beforeEach(() => {
          prWorker.ensurePr = jest.fn();
          prWorker.checkAutoMerge = jest.fn();
          config = {
            ...defaultConfig,
            errors: [],
            warnings: [],
            upgrades: [{ depName: 'some-dep-name' }],
          };
          schedule.isScheduledNow.mockReturnValue(true);
          commit.commitFilesToBranch = jest.fn(() => true);
        });
        afterEach(() => {
          platform.ensureComment.mockClear();
          platform.ensureCommentRemoval.mockClear();
          commit.commitFilesToBranch.mockClear();
          jest.resetAllMocks();
        });
        it('skips branch if not scheduled and branch does not exist', async () => {
          schedule.isScheduledNow.mockReturnValueOnce(false);
          const res = await branchWorker.processBranch(config);
          expect(res).toEqual('not-scheduled');
        });
        it('skips branch if not scheduled and not updating out of schedule', async () => {
          schedule.isScheduledNow.mockReturnValueOnce(false);
          config.updateNotScheduled = false;
          platform.branchExists.mockReturnValueOnce(true);
          const res = await branchWorker.processBranch(config);
          expect(res).toEqual('not-scheduled');
        });
        it('skips branch if not unpublishSafe + pending', async () => {
          schedule.isScheduledNow.mockReturnValueOnce(true);
          config.unpublishSafe = true;
          config.canBeUnpublished = true;
          config.prCreation = 'not-pending';
          platform.branchExists.mockReturnValueOnce(true);
          const res = await branchWorker.processBranch(config);
          expect(res).toEqual('pending');
        });
        it('processes branch if not scheduled but updating out of schedule', async () => {
          schedule.isScheduledNow.mockReturnValueOnce(false);
          config.updateNotScheduled = true;
          platform.branchExists.mockReturnValueOnce(true);
          await branchWorker.processBranch(config);
        });
        it('skips branch if closed major PR found', async () => {
          schedule.isScheduledNow.mockReturnValueOnce(false);
          platform.branchExists.mockReturnValueOnce(true);
          config.updateType = 'major';
          checkExisting.prAlreadyExisted.mockReturnValueOnce({
            number: 13,
            state: 'closed',
          });
          await branchWorker.processBranch(config);
          expect(parent.getParentBranch.mock.calls.length).toBe(0);
        });
        it('skips branch if closed digest PR found', async () => {
          schedule.isScheduledNow.mockReturnValueOnce(false);
          platform.branchExists.mockReturnValueOnce(true);
          config.updateType = 'digest';
          checkExisting.prAlreadyExisted.mockReturnValueOnce({
            number: 13,
            state: 'closed',
          });
          await branchWorker.processBranch(config);
          expect(parent.getParentBranch.mock.calls.length).toBe(0);
        });
        it('skips branch if closed minor PR found', async () => {
          schedule.isScheduledNow.mockReturnValueOnce(false);
          platform.branchExists.mockReturnValueOnce(true);
          checkExisting.prAlreadyExisted.mockReturnValueOnce({
            number: 13,
            state: 'closed',
          });
          await branchWorker.processBranch(config);
          expect(parent.getParentBranch.mock.calls.length).toBe(0);
        });
        it('skips branch if merged PR found', async () => {
          schedule.isScheduledNow.mockReturnValueOnce(false);
          platform.branchExists.mockReturnValueOnce(true);
          checkExisting.prAlreadyExisted.mockReturnValueOnce({
            number: 13,
            state: 'merged',
          });
          await branchWorker.processBranch(config);
          expect(parent.getParentBranch.mock.calls.length).toBe(0);
        });
        it('throws error if closed PR found', async () => {
          schedule.isScheduledNow.mockReturnValueOnce(false);
          platform.branchExists.mockReturnValueOnce(true);
          platform.getBranchPr.mockReturnValueOnce({
            state: 'merged',
            canRebase: false,
          });
          await expect(branchWorker.processBranch(config)).rejects.toThrow(
            /repository-changed/
          );
        });
        it('skips branch if edited PR found', async () => {
          schedule.isScheduledNow.mockReturnValueOnce(false);
          platform.branchExists.mockReturnValueOnce(true);
          platform.getBranchPr.mockReturnValueOnce({
            state: 'open',
            canRebase: false,
          });
          const res = await branchWorker.processBranch(config);
          expect(res).toEqual('pr-edited');
        });
        it('returns if pr creation limit exceeded', async () => {
          getUpdated.getUpdatedPackageFiles.mockReturnValueOnce({
            updatedPackageFiles: [],
          });
          npmPostExtract.getAdditionalFiles.mockReturnValueOnce({
            lockFileError: false,
            updatedLockFiles: [],
          });
          platform.branchExists.mockReturnValue(false);
          config.prHourlyLimitReached = true;
          expect(await branchWorker.processBranch(config)).toEqual(
            'pr-hourly-limit-reached'
          );
        });
        it('returns if no work', async () => {
          getUpdated.getUpdatedPackageFiles.mockReturnValueOnce({
            updatedPackageFiles: [],
          });
          npmPostExtract.getAdditionalFiles.mockReturnValueOnce({
            lockFileError: false,
            updatedLockFiles: [],
          });
          platform.branchExists.mockReturnValueOnce(false);
          expect(await branchWorker.processBranch(config)).toEqual('no-work');
        });
        it('returns if branch automerged', async () => {
          getUpdated.getUpdatedPackageFiles.mockReturnValueOnce({
            updatedPackageFiles: [{}],
          });
          npmPostExtract.getAdditionalFiles.mockReturnValueOnce({
            lockFileError: false,
            updatedLockFiles: [{}],
          });
          platform.branchExists.mockReturnValueOnce(true);
          automerge.tryBranchAutomerge.mockReturnValueOnce('automerged');
          await branchWorker.processBranch(config);
          expect(statusChecks.setUnpublishable.mock.calls).toHaveLength(1);
          expect(automerge.tryBranchAutomerge.mock.calls).toHaveLength(1);
          expect(prWorker.ensurePr.mock.calls).toHaveLength(0);
        });
        it('ensures PR and tries automerge', async () => {
          getUpdated.getUpdatedPackageFiles.mockReturnValueOnce({
            updatedPackageFiles: [{}],
          });
          npmPostExtract.getAdditionalFiles.mockReturnValueOnce({
            lockFileError: false,
            updatedLockFiles: [{}],
          });
          platform.branchExists.mockReturnValueOnce(true);
          automerge.tryBranchAutomerge.mockReturnValueOnce('failed');
          prWorker.ensurePr.mockReturnValueOnce({});
          prWorker.checkAutoMerge.mockReturnValueOnce(true);
          await branchWorker.processBranch(config);
          expect(prWorker.ensurePr.mock.calls).toHaveLength(1);
          expect(platform.ensureCommentRemoval.mock.calls).toHaveLength(1);
          expect(prWorker.checkAutoMerge.mock.calls).toHaveLength(1);
        });
        it('ensures PR and adds lock file error comment', async () => {
          getUpdated.getUpdatedPackageFiles.mockReturnValueOnce({
            updatedPackageFiles: [{}],
          });
          npmPostExtract.getAdditionalFiles.mockReturnValueOnce({
            lockFileError: false,
            updatedLockFiles: [{}],
          });
          platform.branchExists.mockReturnValueOnce(true);
          automerge.tryBranchAutomerge.mockReturnValueOnce('failed');
          prWorker.ensurePr.mockReturnValueOnce({});
          prWorker.checkAutoMerge.mockReturnValueOnce(true);
          config.lockFileErrors = [{}];
          await branchWorker.processBranch(config);
          expect(platform.ensureComment.mock.calls).toHaveLength(1);
          expect(platform.ensureCommentRemoval.mock.calls).toHaveLength(0);
          expect(prWorker.ensurePr.mock.calls).toHaveLength(1);
          expect(prWorker.checkAutoMerge.mock.calls).toHaveLength(0);
        });
        it('ensures PR and adds lock file error comment recreate closed', async () => {
          getUpdated.getUpdatedPackageFiles.mockReturnValueOnce({
            updatedPackageFiles: [{}],
          });
          npmPostExtract.getAdditionalFiles.mockReturnValueOnce({
            lockFileError: false,
            updatedLockFiles: [{}],
          });
          config.recreateClosed = true;
          platform.branchExists.mockReturnValueOnce(true);
          automerge.tryBranchAutomerge.mockReturnValueOnce('failed');
          prWorker.ensurePr.mockReturnValueOnce({});
          prWorker.checkAutoMerge.mockReturnValueOnce(true);
          config.lockFileErrors = [{}];
          await branchWorker.processBranch(config);
          expect(platform.ensureComment.mock.calls).toHaveLength(1);
          expect(platform.ensureCommentRemoval.mock.calls).toHaveLength(0);
          expect(prWorker.ensurePr.mock.calls).toHaveLength(1);
          expect(prWorker.checkAutoMerge.mock.calls).toHaveLength(0);
        });
        it('swallows branch errors', async () => {
          getUpdated.getUpdatedPackageFiles.mockImplementationOnce(() => {
            throw new Error('some error');
          });
          await branchWorker.processBranch(config);
        });
        it('throws and swallows branch errors', async () => {
          getUpdated.getUpdatedPackageFiles.mockReturnValueOnce({
            updatedPackageFiles: [{}],
          });
          npmPostExtract.getAdditionalFiles.mockReturnValueOnce({
            lockFileError: true,
            updatedLockFiles: [{}],
          });
          await branchWorker.processBranch(config);
        });
        it('swallows pr errors', async () => {
          getUpdated.getUpdatedPackageFiles.mockReturnValueOnce({
            updatedPackageFiles: [{}],
          });
          npmPostExtract.getAdditionalFiles.mockReturnValueOnce({
            lockFileError: false,
            updatedLockFiles: [{}],
          });
          platform.branchExists.mockReturnValueOnce(true);
          automerge.tryBranchAutomerge.mockReturnValueOnce(false);
          prWorker.ensurePr.mockImplementationOnce(() => {
            throw new Error('some error');
          });
          await branchWorker.processBranch(config);
        });
      });
    });