Skip to content

Commit

Permalink
Fix ms teams message when without map
Browse files Browse the repository at this point in the history
Terminate action if map is in wrong format
  • Loading branch information
DavideViolante committed Feb 10, 2023
1 parent 7bf47d6 commit 738a8b8
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 27 deletions.
27 changes: 15 additions & 12 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: `<at>${user.login}</at>`,
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: `<at>${user.login}</at>`,
mentioned: {
id: github2provider[user.login],
name: user.login,
},
});
}
}
return mentions;
}
Expand All @@ -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: [
Expand Down Expand Up @@ -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);
Expand Down
25 changes: 14 additions & 11 deletions functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: `<at>${user.login}</at>`,
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: `<at>${user.login}</at>`,
mentioned: {
id: github2provider[user.login],
name: user.login,
},
});
}
}
return mentions;
}
Expand All @@ -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: [
Expand Down
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

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

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

0 comments on commit 738a8b8

Please sign in to comment.