Skip to content

Commit 9b4f2be

Browse files
committed
Authenticate when checking API access and cache response
1 parent 1c1ab60 commit 9b4f2be

File tree

9 files changed

+43
-32
lines changed

9 files changed

+43
-32
lines changed

package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -274,13 +274,13 @@
274274
"viewsWelcome": [
275275
{
276276
"view": "github-actions.empty-view",
277-
"contents": "Unable to connect to the GitHub API, please check your internet connection.\n[Refresh](command:github-actions.explorer.refresh)",
278-
"when": "!github-actions.internet-access"
277+
"contents": "Sign in to GitHub to display runs, workflows, and configure Actions settings.\n[Sign in to GitHub](command:github-actions.sign-in)",
278+
"when": "!github-actions.signed-in"
279279
},
280280
{
281281
"view": "github-actions.empty-view",
282-
"contents": "Sign in to GitHub to display runs, workflows, and configure Actions settings.\n[Sign in to GitHub](command:github-actions.sign-in)",
283-
"when": "github-actions.internet-access && !github-actions.signed-in"
282+
"contents": "Unable to connect to the GitHub API, please check your internet connection.\n[Refresh](command:github-actions.explorer.refresh)",
283+
"when": "github-actions.signed-in && !github-actions.internet-access"
284284
},
285285
{
286286
"view": "github-actions.empty-view",

src/api/canReachGitHubAPI.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import {TTLCache} from "@github/actions-languageserver/utils/cache";
2+
3+
import {getSession} from "../auth/auth";
4+
import {logError} from "../log";
5+
import {getClient} from "./api";
6+
7+
const API_ACCESS_TTL_MS = 10 * 1000;
8+
const cache = new TTLCache(API_ACCESS_TTL_MS);
9+
10+
export async function canReachGitHubAPI() {
11+
const session = await getSession();
12+
if (!session) {
13+
return false;
14+
}
15+
return await cache.get("canReachGitHubAPI", undefined, async () => {
16+
try {
17+
const octokit = getClient(session.accessToken);
18+
await octokit.request("GET /", {
19+
headers: {
20+
"X-GitHub-Api-Version": "2022-11-28"
21+
}
22+
});
23+
} catch (e) {
24+
logError(e as Error, "Error getting GitHub context");
25+
return false;
26+
}
27+
return true;
28+
});
29+
}

src/extension.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as vscode from "vscode";
22

3+
import {canReachGitHubAPI} from "./api/canReachGitHubAPI";
34
import {getSession} from "./auth/auth";
45
import {registerCancelWorkflowRun} from "./commands/cancelWorkflowRun";
56
import {registerOpenWorkflowFile} from "./commands/openWorkflowFile";
@@ -32,22 +33,21 @@ import {initResources} from "./treeViews/icons";
3233
import {initTreeViews} from "./treeViews/treeViews";
3334
import {deactivateLanguageServer, initLanguageServer} from "./workflow/languageServer";
3435
import {registerSignIn} from "./commands/signIn";
35-
import {canReachGitHubAPI} from "./util";
3636

3737
export async function activate(context: vscode.ExtensionContext) {
3838
initLogger();
3939

4040
log("Activating GitHub Actions extension...");
4141

42-
const canReachAPI = await canReachGitHubAPI();
43-
const hasSession = canReachAPI && !!(await getSession());
42+
const hasSession = !!(await getSession());
43+
const canReachAPI = hasSession && (await canReachGitHubAPI());
4444

4545
// Prefetch git repository origin url
4646
const ghContext = hasSession && (await getGitHubContext());
4747
const hasGitHubRepos = ghContext && ghContext.repos.length > 0;
4848

49-
await vscode.commands.executeCommand("setContext", "github-actions.internet-access", canReachAPI);
5049
await vscode.commands.executeCommand("setContext", "github-actions.signed-in", hasSession);
50+
await vscode.commands.executeCommand("setContext", "github-actions.internet-access", canReachAPI);
5151
await vscode.commands.executeCommand("setContext", "github-actions.has-repos", hasGitHubRepos);
5252

5353
initResources(context);

src/git/repository.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import * as vscode from "vscode";
22
import {Octokit} from "@octokit/rest";
33

4+
import {canReachGitHubAPI} from "../api/canReachGitHubAPI";
45
import {handleSamlError} from "../api/handleSamlError";
56
import {getSession} from "../auth/auth";
67
import {getRemoteName} from "../configuration/configuration";
78
import {Protocol} from "../external/protocol";
89
import {logDebug, logError} from "../log";
910
import {API, GitExtension, RefType, RepositoryState} from "../typings/git";
10-
import {canReachGitHubAPI} from "../util";
1111
import {RepositoryPermission, getRepositoryPermission} from "./repository-permissions";
1212

1313
interface GitHubUrls {

src/treeViews/currentBranch.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as vscode from "vscode";
22

3+
import {canReachGitHubAPI} from "../api/canReachGitHubAPI";
34
import {getCurrentBranch, getGitHubContext, GitHubRepoContext} from "../git/repository";
45
import {CurrentBranchRepoNode} from "./current-branch/currentBranchRepoNode";
56

@@ -14,7 +15,6 @@ import {WorkflowJobNode} from "./shared/workflowJobNode";
1415
import {WorkflowRunNode} from "./shared/workflowRunNode";
1516
import {WorkflowRunTreeDataProvider} from "./workflowRunTreeDataProvider";
1617
import {WorkflowStepNode} from "./workflows/workflowStepNode";
17-
import {canReachGitHubAPI} from "../util";
1818

1919
type CurrentBranchTreeNode =
2020
| CurrentBranchRepoNode

src/treeViews/settings.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as vscode from "vscode";
22

3+
import {canReachGitHubAPI} from "../api/canReachGitHubAPI";
34
import {SettingsRepoNode, getSettingNodes} from "./settings/settingsRepoNode";
4-
55
import {EnvironmentNode} from "./settings/environmentNode";
66
import {EnvironmentsNode} from "./settings/environmentsNode";
77
import {RepoSecretsNode} from "./settings/repoSecretsNode";
@@ -15,7 +15,6 @@ import {EnvironmentVariablesNode} from "./settings/environmentVariablesNode";
1515
import {OrgVariablesNode} from "./settings/orgVariablesNode";
1616
import {OrgSecretsNode} from "./settings/orgSecretsNode";
1717
import {GitHubAPIUnreachableNode} from "./shared/gitHubApiUnreachableNode";
18-
import {canReachGitHubAPI} from "../util";
1918

2019
export class SettingsTreeProvider implements vscode.TreeDataProvider<SettingsExplorerNode> {
2120
private _onDidChangeTreeData = new vscode.EventEmitter<SettingsExplorerNode | null>();

src/treeViews/treeViews.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import * as vscode from "vscode";
2+
3+
import {canReachGitHubAPI} from "../api/canReachGitHubAPI";
24
import {executeCacheClearCommand} from "../workflow/languageServer";
35
import {getGitHubContext} from "../git/repository";
46
import {logDebug} from "../log";
57
import {RunStore} from "../store/store";
6-
import {canReachGitHubAPI} from "../util";
78
import {CurrentBranchTreeProvider} from "./currentBranch";
89
import {SettingsTreeProvider} from "./settings";
910
import {WorkflowsTreeProvider} from "./workflows";

src/treeViews/workflows.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as vscode from "vscode";
22

3+
import {canReachGitHubAPI} from "../api/canReachGitHubAPI";
34
import {getGitHubContext} from "../git/repository";
45
import {log, logDebug, logError} from "../log";
56
import {RunStore} from "../store/store";
@@ -16,7 +17,6 @@ import {WorkflowRunTreeDataProvider} from "./workflowRunTreeDataProvider";
1617
import {WorkflowNode} from "./workflows/workflowNode";
1718
import {getWorkflowNodes, WorkflowsRepoNode} from "./workflows/workflowsRepoNode";
1819
import {WorkflowStepNode} from "./workflows/workflowStepNode";
19-
import {canReachGitHubAPI} from "../util";
2020

2121
type WorkflowsTreeNode =
2222
| AuthenticationNode

src/util.ts

Lines changed: 0 additions & 18 deletions
This file was deleted.

0 commit comments

Comments
 (0)