From 3c1df55bdc2f28c5717089a0ab33d3d6fd60b72d Mon Sep 17 00:00:00 2001 From: Mabel Geronimo <107436170+mageroni@users.noreply.github.com> Date: Thu, 21 Sep 2023 00:10:03 -0500 Subject: [PATCH] Minor fixes applied & package update --- .env.example | 3 -- index.js | 50 +++++++++++++-------------- package-lock.json | 86 ++++++++++++++++++++++++++++++++++++++++++---- package.json | 3 ++ test/index.test.js | 23 +++++++++++-- 5 files changed, 129 insertions(+), 36 deletions(-) diff --git a/.env.example b/.env.example index 4348de4..3eab4bc 100644 --- a/.env.example +++ b/.env.example @@ -6,9 +6,6 @@ WEBHOOK_SECRET=development # Use `trace` to get verbose logging or `info` to show less LOG_LEVEL=debug -# Go to https://smee.io/new set this to the URL that you are redirected to. -WEBHOOK_PROXY_URL= - LANGUAGE_API_ENDPOINT= LANGUAGE_API_KEY= APPLICATIONINSIGHTS_CONNECTION_STRING= diff --git a/index.js b/index.js index f3cff1f..77c4436 100644 --- a/index.js +++ b/index.js @@ -29,7 +29,9 @@ module.exports = (app) => { }); let pr_number = context.payload.pull_request.number; let pr_body = context.payload.pull_request.body; - let result = [{ primaryLanguage: { iso6391Name: "en" } }]; + let detectedLanguage = "en"; + let pr_author = context.payload.pull_request.user.login; + let organization_name = context.payload.repository.owner.login // check language for pr_body if (LANGUAGE_API_ENDPOINT && LANGUAGE_API_KEY) { @@ -40,7 +42,7 @@ module.exports = (app) => { if (pr_body) { try { let startTime = Date.now(); - result = await TAclient.analyze("LanguageDetection", [pr_body]); + let result = await TAclient.analyze("LanguageDetection", [pr_body]); let duration = Date.now() - startTime; appInsights.trackDependency({ target: "API:Language Detection", @@ -50,12 +52,10 @@ module.exports = (app) => { success: true, dependencyTypeName: "HTTP", }); - if ( - !["en", "es", "pt", "fr"].includes( - result[0].primaryLanguage.iso6391Name - ) - ) { - result[0].primaryLanguage.iso6391Name = "en"; + if (result.length > 0 && !result[0].error && ["en", "es", "pt", "fr"].includes(result[0].primaryLanguage.iso6391Name) ) { + detectedLanguage = result[0].primaryLanguage.iso6391Name; + }else { + detectedLanguage = "en"; } } catch (err) { app.log.error(err); @@ -67,7 +67,7 @@ module.exports = (app) => { // read file that aligns with detected language const issue_body = fs.readFileSync( "./issue_template/copilot-usage-" + - result[0].primaryLanguage.iso6391Name + + detectedLanguage + ".md", "utf-8" ); @@ -79,11 +79,11 @@ module.exports = (app) => { // display the body for the issue app.log.info(fileContent); - - // create an issue using fileContent as body + + // create an issue using fileContent as body if pr_author is included in copilotSeats try { await context.octokit.issues.create({ - owner: context.payload.repository.owner.login, + owner: organization_name, repo: context.payload.repository.name, title: "Copilot Usage - PR#" + pr_number.toString(), body: fileContent, @@ -402,19 +402,19 @@ module.exports = (app) => { completed_at ) VALUES ( - ${enterprise_name}, - ${organization_name}, - ${context.payload.repository.name}, - ${issue_id}, - ${context.payload.issue.number}, - ${pr_number}, - ${assignee_name}, - ${isCopilotUsed}, - ${pctValue}, - ${freqValue}, - ${comment}, - ${context.payload.issue.created_at}, - ${context.payload.issue.updated_at} + '${enterprise_name}', + '${organization_name}', + '${context.payload.repository.name}', + ${issue_id}, + ${context.payload.issue.number}, + ${pr_number}, + '${assignee_name}', + '${isCopilotUsed}', + '${pctValue}', + '${freqValue}', + '${comment}', + '${context.payload.issue.created_at}', + '${context.payload.issue.updated_at}' )`; let insert_result = await sql.query(insert_query); app.log.info(insert_result); diff --git a/package-lock.json b/package-lock.json index b85b61a..cc3c77c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10,8 +10,11 @@ "license": "ISC", "dependencies": { "@azure/ai-language-text": "^1.1.0", - "applicationinsights": "^2.7.0", + "@azure/ai-text-analytics": "^5.1.0", + "ai-text-analytics": "^0.0.1-security", + "applicationinsights": "^2.7.3", "dedent": "^1.5.1", + "dotenv": "^16.3.1", "mssql": "^9.2.0", "probot": "^12.3.1" }, @@ -77,6 +80,51 @@ "resolved": "https://registry.npmjs.org/tslib/-/tslib-2.5.0.tgz", "integrity": "sha512-336iVw3rtn2BUK7ORdIAHTyxHGRIHVReokCR3XjbckJMK7ms8FysBfhLR8IXnAgy7T0PTPNBWKiH514FOW/WSg==" }, + "node_modules/@azure/ai-text-analytics": { + "version": "5.1.0", + "resolved": "https://registry.npmjs.org/@azure/ai-text-analytics/-/ai-text-analytics-5.1.0.tgz", + "integrity": "sha512-vkAFCxj0dn7kjTDuzpqM4EgQJkn3V0N/KJ0/n+UwralpeCESWll3DLuf8h2kL94vT9pyHWq7xWiNMBrzXxF1yA==", + "dependencies": { + "@azure/abort-controller": "^1.0.0", + "@azure/core-asynciterator-polyfill": "^1.0.0", + "@azure/core-auth": "^1.3.0", + "@azure/core-client": "^1.0.0", + "@azure/core-lro": "^2.0.0", + "@azure/core-paging": "^1.1.1", + "@azure/core-rest-pipeline": "^1.1.0", + "@azure/core-tracing": "1.0.0-preview.12", + "@azure/logger": "^1.0.0", + "tslib": "^2.2.0" + }, + "engines": { + "node": ">=12.0.0" + } + }, + "node_modules/@azure/ai-text-analytics/node_modules/@azure/core-tracing": { + "version": "1.0.0-preview.12", + "resolved": "https://registry.npmjs.org/@azure/core-tracing/-/core-tracing-1.0.0-preview.12.tgz", + "integrity": "sha512-nvo2Wc4EKZGN6eFu9n3U7OXmASmL8VxoPIH7xaD6OlQqi44bouF0YIi9ID5rEsKLiAU59IYx6M297nqWVMWPDg==", + "dependencies": { + "@opentelemetry/api": "^1.0.0", + "tslib": "^2.2.0" + }, + "engines": { + "node": ">=12.0.0" + } + }, + "node_modules/@azure/ai-text-analytics/node_modules/tslib": { + "version": "2.6.2", + "resolved": "https://registry.npmjs.org/tslib/-/tslib-2.6.2.tgz", + "integrity": "sha512-AEYxH93jGFPn/a2iVAwW87VuUIkR1FVUKB77NwMF7nBTDkDrrT/Hpt/IrCJ0QXhW27jTBDcf5ZY7w6RiqTMw2Q==" + }, + "node_modules/@azure/core-asynciterator-polyfill": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/@azure/core-asynciterator-polyfill/-/core-asynciterator-polyfill-1.0.2.tgz", + "integrity": "sha512-3rkP4LnnlWawl0LZptJOdXNrT/fHp2eQMadoasa6afspXdpGrtPZuAQc2PD0cpgyuoXtUWyC3tv7xfntjGS5Dw==", + "engines": { + "node": ">=12.0.0" + } + }, "node_modules/@azure/core-auth": { "version": "1.5.0", "resolved": "https://registry.npmjs.org/@azure/core-auth/-/core-auth-1.5.0.tgz", @@ -387,6 +435,7 @@ "version": "7.22.13", "resolved": "https://registry.npmjs.org/@babel/code-frame/-/code-frame-7.22.13.tgz", "integrity": "sha512-XktuhWlJ5g+3TJXc5upd9Ks1HutSArik6jf2eAjYFyIOf4ej3RN+184cZbzDvbPnuTJIUhPKKJE3cIsYTiAT3w==", + "devOptional": true, "dependencies": { "@babel/highlight": "^7.22.13", "chalk": "^2.4.2" @@ -399,6 +448,7 @@ "version": "3.2.1", "resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-3.2.1.tgz", "integrity": "sha512-VT0ZI6kZRdTh8YyJw3SMbYm/u+NqfsAxEpWO0Pf9sq8/e94WxxOpPKx9FR1FlyCtOVDNOQ+8ntlqFxiRc+r5qA==", + "devOptional": true, "dependencies": { "color-convert": "^1.9.0" }, @@ -410,6 +460,7 @@ "version": "2.4.2", "resolved": "https://registry.npmjs.org/chalk/-/chalk-2.4.2.tgz", "integrity": "sha512-Mti+f9lpJNcwF4tWV8/OrTTtF1gZi+f8FqlyAdouralcFWFQWF2+NgCHShjkCb+IFBLq9buZwE1xckQU4peSuQ==", + "devOptional": true, "dependencies": { "ansi-styles": "^3.2.1", "escape-string-regexp": "^1.0.5", @@ -423,6 +474,7 @@ "version": "1.9.3", "resolved": "https://registry.npmjs.org/color-convert/-/color-convert-1.9.3.tgz", "integrity": "sha512-QfAUtd+vFdAtFQcC8CCyYt1fYWxSqAiK2cSD6zDB8N3cpsEBAvRxp9zOGg6G/SHHJYAT88/az/IuDGALsNVbGg==", + "devOptional": true, "dependencies": { "color-name": "1.1.3" } @@ -430,12 +482,14 @@ "node_modules/@babel/code-frame/node_modules/color-name": { "version": "1.1.3", "resolved": "https://registry.npmjs.org/color-name/-/color-name-1.1.3.tgz", - "integrity": "sha512-72fSenhMw2HZMTVHeCA9KCmpEIbzWiQsjN+BHcBbS9vr1mtt+vJjPdksIBNUmKAW8TFUDPJK5SUU3QhE9NEXDw==" + "integrity": "sha512-72fSenhMw2HZMTVHeCA9KCmpEIbzWiQsjN+BHcBbS9vr1mtt+vJjPdksIBNUmKAW8TFUDPJK5SUU3QhE9NEXDw==", + "devOptional": true }, "node_modules/@babel/code-frame/node_modules/escape-string-regexp": { "version": "1.0.5", "resolved": "https://registry.npmjs.org/escape-string-regexp/-/escape-string-regexp-1.0.5.tgz", "integrity": "sha512-vbRorB5FUQWvla16U8R/qgaFIya2qGzwDrNmCZuYKrbdSUMG6I1ZCGQRefkRVhuOkIGVne7BQ35DSfo1qvJqFg==", + "devOptional": true, "engines": { "node": ">=0.8.0" } @@ -444,6 +498,7 @@ "version": "3.0.0", "resolved": "https://registry.npmjs.org/has-flag/-/has-flag-3.0.0.tgz", "integrity": "sha512-sKJf1+ceQBr4SMkvQnBDNDtf4TXpVhVGateu0t918bl30FnbE2m4vNLX+VWe/dpjlb+HugGYzW7uQXH98HPEYw==", + "devOptional": true, "engines": { "node": ">=4" } @@ -452,6 +507,7 @@ "version": "5.5.0", "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-5.5.0.tgz", "integrity": "sha512-QjVjwdXIt408MIiAqCX4oUKsgU2EqAGzs2Ppkm4aQYbjm+ZEWEcW4SfFNTr4uMNZma0ey4f5lgLrkB0aX0QMow==", + "devOptional": true, "dependencies": { "has-flag": "^3.0.0" }, @@ -664,6 +720,7 @@ "version": "7.22.15", "resolved": "https://registry.npmjs.org/@babel/helper-validator-identifier/-/helper-validator-identifier-7.22.15.tgz", "integrity": "sha512-4E/F9IIEi8WR94324mbDUMo074YTheJmd7eZF5vITTeYchqAi6sYXRLHUVsmkdmY4QjfKTcB2jB7dVP3NaBElQ==", + "devOptional": true, "engines": { "node": ">=6.9.0" } @@ -695,6 +752,7 @@ "version": "7.22.13", "resolved": "https://registry.npmjs.org/@babel/highlight/-/highlight-7.22.13.tgz", "integrity": "sha512-C/BaXcnnvBCmHTpz/VGZ8jgtE2aYlW4hxDhseJAWZb7gqGM/qtCK6iZUb0TyKFf7BOUsBH7Q7fkRsDRhg1XklQ==", + "devOptional": true, "dependencies": { "@babel/helper-validator-identifier": "^7.22.5", "chalk": "^2.4.2", @@ -2660,6 +2718,11 @@ "node": ">=8" } }, + "node_modules/ai-text-analytics": { + "version": "0.0.1-security", + "resolved": "https://registry.npmjs.org/ai-text-analytics/-/ai-text-analytics-0.0.1-security.tgz", + "integrity": "sha512-2OrOTH/Gg9YnQekpDRdbLzOCHOjdLmNt9BZq3m1yFCC2sLvuBsdTRZOr8CGB5M+GvPiEBptd310X1YuI7Nda7w==" + }, "node_modules/ansi-escapes": { "version": "4.3.2", "resolved": "https://registry.npmjs.org/ansi-escapes/-/ansi-escapes-4.3.2.tgz", @@ -3739,11 +3802,14 @@ } }, "node_modules/dotenv": { - "version": "8.6.0", - "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-8.6.0.tgz", - "integrity": "sha512-IrPdXQsk2BbzvCBGBOTmmSH5SodmqZNt4ERAZDmW4CT+tL8VtvinqywuANaFu4bOMWki16nqf0e4oC0QIaDr/g==", + "version": "16.3.1", + "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-16.3.1.tgz", + "integrity": "sha512-IPzF4w4/Rd94bA9imS68tZBaYyBWSCE47V1RGuMrB94iyTOIEwRmVL2x/4An+6mETpLrKJ5hQkB8W4kFAadeIQ==", "engines": { - "node": ">=10" + "node": ">=12" + }, + "funding": { + "url": "https://github.com/motdotla/dotenv?sponsor=1" } }, "node_modules/ecdsa-sig-formatter": { @@ -6769,6 +6835,14 @@ "node": ">=10.21" } }, + "node_modules/probot/node_modules/dotenv": { + "version": "8.6.0", + "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-8.6.0.tgz", + "integrity": "sha512-IrPdXQsk2BbzvCBGBOTmmSH5SodmqZNt4ERAZDmW4CT+tL8VtvinqywuANaFu4bOMWki16nqf0e4oC0QIaDr/g==", + "engines": { + "node": ">=10" + } + }, "node_modules/probot/node_modules/lru-cache": { "version": "6.0.0", "resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-6.0.0.tgz", diff --git a/package.json b/package.json index a523b1e..4e46616 100644 --- a/package.json +++ b/package.json @@ -17,8 +17,11 @@ }, "dependencies": { "@azure/ai-language-text": "^1.1.0", + "@azure/ai-text-analytics": "^5.1.0", + "ai-text-analytics": "^0.0.1-security", "applicationinsights": "^2.7.3", "dedent": "^1.5.1", + "dotenv": "^16.3.1", "mssql": "^9.2.0", "probot": "^12.3.1" }, diff --git a/test/index.test.js b/test/index.test.js index 13906d2..66db4dc 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -8,6 +8,7 @@ const payload_issues_edited = require("./fixtures/issues.edited.json"); const issue_comment_created = require("./fixtures/issue_comment.created.json"); const fs = require("fs"); const path = require("path"); +const LANGUAGE_API_ENDPOINT = process.env.LANGUAGE_API_ENDPOINT; const issue_body = fs.readFileSync( path.join(__dirname, "fixtures/issue_body.md"), @@ -29,7 +30,25 @@ describe("My Probot app", () => { beforeEach(() => { nock.disableNetConnect(); - nock.enableNetConnect('ghazlanguage.cognitiveservices.azure.com'); + nock.enableNetConnect(LANGUAGE_API_ENDPOINT); + nock(LANGUAGE_API_ENDPOINT) + .post('/language/:analyze-text?api-version=2023-04-01') + .reply(200, { + "kind": "LanguageDetectionResults", + "results": { + "documents": [{ + "id": "1", + "detectedLanguage": { + "name": "English", + "iso6391Name": "en", + "confidenceScore": 1.0 + }, + "warnings": [] + }], + "errors": [], + "modelVersion": "2022-10-01" + } + }); probot = new Probot({ appId: 123, privateKey, @@ -53,7 +72,7 @@ describe("My Probot app", () => { issues: "write", }, }) - + // Test that a issue is created .post("/repos/mageroni/TestRepo/issues", (body) => { expect(body).toMatchObject(expected_issue);