Add context menu command to open repo/owner on GitHub (#1921)
This commit is contained in:
@@ -64,6 +64,7 @@
|
||||
"onCommand:codeQLDatabasesExperimental.addNewList",
|
||||
"onCommand:codeQLDatabasesExperimental.setSelectedItem",
|
||||
"onCommand:codeQLDatabasesExperimental.setSelectedItemContextMenu",
|
||||
"onCommand:codeQLDatabasesExperimental.openOnGitHubContextMenu",
|
||||
"onCommand:codeQL.quickQuery",
|
||||
"onCommand:codeQL.restartQueryServer",
|
||||
"onWebviewPanel:resultsView",
|
||||
@@ -382,6 +383,10 @@
|
||||
"command": "codeQLDatabasesExperimental.setSelectedItemContextMenu",
|
||||
"title": "Select"
|
||||
},
|
||||
{
|
||||
"command": "codeQLDatabasesExperimental.openOnGitHubContextMenu",
|
||||
"title": "Open on GitHub"
|
||||
},
|
||||
{
|
||||
"command": "codeQLDatabases.chooseDatabaseFolder",
|
||||
"title": "Choose Database from Folder",
|
||||
@@ -785,6 +790,10 @@
|
||||
"command": "codeQLDatabasesExperimental.setSelectedItemContextMenu",
|
||||
"when": "view == codeQLDatabasesExperimental && viewItem =~ /canBeSelected/"
|
||||
},
|
||||
{
|
||||
"command": "codeQLDatabasesExperimental.openOnGitHubContextMenu",
|
||||
"when": "view == codeQLDatabasesExperimental && viewItem =~ /canBeOpenedOnGitHub/"
|
||||
},
|
||||
{
|
||||
"command": "codeQLDatabases.setCurrentDatabase",
|
||||
"group": "inline",
|
||||
@@ -1017,6 +1026,10 @@
|
||||
"command": "codeQLDatabasesExperimental.setSelectedItemContextMenu",
|
||||
"when": "false"
|
||||
},
|
||||
{
|
||||
"command": "codeQLDatabasesExperimental.openOnGitHubContextMenu",
|
||||
"when": "false"
|
||||
},
|
||||
{
|
||||
"command": "codeQLDatabases.setCurrentDatabase",
|
||||
"when": "false"
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
import {
|
||||
commands,
|
||||
QuickPickItem,
|
||||
TreeView,
|
||||
TreeViewExpansionEvent,
|
||||
Uri,
|
||||
window,
|
||||
workspace,
|
||||
} from "vscode";
|
||||
@@ -18,6 +20,7 @@ import { DbItem, DbItemKind, DbListKind, remoteDbKinds } from "../db-item";
|
||||
import { DbManager } from "../db-manager";
|
||||
import { DbTreeDataProvider } from "./db-tree-data-provider";
|
||||
import { DbTreeViewItem } from "./db-tree-view-item";
|
||||
import { getGitHubUrl } from "./db-tree-view-item-action";
|
||||
|
||||
export interface RemoteDatabaseQuickPickItem extends QuickPickItem {
|
||||
kind: string;
|
||||
@@ -83,6 +86,12 @@ export class DbPanel extends DisposableObject {
|
||||
(treeViewItem: DbTreeViewItem) => this.setSelectedItem(treeViewItem),
|
||||
),
|
||||
);
|
||||
this.push(
|
||||
commandRunner(
|
||||
"codeQLDatabasesExperimental.openOnGitHubContextMenu",
|
||||
(treeViewItem: DbTreeViewItem) => this.openOnGitHub(treeViewItem),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
private async openConfigFile(): Promise<void> {
|
||||
@@ -290,4 +299,18 @@ export class DbPanel extends DisposableObject {
|
||||
// You can only select one item at a time, so selection[0] gives the selection
|
||||
return this.treeView.selection[0]?.dbItem;
|
||||
}
|
||||
|
||||
private async openOnGitHub(treeViewItem: DbTreeViewItem): Promise<void> {
|
||||
if (treeViewItem.dbItem === undefined) {
|
||||
throw new Error("Unable to open on GitHub. Please select a valid item.");
|
||||
}
|
||||
const githubUrl = getGitHubUrl(treeViewItem.dbItem);
|
||||
if (!githubUrl) {
|
||||
throw new Error(
|
||||
"Unable to open on GitHub. Please select a remote repository or owner.",
|
||||
);
|
||||
}
|
||||
|
||||
await commands.executeCommand("vscode.open", Uri.parse(githubUrl));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -59,3 +59,14 @@ function canBeRenamed(dbItem: DbItem): boolean {
|
||||
function canBeOpenedOnGitHub(dbItem: DbItem): boolean {
|
||||
return dbItemKindsThatCanBeOpenedOnGitHub.includes(dbItem.kind);
|
||||
}
|
||||
|
||||
export function getGitHubUrl(dbItem: DbItem): string | undefined {
|
||||
switch (dbItem.kind) {
|
||||
case DbItemKind.RemoteOwner:
|
||||
return `https://github.com/${dbItem.ownerName}`;
|
||||
case DbItemKind.RemoteRepo:
|
||||
return `https://github.com/${dbItem.repoFullName}`;
|
||||
default:
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,7 @@
|
||||
import { getDbItemActions } from "../../../../src/databases/ui/db-tree-view-item-action";
|
||||
import {
|
||||
getDbItemActions,
|
||||
getGitHubUrl,
|
||||
} from "../../../../src/databases/ui/db-tree-view-item-action";
|
||||
import {
|
||||
createLocalDatabaseDbItem,
|
||||
createLocalListDbItem,
|
||||
@@ -107,3 +110,51 @@ describe("getDbItemActions", () => {
|
||||
expect(actions.includes("canBeSelected")).toBeFalsy();
|
||||
});
|
||||
});
|
||||
|
||||
describe("getGitHubUrl", () => {
|
||||
it("should return the correct url for a remote owner", () => {
|
||||
const dbItem = createRemoteOwnerDbItem();
|
||||
|
||||
const actualUrl = getGitHubUrl(dbItem);
|
||||
const expectedUrl = `https://github.com/${dbItem.ownerName}`;
|
||||
|
||||
expect(actualUrl).toEqual(expectedUrl);
|
||||
});
|
||||
|
||||
it("should return the correct url for a remote repo", () => {
|
||||
const dbItem = createRemoteRepoDbItem();
|
||||
|
||||
const actualUrl = getGitHubUrl(dbItem);
|
||||
const expectedUrl = `https://github.com/${dbItem.repoFullName}`;
|
||||
|
||||
expect(actualUrl).toEqual(expectedUrl);
|
||||
});
|
||||
|
||||
it("should return undefined for other remote db items", () => {
|
||||
const dbItem0 = createRootRemoteDbItem();
|
||||
const dbItem1 = createRemoteSystemDefinedListDbItem();
|
||||
const dbItem2 = createRemoteUserDefinedListDbItem();
|
||||
|
||||
const actualUrl0 = getGitHubUrl(dbItem0);
|
||||
const actualUrl1 = getGitHubUrl(dbItem1);
|
||||
const actualUrl2 = getGitHubUrl(dbItem2);
|
||||
|
||||
expect(actualUrl0).toBeUndefined();
|
||||
expect(actualUrl1).toBeUndefined();
|
||||
expect(actualUrl2).toBeUndefined();
|
||||
});
|
||||
|
||||
it("should return undefined for local db items", () => {
|
||||
const dbItem0 = createRootLocalDbItem();
|
||||
const dbItem1 = createLocalDatabaseDbItem();
|
||||
const dbItem2 = createLocalListDbItem();
|
||||
|
||||
const actualUrl0 = getGitHubUrl(dbItem0);
|
||||
const actualUrl1 = getGitHubUrl(dbItem1);
|
||||
const actualUrl2 = getGitHubUrl(dbItem2);
|
||||
|
||||
expect(actualUrl0).toBeUndefined();
|
||||
expect(actualUrl1).toBeUndefined();
|
||||
expect(actualUrl2).toBeUndefined();
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user