Skip to content

Commit

Permalink
fix: group mentions without handle (#583)
Browse files Browse the repository at this point in the history
* test: update group tests to check regex; add test for group short syntax

* fix: update group regex to capture without optional handle
  • Loading branch information
Pactionly authored Aug 21, 2024
1 parent e3ec9be commit a1035ea
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 17 deletions.
12 changes: 3 additions & 9 deletions service/recognition.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const {
} = config;

const userRegex = /<@([a-zA-Z0-9]+)>/g;
const groupRegex = /<!subteam\^([a-zA-Z0-9]+)\|@([a-zA-Z0-9]+)>/g;
const groupRegex = /<!subteam\^([a-zA-Z0-9]+)(\|@([a-zA-Z0-9\-_]+))?>/g;
const tagRegex = /#(\S+)/g;
const generalEmojiRegex = /:([a-z-_']+):/g;
const gratitudeEmojiRegex = new RegExp(config.recognizeEmoji, "g");
Expand Down Expand Up @@ -171,14 +171,8 @@ async function gratitudeReceiverIdsIn(client, text) {
let users = (text.match(userRegex) || []).map((userMention) =>
userMention.slice(2, -1),
);
let groups = (text.match(groupRegex) || []).map((groupMention) =>
groupMention.substring(
groupMention.indexOf("^") + 1,
groupMention.lastIndexOf("|"),
),
);
for (let i = 0; i < groups.length; i++) {
users = users.concat(await groupUsers(client, groups[i]));
for (const groupMatch of text.matchAll(groupRegex)) {
users = users.concat(await groupUsers(client, groupMatch[1]));
}
return users;
}
Expand Down
72 changes: 64 additions & 8 deletions test/service/recognition.js
Original file line number Diff line number Diff line change
Expand Up @@ -387,13 +387,36 @@ describe("service/recognition", () => {
it("should return the users within the group when a group is mentioned", async () => {
const group = "<!subteam^S1234567890|@TestGroupOne>";
const text = ":fistbump: " + group + " Test Message";
const listStub = sinon.stub();
listStub.resolves({});
listStub.withArgs({ usergroup: "S1234567890" }).resolves({
ok: true,
users: ["TestUserOne", "TestUserTwo"],
});
const client = {
usergroups: {
users: {
list: sinon.stub().resolves({
ok: true,
users: ["TestUserOne", "TestUserTwo"],
}),
list: listStub,
},
},
};
const results = await recognition.gratitudeReceiverIdsIn(client, text);
expect(results).to.deep.equal(["TestUserOne", "TestUserTwo"]);
});

it("should return the users within the group when a the short group syntax is used", async () => {
const group = "<!subteam^S1234567890>";
const text = ":fistbump: " + group + " Test Message";
const listStub = sinon.stub();
listStub.resolves({});
listStub.withArgs({ usergroup: "S1234567890" }).resolves({
ok: true,
users: ["TestUserOne", "TestUserTwo"],
});
const client = {
usergroups: {
users: {
list: listStub,
},
},
};
Expand All @@ -404,13 +427,16 @@ describe("service/recognition", () => {
it("should return the users within the group when a group is mentioned and other users", async () => {
const group = "<!subteam^S1234567890|@TestGroupOne>";
const text = ":fistbump: " + group + " <@TestUserThree> Test Message";
const listStub = sinon.stub();
listStub.resolves({});
listStub.withArgs({ usergroup: "S1234567890" }).resolves({
ok: true,
users: ["TestUserOne", "TestUserTwo"],
});
const client = {
usergroups: {
users: {
list: sinon.stub().resolves({
ok: true,
users: ["TestUserOne", "TestUserTwo"],
}),
list: listStub,
},
},
};
Expand All @@ -421,6 +447,36 @@ describe("service/recognition", () => {
"TestUserTwo",
]);
});

it("should return the users within all groups mentioned", async () => {
const group1 = "<!subteam^S111111111|@TestGroupOne>";
const group2 = "<!subteam^S222222222>";
const text = ":fistbump: " + group1 + " " + group2 + " Test Message";
const listStub = sinon.stub();
listStub.resolves({});
listStub.withArgs({ usergroup: "S111111111" }).resolves({
ok: true,
users: ["TestUserOne", "TestUserTwo"],
});
listStub.withArgs({ usergroup: "S222222222" }).resolves({
ok: true,
users: ["TestUserThree", "TestUserFour"],
});
const client = {
usergroups: {
users: {
list: listStub,
},
},
};
const results = await recognition.gratitudeReceiverIdsIn(client, text);
expect(results).to.deep.equal([
"TestUserOne",
"TestUserTwo",
"TestUserThree",
"TestUserFour",
]);
});
});

describe("gratitudeCountIn", () => {
Expand Down

0 comments on commit a1035ea

Please sign in to comment.