Skip to content

Commit

Permalink
Add default responses when rate limit has been hit, or use latest cac…
Browse files Browse the repository at this point in the history
…hed version

This allows the extension to still function after the rate limit has been hit

Signed-off-by: William Vinnicombe <[email protected]>
  • Loading branch information
will-v-pi committed Jun 27, 2024
1 parent 1dea76e commit 927b4c7
Show file tree
Hide file tree
Showing 2 changed files with 185 additions and 3 deletions.
178 changes: 177 additions & 1 deletion src/utils/githubApiCache.mts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { ExtensionContext, Memento } from "vscode";
import type { GithubReleaseResponse, GithubRepository } from "./githubREST.mjs";
import { type GithubReleaseResponse, GithubRepository } from "./githubREST.mjs";
import Logger from "../logger.mjs";

/**
Expand All @@ -25,6 +25,167 @@ export interface GithubApiCacheEntry {
etag: string;
}


function defaultCacheOfRepository(
repository: GithubRepository,
dataType: GithubApiCacheEntryDataType
): GithubApiCacheEntry {
let ret: GithubApiCacheEntry = {

Check failure on line 33 in src/utils/githubApiCache.mts

View workflow job for this annotation

GitHub Actions / build

'ret' is never reassigned. Use 'const' instead
repository: repository,
dataType: dataType,
data: [],
etag: "",
}

Check warning on line 38 in src/utils/githubApiCache.mts

View workflow job for this annotation

GitHub Actions / build

Missing semicolon
switch (repository) {
case GithubRepository.picoSDK:
if (dataType === GithubApiCacheEntryDataType.releases) {
ret.data = ["1.5.1"];
}
break;
case GithubRepository.cmake:
if (dataType === GithubApiCacheEntryDataType.releases) {
ret.data = ["v3.28.0-rc6"];
} else {
ret.data = {
assets: [
{
id: 138286891,
name: 'cmake-3.28.0-rc6-linux-aarch64.tar.gz',
browser_download_url: 'https://github.com/Kitware/CMake/releases/download/v3.28.0-rc6/cmake-3.28.0-rc6-linux-aarch64.tar.gz'

Check warning on line 54 in src/utils/githubApiCache.mts

View workflow job for this annotation

GitHub Actions / build

This line has a length of 138. Maximum allowed is 80

Check warning on line 54 in src/utils/githubApiCache.mts

View workflow job for this annotation

GitHub Actions / build

Object Literal Property name `browser_download_url` must match one of the following formats: camelCase
},
{
id: 138286897,
name: 'cmake-3.28.0-rc6-linux-x86_64.tar.gz',
browser_download_url: 'https://github.com/Kitware/CMake/releases/download/v3.28.0-rc6/cmake-3.28.0-rc6-linux-x86_64.tar.gz'

Check warning on line 59 in src/utils/githubApiCache.mts

View workflow job for this annotation

GitHub Actions / build

This line has a length of 137. Maximum allowed is 80

Check warning on line 59 in src/utils/githubApiCache.mts

View workflow job for this annotation

GitHub Actions / build

Object Literal Property name `browser_download_url` must match one of the following formats: camelCase
},
{
id: 138286905,
name: 'cmake-3.28.0-rc6-macos-universal.tar.gz',
browser_download_url: 'https://github.com/Kitware/CMake/releases/download/v3.28.0-rc6/cmake-3.28.0-rc6-macos-universal.tar.gz'

Check warning on line 64 in src/utils/githubApiCache.mts

View workflow job for this annotation

GitHub Actions / build

This line has a length of 140. Maximum allowed is 80

Check warning on line 64 in src/utils/githubApiCache.mts

View workflow job for this annotation

GitHub Actions / build

Object Literal Property name `browser_download_url` must match one of the following formats: camelCase
},
{
id: 138286932,
name: 'cmake-3.28.0-rc6-windows-arm64.zip',
browser_download_url: 'https://github.com/Kitware/CMake/releases/download/v3.28.0-rc6/cmake-3.28.0-rc6-windows-arm64.zip'

Check warning on line 69 in src/utils/githubApiCache.mts

View workflow job for this annotation

GitHub Actions / build

This line has a length of 135. Maximum allowed is 80

Check warning on line 69 in src/utils/githubApiCache.mts

View workflow job for this annotation

GitHub Actions / build

Object Literal Property name `browser_download_url` must match one of the following formats: camelCase
},
{
id: 138286937,
name: 'cmake-3.28.0-rc6-windows-x86_64.zip',
browser_download_url: 'https://github.com/Kitware/CMake/releases/download/v3.28.0-rc6/cmake-3.28.0-rc6-windows-x86_64.zip'

Check warning on line 74 in src/utils/githubApiCache.mts

View workflow job for this annotation

GitHub Actions / build

This line has a length of 136. Maximum allowed is 80
},
],
assetsUrl: 'https://api.github.com/repos/Kitware/CMake/releases/132188415/assets',
} as GithubReleaseResponse
}
break;
case GithubRepository.ninja:
if (dataType === GithubApiCacheEntryDataType.releases) {
ret.data = ["v1.12.1"];
} else {
ret.data = {
assets: [
{
id: 167333823,
name: 'ninja-linux-aarch64.zip',
browser_download_url: 'https://github.com/ninja-build/ninja/releases/download/v1.12.1/ninja-linux-aarch64.zip'
},
{
id: 167333509,
name: 'ninja-linux.zip',
browser_download_url: 'https://github.com/ninja-build/ninja/releases/download/v1.12.1/ninja-linux.zip'
},
{
id: 167333196,
name: 'ninja-mac.zip',
browser_download_url: 'https://github.com/ninja-build/ninja/releases/download/v1.12.1/ninja-mac.zip'
},
{
id: 167333379,
name: 'ninja-win.zip',
browser_download_url: 'https://github.com/ninja-build/ninja/releases/download/v1.12.1/ninja-win.zip'
},
{
id: 167333478,
name: 'ninja-winarm64.zip',
browser_download_url: 'https://github.com/ninja-build/ninja/releases/download/v1.12.1/ninja-winarm64.zip'
}
],
assetsUrl: 'https://api.github.com/repos/ninja-build/ninja/releases/155357494/assets'
} as GithubReleaseResponse
}
break;
case GithubRepository.tools:
if (dataType === GithubApiCacheEntryDataType.releases) {
ret.data = [];
} else {
ret.data = {
assets: [
{
id: 141973997,
name: 'openocd-0.12.0-x64-win.zip',
browser_download_url: 'https://github.com/will-v-pi/pico-sdk-tools/releases/download/v1.5.1-alpha-1/openocd-0.12.0-x64-win.zip'
},
{
id: 141974002,
name: 'pico-sdk-tools-1.5.1-x64-win.zip',
browser_download_url: 'https://github.com/will-v-pi/pico-sdk-tools/releases/download/v1.5.1-alpha-1/pico-sdk-tools-1.5.1-x64-win.zip'
},
{
id: 141974003,
name: 'picotool-1.1.2-x64-win.zip',
browser_download_url: 'https://github.com/will-v-pi/pico-sdk-tools/releases/download/v1.5.1-alpha-1/picotool-1.1.2-x64-win.zip'
}
],
assetsUrl: 'https://api.github.com/repos/will-v-pi/pico-sdk-tools/releases/134896110/assets'
} as GithubReleaseResponse
}
break;
case GithubRepository.openocd:
if (dataType === GithubApiCacheEntryDataType.releases) {
ret.data = [];
} else {
ret.data = {
assets: [
{
id: 124558743,
name: 'xpack-openocd-0.12.0-2-darwin-arm64.tar.gz',
browser_download_url: 'https://github.com/xpack-dev-tools/openocd-xpack/releases/download/v0.12.0-2/xpack-openocd-0.12.0-2-darwin-arm64.tar.gz'
},
{
id: 124558736,
name: 'xpack-openocd-0.12.0-2-darwin-x64.tar.gz',
browser_download_url: 'https://github.com/xpack-dev-tools/openocd-xpack/releases/download/v0.12.0-2/xpack-openocd-0.12.0-2-darwin-x64.tar.gz'
},
{
id: 124558733,
name: 'xpack-openocd-0.12.0-2-linux-arm.tar.gz',
browser_download_url: 'https://github.com/xpack-dev-tools/openocd-xpack/releases/download/v0.12.0-2/xpack-openocd-0.12.0-2-linux-arm.tar.gz'
},
{
id: 124558729,
name: 'xpack-openocd-0.12.0-2-linux-arm64.tar.gz',
browser_download_url: 'https://github.com/xpack-dev-tools/openocd-xpack/releases/download/v0.12.0-2/xpack-openocd-0.12.0-2-linux-arm64.tar.gz'
},
{
id: 124558723,
name: 'xpack-openocd-0.12.0-2-linux-x64.tar.gz',
browser_download_url: 'https://github.com/xpack-dev-tools/openocd-xpack/releases/download/v0.12.0-2/xpack-openocd-0.12.0-2-linux-x64.tar.gz'
},
{
id: 124558714,
name: 'xpack-openocd-0.12.0-2-win32-x64.zip',
browser_download_url: 'https://github.com/xpack-dev-tools/openocd-xpack/releases/download/v0.12.0-2/xpack-openocd-0.12.0-2-win32-x64.zip'
}
],
assetsUrl: 'https://api.github.com/repos/xpack-dev-tools/openocd-xpack/releases/119866462/assets'
} as GithubReleaseResponse
}
break;
}

return ret;
}

/**
* A simple cache for Github API responses to avoid hittign the rate limit,
* and to avoid putting unnecessary load on the Github servers.
Expand Down Expand Up @@ -118,6 +279,21 @@ export default class GithubApiCache {
return response?.etag;
}

public async getDefaultResponse(
repository: GithubRepository,
dataType: GithubApiCacheEntryDataType
): Promise<GithubApiCacheEntry | undefined> {
const lastEtag = await GithubApiCache.getInstance().getLastEtag(
repository,
dataType
);
if (lastEtag) {
return this.globalState.get(`githubApiCache-${repository}-${dataType}`);
} else {
return defaultCacheOfRepository(repository, dataType);
}
}

/**
* Clears the github api cache.
*/
Expand Down
10 changes: 8 additions & 2 deletions src/utils/githubREST.mts
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,10 @@ async function getReleases(repository: GithubRepository): Promise<string[]> {
} catch (error) {
Logger.log("Error fetching", repoNameOfRepository(repository), "releases");

return [];
return (await GithubApiCache.getInstance().getDefaultResponse(
repository,
GithubApiCacheEntryDataType.releases
))?.data as string[];
}
}

Expand Down Expand Up @@ -281,6 +284,9 @@ export async function getGithubReleaseByTag(
} catch (error) {
Logger.log("Error fetching", repoNameOfRepository(repository), "releases");

return;
return (await GithubApiCache.getInstance().getDefaultResponse(
repository,
GithubApiCacheEntryDataType.tag
))?.data as GithubReleaseResponse;
}
}

0 comments on commit 927b4c7

Please sign in to comment.