refactor: terraform registryUrls

This commit is contained in:
Rhys Arkins 2019-01-29 18:24:51 +01:00
parent 0f12451c5c
commit ba59a21505
4 changed files with 28 additions and 15 deletions

View file

@ -1,5 +1,6 @@
const got = require('got');
const parse = require('github-url-from-git');
const is = require('@sindresorhus/is');
module.exports = {
getPkgReleases,
@ -13,12 +14,14 @@ module.exports = {
* - `homepage` is set to the Terraform registry's page if it's on the official main registry
*/
async function getPkgReleases(purl) {
const { lookupName: dependency, qualifiers } = purl;
const registry = qualifiers.registry || 'registry.terraform.io';
async function getPkgReleases(purl, config) {
const { lookupName: dependency } = purl;
const registry = is.nonEmptyArray(config.repositoryUrls)
? config.repositoryUrls[0]
: 'https://registry.terraform.io';
logger.debug({ dependency, registry }, 'terraform.getDependencies()');
const cacheNamespace = 'terraform';
const pkgUrl = `https://${registry}/v1/modules/${dependency}`;
const pkgUrl = `${registry}/v1/modules/${dependency}`;
const cachedResult = await renovateCache.get(cacheNamespace, pkgUrl);
// istanbul ignore if
if (cachedResult) {

View file

@ -71,11 +71,11 @@ function extractPackageFile(content) {
dep.purl = 'pkg:terraform/' + dep.depName;
} else if (moduleParts.length === 4) {
dep.depType = 'terraform';
dep.registry = moduleParts.shift();
dep.registryUrls = ['https://' + moduleParts.shift()];
dep.depName = moduleParts.join('/');
dep.depNameShort = dep.depName;
dep.lineNumber = dep.versionLine;
dep.purl = 'pkg:terraform/' + dep.depName + '?registry=' + dep.registry;
dep.purl = 'pkg:terraform/' + dep.depName;
}
if (dep.lineNumber) {
if (!isValid(dep.currentValue)) {

View file

@ -59,9 +59,13 @@ describe('datasource/terraform', () => {
got.mockReturnValueOnce({
body: JSON.parse(consulData),
});
const res = await datasource.getPkgReleases({
purl: 'pkg:terraform/consul/foo?registry=hashicorp',
});
const config = { registryUrls: 'https://terraform.company.com' };
const res = await datasource.getPkgReleases(
{
purl: 'pkg:terraform/consul/foo',
},
config
);
expect(res).toBeNull();
});
});

View file

@ -91,8 +91,10 @@ Object {
"depType": "terraform",
"lineNumber": 71,
"moduleName": "consul",
"purl": "pkg:terraform/example-corp/k8s-cluster/azurerm?registry=app.terraform.io",
"registry": "app.terraform.io",
"purl": "pkg:terraform/example-corp/k8s-cluster/azurerm",
"registryUrls": Array [
"https://app.terraform.io",
],
"source": "app.terraform.io/example-corp/k8s-cluster/azurerm",
},
Object {
@ -102,8 +104,10 @@ Object {
"depType": "terraform",
"lineNumber": 76,
"moduleName": "consul2",
"purl": "pkg:terraform/example-corp/k8s-cluster/azurerm?registry=app.terraform.io",
"registry": "app.terraform.io",
"purl": "pkg:terraform/example-corp/k8s-cluster/azurerm",
"registryUrls": Array [
"https://app.terraform.io",
],
"source": "app.terraform.io/example-corp/k8s-cluster/azurerm",
},
Object {
@ -113,8 +117,10 @@ Object {
"depType": "terraform",
"lineNumber": 81,
"moduleName": "consul3",
"purl": "pkg:terraform/example-corp/k8s-cluster/azurerm?registry=app.terraform.io",
"registry": "app.terraform.io",
"purl": "pkg:terraform/example-corp/k8s-cluster/azurerm",
"registryUrls": Array [
"https://app.terraform.io",
],
"skipReason": "unsupported-version",
"source": "app.terraform.io/example-corp/k8s-cluster/azurerm",
},