-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(helm): convert to class-based datasource (#10425)
- Loading branch information
1 parent
3a9fc4f
commit 880b7fb
Showing
8 changed files
with
96 additions
and
110 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,104 +1,90 @@ | ||
import is from '@sindresorhus/is'; | ||
import { load } from 'js-yaml'; | ||
import { logger } from '../../logger'; | ||
import { ExternalHostError } from '../../types/errors/external-host-error'; | ||
import * as packageCache from '../../util/cache/package'; | ||
import { Http } from '../../util/http'; | ||
import { cache } from '../../util/cache/package/decorator'; | ||
import { ensureTrailingSlash } from '../../util/url'; | ||
import { Datasource } from '../datasource'; | ||
import type { GetReleasesConfig, ReleaseResult } from '../types'; | ||
import type { HelmRepository, RepositoryData } from './types'; | ||
|
||
export const id = 'helm'; | ||
export class HelmDatasource extends Datasource { | ||
static readonly id = 'helm'; | ||
|
||
const http = new Http(id); | ||
constructor() { | ||
super(HelmDatasource.id); | ||
} | ||
|
||
export const customRegistrySupport = true; | ||
export const defaultRegistryUrls = ['https://charts.helm.sh/stable']; | ||
export const registryStrategy = 'first'; | ||
readonly defaultRegistryUrls = ['https://charts.helm.sh/stable']; | ||
|
||
export const defaultConfig = { | ||
commitMessageTopic: 'Helm release {{depName}}', | ||
group: { | ||
commitMessageTopic: '{{{groupName}}} Helm releases', | ||
}, | ||
}; | ||
readonly defaultConfig = { | ||
commitMessageTopic: 'Helm release {{depName}}', | ||
group: { | ||
commitMessageTopic: '{{{groupName}}} Helm releases', | ||
}, | ||
}; | ||
|
||
export async function getRepositoryData( | ||
repository: string | ||
): Promise<RepositoryData> { | ||
const cacheNamespace = 'datasource-helm'; | ||
const cacheKey = repository; | ||
const cachedIndex = await packageCache.get<RepositoryData>( | ||
cacheNamespace, | ||
cacheKey | ||
); | ||
// istanbul ignore if | ||
if (cachedIndex) { | ||
return cachedIndex; | ||
} | ||
let res: any; | ||
try { | ||
res = await http.get('index.yaml', { | ||
baseUrl: ensureTrailingSlash(repository), | ||
}); | ||
if (!res || !res.body) { | ||
logger.warn(`Received invalid response from ${repository}`); | ||
return null; | ||
} | ||
} catch (err) { | ||
if ( | ||
err.statusCode === 429 || | ||
(err.statusCode >= 500 && err.statusCode < 600) | ||
) { | ||
throw new ExternalHostError(err); | ||
@cache({ | ||
namespace: `datasource-${HelmDatasource.id}`, | ||
key: (repository: string) => repository, | ||
}) | ||
async getRepositoryData(repository: string): Promise<RepositoryData | null> { | ||
let res: any; | ||
try { | ||
res = await this.http.get('index.yaml', { | ||
baseUrl: ensureTrailingSlash(repository), | ||
}); | ||
if (!res || !res.body) { | ||
logger.warn(`Received invalid response from ${repository}`); | ||
return null; | ||
} | ||
} catch (err) { | ||
this.handleGenericErrors(err); | ||
} | ||
throw err; | ||
} | ||
try { | ||
const doc = load(res.body, { | ||
json: true, | ||
}) as HelmRepository; | ||
if (!is.plainObject<HelmRepository>(doc)) { | ||
try { | ||
const doc = load(res.body, { | ||
json: true, | ||
}) as HelmRepository; | ||
if (!is.plainObject<HelmRepository>(doc)) { | ||
logger.warn(`Failed to parse index.yaml from ${repository}`); | ||
return null; | ||
} | ||
const result: RepositoryData = {}; | ||
for (const [name, releases] of Object.entries(doc.entries)) { | ||
result[name] = { | ||
homepage: releases[0].home, | ||
sourceUrl: releases[0].sources ? releases[0].sources[0] : undefined, | ||
releases: releases.map((release) => ({ | ||
version: release.version, | ||
releaseTimestamp: release.created ? release.created : null, | ||
})), | ||
}; | ||
} | ||
|
||
return result; | ||
} catch (err) { | ||
logger.warn(`Failed to parse index.yaml from ${repository}`); | ||
logger.debug(err); | ||
return null; | ||
} | ||
const result: RepositoryData = {}; | ||
for (const [name, releases] of Object.entries(doc.entries)) { | ||
result[name] = { | ||
homepage: releases[0].home, | ||
sourceUrl: releases[0].sources ? releases[0].sources[0] : undefined, | ||
releases: releases.map((release) => ({ | ||
version: release.version, | ||
releaseTimestamp: release.created ? release.created : null, | ||
})), | ||
}; | ||
} | ||
const cacheMinutes = 20; | ||
await packageCache.set(cacheNamespace, cacheKey, result, cacheMinutes); | ||
return result; | ||
} catch (err) { | ||
logger.warn(`Failed to parse index.yaml from ${repository}`); | ||
logger.debug(err); | ||
return null; | ||
} | ||
} | ||
|
||
export async function getReleases({ | ||
lookupName, | ||
registryUrl: helmRepository, | ||
}: GetReleasesConfig): Promise<ReleaseResult | null> { | ||
const repositoryData = await getRepositoryData(helmRepository); | ||
if (!repositoryData) { | ||
logger.debug(`Couldn't get index.yaml file from ${helmRepository}`); | ||
return null; | ||
} | ||
const releases = repositoryData[lookupName]; | ||
if (!releases) { | ||
logger.debug( | ||
{ dependency: lookupName }, | ||
`Entry ${lookupName} doesn't exist in index.yaml from ${helmRepository}` | ||
); | ||
return null; | ||
async getReleases({ | ||
lookupName, | ||
registryUrl: helmRepository, | ||
}: GetReleasesConfig): Promise<ReleaseResult | null> { | ||
const repositoryData = await this.getRepositoryData(helmRepository); | ||
if (!repositoryData) { | ||
logger.debug(`Couldn't get index.yaml file from ${helmRepository}`); | ||
return null; | ||
} | ||
const releases = repositoryData[lookupName]; | ||
if (!releases) { | ||
logger.debug( | ||
{ dependency: lookupName }, | ||
`Entry ${lookupName} doesn't exist in index.yaml from ${helmRepository}` | ||
); | ||
return null; | ||
} | ||
return releases; | ||
} | ||
return releases; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters