Skip to content

Commit da81ff5

Browse files
committed
Fix issue where refreshing clears the view with no internet
1 parent 0706f09 commit da81ff5

6 files changed

Lines changed: 55 additions & 24 deletions

File tree

src/git/repository.ts

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {getRemoteName} from "../configuration/configuration";
66
import {Protocol} from "../external/protocol";
77
import {logDebug, logError} from "../log";
88
import {API, GitExtension, RefType, RepositoryState} from "../typings/git";
9+
import {hasInternetConnectivity} from "../util"
910

1011
interface GitHubUrls {
1112
workspaceUri: vscode.Uri;
@@ -142,17 +143,8 @@ export async function getGitHubContext(): Promise<GitHubContext | undefined> {
142143
return gitHubContext;
143144
}
144145

145-
// Check internet connectivity
146-
try {
147-
const octokit = new Octokit()
148-
await octokit.request('GET /', {
149-
headers: {
150-
'X-GitHub-Api-Version': '2022-11-28'
151-
}
152-
})
153-
}
154-
catch {
155-
logError(new Error("Cannot fetch github context, unable to connect to the internet"));
146+
if (!(await hasInternetConnectivity())) {
147+
logError(new Error("Cannot fetch github context"));
156148
return undefined;
157149
}
158150

src/treeViews/currentBranch.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {WorkflowRunNode} from "./shared/workflowRunNode";
1212
import {WorkflowRunTreeDataProvider} from "./workflowRunTreeDataProvider";
1313
import {NoInternetConnectivityNode} from "./shared/noInternetConnectivityNode";
1414
import {CurrentBranchTreeNode} from "./settings/types";
15+
import {hasInternetConnectivity} from "../util"
1516

1617
export class CurrentBranchTreeProvider
1718
extends WorkflowRunTreeDataProvider
@@ -28,8 +29,14 @@ export class CurrentBranchTreeProvider
2829
this._onDidChangeTreeData.fire(node);
2930
}
3031

31-
refresh(): void {
32-
this._onDidChangeTreeData.fire(null);
32+
async refresh(): Promise<void> {
33+
// Don't delete all the nodes if we don't have internet connectivity
34+
if (await hasInternetConnectivity()) {
35+
this._onDidChangeTreeData.fire(null);
36+
}
37+
else {
38+
await vscode.window.showWarningMessage("Unable to refresh, you are not connected to the internet")
39+
}
3340
}
3441

3542
getTreeItem(element: CurrentBranchTreeNode): vscode.TreeItem | Thenable<vscode.TreeItem> {

src/treeViews/settings.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,20 @@ import {EnvironmentVariablesNode} from "./settings/environmentVariablesNode";
1515
import {OrgVariablesNode} from "./settings/orgVariablesNode";
1616
import {OrgSecretsNode} from "./settings/orgSecretsNode";
1717
import {NoInternetConnectivityNode} from "./shared/noInternetConnectivityNode";
18+
import {hasInternetConnectivity} from "../util"
1819

1920
export class SettingsTreeProvider implements vscode.TreeDataProvider<SettingsExplorerNode> {
2021
private _onDidChangeTreeData = new vscode.EventEmitter<SettingsExplorerNode | null>();
2122
readonly onDidChangeTreeData = this._onDidChangeTreeData.event;
2223

23-
refresh(): void {
24-
this._onDidChangeTreeData.fire(null);
24+
async refresh(): Promise<void> {
25+
// Don't delete all the nodes if we don't have internet connectivity
26+
if (await hasInternetConnectivity()) {
27+
this._onDidChangeTreeData.fire(null);
28+
}
29+
else {
30+
await vscode.window.showWarningMessage("Unable to refresh, you are not connected to the internet")
31+
}
2532
}
2633

2734
getTreeItem(element: SettingsExplorerNode): vscode.TreeItem | Thenable<vscode.TreeItem> {

src/treeViews/treeViews.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,15 @@ export async function initTreeViews(context: vscode.ExtensionContext, store: Run
2121

2222
context.subscriptions.push(
2323
vscode.commands.registerCommand("github-actions.explorer.refresh", async () => {
24-
workflowTreeProvider.refresh();
25-
settingsTreeProvider.refresh();
24+
await workflowTreeProvider.refresh();
25+
await settingsTreeProvider.refresh();
2626
await executeCacheClearCommand();
2727
})
2828
);
2929

3030
context.subscriptions.push(
31-
vscode.commands.registerCommand("github-actions.explorer.current-branch.refresh", () => {
32-
currentBranchTreeProvider.refresh();
31+
vscode.commands.registerCommand("github-actions.explorer.current-branch.refresh", async () => {
32+
await currentBranchTreeProvider.refresh();
3333
})
3434
);
3535

@@ -46,7 +46,7 @@ export async function initTreeViews(context: vscode.ExtensionContext, store: Run
4646

4747
let currentAhead = repo.repositoryState.HEAD?.ahead;
4848
let currentHeadName = repo.repositoryState.HEAD?.name;
49-
repo.repositoryState.onDidChange(() => {
49+
repo.repositoryState.onDidChange(async () => {
5050
// When the current head/branch changes, or the number of commits ahead changes (which indicates
5151
// a push), refresh the current-branch view
5252
if (
@@ -55,7 +55,7 @@ export async function initTreeViews(context: vscode.ExtensionContext, store: Run
5555
) {
5656
currentHeadName = repo.repositoryState?.HEAD?.name;
5757
currentAhead = repo.repositoryState?.HEAD?.ahead;
58-
currentBranchTreeProvider.refresh();
58+
await currentBranchTreeProvider.refresh();
5959
}
6060
});
6161
}

src/treeViews/workflows.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {WorkflowNode} from "./workflows/workflowNode";
1414
import {getWorkflowNodes, WorkflowsRepoNode} from "./workflows/workflowsRepoNode";
1515
import {WorkflowsTreeNode} from "./settings/types";
1616
import {NoInternetConnectivityNode} from "./shared/noInternetConnectivityNode";
17+
import {hasInternetConnectivity} from "../util"
1718

1819
export class WorkflowsTreeProvider
1920
extends WorkflowRunTreeDataProvider
@@ -30,9 +31,14 @@ export class WorkflowsTreeProvider
3031
this._onDidChangeTreeData.fire(node);
3132
}
3233

33-
refresh(): void {
34-
logDebug("Refreshing workflow tree");
35-
this._onDidChangeTreeData.fire(null);
34+
async refresh(): Promise<void> {
35+
// Don't delete all the nodes if we don't have internet connectivity
36+
if (await hasInternetConnectivity()) {
37+
this._onDidChangeTreeData.fire(null);
38+
}
39+
else {
40+
await vscode.window.showWarningMessage("Unable to refresh, you are not connected to the internet")
41+
}
3642
}
3743

3844
getTreeItem(element: WorkflowsTreeNode): vscode.TreeItem | Thenable<vscode.TreeItem> {

src/util.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import {Octokit} from "@octokit/rest";
2+
3+
import {logError} from "./log";
4+
5+
export async function hasInternetConnectivity() {
6+
try {
7+
const octokit = new Octokit()
8+
await octokit.request('GET /', {
9+
headers: {
10+
'X-GitHub-Api-Version': '2022-11-28'
11+
}
12+
})
13+
}
14+
catch {
15+
logError(new Error("Unable to connect to the internet"));
16+
return false;
17+
}
18+
return true;
19+
}

0 commit comments

Comments
 (0)