Skip to content

Fix tests by using API Key for CI #2255

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

Merged
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 25 additions & 10 deletions src/features/UpdatePowerShell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Copyright (C) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------*/

import fetch from "node-fetch";
import fetch, { RequestInit } from "node-fetch";
import * as semver from "semver";
import { MessageItem, window } from "vscode";
import { LanguageClient } from "vscode-languageclient";
Expand All @@ -16,18 +16,33 @@ const PowerShellGitHubPrereleasesUrl =

export class GitHubReleaseInformation {
public static async FetchLatestRelease(preview: boolean): Promise<GitHubReleaseInformation> {
const requestConfig: RequestInit = {};

// For CI. This prevents GitHub from rate limiting us.
if (process.env.PS_TEST_GITHUB_API_USERNAME && process.env.PS_TEST_GITHUB_API_PAT) {
const authHeaderValue = Buffer
.from(`${process.env.PS_TEST_GITHUB_API_USERNAME}:${process.env.PS_TEST_GITHUB_API_PAT}`)
.toString("base64");
requestConfig.headers = {
Authorization: `Basic ${authHeaderValue}`,
};
}

// Fetch the latest PowerShell releases from GitHub.
let releaseJson: any;
if (preview) {
// This gets all releases and the first one is the latest prerelease if
// there is a prerelease version.
releaseJson = (await fetch(PowerShellGitHubPrereleasesUrl)
.then((res) => res.json())).find((release: any) => release.prerelease);
} else {
releaseJson = await fetch(PowerShellGitHubReleasesUrl)
.then((res) => res.json());
const response = await fetch(
preview ? PowerShellGitHubPrereleasesUrl : PowerShellGitHubReleasesUrl,
requestConfig);

if (!response.ok) {
const json = await response.json();
throw json.message || json || "response was not ok.";
}

// For preview, we grab all the releases and then grab the first prerelease.
const releaseJson = preview
? (await response.json()).find((release: any) => release.prerelease)
: await response.json();

return new GitHubReleaseInformation(
releaseJson.tag_name, releaseJson.assets);
}
Expand Down
3 changes: 2 additions & 1 deletion src/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -591,8 +591,9 @@ export class SessionManager implements Middleware {
localVersion,
this.versionDetails.architecture,
release);
} catch {
} catch (e) {
// best effort. This probably failed to fetch the data from GitHub.
this.log.writeWarning(e.message);
}
});

Expand Down