[extension] Fix submitting

This commit is contained in:
orosmatthew 2023-10-17 12:50:13 -04:00
parent 8bf0ec5d97
commit cb7383a437
3 changed files with 166 additions and 171 deletions

View File

@ -5,6 +5,7 @@ import { extensionSettings } from './extension';
import { runJava } from './run/java'; import { runJava } from './run/java';
import { join } from 'path'; import { join } from 'path';
import { TeamData } from './SidebarProvider'; import { TeamData } from './SidebarProvider';
import { submitProblem } from './submit';
export type ProblemData = { export type ProblemData = {
id: number; id: number;
@ -17,7 +18,8 @@ export type ProblemData = {
export type MessageType = export type MessageType =
| { msg: 'onRequestProblemData' } | { msg: 'onRequestProblemData' }
| { msg: 'onRun'; data: { problemId: number; input: string } } | { msg: 'onRun'; data: { problemId: number; input: string } }
| { msg: 'onKill' }; | { msg: 'onKill' }
| { msg: 'onSubmit'; data: { problemId: number } };
export type WebviewMessageType = export type WebviewMessageType =
| { msg: 'onProblemData'; data: ProblemData } | { msg: 'onProblemData'; data: ProblemData }
| { msg: 'onRunning' } | { msg: 'onRunning' }
@ -91,49 +93,45 @@ export class BWPanel {
this.panel.webview.postMessage(m); this.panel.webview.postMessage(m);
} }
private async update() { private async handleSubmit(problemId: number) {
const webview = this.panel.webview; if (this.problemData === undefined) {
console.error('Problem data undefined');
this.panel.webview.html = this._getHtmlForWebview(webview);
webview.onDidReceiveMessage(async (m: MessageType) => {
switch (m.msg) {
case 'onKill': {
if (this.runningProgram !== undefined) {
this.runningProgram.kill();
return; return;
} }
break; const problem = this.problemData.find((p) => p.id === problemId);
if (problem === undefined) {
console.error('Invalid problem Id');
return;
} }
// this.kill(); const sessionToken = this.context.globalState.get<string>('token');
// } if (sessionToken === undefined) {
// case 'onSubmit': { console.error('No session token');
// await vscode.workspace.saveAll(); return;
// if (!data.value) { }
// return; const teamData = this.context.globalState.get<TeamData>('teamData');
// } if (teamData === undefined) {
// const ans = await vscode.window.showInformationMessage( console.error('No team data');
// `Are you sure you want to submit ${data.value.problemName}?`, return;
// 'Yes', }
// 'No' await vscode.workspace.saveAll();
// ); const ans = await vscode.window.showInformationMessage(
// if (ans !== 'Yes') { `Are you sure you want to submit ${problem.name}?`,
// break; 'Yes',
// } 'No'
// try { );
// await submitProblem( if (ans !== 'Yes') {
// data.value.sessionToken, return;
// data.value.contestId, }
// data.value.teamId, submitProblem(sessionToken, teamData.contestId, teamData.teamId, problemId).then((result) => {
// data.value.problemId if (result.success === true) {
// ); vscode.window.showInformationMessage('Submitted!');
// } catch (err: any) { } else {
// vscode.window.showErrorMessage(err.message ?? 'Submission unsuccessful'); vscode.window.showErrorMessage(`Error: ${result.message}`);
// break; }
// } });
// vscode.window.showInformationMessage('Submitted!'); }
// break;
// } private async handleRun(problemId: number, input: string) {
case 'onRun': {
const teamData: TeamData | undefined = this.context.globalState.get('teamData'); const teamData: TeamData | undefined = this.context.globalState.get('teamData');
if (teamData === undefined) { if (teamData === undefined) {
return; return;
@ -145,7 +143,7 @@ export class BWPanel {
vscode.window.showErrorMessage('Already Running'); vscode.window.showErrorMessage('Already Running');
return; return;
} }
const problem = this.problemData.find((p) => (p.id = m.data.problemId)); const problem = this.problemData.find((p) => (p.id = problemId));
if (problem === undefined) { if (problem === undefined) {
return; return;
} }
@ -172,7 +170,7 @@ export class BWPanel {
`${problem.pascalName}.java` `${problem.pascalName}.java`
), ),
problem.pascalName, problem.pascalName,
m.data.input, input,
(data: string) => { (data: string) => {
outputBuffer.push(data); outputBuffer.push(data);
this.webviewPostMessage({ msg: 'onRunningOutput', data: outputBuffer.join('') }); this.webviewPostMessage({ msg: 'onRunningOutput', data: outputBuffer.join('') });
@ -184,16 +182,16 @@ export class BWPanel {
); );
if (killFunc !== undefined) { if (killFunc !== undefined) {
this.runningProgram = { this.runningProgram = {
problemId: m.data.problemId, problemId: problemId,
outputBuffer: outputBuffer, outputBuffer: outputBuffer,
kill: killFunc kill: killFunc
}; };
} else { } else {
this.webviewPostMessage({ msg: 'onRunningDone' }); this.webviewPostMessage({ msg: 'onRunningDone' });
} }
break;
} }
case 'onRequestProblemData': {
private async handleRequestProblemData() {
const token: string | undefined = this.context.globalState.get('token'); const token: string | undefined = this.context.globalState.get('token');
if (token !== undefined) { if (token !== undefined) {
const res = await fetch(urlJoin(this.webUrl, `/api/contest/${token}`)); const res = await fetch(urlJoin(this.webUrl, `/api/contest/${token}`));
@ -206,22 +204,33 @@ export class BWPanel {
}); });
} }
} }
}
private async update() {
const webview = this.panel.webview;
this.panel.webview.html = this._getHtmlForWebview(webview);
webview.onDidReceiveMessage((m: MessageType) => {
switch (m.msg) {
case 'onKill': {
if (this.runningProgram !== undefined) {
this.runningProgram.kill();
return;
}
break;
}
case 'onSubmit': {
this.handleSubmit(m.data.problemId);
break;
}
case 'onRun': {
this.handleRun(m.data.problemId, m.data.input);
break;
}
case 'onRequestProblemData': {
this.handleRequestProblemData();
break; break;
} }
// case 'onInfo': {
// if (!data.value) {
// return;
// }
// vscode.window.showInformationMessage(data.value);
// break;
// }
// case 'onError': {
// if (!data.value) {
// return;
// }
// vscode.window.showErrorMessage(data.value);
// break;
// }
} }
}); });
} }

View File

@ -1,63 +1,58 @@
import { extensionSettings } from './extension'; import { extensionSettings } from './extension';
import { exec } from 'child_process'; import * as fs from 'fs-extra';
import util = require('node:util'); import git from 'isomorphic-git';
import axios from 'axios'; import path = require('path');
import http from 'isomorphic-git/http/node';
const execPromise = util.promisify(exec); import urlJoin from 'url-join';
export async function submitProblem( export async function submitProblem(
sessionToken: string, sessionToken: string,
contestId: number, contestId: number,
teamId: number, teamId: number,
problemId: number problemId: number
) { ): Promise<{ success: true } | { success: false; message: string }> {
const repoClonePath = extensionSettings().repoClonePath; const repoClonePath = extensionSettings().repoClonePath;
console.log(repoClonePath);
const clonedRepoPath = `${repoClonePath}/BWContest/${contestId.toString()}/${teamId.toString()}`; const repoDir = path.join(repoClonePath, 'BWContest', contestId.toString(), teamId.toString());
await git.add({ fs, dir: repoDir, filepath: '.' });
let output: { stdout: string; stderr: string }; const hash = await git.commit({
output = await execPromise(`git add .`, { fs,
cwd: clonedRepoPath dir: repoDir,
author: { name: `Team ${teamId}` },
message: `Submit problem ${problemId}`
}); });
if (output.stderr) { try {
console.error(output.stderr); const result = await git.push({
} fs,
http,
output = await execPromise(`git commit -m "Submit problem ${problemId}"`, { dir: repoDir
cwd: clonedRepoPath
}); });
if (result.ok !== true) {
if (output.stderr) { return { success: false, message: 'Push failure' };
console.error(output.stderr); }
} catch (error) {
return { success: false, message: 'Unable to push' };
} }
output = await execPromise(`git push`, { const res = await fetch(
cwd: clonedRepoPath urlJoin(extensionSettings().webUrl, '/api/team', sessionToken, '/submit'),
}); {
method: 'POST',
if (output.stderr) { headers: { 'Content-Type': 'application/json' },
console.error(output.stderr); body: JSON.stringify({
} commitHash: hash,
output = await execPromise(`git rev-parse HEAD`, {
cwd: clonedRepoPath
});
if (output.stderr) {
console.error(output.stderr);
}
const commitHash = output.stdout.toString().replace('\n', '');
const res = await axios.post(`http://localhost:5173/api/team/${sessionToken}/submit`, {
commitHash: commitHash,
problemId: problemId problemId: problemId
}); })
}
);
if (res.status !== 200) { if (res.status !== 200) {
throw Error('Failed to post submission'); return { success: false, message: 'Submission POST failure' };
} }
if (!res.data.success) { const resData = await res.json();
throw Error(res.data.message ?? 'Unknown error'); if (resData.success !== true) {
return { success: false, message: resData.message };
} }
return { success: true };
} }

View File

@ -50,18 +50,9 @@
} }
function onSubmit() { function onSubmit() {
// if (teamId && contestId && sessionToken) { if (problemData !== undefined) {
// postMessage({ postMessage({ msg: 'onSubmit', data: { problemId: problemData[activeProblemIndex].id } });
// type: 'onSubmit', }
// value: {
// sessionToken: sessionToken,
// contestId: contestId,
// teamId: teamId,
// problemId: activeProblem.id,
// problemName: activeProblem.pascalName
// }
// });
// }
} }
function onKill() { function onKill() {