Skip to content

Commit

Permalink
fix: testing user notifications (#22)
Browse files Browse the repository at this point in the history
* fix: testing user notifications

* add found slack users to the json

* change the text

* update the expected json

* update expected again
  • Loading branch information
bahmutov authored Oct 3, 2022
1 parent a24d217 commit 4bd94fd
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 13 deletions.
2 changes: 1 addition & 1 deletion cypress.multiple.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ module.exports = defineConfig({
// any recorded run tagged "sanity" should notify #cypress-slack-notify-multiple-sanity channel
registerSlackNotify(
on,
'#cypress-slack-notify-multiple-sanity',
'#cypress-slack-notify-multiple-sanity @gleb.bahmutov',
{
whenRecordingDashboardTag: ['sanity'],
},
Expand Down
20 changes: 13 additions & 7 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,26 +105,29 @@ async function postCypressSlackResult(
debug('run info has no dashboard tags')
}

const foundSlackUsers = []

if (people && people.length) {
if (!usersStore) {
// only try fetching the Slack users once
usersStore = fetchSlackUsers()
usersStore = await fetchSlackUsers()
if (!usersStore) {
debug('could not fetch Slack users')
usersStore = {}
}
}
// https://api.slack.com/reference/surfaces/formatting#mentioning-users
const userIds = people
.map((username) => {
.map((uname) => {
// Slack keeps internal names without '@' symbol
if (username.startsWith('@')) {
username = username.substr(1)
}
const username = uname.startsWith('@') ? uname.slice(1) : uname
const userId = usersStore[username]
if (!userId) {
console.error('Cannot find Slack user id for user "%s"', username)
} else {
foundSlackUsers.push(uname)
}

return userId
})
.filter(Boolean)
Expand All @@ -147,11 +150,14 @@ async function postCypressSlackResult(
spec.relative,
channel,
)
return { channel, people, sent: true, ...runInfo }
if (foundSlackUsers.length) {
console.log('notifying %s', foundSlackUsers.join(', '))
}
return { channel, people, foundSlackUsers, sent: true, ...runInfo }
} else {
console.error('could not post the test results to "%s"', channel)
console.error(result)
return { channel, people, sent: false, ...runInfo }
return { channel, people, foundSlackUsers, sent: false, ...runInfo }
}
} else {
console.error('no need to notify')
Expand Down
6 changes: 4 additions & 2 deletions src/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,17 @@ async function findSlackUsers(usernames) {
return
}

const ids = usernames.map((uname) => {
const normalizedUsernames = usernames.map((s) => s.trim()).filter(Boolean)

const ids = normalizedUsernames.map((uname) => {
// the username should not include "@"
const username = uname.startsWith('@') ? uname.slice(1) : uname

const id = users[username]
debug('Slack user "%s" id %s', username, id)

if (id) {
console.log('found Slack %s user ID:', uname, id)
console.log('found Slack user %s ID:', uname, id)
} else {
console.error('could not find Slack user', username)
}
Expand Down
3 changes: 3 additions & 0 deletions test-logs/ci.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,21 @@ const expectedLog = [
{
channel: '#cypress-slack-notify',
people: ['@gleb.bahmutov'],
foundSlackUsers: ['@gleb.bahmutov'],
sent: true,
runDashboardTags: ['notify'],
},
{
channel: '#cypress-slack-notify',
people: [],
foundSlackUsers: [],
sent: true,
runDashboardTags: ['notify'],
},
{
channel: '#cypress-slack-notify-minimatch',
people: [],
foundSlackUsers: [],
sent: true,
runDashboardTags: ['notify'],
},
Expand Down
9 changes: 6 additions & 3 deletions test-logs/ci.multiple.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,22 @@ const jsonLog = JSON.parse(ciJsonLog)
const expectedLog = [
{
channel: '#cypress-slack-notify-multiple-sanity',
people: [],
people: ['@gleb.bahmutov'],
foundSlackUsers: ['@gleb.bahmutov'],
sent: true,
runDashboardTags: ['sanity'],
},
{
channel: '#cypress-slack-notify-multiple-sanity',
people: [],
people: ['@gleb.bahmutov'],
foundSlackUsers: ['@gleb.bahmutov'],
sent: true,
runDashboardTags: ['sanity'],
},
{
channel: '#cypress-slack-notify-multiple-sanity',
people: [],
people: ['@gleb.bahmutov'],
foundSlackUsers: ['@gleb.bahmutov'],
sent: true,
runDashboardTags: ['sanity'],
},
Expand Down

0 comments on commit 4bd94fd

Please sign in to comment.