[extension] Clone and open correct repo
This commit is contained in:
parent
559666e34a
commit
5d48bd0b37
@ -28,7 +28,7 @@ export class BWPanel {
|
|||||||
// Otherwise, create a new panel.
|
// Otherwise, create a new panel.
|
||||||
const panel = vscode.window.createWebviewPanel(
|
const panel = vscode.window.createWebviewPanel(
|
||||||
BWPanel.viewType,
|
BWPanel.viewType,
|
||||||
'VSinder',
|
'BWContest',
|
||||||
column || vscode.ViewColumn.One,
|
column || vscode.ViewColumn.One,
|
||||||
{
|
{
|
||||||
// Enable javascript in the webview
|
// Enable javascript in the webview
|
||||||
|
@ -1,10 +1,14 @@
|
|||||||
import * as vscode from 'vscode';
|
import * as vscode from 'vscode';
|
||||||
import { getNonce } from './getNonce';
|
import { getNonce } from './getNonce';
|
||||||
|
import { cloneAndOpenRepo } from './extension';
|
||||||
|
|
||||||
export class SidebarProvider implements vscode.WebviewViewProvider {
|
export class SidebarProvider implements vscode.WebviewViewProvider {
|
||||||
_view?: vscode.WebviewView;
|
_view?: vscode.WebviewView;
|
||||||
|
_context?: vscode.ExtensionContext;
|
||||||
|
|
||||||
constructor(private readonly _extensionUri: vscode.Uri) {}
|
constructor(private readonly _extensionUri: vscode.Uri, context: vscode.ExtensionContext) {
|
||||||
|
this._context = context;
|
||||||
|
}
|
||||||
|
|
||||||
public resolveWebviewView(webviewView: vscode.WebviewView) {
|
public resolveWebviewView(webviewView: vscode.WebviewView) {
|
||||||
this._view = webviewView;
|
this._view = webviewView;
|
||||||
@ -20,6 +24,34 @@ export class SidebarProvider implements vscode.WebviewViewProvider {
|
|||||||
|
|
||||||
webviewView.webview.onDidReceiveMessage(async (data) => {
|
webviewView.webview.onDidReceiveMessage(async (data) => {
|
||||||
switch (data.type) {
|
switch (data.type) {
|
||||||
|
case 'onStartup': {
|
||||||
|
const token: string | undefined = this._context?.globalState.get('token');
|
||||||
|
if (token) {
|
||||||
|
this._view?.webview.postMessage({
|
||||||
|
type: 'onSession',
|
||||||
|
value: token
|
||||||
|
});
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case 'onClone': {
|
||||||
|
if (!data.value || !data.value.contestId || !data.value.teamId) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
await cloneAndOpenRepo(parseInt(data.value.contestId), parseInt(data.value.teamId));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case 'onLogin': {
|
||||||
|
if (!data.value) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
this._context?.globalState.update('token', data.value);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case 'onLogout': {
|
||||||
|
this._context?.globalState.update('token', null);
|
||||||
|
break;
|
||||||
|
}
|
||||||
case 'onInfo': {
|
case 'onInfo': {
|
||||||
if (!data.value) {
|
if (!data.value) {
|
||||||
return;
|
return;
|
||||||
@ -38,7 +70,6 @@ export class SidebarProvider implements vscode.WebviewViewProvider {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private _getHtmlForWebview(webview: vscode.Webview) {
|
private _getHtmlForWebview(webview: vscode.Webview) {
|
||||||
const styleResetUri = webview.asWebviewUri(
|
const styleResetUri = webview.asWebviewUri(
|
||||||
vscode.Uri.joinPath(this._extensionUri, 'media', 'reset.css')
|
vscode.Uri.joinPath(this._extensionUri, 'media', 'reset.css')
|
||||||
|
@ -1,7 +1,5 @@
|
|||||||
import * as vscode from 'vscode';
|
import * as vscode from 'vscode';
|
||||||
import { BWPanel } from './BWPanel';
|
|
||||||
import { SidebarProvider } from './SidebarProvider';
|
import { SidebarProvider } from './SidebarProvider';
|
||||||
import { notDeepEqual } from 'assert';
|
|
||||||
import * as child_process from 'child_process';
|
import * as child_process from 'child_process';
|
||||||
import * as fs from 'fs-extra';
|
import * as fs from 'fs-extra';
|
||||||
|
|
||||||
@ -21,11 +19,30 @@ function closeAllWorkspaces() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function cloneAndOpenRepo(baseUrl: string, path: string, contestId: number, teamId: number) {
|
export async function cloneAndOpenRepo(contestId: number, teamId: number) {
|
||||||
const repoUrl = `${baseUrl}/${contestId.toString()}/${teamId.toString()}.git`;
|
const currentSettings = vscode.workspace.getConfiguration().get<BWContestSettings>('BWContest');
|
||||||
|
|
||||||
|
if (!currentSettings || currentSettings.repoBaseUrl == '') {
|
||||||
|
vscode.window.showErrorMessage('BWContest: BWContest.repoBaseURL not set');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (!currentSettings || currentSettings.repoClonePath == '') {
|
||||||
|
vscode.window.showErrorMessage('BWContest: BWContest.repoClonePath not set');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const repoUrl = `${currentSettings.repoBaseUrl}/${contestId.toString()}/${teamId.toString()}.git`;
|
||||||
|
|
||||||
const repoName = repoUrl.split('/').pop()?.replace('.git', '')!;
|
const repoName = repoUrl.split('/').pop()?.replace('.git', '')!;
|
||||||
const clonedRepoPath = `${path}/${repoName}`;
|
|
||||||
|
if (!fs.existsSync(`${currentSettings.repoClonePath}/BWContest`)) {
|
||||||
|
fs.mkdirSync(`${currentSettings.repoClonePath}/BWContest`);
|
||||||
|
}
|
||||||
|
if (!fs.existsSync(`${currentSettings.repoClonePath}/BWContest/${contestId.toString()}`)) {
|
||||||
|
fs.mkdirSync(`${currentSettings.repoClonePath}/BWContest/${contestId.toString()}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
const clonedRepoPath = `${currentSettings.repoClonePath}/BWContest/${contestId.toString()}/${repoName}`;
|
||||||
|
|
||||||
if (fs.existsSync(clonedRepoPath)) {
|
if (fs.existsSync(clonedRepoPath)) {
|
||||||
const confirm = await vscode.window.showWarningMessage(
|
const confirm = await vscode.window.showWarningMessage(
|
||||||
@ -40,12 +57,16 @@ async function cloneAndOpenRepo(baseUrl: string, path: string, contestId: number
|
|||||||
fs.removeSync(clonedRepoPath);
|
fs.removeSync(clonedRepoPath);
|
||||||
}
|
}
|
||||||
|
|
||||||
child_process.exec(`git clone ${repoUrl}`, { cwd: path }, (error, stdout, stderr) => {
|
child_process.exec(
|
||||||
if (error) {
|
`git clone ${repoUrl}`,
|
||||||
vscode.window.showErrorMessage(`BWContest: Failed to clone repo: ${error.message}`);
|
{ cwd: `${currentSettings.repoClonePath}/BWContest/${contestId.toString()}` },
|
||||||
return;
|
(error, stdout, stderr) => {
|
||||||
|
if (error) {
|
||||||
|
vscode.window.showErrorMessage(`BWContest: Failed to clone repo: ${error.message}`);
|
||||||
|
return;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
);
|
||||||
|
|
||||||
const addedFolder = vscode.workspace.updateWorkspaceFolders(
|
const addedFolder = vscode.workspace.updateWorkspaceFolders(
|
||||||
vscode.workspace.workspaceFolders?.length ?? 0,
|
vscode.workspace.workspaceFolders?.length ?? 0,
|
||||||
@ -62,28 +83,12 @@ async function cloneAndOpenRepo(baseUrl: string, path: string, contestId: number
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function activate(context: vscode.ExtensionContext) {
|
export function activate(context: vscode.ExtensionContext) {
|
||||||
const sidebarProvider = new SidebarProvider(context.extensionUri);
|
const sidebarProvider = new SidebarProvider(context.extensionUri, context);
|
||||||
context.subscriptions.push(
|
context.subscriptions.push(
|
||||||
vscode.window.registerWebviewViewProvider('bwcontest-sidebar', sidebarProvider)
|
vscode.window.registerWebviewViewProvider('bwcontest-sidebar', sidebarProvider)
|
||||||
);
|
);
|
||||||
|
|
||||||
context.subscriptions.push(
|
context.subscriptions.push(vscode.commands.registerCommand('bwcontest.helloWorld', () => {}));
|
||||||
vscode.commands.registerCommand('bwcontest.helloWorld', () => {
|
|
||||||
const currentSettings = vscode.workspace
|
|
||||||
.getConfiguration()
|
|
||||||
.get<BWContestSettings>('BWContest');
|
|
||||||
|
|
||||||
if (!currentSettings || currentSettings.repoBaseUrl == '') {
|
|
||||||
vscode.window.showErrorMessage('BWContest: BWContest.repoBaseURL not set');
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (!currentSettings || currentSettings.repoClonePath == '') {
|
|
||||||
vscode.window.showErrorMessage('BWContest: BWContest.repoClonePath not set');
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
cloneAndOpenRepo(currentSettings.repoBaseUrl, currentSettings.repoClonePath, 102, 1);
|
|
||||||
})
|
|
||||||
);
|
|
||||||
|
|
||||||
context.subscriptions.push(
|
context.subscriptions.push(
|
||||||
vscode.commands.registerCommand('bwcontest.askQuestion', async () => {
|
vscode.commands.registerCommand('bwcontest.askQuestion', async () => {
|
||||||
|
@ -1,49 +1,94 @@
|
|||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
function postMessage(message: any) {
|
import { onMount } from "svelte";
|
||||||
vscode.postMessage(message);
|
|
||||||
}
|
|
||||||
|
|
||||||
let teamname: HTMLInputElement;
|
function postMessage(message: any) {
|
||||||
let password: HTMLInputElement;
|
vscode.postMessage(message);
|
||||||
|
}
|
||||||
|
|
||||||
let sessionToken: string | undefined;
|
let teamname: HTMLInputElement;
|
||||||
|
let password: HTMLInputElement;
|
||||||
|
|
||||||
async function onLogin() {
|
let sessionToken: string | undefined;
|
||||||
try {
|
|
||||||
const res = await fetch("http://localhost:5173/api/team/login", {method: "POST", headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({
|
interface TeamData {
|
||||||
teamname: teamname.value, password: password.value
|
teamId: number,
|
||||||
})});
|
contestId: number
|
||||||
|
}
|
||||||
|
|
||||||
|
let teamData: TeamData | undefined;
|
||||||
|
|
||||||
|
async function fetchTeamData() {
|
||||||
|
if (sessionToken) {
|
||||||
|
const res = await fetch(`http://localhost:5173/api/team/${sessionToken}`, {method: "GET"});
|
||||||
|
const data = await res.json();
|
||||||
|
if (!data.success) {
|
||||||
|
postMessage({type: 'onError', value: "BWContest: Failed to fetch team data"});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
teamData = data.data as TeamData;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function onClone() {
|
||||||
|
if (teamData) {
|
||||||
|
postMessage({type: 'onClone', value: {contestId: teamData.contestId, teamId: teamData.teamId}});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function onLogin() {
|
||||||
|
try {
|
||||||
|
const res = await fetch("http://localhost:5173/api/team/login", {method: "POST", headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({
|
||||||
|
teamname: teamname.value, password: password.value
|
||||||
|
})});
|
||||||
|
if (res.status !== 200) {
|
||||||
|
postMessage({type: 'onError', value: 'Error logging in'});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const data = await res.json();
|
||||||
|
if (data.success === false) {
|
||||||
|
postMessage({type: 'onError', value: data.message ?? "Unknown error logging in"});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
sessionToken = data.token;
|
||||||
|
postMessage({type: 'onLogin', value: sessionToken});
|
||||||
|
} catch (err) {
|
||||||
|
console.error('Failed to fetch:', err);
|
||||||
|
}
|
||||||
|
fetchTeamData();
|
||||||
|
}
|
||||||
|
|
||||||
|
async function onLogout() {
|
||||||
|
const res = await fetch("http://localhost:5173/api/team/logout", {method: "POST", headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({
|
||||||
|
token: sessionToken,
|
||||||
|
})})
|
||||||
if (res.status !== 200) {
|
if (res.status !== 200) {
|
||||||
postMessage({type: 'onError', value: 'Error logging in'});
|
postMessage({type: 'onError', value: 'Error logging out'});
|
||||||
|
sessionToken = undefined;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const data = await res.json();
|
const data = await res.json();
|
||||||
if (data.success === false) {
|
if (data.success === true) {
|
||||||
postMessage({type: 'onError', value: data.message ?? "Unknown error logging in"});
|
postMessage({type: 'onInfo', value: 'BWContest: Logged out'});
|
||||||
return;
|
sessionToken = undefined;
|
||||||
|
postMessage({type: 'onLogout'});
|
||||||
|
} else {
|
||||||
|
postMessage({type: 'onError', value: 'Log out unsuccessful'});
|
||||||
}
|
}
|
||||||
sessionToken = data.token;
|
|
||||||
} catch (err) {
|
|
||||||
console.error('Failed to fetch:', err);
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
async function onLogout() {
|
window.addEventListener("message", event => {
|
||||||
const res = await fetch("http://localhost:5173/api/team/logout", {method: "POST", headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({
|
const message = (event as MessageEvent).data;
|
||||||
token: sessionToken,
|
if (message.type === "onSession") {
|
||||||
})})
|
if (message.value !== "") {
|
||||||
if (res.status !== 200) {
|
sessionToken = message.value;
|
||||||
postMessage({type: 'onError', value: 'Error logging out'});
|
fetchTeamData();
|
||||||
return;
|
}
|
||||||
}
|
}
|
||||||
const data = await res.json();
|
})
|
||||||
if (data.success === true) {
|
|
||||||
postMessage({type: 'onInfo', value: 'BWContest: Logged out'});
|
onMount(() => {
|
||||||
sessionToken = undefined;
|
postMessage({type: "onStartup"});
|
||||||
} else {
|
})
|
||||||
postMessage({type: 'onError', value: 'Log out unsuccessful'});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<h1>Contest</h1>
|
<h1>Contest</h1>
|
||||||
@ -58,4 +103,9 @@ async function onLogout() {
|
|||||||
<button on:click={onLogin}>Login</button>
|
<button on:click={onLogin}>Login</button>
|
||||||
{:else}
|
{:else}
|
||||||
<button on:click={onLogout}>Logout</button>
|
<button on:click={onLogout}>Logout</button>
|
||||||
|
{#if teamData}
|
||||||
|
<p>TeamID: {teamData.teamId}</p>
|
||||||
|
<p>ContestID: {teamData.contestId}</p>
|
||||||
|
<button on:click={onClone}>Clone and Open Repo</button>
|
||||||
|
{/if}
|
||||||
{/if}
|
{/if}
|
Loading…
Reference in New Issue
Block a user