Skip to content

Commit 1bfc436

Browse files
authored
Scope Slack name mapping to summary message (#166)
1 parent bf0730c commit 1bfc436

File tree

9 files changed

+188
-79
lines changed

9 files changed

+188
-79
lines changed

dist/index.js

Lines changed: 30 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/__tests__/getMessageAuthor.test.ts renamed to src/__tests__/getMessageAuthorFactory.test.ts

Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
import * as github from '@actions/github'
22
import {afterEach, beforeEach, describe, expect, it, jest} from '@jest/globals'
33
import {
4-
getMessageAuthor,
4+
GetMessageAuthor,
5+
getMessageAuthorFactory,
56
GH_MERGE_QUEUE_BOT_USERNAME
6-
} from '../getMessageAuthor'
7+
} from '../getMessageAuthorFactory'
78
import {OctokitClient} from '../github/types'
89
import {SlackClient} from '../slack/SlackClient'
910
import {Member, MessageAuthor} from '../slack/types'
1011

11-
describe('getMessageAuthor', () => {
12+
describe('getMessageAuthorFactory', () => {
13+
let getMessageAuthor: GetMessageAuthor
14+
1215
let octokit: OctokitClient
1316
let slack: SlackClient
1417
let messageAuthor: MessageAuthor | null
@@ -55,6 +58,8 @@ describe('getMessageAuthor', () => {
5558
slack = {
5659
getRealUsers: jest.fn()
5760
} as unknown as SlackClient
61+
62+
getMessageAuthor = getMessageAuthorFactory(octokit, slack)
5863
})
5964

6065
afterEach(() => {
@@ -64,7 +69,7 @@ describe('getMessageAuthor', () => {
6469

6570
describe('missing Slack OAuth scope', () => {
6671
beforeEach(async () => {
67-
messageAuthor = await getMessageAuthor(octokit, slack)
72+
messageAuthor = await getMessageAuthor({withSlackUserId: true})
6873
})
6974

7075
it('should fetch Slack users', () => {
@@ -79,6 +84,23 @@ describe('getMessageAuthor', () => {
7984
})
8085
})
8186

87+
describe('without Slack user ID', () => {
88+
beforeEach(async () => {
89+
messageAuthor = await getMessageAuthor({withSlackUserId: false})
90+
})
91+
92+
it('should not fetch Slack users', () => {
93+
expect(slack.getRealUsers).not.toHaveBeenCalled()
94+
})
95+
96+
it('should return GitHub username', () => {
97+
expect(messageAuthor).toStrictEqual({
98+
username: 'namoscato',
99+
icon_url: 'github.com/namoscato'
100+
})
101+
})
102+
})
103+
82104
describe('Slack user not found', () => {
83105
beforeEach(async () => {
84106
jest.mocked(slack.getRealUsers).mockReturnValue(
@@ -103,7 +125,7 @@ describe('getMessageAuthor', () => {
103125

104126
describe('with GitHub context', () => {
105127
beforeEach(async () => {
106-
messageAuthor = await getMessageAuthor(octokit, slack)
128+
messageAuthor = await getMessageAuthor({withSlackUserId: true})
107129
})
108130

109131
it('should fallback to GitHub username', () => {
@@ -115,7 +137,7 @@ describe('getMessageAuthor', () => {
115137
beforeEach(async () => {
116138
github.context.payload = {}
117139

118-
messageAuthor = await getMessageAuthor(octokit, slack)
140+
messageAuthor = await getMessageAuthor({withSlackUserId: true})
119141
})
120142

121143
it('should return null', () => {
@@ -147,7 +169,7 @@ describe('getMessageAuthor', () => {
147169
])
148170
)
149171

150-
messageAuthor = await getMessageAuthor(octokit, slack)
172+
messageAuthor = await getMessageAuthor({withSlackUserId: true})
151173
})
152174

153175
it('should fallback to GitHub username', () => {
@@ -178,7 +200,7 @@ describe('getMessageAuthor', () => {
178200
])
179201
)
180202

181-
messageAuthor = await getMessageAuthor(octokit, slack)
203+
messageAuthor = await getMessageAuthor({withSlackUserId: true})
182204
})
183205

184206
it('should return Slack user', () => {
@@ -224,7 +246,7 @@ describe('getMessageAuthor', () => {
224246
])
225247
)
226248

227-
messageAuthor = await getMessageAuthor(octokit, slack)
249+
messageAuthor = await getMessageAuthor({withSlackUserId: true})
228250
})
229251

230252
it('fetches the GH user that merged the PR', () => {
@@ -258,7 +280,7 @@ describe('getMessageAuthor', () => {
258280
})
259281

260282
it('does not fallback on the merge queue user', async () => {
261-
expect(await getMessageAuthor(octokit, slack)).toStrictEqual({
283+
expect(await getMessageAuthor({withSlackUserId: true})).toStrictEqual({
262284
username: 'mdavis',
263285
icon_url: 'github.com/mdavis'
264286
})
@@ -271,9 +293,9 @@ describe('getMessageAuthor', () => {
271293
})
272294

273295
it('falls back on merge queue user', async () => {
274-
expect((await getMessageAuthor(octokit, slack))?.username).toBe(
275-
GH_MERGE_QUEUE_BOT_USERNAME
276-
)
296+
expect(
297+
(await getMessageAuthor({withSlackUserId: true}))?.username
298+
).toBe(GH_MERGE_QUEUE_BOT_USERNAME)
277299
})
278300
})
279301

@@ -286,7 +308,7 @@ describe('getMessageAuthor', () => {
286308
})
287309

288310
it('does not fallback on the merge queue user', async () => {
289-
expect(await getMessageAuthor(octokit, slack)).toStrictEqual({
311+
expect(await getMessageAuthor({withSlackUserId: true})).toStrictEqual({
290312
username: 'mdavis',
291313
icon_url: 'github.com/mdavis'
292314
})
@@ -303,9 +325,9 @@ describe('getMessageAuthor', () => {
303325
})
304326

305327
it('falls back on merge queue user', async () => {
306-
expect((await getMessageAuthor(octokit, slack))?.username).toBe(
307-
GH_MERGE_QUEUE_BOT_USERNAME
308-
)
328+
expect(
329+
(await getMessageAuthor({withSlackUserId: true}))?.username
330+
).toBe(GH_MERGE_QUEUE_BOT_USERNAME)
309331
})
310332
})
311333
})

0 commit comments

Comments
 (0)