Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhances command spo list remove #6378

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion docs/docs/cmd/spo/list/list-remove.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Removes the specified list
```sh
m365 spo list remove [options]
```

## Options

```md definition-list
Expand All @@ -22,6 +22,9 @@ m365 spo list remove [options]
`-t, --title [title]`
: Title of the list to remove. Specify either `id` or `title` but not both.

`--recycle`
: Instead of permanently deleting, send the list to the recycle bin.

`-f, --force`
: Don't prompt for confirming removing the list.
```
Expand All @@ -42,6 +45,12 @@ Remove the list with a specific title located in a specific site.
m365 spo list remove --webUrl https://contoso.sharepoint.com/sites/project-x --title 'List 1'
```

Remove a list specified by id by sending it to the recycle bin instead of permanently removing it

```sh
m365 spo list remove --webUrl https://contoso.sharepoint.com/sites/project-x --id 0cd891ef-afce-4e55-b836-fce03286cccf --recycle
```

## Response

The command won't return a response on success.
67 changes: 44 additions & 23 deletions src/m365/spo/commands/list/list-remove.spec.ts
milanholemans marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ import command from './list-remove.js';
import { settingsNames } from '../../../../settingsNames.js';

describe(commands.LIST_REMOVE, () => {
const listId = 'b2307a39-e878-458b-bc90-03bc578531d6';
const webUrl = 'https://contoso.sharepoint.com';
const listTitle = 'Documents';

let log: any[];
let logger: Logger;
let commandInfo: CommandInfo;
Expand Down Expand Up @@ -44,9 +48,10 @@ describe(commands.LIST_REMOVE, () => {
}
};
requests = [];
sinon.stub(cli, 'promptForConfirmation').callsFake(() => {
sinon.stub(cli, 'promptForConfirmation').callsFake(async () => {
promptIssued = true;
return Promise.resolve(false);
//return Promise.resolve(false);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's remove comments

return false;
});
promptIssued = false;
});
Expand Down Expand Up @@ -95,14 +100,10 @@ describe(commands.LIST_REMOVE, () => {
sinon.stub(request, 'post').callsFake(async (opts) => {
requests.push(opts);

if ((opts.url as string).indexOf(`/_api/web/lists(guid'`) > -1) {
if (opts.headers &&
opts.headers.accept &&
(opts.headers.accept as string).indexOf('application/json') === 0) {
return;
}
}

if (opts.url === `${webUrl}/_api/web/lists(guid'${listId}')`) {
return;
}
throw 'Invalid request';
});

