Skip to content

Commit 0a5d9a9

Browse files
committed
aligned with archive command
1 parent 724c0da commit 0a5d9a9

4 files changed

Lines changed: 62 additions & 59 deletions

File tree

.devproxy/api-specs/sharepoint.yaml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,30 @@ paths:
246246
responses:
247247
200:
248248
description: OK
249+
/_api/web/GetFileById({fileId}):
250+
get:
251+
parameters:
252+
- name: fileId
253+
in: path
254+
required: true
255+
description: file unique ID
256+
schema:
257+
type: string
258+
example: "'7a8c9207-7745-4cda-b0e2-be2618ee3030'"
259+
security:
260+
- delegated:
261+
- AllSites.Read
262+
- AllSites.Write
263+
- AllSites.Manage
264+
- AllSites.FullControl
265+
- application:
266+
- Sites.Read.All
267+
- Sites.ReadWrite.All
268+
- Sites.Manage.All
269+
- Sites.FullControl.All
270+
responses:
271+
200:
272+
description: OK
249273
/_api/web/GetFileById({fileId})/versions:
250274
get:
251275
parameters:

docs/docs/cmd/spo/file/file-unarchive.mdx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,13 @@ m365 spo file unarchive [options]
5151

5252
## Examples
5353

54-
Unarchive a file without prompting for confirmation.
54+
Unarchive a file by id without prompting for confirmation
5555

5656
```sh
5757
m365 spo file unarchive --webUrl https://contoso.sharepoint.com/sites/Marketing --id 7a8c9207-7745-4cda-b0e2-be2618ee3030 --force
5858
```
5959

60-
Unarchive a file by URL with prompting for confirmation.
60+
Unarchive a file by URL with prompting for confirmation
6161

