diff --git a/extensions/compose/src/compose-github-releases.ts b/extensions/compose/src/compose-github-releases.ts index 29300ae9aabca..aba16b5b13cbe 100644 --- a/extensions/compose/src/compose-github-releases.ts +++ b/extensions/compose/src/compose-github-releases.ts @@ -41,17 +41,21 @@ export class ComposeGitHubReleases { } } - // Provides last 5 majors releases from GitHub using the GitHub API - // return name, tag and id of the release - async grabLatestsReleasesMetadata(): Promise { - // Grab last 5 majors releases from GitHub using the GitHub API + private async getOctokitOrThrow(): Promise { await this.ensureOctokit(); - if (!this.octokit) { throw new Error('Octokit instance not initialized'); } + return this.octokit; + } + + // Provides last 5 majors releases from GitHub using the GitHub API + // return name, tag and id of the release + async grabLatestsReleasesMetadata(): Promise { + // Grab last 5 majors releases from GitHub using the GitHub API + const octokit = await this.getOctokitOrThrow(); - const lastReleases = await this.octokit.repos.listReleases({ + const lastReleases = await octokit.repos.listReleases({ owner: ComposeGitHubReleases.COMPOSE_GITHUB_OWNER, repo: ComposeGitHubReleases.COMPOSE_GITHUB_REPOSITORY, }); @@ -75,11 +79,7 @@ export class ComposeGitHubReleases { // operatingSystem: win32, darwin, linux (see os.platform()) // arch: x64, arm64 (see os.arch()) async getReleaseAssetId(releaseId: number, operatingSystem: string, arch: string): Promise { - await this.ensureOctokit(); - - if (!this.octokit) { - throw new Error('Octokit instance not initialized'); - } + const octokit = await this.getOctokitOrThrow(); let extension = ''; if (operatingSystem === 'win32') { @@ -93,7 +93,7 @@ export class ComposeGitHubReleases { arch = 'aarch64'; } - const listOfAssets = await this.octokit.paginate(this.octokit.repos.listReleaseAssets, { + const listOfAssets = await octokit.paginate(octokit.repos.listReleaseAssets, { owner: ComposeGitHubReleases.COMPOSE_GITHUB_OWNER, repo: ComposeGitHubReleases.COMPOSE_GITHUB_REPOSITORY, release_id: releaseId, @@ -112,13 +112,9 @@ export class ComposeGitHubReleases { // download the given asset id async downloadReleaseAsset(assetId: number, destination: string): Promise { - await this.ensureOctokit(); - - if (!this.octokit) { - throw new Error('Octokit instance not initialized'); - } + const octokit = await this.getOctokitOrThrow(); - const asset = await this.octokit.repos.getReleaseAsset({ + const asset = await octokit.repos.getReleaseAsset({ owner: ComposeGitHubReleases.COMPOSE_GITHUB_OWNER, repo: ComposeGitHubReleases.COMPOSE_GITHUB_REPOSITORY, asset_id: assetId, diff --git a/extensions/kind/src/kind-installer.ts b/extensions/kind/src/kind-installer.ts index 57ccf4e4f66e0..94ef4b8165352 100644 --- a/extensions/kind/src/kind-installer.ts +++ b/extensions/kind/src/kind-installer.ts @@ -76,6 +76,14 @@ export class KindInstaller { } } + private async getOctokitOrThrow(): Promise { + await this.ensureOctokit(); + if (!this.octokit) { + throw new Error('Octokit instance not initialized'); + } + return this.octokit; + } + // Get the latest version of kubectl from GitHub Releases // and return the artifact metadata async getLatestVersionAsset(): Promise { @@ -88,13 +96,9 @@ export class KindInstaller { // return name, tag and id of the release async grabLatestsReleasesMetadata(): Promise { // Grab last 5 majors releases from GitHub using the GitHub API - await this.ensureOctokit(); - - if (!this.octokit) { - throw new Error('Octokit instance not initialized'); - } + const octokit = await this.getOctokitOrThrow(); - const lastReleases = await this.octokit.repos.listReleases({ + const lastReleases = await octokit.repos.listReleases({ owner: this.KIND_GITHUB_OWNER, repo: this.KIND_GITHUB_REPOSITORY, }); @@ -138,11 +142,7 @@ export class KindInstaller { // operatingSystem: win32, darwin, linux (see os.platform()) // arch: x64, arm64 (see os.arch()) async getReleaseAssetId(releaseId: number, operatingSystem: string, arch: string): Promise { - await this.ensureOctokit(); - - if (!this.octokit) { - throw new Error('Octokit instance not initialized'); - } + const octokit = await this.getOctokitOrThrow(); if (operatingSystem === 'win32') { operatingSystem = 'windows'; @@ -151,7 +151,7 @@ export class KindInstaller { arch = 'amd64'; } - const listOfAssets = await this.octokit.repos.listReleaseAssets({ + const listOfAssets = await octokit.repos.listReleaseAssets({ owner: this.KIND_GITHUB_OWNER, repo: this.KIND_GITHUB_REPOSITORY, release_id: releaseId, @@ -201,13 +201,9 @@ export class KindInstaller { } async downloadReleaseAsset(assetId: number, destination: string): Promise { - await this.ensureOctokit(); - - if (!this.octokit) { - throw new Error('Octokit instance not initialized'); - } + const octokit = await this.getOctokitOrThrow(); - const asset = await this.octokit.repos.getReleaseAsset({ + const asset = await octokit.repos.getReleaseAsset({ owner: this.KIND_GITHUB_OWNER, repo: this.KIND_GITHUB_REPOSITORY, asset_id: assetId, diff --git a/extensions/kubectl-cli/src/kubectl-github-releases.ts b/extensions/kubectl-cli/src/kubectl-github-releases.ts index 63b49415c426c..54f1c43951596 100644 --- a/extensions/kubectl-cli/src/kubectl-github-releases.ts +++ b/extensions/kubectl-cli/src/kubectl-github-releases.ts @@ -42,17 +42,21 @@ export class KubectlGitHubReleases { } } - // Provides last 5 majors releases from GitHub using the GitHub API - // return name, tag and id of the release - async grabLatestsReleasesMetadata(): Promise { - // Grab last 5 majors releases from GitHub using the GitHub API + private async getOctokitOrThrow(): Promise { await this.ensureOctokit(); - if (!this.octokit) { throw new Error('Octokit instance not initialized'); } + return this.octokit; + } + + // Provides last 5 majors releases from GitHub using the GitHub API + // return name, tag and id of the release + async grabLatestsReleasesMetadata(): Promise { + // Grab last 5 majors releases from GitHub using the GitHub API + const octokit = await this.getOctokitOrThrow(); - const lastReleases = await this.octokit.repos.listReleases({ + const lastReleases = await octokit.repos.listReleases({ owner: KubectlGitHubReleases.KUBECTL_GITHUB_OWNER, repo: KubectlGitHubReleases.KUBECTL_GITHUB_REPOSITORY, per_page: 10, // limit to last 5 releases