Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(helm): convert to typescript #4859

Merged
merged 1 commit into from
Nov 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
41 changes: 22 additions & 19 deletions lib/datasource/helm/index.js → lib/datasource/helm/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import yaml from 'js-yaml';

import { PkgReleaseConfig, ReleaseResult } from '../common';
import got from '../../util/got';
import { logger } from '../../logger';

export async function getPkgReleases({ lookupName, registryUrls }) {
export async function getPkgReleases({
lookupName,
registryUrls,
}: PkgReleaseConfig): Promise<ReleaseResult | null> {
if (!lookupName) {
logger.warn(`lookupName was not provided to getPkgReleases`);
return null;
Expand All @@ -18,27 +22,27 @@ export async function getPkgReleases({ lookupName, registryUrls }) {
logger.warn(`Couldn't get index.yaml file from ${helmRepository}`);
return null;
}
const releases = repositoryData[lookupName];
const releases = repositoryData.find(chart => chart.name === lookupName);
if (!releases) {
logger.warn(
{ dependency: lookupName },
`Entry ${lookupName} doesn't exist in index.yaml from ${helmRepository}`
);
return null;
}
return {
releases,
};
return releases;
}

export async function getRepositoryData(repository) {
export async function getRepositoryData(
repository: string
): Promise<ReleaseResult[]> {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What was the reason for needing to switch the return value from being an object to it being an array?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The previous return type was:

interface RepositoryData {
  [key: string]: {
    version: string;
    homepage: string;
    sources: string[];
    urls: string[]
  }[]
}

Compared with ReleaseResult that we use everywhere else:

interface ReleaseResult {
  name: string;
  homepage: string;
  sourceUrl: string;
  releases: {
    version: string;
    ....
  }[]
  ...
}

It was just much easier to re-use the interface we use everywhere else, including the calling method, instead of creating a new type specifically for Helm.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I missed that

const cacheNamespace = 'datasource-helm';
const cacheKey = repository;
const cachedIndex = await renovateCache.get(cacheNamespace, cacheKey);
if (cachedIndex) {
return cachedIndex;
}
let res;
let res: any;
try {
res = await got('index.yaml', { baseUrl: repository });
if (!res || !res.body) {
Expand All @@ -60,29 +64,28 @@ export async function getRepositoryData(repository) {
logger.warn({ err }, `${repository} lookup failure: Unknown error`);
return null;
}
let result;
try {
const doc = yaml.safeLoad(res.body, { json: true });
if (!doc) {
logger.warn(`Failed to parse index.yaml from ${repository}`);
return null;
}
result = {};
Object.keys(doc.entries).forEach(depName => {
const versions = doc.entries[depName].map(release => ({
version: release.version,
homepage: release.home,
sources: release.sources,
urls: release.urls,
}));
result[depName] = versions;
});
const result: ReleaseResult[] = Object.entries(doc.entries).map(
([k, v]: [string, any]): ReleaseResult => ({
name: k,
homepage: v[0].home,
sourceUrl: v[0].sources ? v[0].sources[0] : undefined,
releases: v.map((x: any) => ({
version: x.version,
})),
})
);
const cacheMinutes = 20;
await renovateCache.set(cacheNamespace, cacheKey, result, cacheMinutes);
return result;
} catch (err) {
logger.warn(`Failed to parse index.yaml from ${repository}`);
logger.debug(err);
return null;
}
return result;
}