Skip to content
This repository was archived by the owner on Jan 15, 2025. It is now read-only.

Commit 0ba6c5a

Browse files
authored
Merge pull request #153 from microsoft/qna-fixes
Applying fixes from Spec review meeting
2 parents af5b47f + e505ef7 commit 0ba6c5a

File tree

5 files changed

+47
-35
lines changed

5 files changed

+47
-35
lines changed

packages/qnamaker/src/commands/qnamaker/create/kb.ts

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ const createKbJSON = require('./../../../../utils/payloads/createKb')
99
const fs = require('fs-extra')
1010
const path = require('path')
1111
const readlineSync = require('readline-sync')
12-
const {cli} = require('cli-ux')
1312

1413
import {Inputs, processInputs} from '../../../utils/qnamakerbase'
1514

@@ -19,7 +18,7 @@ export default class QnamakerCreateKb extends Command {
1918
static flags: flags.Input<any> = {
2019
in: flags.string({description: 'The CreateKbDTO object to send in the body of the request.', required: true}),
2120
name: flags.string({description: 'Name of the kb you want to create. This will override the name of KB that might be present in the CreateKb DTO'}),
22-
wait: flags.boolean({description: 'Wait for the operation to complete.'}),
21+
save: flags.boolean({description: 'Save the kbId in config.'}),
2322
subscriptionKey: flags.string({description: 'Specifies the qnamaker Ocp-Apim-Subscription Key (found in Keys under Resource Management section for your Qna Maker cognitive service). Overrides the subscriptionkey value present in config'}),
2423
help: flags.help({char: 'h', description: 'qnamaker:create:kb command help'}),
2524
}
@@ -53,30 +52,24 @@ export default class QnamakerCreateKb extends Command {
5352
throw new CLIError(JSON.stringify(result.error, null, 4))
5453
}
5554

56-
if (flags.wait) {
57-
result = await qnaconfig.waitForOperationSucceeded(input.config, result)
55+
result = await qnaconfig.waitForOperationSucceeded(input.config, result)
5856

57+
if (flags.save) {
5958
let kbId = result.resourceLocation.split('/')[2]
6059
input.config.kbId = kbId
61-
let kb = await this.updateKbId(input.config)
60+
await this.updateKbId(input.config)
61+
let userConfig: any = {}
6262

63-
this.log(JSON.stringify(result, null, 2))
64-
65-
if (flags.wait) {
66-
const answer = await cli.prompt(`Would you like to save ${kb.name} ${kb.id} in your .qnamakerrc so that future commands will be with this KB? [yes] `, {default: 'yes'})
67-
if (answer[0] === 'y') {
68-
let userConfig: any = {}
69-
if (fs.existsSync(path.join(this.config.configDir, 'config.json'))) {
70-
userConfig = await fs.readJSON(path.join(this.config.configDir, 'config.json'))
71-
} else {
72-
await fs.mkdirp(this.config.configDir)
73-
}
74-
75-
userConfig.qnamaker = input.config
76-
await fs.writeJson(path.join(this.config.configDir, 'config.json'), userConfig, {spaces: 2})
77-
this.log('qnamaker config updated')
78-
}
63+
if (fs.existsSync(path.join(this.config.configDir, 'config.json'))) {
64+
userConfig = await fs.readJSON(path.join(this.config.configDir, 'config.json'))
65+
} else {
66+
await fs.mkdirp(this.config.configDir)
7967
}
68+
69+
userConfig.qnamaker = input.config
70+
await fs.writeJson(path.join(this.config.configDir, 'config.json'), userConfig, {spaces: 2})
71+
} else {
72+
this.log(JSON.stringify(result, null, 2))
8073
}
8174
}
8275

packages/qnamaker/src/commands/qnamaker/get/kb.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export default class QnamakerGetKb extends Command {
88
static description = 'Get metadata about a knowledgebase'
99

1010
static flags: flags.Input<any> = {
11-
kbId: flags.string({description: 'Knowledgebase id to get metadata.', required: true}),
11+
kbId: flags.string({description: 'Knowledgebase id to get metadata.'}),
1212
subscriptionKey: flags.string({description: 'Specifies the qnamaker Ocp-Apim-Subscription Key (found in Keys under Resource Management section for your Qna Maker cognitive service). Overrides the subscriptionkey value present in config'}),
1313
help: flags.help({char: 'h', description: 'qnamaker:get:kb command help'}),
1414
}

packages/qnamaker/src/commands/qnamaker/init.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ export default class QnamakerInit extends Command {
1717
async run() {
1818
const result = await this.initializeConfig()
1919
if (result) {
20-
this.log(`Successfully wrote ${process.cwd()}/.qnamakerrc`)
20+
this.log(`Successfully wrote ${this.config.configDir}/config.json`)
2121
}
2222
}
2323

2424
async initializeConfig(): Promise<boolean> {
25-
this.log(chalk.cyan.bold('\nThis util will walk you through creating a .qnamakerrc file\n\nPress ^C at any time to quit.\n\n'))
25+
this.log(chalk.cyan.bold('\nThis util will walk you through the QnA Maker config settings\n\nPress ^C at any time to quit.\n\n'))
2626
const questions = [
2727
'What is your QnAMaker access/subscription key? (found on the Cognitive Services Azure portal page under "access keys") ',
2828
'What would you like to use as your active knowledgebase ID? [none] '

packages/qnamaker/src/commands/qnamaker/query.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ export default class QnamakerQuery extends Command {
1212
top: flags.integer({description: 'Query to get a prediction for.'}),
1313
isTest: flags.boolean({description: 'Query against the test index.', default: false}),
1414
scoreThreshold: flags.integer({description: 'Query to get a prediction for.'}),
15-
subscriptionKey: flags.string({description: 'Specifies the qnamaker Ocp-Apim-Subscription Key (found in Keys under Resource Management section for your Qna Maker cognitive service). Overrides the subscriptionkey value present in config'}),
1615
hostname: flags.string({description: 'Specifies the url for your private QnA service. Overrides the value present in config.'}),
1716
endpointKey: flags.string({description: 'Specifies the endpoint key for your private QnA service.(from qnamaker.ai portal user settings page). Overrides the value present in config.'}),
1817
kbId: flags.string({description: 'Specifies the active qnamaker knowledgebase id. Overrides the value present in the config'}),

packages/qnamaker/test/commands/qnamaker/create/kb.test.ts

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import {expect, test} from '@oclif/test'
22
import * as path from 'path'
33
import cli from 'cli-ux'
4-
import {initTestConfigFile, deleteTestConfigFile} from '../../../configfilehelper'
4+
import {initTestConfigFile, deleteTestConfigFile, getConfigFile} from '../../../configfilehelper'
55
const nock = require('nock')
6+
const fs = require('fs-extra')
67

78
describe('qnamaker:create:kb', () => {
89
before(async function() {
@@ -11,7 +12,27 @@ describe('qnamaker:create:kb', () => {
1112
// runs before all tests in this block
1213
const scope = nock('https://westus.api.cognitive.microsoft.com/qnamaker/v4.0')
1314
.post('/knowledgebases/createasync')
14-
.reply(200, {operationState: "Succeeded"})
15+
.reply(200,
16+
{
17+
operationState: "Succeeded",
18+
createdTimestamp: "2019-08-06T12:46:03Z",
19+
lastActionTimestamp: "2019-08-06T12:46:19Z",
20+
resourceLocation: "/knowledgebases/8600c573-2acf-4466-97e8-999ad4cecbc2",
21+
userId: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
22+
operationId: "5690998c-4438-4ae1-900a-88a2aa3bfa68"
23+
})
24+
25+
const scope2 = nock('https://westus.api.cognitive.microsoft.com/qnamaker/v4.0')
26+
.get('/operations/5690998c-4438-4ae1-900a-88a2aa3bfa68')
27+
.reply(200,
28+
{
29+
operationState: "Succeeded",
30+
createdTimestamp: "2019-08-06T12:46:03Z",
31+
lastActionTimestamp: "2019-08-06T12:46:19Z",
32+
resourceLocation: "/knowledgebases/8600c573-2acf-4466-97e8-999ad4cecbc2",
33+
userId: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
34+
operationId: "5690998c-4438-4ae1-900a-88a2aa3bfa68"
35+
})
1536
});
1637

1738
after(async function(){
@@ -22,11 +43,11 @@ describe('qnamaker:create:kb', () => {
2243
.stdout()
2344
.command(['qnamaker:create:kb','--in', `${path.join(__dirname, '../../../fixtures/kb.json')}`])
2445
.it('Creates kb qnamaker:create:kb --in', ctx => {
25-
expect(ctx.stdout).to.empty
46+
expect(ctx.stdout).to.contain('"resourceLocation": "/knowledgebases/8600c573-2acf-4466-97e8-999ad4cecbc2",\n ')
2647
})
2748
})
2849

29-
describe('qnamaker:create:kb --wait', () => {
50+
describe('qnamaker:create:kb --save', () => {
3051
before(async function() {
3152
await initTestConfigFile()
3253
// runs before all tests in this block
@@ -68,7 +89,7 @@ describe('qnamaker:create:kb --wait', () => {
6889
.get('/knowledgebases/8600c573-2acf-4466-97e8-999ad4cecbc2')
6990
.reply(200,
7091
{
71-
id: "f8654745-2406-4a51-b3e6-bba5fb4942ba",
92+
id: "8600c573-2acf-4466-97e8-999ad4cecbc2",
7293
hostName: "https://somehostname.net",
7394
lastAccessedTimestamp: "2019-08-06T18:00:50Z",
7495
lastChangedTimestamp: "2019-08-06T18:00:50Z",
@@ -92,10 +113,9 @@ describe('qnamaker:create:kb --wait', () => {
92113

93114

94115
test
95-
.stub(cli, 'prompt', () => async () => 'N')
96-
.stdout()
97-
.command(['qnamaker:create:kb', '--in', `${path.join(__dirname, '../../../fixtures/kb.json')}`, '--wait'])
98-
.it('Creates kb and awaits for the creation of it', ctx => {
99-
expect(ctx.stdout).to.contain('{\n "operationState": "Succeeded",\n "createdTimestamp": "2019-08-06T12:46:03Z",\n "lastActionTimestamp": "2019-08-06T12:46:19Z",\n "resourceLocation": "/knowledgebases/8600c573-2acf-4466-97e8-999ad4cecbc2"')
116+
.command(['qnamaker:create:kb', '--in', `${path.join(__dirname, '../../../fixtures/kb.json')}`, '--save'])
117+
.it('Creates kb and awaits for the creation of it', async () => {
118+
let config = await fs.readJSON(getConfigFile())
119+
expect(config.qnamaker.kbId).to.contain('8600c573-2acf-4466-97e8-999ad4cecbc2')
100120
})
101121
})

0 commit comments

Comments
 (0)