Skip to content

Commit

Permalink
Merge pull request #5923 from gitbutlerapp/e-branch-1
Browse files Browse the repository at this point in the history
Fix downloads: Filter out invalid releases
  • Loading branch information
estib-vega authored Jan 10, 2025
2 parents ea668c6 + 61b185e commit f425501
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 4 deletions.
50 changes: 49 additions & 1 deletion apps/web/src/lib/types/releases.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { captureMessage } from '@sentry/sveltekit';

export interface Build {
os: 'windows' | 'darwin' | 'linux';
arch: 'x86_64' | 'aarch64';
Expand All @@ -6,12 +8,58 @@ export interface Build {
platform: string;
}

export function isReleaseBuild(something: unknown): something is Build {
return (
typeof something === 'object' &&
something !== null &&
typeof (something as any).os === 'string' &&
typeof (something as any).arch === 'string' &&
typeof (something as any).url === 'string' &&
typeof (something as any).file === 'string' &&
typeof (something as any).platform === 'string'
);
}

export interface Release {
version: string;
notes: string;
notes: string | null;
sha: string;
channel: 'release' | string;
build_version: string;
released_at: string;
builds: Build[];
}

export function isRelease(something: unknown): something is Release {
return (
typeof something === 'object' &&
something !== null &&
typeof (something as any).version === 'string' &&
(typeof (something as any).notes === 'string' || (something as any).notes === null) &&
typeof (something as any).sha === 'string' &&
typeof (something as any).channel === 'string' &&
typeof (something as any).build_version === 'string' &&
typeof (something as any).released_at === 'string' &&
Array.isArray((something as any).builds) &&
(something as any).builds.every(isReleaseBuild)
);
}

export function getValidReleases(something: unknown): Release[] {
if (!Array.isArray(something)) return [];
const result: Release[] = [];
for (const item of something) {
if (isRelease(item)) {
result.push(item);
continue;
}

captureMessage('Invalid release filtered out', {
level: 'warning',
extra: {
release: item
}
});
}
return result;
}
6 changes: 3 additions & 3 deletions apps/web/src/routes/downloads/+page.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Build, Release } from '$lib/types/releases';
import { getValidReleases, type Build, type Release } from '$lib/types/releases';
import type { PageLoad } from './$types';

function processBuilds(builds: Build[]) {
Expand Down Expand Up @@ -26,7 +26,7 @@ export const load: PageLoad = async () => {
const releaseResponse = await fetch(
'https://gitbutler.com/api/downloads?limit=4&channel=release'
);
releases = await releaseResponse.json();
releases = getValidReleases(await releaseResponse.json());
const latestRelease = releases[0];

releases.forEach((release) => {
Expand All @@ -45,7 +45,7 @@ export const load: PageLoad = async () => {
const nightlyResponse = await fetch(
'https://gitbutler.com/api/downloads?limit=9&channel=nightly'
);
nightlies = await nightlyResponse.json();
nightlies = getValidReleases(await nightlyResponse.json());
nightlies.forEach((nightlyRelease) => {
nightlyRelease.builds = processBuilds(nightlyRelease.builds);
});
Expand Down

0 comments on commit f425501

Please sign in to comment.