diff --git a/ModerateIssue/get-luis-intent.js b/ModerateIssue/get-luis-intent.js index 5fb0f54..ae1aa5b 100644 --- a/ModerateIssue/get-luis-intent.js +++ b/ModerateIssue/get-luis-intent.js @@ -1,10 +1,10 @@ const luisAppId = process.env.LUIS_APP_ID; const endpointKey = process.env.LUIS_ENDPOINT_KEY; -const request = require('request'); -const querystring = require('querystring'); - -module.exports = function getLuisIntent(utterance) { +module.exports = function (utterance) { + const request = require('request'); + const querystring = require('querystring'); + const luisEndpoint = "https://westus.api.cognitive.microsoft.com/luis/v2.0/apps/"; const queryParams = { diff --git a/ModerateIssue/index.js b/ModerateIssue/index.js index ed92ae4..5bdc5d8 100644 --- a/ModerateIssue/index.js +++ b/ModerateIssue/index.js @@ -1,10 +1,11 @@ const octokit = require("@octokit/rest")(); const authenticate = require('./authenticate'); +const getLuisIntent = require('./get-luis-intent'); module.exports = async function (context, data) { const { body } = data const { action, repository, issue, installation } = body; - const { number } = issue; + const { number, title } = issue; const repo = repository.name; const owner = repository.owner.login; const installationId = installation.id; @@ -13,11 +14,12 @@ module.exports = async function (context, data) { let response = ""; if (action === "opened") { await authenticate(octokit, installationId) - response = await octokit.issues.createComment({ + const label = await getLuisIntent(title); + response = await octokit.issues.addLabels({ owner, repo, number, - body: "Thanks for submitting this issue. We will take a look at it later!" + labels: [label] }) } context.res = { diff --git a/test/moderateIssueTest.js b/test/moderateIssueTest.js index 3b6d0f1..95d87da 100644 --- a/test/moderateIssueTest.js +++ b/test/moderateIssueTest.js @@ -2,32 +2,36 @@ var sinon = require("sinon"); var proxyquire = require("proxyquire"); var issueOpenedPayload = require("./fixtures/issue-opened.json"); -describe("ModerateIssue", function() { - var moderateIssue; - before(function() { - createCommentStub = sinon.spy(); - addLabelsStub = sinon.spy(); +describe("ModerateIssue", () => { + var moderateIssue + var defaultLabel; + + before(() => { + createCommentStub = sinon.spy() + addLabelsStub = sinon.spy() + octoKitStubs = { issues: { - addLabelsStub: addLabelsStub, + addLabels: addLabelsStub, createComment: createCommentStub } - }; + } + + defaultLabel = 'question' moderateIssue = proxyquire("../ModerateIssue/index", { - "@octokit/rest": () => octoKitStubs, - "./authenticate": () => true, - "./get-luis-intent": () => "question" + '@octokit/rest': () => octoKitStubs, + './authenticate': () => true, + './get-luis-intent': () => new Promise(resolve => resolve(defaultLabel)) }); }); it("should create comment when issue is opened", async () => { await moderateIssue({}, { body: issueOpenedPayload }); - sinon.assert.calledWith(createCommentStub, { - body: - "Thanks for submitting this issue. We will take a look at it later!", + sinon.assert.calledWith(addLabelsStub, { + labels: [defaultLabel], number: issueOpenedPayload.issue.number, owner: issueOpenedPayload.repository.owner.login, repo: issueOpenedPayload.repository.name