Skip to content

Commit

Permalink
Minor fixes applied & package update
Browse files Browse the repository at this point in the history
  • Loading branch information
mageroni committed Sep 21, 2023
1 parent ad40d17 commit 3c1df55
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 36 deletions.
3 changes: 0 additions & 3 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
50 changes: 25 additions & 25 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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",
Expand All @@ -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);
Expand All @@ -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"
);
Expand All @@ -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,
Expand Down Expand Up @@ -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);
Expand Down
86 changes: 80 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
23 changes: 21 additions & 2 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand All @@ -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,
Expand All @@ -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);
Expand Down

0 comments on commit 3c1df55

Please sign in to comment.