From 4a41a22a485793674f683f44cd512a09326d702e Mon Sep 17 00:00:00 2001
From: Rhys Arkins <rhys@arkins.net>
Date: Thu, 23 Jul 2020 15:23:16 +0200
Subject: [PATCH] fix: commit limit when rebasing (#6837)

---
 lib/workers/branch/index.spec.ts              | 15 ++++++++++++++-
 lib/workers/branch/index.ts                   | 19 +++++++++++++------
 lib/workers/common.ts                         |  3 ++-
 .../repository/dependency-dashboard.spec.ts   |  4 ++--
 .../repository/dependency-dashboard.ts        |  6 ++++--
 lib/workers/repository/process/write.ts       |  7 +++----
 6 files changed, 38 insertions(+), 16 deletions(-)

diff --git a/lib/workers/branch/index.spec.ts b/lib/workers/branch/index.spec.ts
index 2a1e431b39..e8b19bdcda 100644
--- a/lib/workers/branch/index.spec.ts
+++ b/lib/workers/branch/index.spec.ts
@@ -216,7 +216,20 @@ describe('workers/branch', () => {
       });
       git.branchExists.mockResolvedValue(false);
       expect(await branchWorker.processBranch(config, true)).toEqual(
-        'pr-hourly-limit-reached'
+        'pr-limit-reached'
+      );
+    });
+    it('returns if commit limit exceeded', async () => {
+      getUpdated.getUpdatedPackageFiles.mockResolvedValueOnce({
+        ...updatedPackageFiles,
+      });
+      npmPostExtract.getAdditionalFiles.mockResolvedValueOnce({
+        artifactErrors: [],
+        updatedArtifacts: [],
+      });
+      git.branchExists.mockResolvedValue(false);
+      expect(await branchWorker.processBranch(config, false, true)).toEqual(
+        'commit-limit-reached'
       );
     });
     it('returns if no work', async () => {
diff --git a/lib/workers/branch/index.ts b/lib/workers/branch/index.ts
index 44c884be16..6c9df3ff13 100644
--- a/lib/workers/branch/index.ts
+++ b/lib/workers/branch/index.ts
@@ -56,7 +56,8 @@ const rebasingRegex = /\*\*Rebasing\*\*: .*/;
 
 export async function processBranch(
   branchConfig: BranchConfig,
-  prHourlyLimitReached?: boolean
+  prLimitReached?: boolean,
+  commitLimitReached?: boolean
 ): Promise<ProcessBranchResult> {
   const config: BranchConfig = { ...branchConfig };
   const dependencies = config.upgrades
@@ -148,14 +149,20 @@ export async function processBranch(
     }
     if (
       !branchExists &&
-      prHourlyLimitReached &&
+      prLimitReached &&
       !dependencyDashboardCheck &&
       !config.vulnerabilityAlert
     ) {
-      logger.debug(
-        'Reached PR creation limit or per run commits limit - skipping branch creation'
-      );
-      return 'pr-hourly-limit-reached';
+      logger.debug('Reached PR limit - skipping branch creation');
+      return 'pr-limit-reached';
+    }
+    if (
+      commitLimitReached &&
+      !dependencyDashboardCheck &&
+      !config.vulnerabilityAlert
+    ) {
+      logger.debug('Reached commits limit - skipping branch');
+      return 'commit-limit-reached';
     }
     if (branchExists) {
       logger.debug('Checking if PR has been edited');
diff --git a/lib/workers/common.ts b/lib/workers/common.ts
index 5082cb40eb..7ba9299753 100644
--- a/lib/workers/common.ts
+++ b/lib/workers/common.ts
@@ -86,7 +86,8 @@ export type ProcessBranchResult =
   | 'pending'
   | 'pr-created'
   | 'pr-edited'
-  | 'pr-hourly-limit-reached'
+  | 'pr-limit-reached'
+  | 'commit-limit-reached'
   | 'rebase';
 
 export interface BranchConfig
diff --git a/lib/workers/repository/dependency-dashboard.spec.ts b/lib/workers/repository/dependency-dashboard.spec.ts
index 2717c76ae9..47b8076026 100644
--- a/lib/workers/repository/dependency-dashboard.spec.ts
+++ b/lib/workers/repository/dependency-dashboard.spec.ts
@@ -169,14 +169,14 @@ describe('workers/repository/master-issue', () => {
           ...mock<BranchConfig>(),
           prTitle: 'pr5',
           upgrades: [{ ...mock<PrUpgrade>(), depName: 'dep5' }],
-          res: 'pr-hourly-limit-reached',
+          res: 'pr-limit-reached',
           branchName: 'branchName5',
         },
         {
           ...mock<BranchConfig>(),
           prTitle: 'pr6',
           upgrades: [{ ...mock<PrUpgrade>(), depName: 'dep6' }],
-          res: 'pr-hourly-limit-reached',
+          res: 'pr-limit-reached',
           branchName: 'branchName6',
         },
         {
diff --git a/lib/workers/repository/dependency-dashboard.ts b/lib/workers/repository/dependency-dashboard.ts
index c80d728972..69d62579bc 100644
--- a/lib/workers/repository/dependency-dashboard.ts
+++ b/lib/workers/repository/dependency-dashboard.ts
@@ -96,7 +96,8 @@ export async function ensureMasterIssue(
     issueBody += '\n';
   }
   const rateLimited = branches.filter(
-    (branch) => branch.res && branch.res.endsWith('pr-hourly-limit-reached')
+    (branch) =>
+      branch.res === 'pr-limit-reached' || branch.res === 'commit-limit-reached'
   );
   if (rateLimited.length) {
     issueBody += '## Rate Limited\n\n';
@@ -155,7 +156,8 @@ export async function ensureMasterIssue(
     'needs-approval',
     'needs-pr-approval',
     'not-scheduled',
-    'pr-hourly-limit-reached',
+    'pr-limit-reached',
+    'commit-limit-reached',
     'already-existed',
     'error',
     'automerged',
diff --git a/lib/workers/repository/process/write.ts b/lib/workers/repository/process/write.ts
index 432b3b6cab..1ef19d351c 100644
--- a/lib/workers/repository/process/write.ts
+++ b/lib/workers/repository/process/write.ts
@@ -31,10 +31,9 @@ export async function writeUpdates(
   logger.debug({ prsRemaining }, 'Calculated maximum PRs remaining this run');
   for (const branch of branches) {
     addMeta({ branch: branch.branchName });
-    const res = await processBranch(
-      branch,
-      prsRemaining <= 0 || getLimitRemaining('prCommitsPerRunLimit') <= 0
-    );
+    const prLimitReached = prsRemaining <= 0;
+    const commitLimitReached = getLimitRemaining('prCommitsPerRunLimit') <= 0;
+    const res = await processBranch(branch, prLimitReached, commitLimitReached);
     branch.res = res;
     if (res === 'automerged' && branch.automergeType !== 'pr-comment') {
       // Stop procesing other branches because base branch has been changed
-- 
GitLab