From 79c2532a2d08633db8a564779d896a80461011b8 Mon Sep 17 00:00:00 2001
From: RahulGautamSingh <rahultesnik@gmail.com>
Date: Fri, 20 Jan 2023 14:49:08 +0530
Subject: [PATCH] refactor: add new fn getElapsedHours (#19892)

---
 lib/util/date.spec.ts                     | 37 +++++++++++++++++++++++
 lib/util/date.ts                          | 16 ++++++++++
 lib/workers/repository/update/pr/index.ts | 14 ++-------
 3 files changed, 56 insertions(+), 11 deletions(-)
 create mode 100644 lib/util/date.spec.ts

diff --git a/lib/util/date.spec.ts b/lib/util/date.spec.ts
new file mode 100644
index 0000000000..22e39a6b26
--- /dev/null
+++ b/lib/util/date.spec.ts
@@ -0,0 +1,37 @@
+import { getElapsedDays, getElapsedHours, getElapsedMinutes } from './date';
+
+const ONE_MINUTE_MS = 60 * 1000;
+const ONE_HOUR_MS = 60 * ONE_MINUTE_MS;
+const ONE_DAY_MS = 24 * ONE_HOUR_MS;
+
+describe('util/date', () => {
+  const Jan1 = new Date(new Date().getFullYear(), 0, 1);
+
+  it('returns elapsed days', () => {
+    const elapsedDays = Math.floor(
+      (new Date().getTime() - new Date(Jan1).getTime()) / ONE_DAY_MS
+    );
+    expect(getElapsedDays(Jan1.toDateString())).toBe(elapsedDays);
+  });
+
+  it('returns elapsed minutes', () => {
+    const elapsedMinutes = Math.floor(
+      (new Date().getTime() - new Date(Jan1).getTime()) / ONE_MINUTE_MS
+    );
+    expect(getElapsedMinutes(new Date(Jan1))).toBe(elapsedMinutes);
+  });
+
+  describe('getElapsedHours', () => {
+    it('returns elapsed hours', () => {
+      const elapsedHours = Math.floor(
+        (new Date().getTime() - new Date(Jan1).getTime()) / ONE_HOUR_MS
+      );
+      expect(getElapsedHours(Jan1.toISOString())).toBe(elapsedHours); // ISOstring
+      expect(getElapsedHours(Jan1)).toBe(elapsedHours); // JS Date
+    });
+
+    it('throws when invalid date is passed', () => {
+      expect(getElapsedHours(new Date('invalid_date_string'))).toBe(0);
+    });
+  });
+});
diff --git a/lib/util/date.ts b/lib/util/date.ts
index 93d4f46f00..4ef9ff01bc 100644
--- a/lib/util/date.ts
+++ b/lib/util/date.ts
@@ -1,3 +1,5 @@
+import { DateTime } from 'luxon';
+
 const ONE_MINUTE_MS = 60 * 1000;
 const ONE_DAY_MS = 24 * 60 * ONE_MINUTE_MS;
 
@@ -10,3 +12,17 @@ export function getElapsedDays(timestamp: string): number {
 export function getElapsedMinutes(date: Date): number {
   return Math.floor((new Date().getTime() - date.getTime()) / ONE_MINUTE_MS);
 }
+
+export function getElapsedHours(date: Date | string): number {
+  const lastDate =
+    typeof date === 'string'
+      ? DateTime.fromISO(date)
+      : DateTime.fromJSDate(date);
+
+  if (!lastDate.isValid) {
+    return 0;
+  }
+
+  const diff = DateTime.now().diff(lastDate, 'hours');
+  return Math.floor(diff.hours);
+}
diff --git a/lib/workers/repository/update/pr/index.ts b/lib/workers/repository/update/pr/index.ts
index be6813cc43..5091ef1abf 100644
--- a/lib/workers/repository/update/pr/index.ts
+++ b/lib/workers/repository/update/pr/index.ts
@@ -17,6 +17,7 @@ import {
 import { ensureComment } from '../../../../modules/platform/comment';
 import { hashBody } from '../../../../modules/platform/pr-body';
 import { ExternalHostError } from '../../../../types/errors/external-host-error';
+import { getElapsedHours } from '../../../../util/date';
 import { stripEmojis } from '../../../../util/emoji';
 import { deleteBranch, getBranchLastCommitTime } from '../../../../util/git';
 import { memoize } from '../../../../util/memoize';
@@ -114,12 +115,7 @@ export async function ensurePr(
     ) {
       logger.debug('Checking how long this branch has been pending');
       const lastCommitTime = await getBranchLastCommitTime(branchName);
-      const currentTime = new Date();
-      const millisecondsPerHour = 1000 * 60 * 60;
-      const elapsedHours = Math.round(
-        (currentTime.getTime() - lastCommitTime.getTime()) / millisecondsPerHour
-      );
-      if (elapsedHours >= config.prNotPendingHours) {
+      if (getElapsedHours(lastCommitTime) >= config.prNotPendingHours) {
         logger.debug('Branch exceeds prNotPending hours - forcing PR creation');
         config.forcePr = true;
       }
@@ -153,11 +149,7 @@ export async function ensurePr(
     if ((await getBranchStatus()) === 'yellow') {
       logger.debug(`Branch status is yellow - checking timeout`);
       const lastCommitTime = await getBranchLastCommitTime(branchName);
-      const currentTime = new Date();
-      const millisecondsPerHour = 1000 * 60 * 60;
-      const elapsedHours = Math.round(
-        (currentTime.getTime() - lastCommitTime.getTime()) / millisecondsPerHour
-      );
+      const elapsedHours = getElapsedHours(lastCommitTime);
       if (
         !dependencyDashboardCheck &&
         ((config.stabilityStatus && config.stabilityStatus !== 'yellow') ||
-- 
GitLab