diff --git a/index.js b/index.js index ee62c28..d8ed44b 100644 --- a/index.js +++ b/index.js @@ -171,10 +171,10 @@ export default (app) => { } // find regex [0-9]\+ in issue_body and get first result - let pr_number = issue_body.match(/[0-9]+/)[0]; + let pr_number = issue_body?.match(/#?\s*([0-9]+)/i)?.[1] || ''; // Get answers to first question and find if they contain affirmative answer - let firstQuestionResponse = await getQuestionResponse(1, 2, issue_body); + let firstQuestionResponse = (await getQuestionResponse(1, 2, issue_body)) || []; let isCopilotUsed = firstQuestionResponse.some((response) => { return ( response.includes("Sim") || @@ -345,7 +345,8 @@ export default (app) => { let AnswerSelected = false; let Answers = new Array(); let Expression = end ? new RegExp(start + "\\. (.*" + end + "\\." + ")?", "s") : new RegExp(start + "\\. (.*" + ")?", "s"); - let QuestionOptions = issue_body.match(Expression)[0].match(/\[x\].*/g); + let question = issue_body?.match(Expression); + let QuestionOptions = question ? question[0].match(/\[x\].*/g) : null; if(QuestionOptions){ AnswerSelected = true; QuestionOptions.forEach((option) => { @@ -383,4 +384,4 @@ class AppInsights { this.appIClient.trackException(exception); } } -} \ No newline at end of file +} diff --git a/test/index.test.js b/test/index.test.js index 927b2bd..0538757 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -156,6 +156,17 @@ describe("My Probot app", () => { expect(mock.pendingMocks()).toStrictEqual([]); }); + test("handles edited survey bodies without PR numbers or question markers", async () => { + const payload = structuredClone(payload_issues_edited); + payload.issue.body = "This edited survey has no numbered question markers."; + + sql.Request.mockClear(); + await probot.receive({ name: "issues", payload }); + + const request = sql.Request.mock.results[0].value; + expect(request.input).toHaveBeenCalledWith("value5", ""); + }); + test("closes an issue if a comment is received", async () => { const mock = nock("https://api.github.com") // Test that we correctly return a test token