6262
```sh
6363
m365 spo file unarchive --webUrl https://contoso.sharepoint.com/sites/Marketing --url '/sites/Marketing/shared documents/document.docx'

src/m365/spo/commands/file/file-unarchive.spec.ts

Lines changed: 28 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { telemetry } from '../../../../telemetry.js';
1010
import { pid } from '../../../../utils/pid.js';
1111
import { session } from '../../../../utils/session.js';
1212
import { sinonUtil } from '../../../../utils/sinonUtil.js';
13+
import { formatting } from '../../../../utils/formatting.js';
1314
import { z } from 'zod';
1415
import commands from '../../commands.js';
1516
import command from './file-unarchive.js';
@@ -20,6 +21,7 @@ describe(commands.FILE_UNARCHIVE, () => {
2021
let commandInfo: CommandInfo;
2122
let commandOptionsSchema: z.ZodTypeAny;
2223
let confirmationPromptStub: sinon.SinonStub;
24+
let loggerLogSpy: sinon.SinonSpy;
2325

2426
before(() => {
2527
sinon.stub(auth, 'restoreAuth').resolves();
@@ -46,6 +48,7 @@ describe(commands.FILE_UNARCHIVE, () => {
4648
log.push(msg);
4749
}
4850
};
51+
loggerLogSpy = sinon.spy(logger, 'log');
4952
confirmationPromptStub = sinon.stub(cli, 'promptForConfirmation').resolves(false);
5053
});
5154

@@ -130,6 +133,9 @@ describe(commands.FILE_UNARCHIVE, () => {
130133
});
131134

132135
it('prompts before unarchiving file when confirmation argument not passed', async () => {
136+
sinon.stub(request, 'get').resolves({ ListId: 'b2307a39-e878-458b-bc90-03bc578531d6', ListItemAllFields: { Id: 1 } });
137+
sinon.stub(request, 'post').resolves();
138+
133139
await command.action(logger, {
134140
options: {
135141
webUrl: 'https://contoso.sharepoint.com',
@@ -140,6 +146,7 @@ describe(commands.FILE_UNARCHIVE, () => {
140146
});
141147

142148
it('aborts unarchiving file when prompt not confirmed', async () => {
149+
const getStub = sinon.stub(request, 'get').resolves({ ListId: 'b2307a39-e878-458b-bc90-03bc578531d6', ListItemAllFields: { Id: 1 } });
143150
const postStub = sinon.stub(request, 'post').resolves();
144151

145152
await command.action(logger, {
@@ -149,12 +156,13 @@ describe(commands.FILE_UNARCHIVE, () => {
149156
}
150157
});
151158

159+
assert(getStub.notCalled);
152160
assert(postStub.notCalled);
153161
});
154162

155-
it('unarchives file by url when prompt confirmed', async () => {
163+
it('unarchives file by url', async () => {
156164
sinon.stub(request, 'get').callsFake(async (opts) => {
157-
if (opts.url === `https://contoso.sharepoint.com/sites/test/_api/web/GetFileByServerRelativePath(DecodedUrl=@f)?$select=ListId&$expand=ListItemAllFields&@f='%2Fsites%2Ftest%2FShared%20documents%2Fdocument.docx'`) {
165+
if (opts.url === `https://contoso.sharepoint.com/sites/test/_api/web/GetFileByServerRelativePath(DecodedUrl='${formatting.encodeQueryParameter('/sites/test/Shared documents/document.docx')}')?$select=ListId,ListItemAllFields/Id&$expand=ListItemAllFields`) {
158166
return {
159167
ListId: 'b2307a39-e878-458b-bc90-03bc578531d6',
160168
ListItemAllFields: {
@@ -174,22 +182,20 @@ describe(commands.FILE_UNARCHIVE, () => {
174182
throw 'Invalid request';
175183
});
176184

177-
sinonUtil.restore(cli.promptForConfirmation);
178-
sinon.stub(cli, 'promptForConfirmation').resolves(true);
179-
180185
await command.action(logger, {
181186
options: {
182187
webUrl: 'https://contoso.sharepoint.com/sites/test',
183-
url: '/sites/test/Shared documents/document.docx'
188+
url: '/sites/test/Shared documents/document.docx',
189+
force: true
184190
}
185191
});
186192

187-
assert.deepStrictEqual(postStub.lastCall.args[0].url, `https://contoso.sharepoint.com/sites/test/_api/Lists(guid'b2307a39-e878-458b-bc90-03bc578531d6')/items(1)/UnArchive`);
193+
assert(postStub.calledOnce);
188194
});
189195

190-
it('unarchives file by id when prompt confirmed', async () => {
196+
it('unarchives file by id', async () => {
191197
sinon.stub(request, 'get').callsFake(async (opts) => {
192-
if (opts.url === `https://contoso.sharepoint.com/sites/test/_api/web/GetFileById('00000000-0000-0000-0000-000000000000')?$select=ListId&$expand=ListItemAllFields`) {
198+
if (opts.url === `https://contoso.sharepoint.com/sites/test/_api/web/GetFileById('${formatting.encodeQueryParameter('00000000-0000-0000-0000-000000000000')}')?$select=ListId,ListItemAllFields/Id&$expand=ListItemAllFields`) {
193199
return {
194200
ListId: 'b2307a39-e878-458b-bc90-03bc578531d6',
195201
ListItemAllFields: {
@@ -210,22 +216,21 @@ describe(commands.FILE_UNARCHIVE, () => {
210216
throw 'Invalid request';
211217
});
212218

213-
sinonUtil.restore(cli.promptForConfirmation);
214-
sinon.stub(cli, 'promptForConfirmation').resolves(true);
215-
216219
await command.action(logger, {
217220
options: {
218221
webUrl: 'https://contoso.sharepoint.com/sites/test',
219-
id: '00000000-0000-0000-0000-000000000000'
222+
id: '00000000-0000-0000-0000-000000000000',
223+
verbose: true,
224+
force: true
220225
}
221226
});
222227

223-
assert.deepStrictEqual(postStub.lastCall.args[0].url, `https://contoso.sharepoint.com/sites/test/_api/Lists(guid'b2307a39-e878-458b-bc90-03bc578531d6')/items(1)/UnArchive`);
228+
assert(postStub.calledOnce);
224229
});
225230

226-
it('unarchives file by url', async () => {
231+
it('unarchives file using site-relative url', async () => {
227232
sinon.stub(request, 'get').callsFake(async (opts) => {
228-
if (opts.url === `https://contoso.sharepoint.com/sites/test/_api/web/GetFileByServerRelativePath(DecodedUrl=@f)?$select=ListId&$expand=ListItemAllFields&@f='%2Fsites%2Ftest%2FShared%20documents%2Fdocument.docx'`) {
233+
if (opts.url === `https://contoso.sharepoint.com/sites/test/_api/web/GetFileByServerRelativePath(DecodedUrl='${formatting.encodeQueryParameter('/sites/test/Shared Documents/document.docx')}')?$select=ListId,ListItemAllFields/Id&$expand=ListItemAllFields`) {
229234
return {
230235
ListId: 'b2307a39-e878-458b-bc90-03bc578531d6',
231236
ListItemAllFields: {
@@ -248,47 +253,27 @@ describe(commands.FILE_UNARCHIVE, () => {
248253
await command.action(logger, {
249254
options: {
250255
webUrl: 'https://contoso.sharepoint.com/sites/test',
251-
url: '/sites/test/Shared documents/document.docx',
256+
url: '/Shared Documents/document.docx',
252257
force: true
253258
}
254259
});
255260

256-
assert.deepStrictEqual(postStub.lastCall.args[0].url, `https://contoso.sharepoint.com/sites/test/_api/Lists(guid'b2307a39-e878-458b-bc90-03bc578531d6')/items(1)/UnArchive`);
261+
assert(postStub.calledOnce);
257262
});
258263

259-
it('unarchives file by id', async () => {
260-
sinon.stub(request, 'get').callsFake(async (opts) => {
261-
if (opts.url === `https://contoso.sharepoint.com/sites/test/_api/web/GetFileById('00000000-0000-0000-0000-000000000000')?$select=ListId&$expand=ListItemAllFields`) {
262-
return {
263-
ListId: 'b2307a39-e878-458b-bc90-03bc578531d6',
264-
ListItemAllFields: {
265-
Id: 1
266-
}
267-
};
268-
}
269-
270-
throw 'Invalid request';
271-
}
272-
);
273-
274-
const postStub = sinon.stub(request, 'post').callsFake(async (opts) => {
275-
if (opts.url === `https://contoso.sharepoint.com/sites/test/_api/Lists(guid'b2307a39-e878-458b-bc90-03bc578531d6')/items(1)/UnArchive`) {
276-
return;
277-
}
278-
279-
throw 'Invalid request';
280-
});
264+
it('outputs no result when unarchiving a file', async () => {
265+
sinon.stub(request, 'get').resolves({ ListId: 'b2307a39-e878-458b-bc90-03bc578531d6', ListItemAllFields: { Id: 1 } });
266+
sinon.stub(request, 'post').resolves();
281267

282268
await command.action(logger, {
283269
options: {
284270
webUrl: 'https://contoso.sharepoint.com/sites/test',
285-
id: '00000000-0000-0000-0000-000000000000',
286-
verbose: true,
271+
url: '/sites/test/Shared documents/document.docx',
287272
force: true
288273
}
289274
});
290275

291-
assert.deepStrictEqual(postStub.lastCall.args[0].url, `https://contoso.sharepoint.com/sites/test/_api/Lists(guid'b2307a39-e878-458b-bc90-03bc578531d6')/items(1)/UnArchive`);
276+
assert(loggerLogSpy.notCalled);
292277
});
293278

294279
it('handles error correctly', async () => {

src/m365/spo/commands/file/file-unarchive.ts

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -55,44 +55,38 @@ class SpoFileUnarchiveCommand extends SpoCommand {
5555
const { webUrl, url, id, force, verbose } = args.options;
5656

5757
if (!force) {
58-
const result = await cli.promptForConfirmation({ message: `Are you sure you want to unarchive the file ${url || id} at site ${webUrl}?` });
58+
const result = await cli.promptForConfirmation({ message: `This item is archived. Reactivation could take up to 24 hours. Are you sure you would like to unarchive this item?` });
5959
if (!result) {
6060
return;
6161
}
6262
}
6363

6464
try {
6565
if (verbose) {
66-
await logger.logToStderr(`Unarchiving file ${url || id} at site ${webUrl}...`);
66+
await logger.logToStderr(`Unarchiving file '${url || id}' at site '${webUrl}'...`);
6767
}
6868

69-
let requestUrl: string = '';
69+
let requestUrl: string = `${webUrl}/_api/web`;
7070

7171
if (id) {
72-
requestUrl = `${webUrl}/_api/web/GetFileById('${formatting.encodeQueryParameter(id)}')`;
72+
requestUrl += `/GetFileById('${formatting.encodeQueryParameter(id)}')`;
7373
}
7474
else if (url) {
75-
requestUrl = `${webUrl}/_api/web/GetFileByServerRelativePath(DecodedUrl=@f)`;
76-
}
77-
78-
let queryString: string = '?$select=ListId&$expand=ListItemAllFields';
79-
80-
if (url) {
8175
const serverRelativePath = urlUtil.getServerRelativePath(webUrl, url);
82-
queryString += `&@f='${formatting.encodeQueryParameter(serverRelativePath)}'`;
76+
requestUrl += `/GetFileByServerRelativePath(DecodedUrl='${formatting.encodeQueryParameter(serverRelativePath)}')`;
8377
}
78+
requestUrl += '?$select=ListId,ListItemAllFields/Id&$expand=ListItemAllFields';
8479

8580
const fileInfo = await request.get<{ ListId: string; ListItemAllFields: { Id: number } }>({
86-
url: requestUrl + queryString,
81+
url: requestUrl,
8782
headers: {
8883
accept: 'application/json;odata=nometadata'
8984
},
9085
responseType: 'json'
9186
});
9287

93-
const unarchiveUrl = `${webUrl}/_api/Lists(guid'${fileInfo.ListId}')/items(${fileInfo.ListItemAllFields.Id})/UnArchive`;
9488
const requestOptions: CliRequestOptions = {
95-
url: unarchiveUrl,
89+
url: `${webUrl}/_api/Lists(guid'${fileInfo.ListId}')/items(${fileInfo.ListItemAllFields.Id})/UnArchive`,
9690
headers: {
9791
accept: 'application/json;odata=nometadata'
9892
},

0 commit comments

Comments
 (0)