Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
renovate
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
SmartDataLab
public
applications
renovate
Commits
c1fd79ba
Commit
c1fd79ba
authored
6 years ago
by
Rhys Arkins
Browse files
Options
Downloads
Patches
Plain Diff
feat: cache github datasource results for 10 minutes
parent
6cf6eab8
No related branches found
No related tags found
No related merge requests found
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
lib/datasource/github.js
+49
-0
49 additions, 0 deletions
lib/datasource/github.js
test/datasource/__snapshots__/github.spec.js.snap
+16
-0
16 additions, 0 deletions
test/datasource/__snapshots__/github.spec.js.snap
test/datasource/github.spec.js
+14
-0
14 additions, 0 deletions
test/datasource/github.spec.js
with
79 additions
and
0 deletions
lib/datasource/github.js
+
49
−
0
View file @
c1fd79ba
const
cacache
=
require
(
'
cacache/en
'
);
const
os
=
require
(
'
os
'
);
const
{
DateTime
}
=
require
(
'
luxon
'
);
const
ghGot
=
require
(
'
../platform/github/gh-got-wrapper
'
);
const
ghGot
=
require
(
'
../platform/github/gh-got-wrapper
'
);
const
versioning
=
require
(
'
../versioning
'
);
const
versioning
=
require
(
'
../versioning
'
);
module
.
exports
=
{
module
.
exports
=
{
getPreset
,
getPreset
,
getPkgReleases
,
getPkgReleases
,
rmAllCache
,
};
};
const
datasourceCache
=
(
process
.
env
.
RENOVATE_TMPDIR
||
os
.
tmpdir
())
+
'
/renovate-gh-datasource-cache-v1
'
;
async
function
getCachedResult
(
repo
,
type
)
{
try
{
const
cacheVal
=
await
cacache
.
get
(
datasourceCache
,
`
${
repo
}
-
${
type
}
`
);
const
cachedResult
=
JSON
.
parse
(
cacheVal
.
data
.
toString
());
if
(
cachedResult
)
{
if
(
DateTime
.
local
()
<
DateTime
.
fromISO
(
cachedResult
.
expiry
))
{
logger
.
debug
(
{
repo
,
type
},
'
Returning cached github datasource result
'
);
delete
cachedResult
.
expiry
;
return
cachedResult
;
}
// istanbul ignore next
logger
.
debug
(
'
Cache expiry
'
);
}
}
catch
(
err
)
{
logger
.
debug
(
'
Cache miss
'
);
}
return
null
;
}
async
function
setCachedResult
(
repo
,
type
,
res
)
{
logger
.
debug
({
repo
,
type
},
'
Saving cached github datasource
'
);
await
cacache
.
put
(
datasourceCache
,
`
${
repo
}
-
${
type
}
`
,
JSON
.
stringify
({
...
res
,
expiry
:
DateTime
.
local
().
plus
({
minutes
:
10
})
})
);
}
async
function
rmAllCache
()
{
await
cacache
.
rm
.
all
(
datasourceCache
);
}
const
map
=
new
Map
();
const
map
=
new
Map
();
async
function
getPreset
(
pkgName
,
presetName
=
'
default
'
)
{
async
function
getPreset
(
pkgName
,
presetName
=
'
default
'
)
{
...
@@ -35,6 +79,10 @@ async function getPkgReleases(purl, config) {
...
@@ -35,6 +79,10 @@ async function getPkgReleases(purl, config) {
const
{
versionScheme
}
=
config
||
{};
const
{
versionScheme
}
=
config
||
{};
const
{
fullname
:
repo
,
qualifiers
:
options
}
=
purl
;
const
{
fullname
:
repo
,
qualifiers
:
options
}
=
purl
;
let
versions
;
let
versions
;
const
cachedResult
=
await
getCachedResult
(
repo
,
options
.
ref
);
if
(
cachedResult
)
{
return
cachedResult
;
}
try
{
try
{
if
(
options
.
ref
===
'
release
'
)
{
if
(
options
.
ref
===
'
release
'
)
{
const
url
=
`repos/
${
repo
}
/releases?per_page=100`
;
const
url
=
`repos/
${
repo
}
/releases?per_page=100`
;
...
@@ -69,5 +117,6 @@ async function getPkgReleases(purl, config) {
...
@@ -69,5 +117,6 @@ async function getPkgReleases(purl, config) {
version
:
options
.
sanitize
===
'
true
'
?
isVersion
(
version
)
:
version
,
version
:
options
.
sanitize
===
'
true
'
?
isVersion
(
version
)
:
version
,
gitRef
:
version
,
gitRef
:
version
,
}));
}));
setCachedResult
(
repo
,
options
.
ref
,
dependency
);
return
dependency
;
return
dependency
;
}
}
This diff is collapsed.
Click to expand it.
test/datasource/__snapshots__/github.spec.js.snap
+
16
−
0
View file @
c1fd79ba
...
@@ -31,3 +31,19 @@ Object {
...
@@ -31,3 +31,19 @@ Object {
"repositoryUrl": "https://github.com/some/dep",
"repositoryUrl": "https://github.com/some/dep",
}
}
`;
`;
exports[`datasource/github getPkgReleases returns releases from cache 1`] = `
Object {
"releases": Array [
Object {
"gitRef": "1.0.0",
"version": "1.0.0",
},
Object {
"gitRef": "v1.1.0",
"version": "v1.1.0",
},
],
"repositoryUrl": "https://github.com/some/dep",
}
`;
This diff is collapsed.
Click to expand it.
test/datasource/github.spec.js
+
14
−
0
View file @
c1fd79ba
const
delay
=
require
(
'
delay
'
);
const
datasource
=
require
(
'
../../lib/datasource
'
);
const
datasource
=
require
(
'
../../lib/datasource
'
);
const
github
=
require
(
'
../../lib/datasource/github
'
);
const
github
=
require
(
'
../../lib/datasource/github
'
);
const
ghGot
=
require
(
'
../../lib/platform/github/gh-got-wrapper
'
);
const
ghGot
=
require
(
'
../../lib/platform/github/gh-got-wrapper
'
);
...
@@ -37,6 +39,7 @@ describe('datasource/github', () => {
...
@@ -37,6 +39,7 @@ describe('datasource/github', () => {
});
});
});
});
describe
(
'
getPkgReleases
'
,
()
=>
{
describe
(
'
getPkgReleases
'
,
()
=>
{
beforeAll
(()
=>
github
.
rmAllCache
());
it
(
'
returns cleaned tags
'
,
async
()
=>
{
it
(
'
returns cleaned tags
'
,
async
()
=>
{
const
body
=
[
const
body
=
[
{
name
:
'
a
'
},
{
name
:
'
a
'
},
...
@@ -71,6 +74,17 @@ describe('datasource/github', () => {
...
@@ -71,6 +74,17 @@ describe('datasource/github', () => {
res
.
releases
.
find
(
release
=>
release
.
version
===
'
v1.1.0
'
)
res
.
releases
.
find
(
release
=>
release
.
version
===
'
v1.1.0
'
)
).
toBeDefined
();
).
toBeDefined
();
});
});
it
(
'
returns releases from cache
'
,
async
()
=>
{
await
delay
(
1000
);
const
res
=
await
datasource
.
getPkgReleases
(
'
pkg:github/some/dep?ref=release
'
);
expect
(
res
).
toMatchSnapshot
();
expect
(
res
.
releases
).
toHaveLength
(
2
);
expect
(
res
.
releases
.
find
(
release
=>
release
.
version
===
'
v1.1.0
'
)
).
toBeDefined
();
});
it
(
'
returns null for invalid ref
'
,
async
()
=>
{
it
(
'
returns null for invalid ref
'
,
async
()
=>
{
expect
(
expect
(
await
datasource
.
getPkgReleases
(
'
pkg:github/some/dep?ref=invalid
'
)
await
datasource
.
getPkgReleases
(
'
pkg:github/some/dep?ref=invalid
'
)
...
...
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