Fix filtering deprecated charts (#3635)

This commit is contained in:
Sebastian Malton authored and GitHub committed 2021-08-24 08:29:36 -04:00
1 parent 54ac311933
commit 5f89b3e31f
10 files changed
+324 -284

No files matched your search

@@ -26,8 +26,7 @@ import type { RequestInit } from "node-fetch";
import { autoBind, bifurcateArray } from "../../utils";
import Joi from "joi";
export type RepoHelmChartList = Record<string, HelmChart[]>;
export type HelmChartList = Record<string, RepoHelmChartList>;
export type RepoHelmChartList = Record<string, RawHelmChart[]>;
export interface IHelmChartDetails {
readme: string;
@@ -43,7 +42,7 @@ const endpoint = compile(`/v2/charts/:repo?/:name?`) as (params?: {
* Get a list of all helm charts from all saved helm repos
*/
export async function listCharts(): Promise<HelmChart[]> {
const data = await apiBase.get<HelmChartList>(endpoint());
const data = await apiBase.get<Record<string, RepoHelmChartList>>(endpoint());
return Object
.values(data)
@@ -311,11 +310,9 @@ export class HelmChart {
}
static create(data: RawHelmChart, { onError = "throw" }: HelmChartCreateOpts = {}): HelmChart | undefined {
const result = helmChartValidator.validate(data, {
const { value, error } = helmChartValidator.validate(data, {
abortEarly: false,
});
let { error } = result;
const { value } = result;
if (!error) {
return new HelmChart(value);
@@ -331,13 +328,13 @@ export class HelmChart {
return new HelmChart(value);
}
error = new Joi.ValidationError(actualErrors.map(er => er.message).join(". "), actualErrors, error._original);
const validationError = new Joi.ValidationError(actualErrors.map(er => er.message).join(". "), actualErrors, error._original);
if (onError === "throw") {
throw error;
throw validationError;
}
console.warn("[HELM-CHART]: failed to validate data", data, error);
console.warn("[HELM-CHART]: failed to validate data", data, validationError);
return undefined;
}
+3 -1
View File
@@ -31,7 +31,9 @@ const logLevel = process.env.LOG_LEVEL
? process.env.LOG_LEVEL
: isDebugging
? "debug"
: "info";
: isTestEnv
? "error"
: "info";
const transports: Transport[] = [
new SentryTransport("error")
+33 -1
View File
@@ -19,7 +19,10 @@
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
import semver, { SemVer } from "semver";
import semver, { coerce, SemVer } from "semver";
import * as iter from "./iter";
import type { RawHelmChart } from "../k8s-api/endpoints/helm-charts.api";
import logger from "../logger";
export function sortCompare<T>(left: T, right: T): -1 | 0 | 1 {
if (left < right) {
@@ -53,3 +56,32 @@ export function sortCompareChartVersions(left: ChartVersion, right: ChartVersion
return sortCompare(left.version, right.version);
}
export function sortCharts(charts: RawHelmChart[]) {
interface ExtendedHelmChart extends RawHelmChart {
__version: SemVer;
}
const chartsWithVersion = Array.from(
iter.map(
charts,
(chart => {
const __version = coerce(chart.version, { includePrerelease: true, loose: true });
if (!__version) {
logger.warn(`[HELM-SERVICE]: Version from helm chart is not loosely coercable to semver.`, { name: chart.name, version: chart.version, repo: chart.repo });
}
(chart as ExtendedHelmChart).__version = __version;
return chart as ExtendedHelmChart;
})
),
);
return chartsWithVersion
.sort(sortCompareChartVersions)
.map(chart => (delete chart.__version, chart));
}