Expand All @@ -111,9 +112,7 @@ describe(commands.LIST_REMOVE, () => {
await command.action(logger, { options: { id: 'b2307a39-e878-458b-bc90-03bc578531d6', webUrl: 'https://contoso.sharepoint.com' } });
let correctRequestIssued = false;
requests.forEach(r => {
if (r.url.indexOf(`/_api/web/lists(guid'`) > -1 &&
r.headers.accept &&
r.headers.accept.indexOf('application/json') === 0) {
if (r.url === `${webUrl}/_api/web/lists(guid'${listId}')`) {
correctRequestIssued = true;
}
});
Expand All @@ -123,13 +122,8 @@ describe(commands.LIST_REMOVE, () => {
it('removes the list when prompt confirmed by option', async () => {
sinon.stub(request, 'post').callsFake(async (opts) => {
requests.push(opts);

if ((opts.url as string).indexOf(`/_api/web/lists(guid'`) > -1) {
if (opts.headers &&
opts.headers.accept &&
(opts.headers.accept as string).indexOf('application/json') === 0) {
return;
}
if (opts.url === `${webUrl}/_api/web/lists(guid'${listId}')`) {
return;
}

throw 'Invalid request';
Expand All @@ -138,15 +132,41 @@ describe(commands.LIST_REMOVE, () => {
await command.action(logger, { options: { id: 'b2307a39-e878-458b-bc90-03bc578531d6', webUrl: 'https://contoso.sharepoint.com', force: true } });
let correctRequestIssued = false;
requests.forEach(r => {
if (r.url.indexOf(`/_api/web/lists(guid'`) > -1 &&
r.headers.accept &&
r.headers.accept.indexOf('application/json') === 0) {
if (r.url === `${webUrl}/_api/web/lists(guid'${listId}')`) {
correctRequestIssued = true;
}
});
assert(correctRequestIssued);
});

it('uses correct API url when recycle option is passed', async () => {
sinon.stub(request, 'post').callsFake(async (opts) => {
requests.push(opts);
if (opts.url === `${webUrl}/_api/web/lists/GetByTitle('${listTitle}')/recycle`) {
return 'Correct URL';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This API doesn't return any result. Let's also return nothing in our stub.

}

throw 'Invalid request';
});
sinonUtil.restore(cli.promptForConfirmation);
sinon.stub(cli, 'promptForConfirmation').resolves(true);
await command.action(logger, {
options: {
title: 'Documents',
recycle: true,
webUrl: 'https://contoso.sharepoint.com'
}
});
let correctRequestIssued = false;
requests.forEach(r => {
if (r.url === `${webUrl}/_api/web/lists/GetByTitle('${listTitle}')/recycle`) {
correctRequestIssued = true;
}
});
Comment on lines +161 to +165
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of doing this, let's use assert(postStub.calledOnce). This ensures that the request was called once and didn't throw an error.


assert(correctRequestIssued);
});

it('command correctly handles list get reject request', async () => {
const error = {
error: {
Expand All @@ -159,7 +179,8 @@ describe(commands.LIST_REMOVE, () => {
}
};
sinon.stub(request, 'post').callsFake(async (opts) => {
if ((opts.url as string).indexOf('/_api/web/lists/GetByTitle(') > -1) {

if (opts.url === `${webUrl}/_api/web/lists/GetByTitle('${listTitle}')`) {
throw error;
}

Expand Down
21 changes: 18 additions & 3 deletions src/m365/spo/commands/list/list-remove.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ interface Options extends GlobalOptions {
webUrl: string;
id?: string;
title?: string;
recycle?: boolean;
force?: boolean;
}

Expand All @@ -32,16 +33,18 @@ class SpoListRemoveCommand extends SpoCommand {

this.#initTelemetry();
this.#initOptions();
this.#initTypes();
this.#initValidators();
this.#initOptionSets();
}

#initTelemetry(): void {
this.telemetry.push((args: CommandArgs) => {
Object.assign(this.telemetryProperties, {
id: (!(!args.options.id)).toString(),
title: (!(!args.options.title)).toString(),
force: (!(!args.options.force)).toString()
id: typeof args.options.id !== 'undefined',
title: typeof args.options.title !== 'undefined',
force: !!args.options.force,
recycle: !!args.options.recycle
});
});
}
Expand All @@ -57,6 +60,9 @@ class SpoListRemoveCommand extends SpoCommand {
{
option: '-t, --title [title]'
},
{
option: '--recycle'
milanholemans marked this conversation as resolved.
Show resolved Hide resolved
},
{
option: '-f, --force'
}
Expand Down Expand Up @@ -85,6 +91,11 @@ class SpoListRemoveCommand extends SpoCommand {
this.optionSets.push({ options: ['id', 'title'] });
}

#initTypes(): void {
this.types.string.push('id', 'title', 'webUrl');
this.types.boolean.push('force', 'recycle');
}

public async commandAction(logger: Logger, args: CommandArgs): Promise<void> {
const removeList = async (): Promise<void> => {
if (this.verbose) {
Expand All @@ -100,6 +111,10 @@ class SpoListRemoveCommand extends SpoCommand {
requestUrl = `${args.options.webUrl}/_api/web/lists/GetByTitle('${formatting.encodeQueryParameter(args.options.title as string)}')`;
}

if (args.options.recycle) {
requestUrl += `/recycle`;
}

const requestOptions: CliRequestOptions = {
url: requestUrl,
method: 'POST',
Expand Down