Skip to content
Snippets Groups Projects
Commit 8a38c351 authored by michael.minelli's avatar michael.minelli
Browse files

Add publish/unpublish commands

parent b2def83e
No related branches found
No related tags found
No related merge requests found
Pipeline #25474 failed
import EnoncePublishUnpublishCommandBase from './EnoncePublishUnpublishCommandBase';
class EnoncePublishCommand extends EnoncePublishUnpublishCommandBase {
protected commandName: string = 'publish';
protected publish: boolean = true;
}
export default new EnoncePublishCommand();
\ No newline at end of file
import CommanderCommand from '../CommanderCommand';
import inquirer from 'inquirer';
import Enonce from '../../types/Enonce';
import chalk from 'chalk';
import SessionManager from '../../managers/SessionManager';
import ora from 'ora';
import DojoBackendManager from '../../managers/DojoBackendManager';
abstract class EnoncePublishUnpublishCommandBase extends CommanderCommand {
protected abstract publish: boolean;
protected defineCommand() {
this.command
.description('publish an enonce')
.argument('<name or url>', 'name or url (http/s or ssh) of the enonce')
.option('-f, --force', 'don\'t ask for confirmation')
.action(this.commandAction.bind(this));
}
protected async commandAction(enonceNameOrUrl: string, options: { force: boolean }): Promise<void> {
if ( !options.force ) {
options.force = (await inquirer.prompt({
type : 'confirm',
name : 'force',
message: this.publish ? 'Are you sure you want to publish this enonce?' : 'Are you sure you want to unpublish this enonce?'
})).force;
}
if ( !options.force ) {
return;
}
let enonce!: Enonce | undefined;
{
console.log(chalk.cyan('Please wait while we verify and retrieve data...'));
if ( !await SessionManager.testSession(true, null) ) {
return;
}
ora('Checking enonce:').start().info();
ora({
text : enonceNameOrUrl,
indent: 4
}).start().info();
const enonceGetSpinner: ora.Ora = ora({
text : 'Checking if enonce exists',
indent: 8
}).start();
enonce = await DojoBackendManager.getEnonce(enonceNameOrUrl);
if ( !enonce ) {
enonceGetSpinner.fail(`The enonce doesn't exists`);
return;
}
enonceGetSpinner.succeed(`The enonce exists`);
const enonceCheckAccessSpinner: ora.Ora = ora({
text : 'Checking accesses',
indent: 8
}).start();
if ( !enonce.staff ) {
enonceCheckAccessSpinner.fail(`You are not in the staff of this enonce`);
return;
}
enonceCheckAccessSpinner.succeed(`You are in the staff of this enonce`);
}
{
console.log(chalk.cyan(`Please wait while we ${ this.publish ? 'publish' : 'unpublish' } the enonce...`));
try {
await DojoBackendManager.changeEnoncePublishedStatus(enonce, this.publish);
} catch ( error ) {
return;
}
}
}
}
export default EnoncePublishUnpublishCommandBase;
\ No newline at end of file
import EnoncePublishUnpublishCommandBase from './EnoncePublishUnpublishCommandBase';
class EnonceUnpublishCommand extends EnoncePublishUnpublishCommandBase {
protected commandName: string = 'unpublish';
protected publish: boolean = false;
}
export default new EnonceUnpublishCommand();
\ No newline at end of file
import CommanderCommand from '../CommanderCommand';
import chalk from 'chalk';
import GitlabManager from '../../managers/GitlabManager';
import SessionManager from '../../managers/SessionManager';
import GitlabUser from '../../shared/types/Gitlab/GitlabUser';
import Enonce from '../../types/Enonce';
import ora from 'ora';
......@@ -55,11 +54,10 @@ class ExerciceCreateCommand extends CommanderCommand {
text : 'Checking if enonce is published',
indent: 4
}).start();
//TODO : Check if the enonce is published
//if ( false ) {
//enoncePublishedSpinner.fail(`Enonce "${ enonce.name }" isn't published`);
//return;
//}
if ( !enonce.published ) {
enoncePublishedSpinner.fail(`Enonce "${ enonce.name }" isn't published`);
return;
}
enoncePublishedSpinner.succeed(`Enonce "${ enonce.name }" is published`);
}
......
......@@ -129,6 +129,34 @@ class DojoBackendManager {
throw error;
}
}
public async changeEnoncePublishedStatus(enonce: Enonce, publish: boolean, verbose: boolean = true) {
const spinner: ora.Ora = ora('Changing published status...');
if ( verbose ) {
spinner.start();
}
try {
await axios.patch<DojoResponse<null>>(this.getApiUrl(publish ? ApiRoutes.ENONCE_PUBLISH : ApiRoutes.ENONCE_UNPUBLISH).replace('{{nameOrUrl}}', encodeURIComponent(enonce.name)), {});
if ( verbose ) {
spinner.succeed(`Enonce ${ enonce.name } successfully ${ publish ? 'published' : 'unpublished' }`);
}
return;
} catch ( error ) {
if ( verbose ) {
if ( error instanceof AxiosError && error.response ) {
spinner.fail(`Enonce visibility change error: ${ error.response.statusText }`);
} else {
spinner.fail(`Enonce visibility change error: unknown error`);
}
}
throw error;
}
}
}
......
......@@ -4,6 +4,8 @@ enum ApiRoutes {
GITLAB_CHECK_TEMPLATE_ACCESS = '/gitlab/project/{{id}}/checkTemplateAccess',
ENONCE_GET = '/enonces/{{nameOrUrl}}',
ENONCE_CREATE = '/enonces',
ENONCE_PUBLISH = '/enonces/{{nameOrUrl}}/publish',
ENONCE_UNPUBLISH = '/enonces/{{nameOrUrl}}/unpublish',
EXERCICE_CREATE = '/enonces/{{nameOrUrl}}/exercices',
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment