Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
renovate
Manage
Activity
Members
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Model registry
Analyze
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
GitHub Mirror
Renovate Bot
renovate
Commits
79c2532a
Unverified
Commit
79c2532a
authored
2 years ago
by
RahulGautamSingh
Committed by
GitHub
2 years ago
Browse files
Options
Downloads
Patches
Plain Diff
refactor: add new fn getElapsedHours (#19892)
parent
6be73f19
Loading
Loading
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
lib/util/date.spec.ts
+37
-0
37 additions, 0 deletions
lib/util/date.spec.ts
lib/util/date.ts
+16
-0
16 additions, 0 deletions
lib/util/date.ts
lib/workers/repository/update/pr/index.ts
+3
-11
3 additions, 11 deletions
lib/workers/repository/update/pr/index.ts
with
56 additions
and
11 deletions
lib/util/date.spec.ts
0 → 100644
+
37
−
0
View file @
79c2532a
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
);
});
});
});
This diff is collapsed.
Click to expand it.
lib/util/date.ts
+
16
−
0
View file @
79c2532a
import
{
DateTime
}
from
'
luxon
'
;
const
ONE_MINUTE_MS
=
60
*
1000
;
const
ONE_MINUTE_MS
=
60
*
1000
;
const
ONE_DAY_MS
=
24
*
60
*
ONE_MINUTE_MS
;
const
ONE_DAY_MS
=
24
*
60
*
ONE_MINUTE_MS
;
...
@@ -10,3 +12,17 @@ export function getElapsedDays(timestamp: string): number {
...
@@ -10,3 +12,17 @@ export function getElapsedDays(timestamp: string): number {
export
function
getElapsedMinutes
(
date
:
Date
):
number
{
export
function
getElapsedMinutes
(
date
:
Date
):
number
{
return
Math
.
floor
((
new
Date
().
getTime
()
-
date
.
getTime
())
/
ONE_MINUTE_MS
);
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
);
}
This diff is collapsed.
Click to expand it.
lib/workers/repository/update/pr/index.ts
+
3
−
11
View file @
79c2532a
...
@@ -17,6 +17,7 @@ import {
...
@@ -17,6 +17,7 @@ import {
import
{
ensureComment
}
from
'
../../../../modules/platform/comment
'
;
import
{
ensureComment
}
from
'
../../../../modules/platform/comment
'
;
import
{
hashBody
}
from
'
../../../../modules/platform/pr-body
'
;
import
{
hashBody
}
from
'
../../../../modules/platform/pr-body
'
;
import
{
ExternalHostError
}
from
'
../../../../types/errors/external-host-error
'
;
import
{
ExternalHostError
}
from
'
../../../../types/errors/external-host-error
'
;
import
{
getElapsedHours
}
from
'
../../../../util/date
'
;
import
{
stripEmojis
}
from
'
../../../../util/emoji
'
;
import
{
stripEmojis
}
from
'
../../../../util/emoji
'
;
import
{
deleteBranch
,
getBranchLastCommitTime
}
from
'
../../../../util/git
'
;
import
{
deleteBranch
,
getBranchLastCommitTime
}
from
'
../../../../util/git
'
;
import
{
memoize
}
from
'
../../../../util/memoize
'
;
import
{
memoize
}
from
'
../../../../util/memoize
'
;
...
@@ -114,12 +115,7 @@ export async function ensurePr(
...
@@ -114,12 +115,7 @@ export async function ensurePr(
)
{
)
{
logger
.
debug
(
'
Checking how long this branch has been pending
'
);
logger
.
debug
(
'
Checking how long this branch has been pending
'
);
const
lastCommitTime
=
await
getBranchLastCommitTime
(
branchName
);
const
lastCommitTime
=
await
getBranchLastCommitTime
(
branchName
);
const
currentTime
=
new
Date
();
if
(
getElapsedHours
(
lastCommitTime
)
>=
config
.
prNotPendingHours
)
{
const
millisecondsPerHour
=
1000
*
60
*
60
;
const
elapsedHours
=
Math
.
round
(
(
currentTime
.
getTime
()
-
lastCommitTime
.
getTime
())
/
millisecondsPerHour
);
if
(
elapsedHours
>=
config
.
prNotPendingHours
)
{
logger
.
debug
(
'
Branch exceeds prNotPending hours - forcing PR creation
'
);
logger
.
debug
(
'
Branch exceeds prNotPending hours - forcing PR creation
'
);
config
.
forcePr
=
true
;
config
.
forcePr
=
true
;
}
}
...
@@ -153,11 +149,7 @@ export async function ensurePr(
...
@@ -153,11 +149,7 @@ export async function ensurePr(
if
((
await
getBranchStatus
())
===
'
yellow
'
)
{
if
((
await
getBranchStatus
())
===
'
yellow
'
)
{
logger
.
debug
(
`Branch status is yellow - checking timeout`
);
logger
.
debug
(
`Branch status is yellow - checking timeout`
);
const
lastCommitTime
=
await
getBranchLastCommitTime
(
branchName
);
const
lastCommitTime
=
await
getBranchLastCommitTime
(
branchName
);
const
currentTime
=
new
Date
();
const
elapsedHours
=
getElapsedHours
(
lastCommitTime
);
const
millisecondsPerHour
=
1000
*
60
*
60
;
const
elapsedHours
=
Math
.
round
(
(
currentTime
.
getTime
()
-
lastCommitTime
.
getTime
())
/
millisecondsPerHour
);
if
(
if
(
!
dependencyDashboardCheck
&&
!
dependencyDashboardCheck
&&
((
config
.
stabilityStatus
&&
config
.
stabilityStatus
!==
'
yellow
'
)
||
((
config
.
stabilityStatus
&&
config
.
stabilityStatus
!==
'
yellow
'
)
||
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment