diff --git a/dist/index.js b/dist/index.js index d780839..36129f1 100644 --- a/dist/index.js +++ b/dist/index.js @@ -129,15 +129,18 @@ function prettyMessage(pr2user, github2provider, provider) { */ function getTeamsMentions(github2provider, pr2user) { const mentions = []; - for (const user of pr2user) { - mentions.push({ - type: `mention`, - text: `${user.login}`, - mentioned: { - id: github2provider[user.login], - name: user.login, - }, - }); + // Add mentions array only if the map is provided, or no notification is sent + if (Object.keys(github2provider).length > 0) { + for (const user of pr2user) { + mentions.push({ + type: `mention`, + text: `${user.login}`, + mentioned: { + id: github2provider[user.login], + name: user.login, + }, + }); + } } return mentions; } @@ -161,10 +164,10 @@ function formatSlackMessage(channel, message) { * Format the MS Teams message request object * Docs: https://bit.ly/3UlOoqo * @param {String} message formatted message string - * @param {Array} mentionsArray teams mention objects + * @param {Array} [mentionsArray] teams mention objects array * @return {Object} Ms Teams message data object */ -function formatTeamsMessage(message, mentionsArray) { +function formatTeamsMessage(message, mentionsArray = []) { const messageData = { type: `message`, attachments: [ @@ -10756,7 +10759,7 @@ async function main() { if (pullRequestsWithoutLabel.length) { const pr2user = createPr2UserArray(pullRequestsWithoutLabel); if (github2providerString && !checkGithubProviderFormat(github2providerString)) { - core.setFailed(`The github-provider-map string is not in correct format: "name1:id1,name2:id2,..."`); + return core.setFailed(`The github-provider-map string is not in correct format: "name1:id1,name2:id2,..."`); } const github2provider = stringToObject(github2providerString); const messageText = prettyMessage(pr2user, github2provider, provider); diff --git a/functions.js b/functions.js index 53cbc6f..8d09a73 100644 --- a/functions.js +++ b/functions.js @@ -123,15 +123,18 @@ function prettyMessage(pr2user, github2provider, provider) { */ function getTeamsMentions(github2provider, pr2user) { const mentions = []; - for (const user of pr2user) { - mentions.push({ - type: `mention`, - text: `${user.login}`, - mentioned: { - id: github2provider[user.login], - name: user.login, - }, - }); + // Add mentions array only if the map is provided, or no notification is sent + if (Object.keys(github2provider).length > 0) { + for (const user of pr2user) { + mentions.push({ + type: `mention`, + text: `${user.login}`, + mentioned: { + id: github2provider[user.login], + name: user.login, + }, + }); + } } return mentions; } @@ -155,10 +158,10 @@ function formatSlackMessage(channel, message) { * Format the MS Teams message request object * Docs: https://bit.ly/3UlOoqo * @param {String} message formatted message string - * @param {Array} mentionsArray teams mention objects + * @param {Array} [mentionsArray] teams mention objects array * @return {Object} Ms Teams message data object */ -function formatTeamsMessage(message, mentionsArray) { +function formatTeamsMessage(message, mentionsArray = []) { const messageData = { type: `message`, attachments: [ diff --git a/index.js b/index.js index 2379df6..7421ecf 100644 --- a/index.js +++ b/index.js @@ -67,7 +67,7 @@ async function main() { if (pullRequestsWithoutLabel.length) { const pr2user = createPr2UserArray(pullRequestsWithoutLabel); if (github2providerString && !checkGithubProviderFormat(github2providerString)) { - core.setFailed(`The github-provider-map string is not in correct format: "name1:id1,name2:id2,..."`); + return core.setFailed(`The github-provider-map string is not in correct format: "name1:id1,name2:id2,..."`); } const github2provider = stringToObject(github2providerString); const messageText = prettyMessage(pr2user, github2provider, provider); diff --git a/package-lock.json b/package-lock.json index a43ea91..af81a13 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "pr-reviews-reminder-action", - "version": "2.4.1", + "version": "2.5.0", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "pr-reviews-reminder-action", - "version": "2.4.1", + "version": "2.5.0", "license": "MIT", "dependencies": { "@actions/core": "^1.10.0", diff --git a/package.json b/package.json index 823af79..23b2970 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "pr-reviews-reminder-action", - "version": "2.4.1", + "version": "2.5.0", "description": "Reminder for Pull Request pending reviews", "scripts": { "build": "ncc build index.js", diff --git a/test/test.js b/test/test.js index 4438b58..f8a4b67 100644 --- a/test/test.js +++ b/test/test.js @@ -187,6 +187,31 @@ const mockTeamsMessageRequest = { ], }; +const mockTeamsMessageRequestNoMentions = { + type: `message`, + attachments: [ + { + contentType: `application/vnd.microsoft.card.adaptive`, + content: { + type: `AdaptiveCard`, + body: [ + { + type: `TextBlock`, + text: 'Hey @User1, the PR "Title1" is waiting for your review: [https://example.com/1](https://example.com/1)', + wrap: true, + }, + ], + $schema: `http://adaptivecards.io/schemas/adaptive-card.json`, + version: `1.0`, + msteams: { + width: 'Full', + entities: [], + }, + }, + }, + ], +}; + describe('Pull Request Reviews Reminder Action tests', () => { it('Should get pull requests with requested reviewers (some reviewers)', () => { const pullRequests = getPullRequestsToReview(mockPullRequests); @@ -372,6 +397,11 @@ describe('Pull Request Reviews Reminder Action tests', () => { assert.deepEqual(mentions, mockTeamsMentions); }); + it('Should create empty mentions array, Teams', () => { + const mentions = getTeamsMentions({}, mockPr2User); + assert.deepEqual(mentions, []); + }); + it('Should format a Slack message to send the request', () => { const channel = '#developers'; const message = 'Hey @User1, the PR "Title1" is waiting for your review: https://example.com/1'; @@ -389,4 +419,10 @@ describe('Pull Request Reviews Reminder Action tests', () => { const teamsMessageRequest = formatTeamsMessage(message, mockTeamsMentions); assert.deepEqual(teamsMessageRequest, mockTeamsMessageRequest); }); + + it('Should format a Teams message to send the request (no mentions array)', () => { + const message = `Hey @User1, the PR "Title1" is waiting for your review: [https://example.com/1](https://example.com/1)`; + const teamsMessageRequest = formatTeamsMessage(message); + assert.deepEqual(teamsMessageRequest, mockTeamsMessageRequestNoMentions); + }); });