Skip to content

Commit 331668c

Browse files
committed
feat: image_72 fallbacks
1 parent 62e3406 commit 331668c

File tree

14 files changed

+207
-70
lines changed

14 files changed

+207
-70
lines changed

dist/index.js

Lines changed: 52 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -80746,10 +80746,10 @@ __nccwpck_require__.a(module, async (__webpack_handle_async_dependencies__, __we
8074680746
/* harmony import */ var _krauters_utils__WEBPACK_IMPORTED_MODULE_2___default = /*#__PURE__*/__nccwpck_require__.n(_krauters_utils__WEBPACK_IMPORTED_MODULE_2__);
8074780747
/* harmony import */ var _package_json__WEBPACK_IMPORTED_MODULE_3__ = __nccwpck_require__(8330);
8074880748
/* harmony import */ var _constants_js__WEBPACK_IMPORTED_MODULE_4__ = __nccwpck_require__(27242);
80749-
/* harmony import */ var _utils_github_github_client_js__WEBPACK_IMPORTED_MODULE_5__ = __nccwpck_require__(32031);
80749+
/* harmony import */ var _utils_github_client_js__WEBPACK_IMPORTED_MODULE_5__ = __nccwpck_require__(10598);
8075080750
/* harmony import */ var _utils_github_structures_js__WEBPACK_IMPORTED_MODULE_6__ = __nccwpck_require__(28315);
8075180751
/* harmony import */ var _utils_slack_blocks_js__WEBPACK_IMPORTED_MODULE_7__ = __nccwpck_require__(96114);
80752-
/* harmony import */ var _utils_slack_slack_client_js__WEBPACK_IMPORTED_MODULE_8__ = __nccwpck_require__(70741);
80752+
/* harmony import */ var _utils_slack_client_js__WEBPACK_IMPORTED_MODULE_8__ = __nccwpck_require__(90294);
8075380753
/* harmony import */ var _utils_test_data_js__WEBPACK_IMPORTED_MODULE_9__ = __nccwpck_require__(23930);
8075480754
/* harmony import */ var _input_parser_js__WEBPACK_IMPORTED_MODULE_10__ = __nccwpck_require__(40935);
8075580755
// https://github.com/actions/github-script
@@ -80772,12 +80772,12 @@ async function main() {
8077280772
try {
8077380773
(0,_actions_core__WEBPACK_IMPORTED_MODULE_1__.debug)('Starting main...');
8077480774
const { githubConfig, repositoryFilter, slackConfig, withArchived, withDrafts, withPublic, withTestData, withUserMentions, } = (0,_input_parser_js__WEBPACK_IMPORTED_MODULE_10__/* .parseInputs */ .T)();
80775-
const slack = new _utils_slack_slack_client_js__WEBPACK_IMPORTED_MODULE_8__/* .SlackClient */ .Q(slackConfig);
80775+
const slack = new _utils_slack_client_js__WEBPACK_IMPORTED_MODULE_8__/* .SlackClient */ .Q(slackConfig);
8077680776
const results = await githubConfig.tokens.reduce(async (accPromise, token) => {
8077780777
const acc = await accPromise;
8077880778
try {
8077980779
// TODO - Consider making this thread safe so requests can be made in parallel
80780-
const client = new _utils_github_github_client_js__WEBPACK_IMPORTED_MODULE_5__/* .GitHubClient */ .j({
80780+
const client = new _utils_github_client_js__WEBPACK_IMPORTED_MODULE_5__/* .GitHubClient */ .j({
8078180781
options: githubConfig.options,
8078280782
token,
8078380783
});
@@ -80933,7 +80933,7 @@ function parseInputs() {
8093380933

8093480934
/***/ }),
8093580935

80936-
/***/ 32031:
80936+
/***/ 10598:
8093780937
/***/ ((__unused_webpack_module, __webpack_exports__, __nccwpck_require__) => {
8093880938

8093980939
/* harmony export */ __nccwpck_require__.d(__webpack_exports__, {
@@ -81376,7 +81376,7 @@ var PullState;
8137681376
/**
8137781377
* Get an emoji based on the age of something.
8137881378
*
81379-
* @param {number} hoursAgo - The number of hours ago since something happened.
81379+
* @param {number} hoursAgo The number of hours ago since something happened.
8138081380
* @returns {string}
8138181381
*/
8138281382
function getAgeBasedEmoji(hoursAgo) {
@@ -81391,8 +81391,8 @@ function getAgeBasedEmoji(hoursAgo) {
8139181391
/**
8139281392
* Get the relative human readable age of something.
8139381393
*
81394-
* @param {number} hoursAgo - The number of hours ago since something happened.
81395-
* @param {boolean} [withAgo=true] - Whether to include the ' ago' suffix when appropriate.
81394+
* @param {number} hoursAgo The number of hours ago since something happened.
81395+
* @param {boolean} [withAgo=true] Whether to include the ' ago' suffix when appropriate.
8139681396
* @returns {string}
8139781397
*/
8139881398
function getRelativeHumanReadableAge(hoursAgo, withAgo = true) {
@@ -81409,7 +81409,7 @@ function getRelativeHumanReadableAge(hoursAgo, withAgo = true) {
8140981409
}
8141081410
/**
8141181411
* Get have or has depending on quantity context.
81412-
* @param {number} number - The number of entities in question.
81412+
* @param {number} number The number of entities in question.
8141381413
* @returns {string}
8141481414
*/
8141581415
function haveOrHas(number) {
@@ -81584,13 +81584,17 @@ async function getPullBlocks(pull, slack, withUserMentions) {
8158481584
const activityBlocks = [];
8158581585
for (const review of Object.values(reviews ?? [])) {
8158681586
const { context, email, login: username, relativeHumanReadableAge } = review;
81587-
const slackUser = await slack.getSlackUser({
81587+
const slackUser = await slack.getUser({
8158881588
email,
8158981589
username,
8159081590
});
81591-
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
8159281591
const displayName = slackUser?.profile?.display_name || slackUser?.profile?.real_name_normalized || username;
81593-
const imageUrl = slackUser?.profile?.image_72;
81592+
const imageUrl = slackUser?.profile?.image_512 ||
81593+
slackUser?.profile?.image_192 ||
81594+
slackUser?.profile?.image_72 ||
81595+
slackUser?.profile?.image_48 ||
81596+
slackUser?.profile?.image_32 ||
81597+
slackUser?.profile?.image_24;
8159481598
activityBlocks.push({
8159581599
elements: [
8159681600
{
@@ -81613,8 +81617,7 @@ async function getPullBlocks(pull, slack, withUserMentions) {
8161381617
if (requestedReviewers.length) {
8161481618
const slackUserIdsOrLogins = [];
8161581619
for (const username of requestedReviewers) {
81616-
const slackUser = await slack.getSlackUser({ username });
81617-
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
81620+
const slackUser = await slack.getUser({ username });
8161881621
slackUserIdsOrLogins.push((withUserMentions && slackUser?.id && `<@${slackUser.id}>`) || username);
8161981622
}
8162081623
activityBlocks.unshift(...getContextMarkdownBlock((0,_krauters_utils__WEBPACK_IMPORTED_MODULE_0__.formatStringList)(slackUserIdsOrLogins) +
@@ -81660,7 +81663,7 @@ async function getPullBlocks(pull, slack, withUserMentions) {
8166081663

8166181664
/***/ }),
8166281665

81663-
/***/ 70741:
81666+
/***/ 90294:
8166481667
/***/ ((__unused_webpack_module, __webpack_exports__, __nccwpck_require__) => {
8166581668

8166681669

@@ -81680,7 +81683,7 @@ var SlackAppUrl;
8168081683
SlackAppUrl["SuffixDisplayInfo"] = "general#display_info_form";
8168181684
})(SlackAppUrl || (SlackAppUrl = {}));
8168281685

81683-
;// CONCATENATED MODULE: ./src/utils/slack/slack-client.ts
81686+
;// CONCATENATED MODULE: ./src/utils/slack/client.ts
8168481687

8168581688

8168681689

@@ -81759,27 +81762,47 @@ class SlackClient {
8175981762
* @param username An email address that hopefully as matched to a Slack user account.
8176081763
* @param [botId] The botId for the bot to find.
8176181764
*/
81762-
async getSlackUser({ email, userId, username }) {
81765+
async getUser({ email, userId, username }) {
8176381766
console.log(`Getting Slack UserId for email [${email}], username [${username}], and userId [${userId}]...`);
8176481767
const users = this.users ?? (await this.getAllusers());
81768+
// Define matching functions for better readability and extensibility
81769+
const matchById = (user) => userId && user.id === userId;
81770+
const matchByEmail = (user) => email && user.profile?.email === email;
81771+
const matchByEmailContainsUsername = (user) => username && String(user.profile?.email ?? '').includes(username);
81772+
const matchByDisplayName = (user) => username && user.profile?.display_name === username;
81773+
const matchByRealName = (user) => username && user.profile?.real_name === username;
8176581774
const user = users.find((user) => {
81766-
if (userId) {
81767-
return user?.id === userId;
81768-
}
81769-
const profile = user.profile;
81770-
return (
81775+
const idMatch = matchById(user);
81776+
const emailMatch = matchByEmail(user);
81777+
const emailContainsUsernameMatch = matchByEmailContainsUsername(user);
81778+
const displayNameMatch = matchByDisplayName(user);
81779+
const realNameMatch = matchByRealName(user);
81780+
// Log the first match attempt that succeeds for debugging
81781+
if (idMatch && userId)
81782+
console.log(`Match found by userId [${userId}] with Slack userId [${user.id}]`);
81783+
else if (emailMatch && email)
81784+
console.log(`Match found by email [${email}] with Slack email [${user.profile?.email}]`);
81785+
else if (emailContainsUsernameMatch && username)
81786+
console.log(`Match found by username [${username}] contained in Slack email [${user.profile?.email}]`);
81787+
else if (displayNameMatch && username)
81788+
console.log(`Match found by username [${username}] matching Slack display_name [${user.profile?.display_name}]`);
81789+
else if (realNameMatch && username)
81790+
console.log(`Match found by username [${username}] matching Slack real_name [${user.profile?.real_name}]`);
8177181791
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
81772-
(email && profile?.email === email) ||
81773-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
81774-
String(profile?.email).includes(username) ||
81775-
profile?.display_name === username ||
81776-
profile?.real_name === username);
81792+
return idMatch || emailMatch || emailContainsUsernameMatch || displayNameMatch || realNameMatch;
8177781793
});
8177881794
if (user) {
8177981795
console.log(`User found with userId [${user.id}]`);
8178081796
return user;
8178181797
}
81782-
console.log('User not found, returning undefined');
81798+
console.log(`No user match found after checking against [${users.length}] users`);
81799+
if (userId)
81800+
console.log(`Tried to match userId [${userId}] against Slack user.id fields`);
81801+
if (email)
81802+
console.log(`Tried to match email [${email}] against Slack user.profile.email fields`);
81803+
if (username)
81804+
console.log(`Tried to match username [${username}] against Slack user.profile.email (contains), display_name and real_name fields`);
81805+
console.log(`Since no Slack user match found, unable to @mention user or use their profile image`);
8178381806
}
8178481807
/**
8178581808
* Post a message with blocks to Slack channels.
@@ -90365,7 +90388,7 @@ module.exports = {"version":"3.17.0"};
9036590388
/***/ 8330:
9036690389
/***/ ((module) => {
9036790390

90368-
module.exports = /*#__PURE__*/JSON.parse('{"UU":"@krauters/github-notifier","rE":"1.1.0","TB":"https://buymeacoffee.com/coltenkrauter"}');
90391+
module.exports = /*#__PURE__*/JSON.parse('{"UU":"@krauters/github-notifier","rE":"1.2.0","TB":"https://buymeacoffee.com/coltenkrauter"}');
9036990392

9037090393
/***/ })
9037190394

dist/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package-lock.json

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

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@krauters/github-notifier",
33
"description": "GitHub Notifier by Krauters – Post Open Pull Requests to Slack",
4-
"version": "1.1.0",
4+
"version": "1.2.0",
55
"author": "Colten Krauter <coltenkrauter>",
66
"type": "module",
77
"homepage": "https://buymeacoffee.com/coltenkrauter",

src/app.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ import { formatStringList, plural } from '@krauters/utils'
88

99
import pkg from '../package.json' with { type: 'json' }
1010
import { workflowLogsUrl, workflowUrl } from './constants.js'
11-
import { GitHubClient } from './utils/github/github-client.js'
11+
import { GitHubClient } from './utils/github/client.js'
1212
import type { Pull } from './utils/github/structures.js'
1313
import { PullState, RepositoryType } from './utils/github/structures.js'
1414
import { getFirstBlocks, getLastBlocks, getPullBlocks } from './utils/slack/blocks.js'
15-
import { SlackClient } from './utils/slack/slack-client.js'
15+
import { SlackClient } from './utils/slack/client.js'
1616
import { getApprovedPullRequest } from './utils/test-data.js'
1717
import { parseInputs as getInputs } from './input-parser.js'
1818

File renamed without changes.

src/utils/misc.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { plural } from '@krauters/utils'
33
/**
44
* Get an emoji based on the age of something.
55
*
6-
* @param {number} hoursAgo - The number of hours ago since something happened.
6+
* @param {number} hoursAgo The number of hours ago since something happened.
77
* @returns {string}
88
*/
99
export function getAgeBasedEmoji(hoursAgo: number): string {
@@ -22,8 +22,8 @@ export function getAgeBasedEmoji(hoursAgo: number): string {
2222
/**
2323
* Get the relative human readable age of something.
2424
*
25-
* @param {number} hoursAgo - The number of hours ago since something happened.
26-
* @param {boolean} [withAgo=true] - Whether to include the ' ago' suffix when appropriate.
25+
* @param {number} hoursAgo The number of hours ago since something happened.
26+
* @param {boolean} [withAgo=true] Whether to include the ' ago' suffix when appropriate.
2727
* @returns {string}
2828
*/
2929
export function getRelativeHumanReadableAge(hoursAgo: number, withAgo = true): string {
@@ -41,7 +41,7 @@ export function getRelativeHumanReadableAge(hoursAgo: number, withAgo = true): s
4141

4242
/**
4343
* Get have or has depending on quantity context.
44-
* @param {number} number - The number of entities in question.
44+
* @param {number} number The number of entities in question.
4545
* @returns {string}
4646
*/
4747
export function haveOrHas(number: number): string {

src/utils/slack/blocks.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
/* eslint-disable @typescript-eslint/naming-convention */
2+
/* eslint-disable @typescript-eslint/prefer-nullish-coalescing */
23
import type { KnownBlock, PlainTextElement, RichTextElement } from '@slack/web-api'
34
import type { Button } from '@slack/types'
45

56
import { capitalize, formatStringList, plural } from '@krauters/utils'
67

78
import type { Pull } from '../github/structures.js'
8-
import type { SlackClient } from './slack-client.js'
9+
import type { SlackClient } from './client.js'
910

1011
import { prBaseUrl, scmUrl } from '../../constants.js'
1112
import { getAgeBasedEmoji, haveOrHas } from '../misc.js'
@@ -190,14 +191,19 @@ export async function getPullBlocks(pull: Pull, slack: SlackClient, withUserMent
190191
const activityBlocks: KnownBlock[] = []
191192
for (const review of Object.values(reviews ?? [])) {
192193
const { context, email, login: username, relativeHumanReadableAge } = review
193-
const slackUser = await slack.getSlackUser({
194+
const slackUser = await slack.getUser({
194195
email,
195196
username,
196197
})
197198

198-
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
199199
const displayName = slackUser?.profile?.display_name || slackUser?.profile?.real_name_normalized || username
200-
const imageUrl = slackUser?.profile?.image_72
200+
const imageUrl =
201+
slackUser?.profile?.image_512 ||
202+
slackUser?.profile?.image_192 ||
203+
slackUser?.profile?.image_72 ||
204+
slackUser?.profile?.image_48 ||
205+
slackUser?.profile?.image_32 ||
206+
slackUser?.profile?.image_24
201207

202208
activityBlocks.push({
203209
elements: [
@@ -222,8 +228,7 @@ export async function getPullBlocks(pull: Pull, slack: SlackClient, withUserMent
222228
if (requestedReviewers.length) {
223229
const slackUserIdsOrLogins: string[] = []
224230
for (const username of requestedReviewers) {
225-
const slackUser = await slack.getSlackUser({ username })
226-
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
231+
const slackUser = await slack.getUser({ username })
227232
slackUserIdsOrLogins.push((withUserMentions && slackUser?.id && `<@${slackUser.id}>`) || username)
228233
}
229234
activityBlocks.unshift(

src/utils/slack/slack-client.ts renamed to src/utils/slack/client.ts

Lines changed: 42 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* eslint-disable @typescript-eslint/naming-convention */
22
import type { Block, BotsInfoResponse } from '@slack/web-api'
33
import type { Bot } from '@slack/web-api/dist/types/response/BotsInfoResponse.js'
4-
import type { Member, Profile } from '@slack/web-api/dist/types/response/UsersListResponse.js'
4+
import type { Member } from '@slack/web-api/dist/types/response/UsersListResponse.js'
55
import type { User } from '@slack/web-api/dist/types/response/UsersLookupByEmailResponse.js'
66

77
import { getBatches } from '@krauters/utils'
@@ -100,25 +100,43 @@ export class SlackClient {
100100
* @param username An email address that hopefully as matched to a Slack user account.
101101
* @param [botId] The botId for the bot to find.
102102
*/
103-
async getSlackUser({ email, userId, username }: GetUser): Promise<Member | undefined> {
103+
async getUser({ email, userId, username }: GetUser): Promise<Member | undefined> {
104104
console.log(`Getting Slack UserId for email [${email}], username [${username}], and userId [${userId}]...`)
105105

106106
const users = this.users ?? (await this.getAllusers())
107-
const user = users.find((user: Member) => {
108-
if (userId) {
109-
return user?.id === userId
110-
}
111107

112-
const profile: Profile | undefined = user.profile
108+
// Define matching functions for better readability and extensibility
109+
const matchById = (user: Member) => userId && user.id === userId
110+
const matchByEmail = (user: Member) => email && user.profile?.email === email
111+
const matchByEmailContainsUsername = (user: Member) =>
112+
username && String(user.profile?.email ?? '').includes(username)
113+
const matchByDisplayName = (user: Member) => username && user.profile?.display_name === username
114+
const matchByRealName = (user: Member) => username && user.profile?.real_name === username
113115

114-
return (
115-
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
116-
(email && profile?.email === email) ||
117-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
118-
String(profile?.email).includes(username!) ||
119-
profile?.display_name === username ||
120-
profile?.real_name === username
121-
)
116+
const user = users.find((user: Member) => {
117+
const idMatch = matchById(user)
118+
const emailMatch = matchByEmail(user)
119+
const emailContainsUsernameMatch = matchByEmailContainsUsername(user)
120+
const displayNameMatch = matchByDisplayName(user)
121+
const realNameMatch = matchByRealName(user)
122+
123+
// Log the first match attempt that succeeds for debugging
124+
if (idMatch && userId) console.log(`Match found by userId [${userId}] with Slack userId [${user.id}]`)
125+
else if (emailMatch && email)
126+
console.log(`Match found by email [${email}] with Slack email [${user.profile?.email}]`)
127+
else if (emailContainsUsernameMatch && username)
128+
console.log(`Match found by username [${username}] contained in Slack email [${user.profile?.email}]`)
129+
else if (displayNameMatch && username)
130+
console.log(
131+
`Match found by username [${username}] matching Slack display_name [${user.profile?.display_name}]`,
132+
)
133+
else if (realNameMatch && username)
134+
console.log(
135+
`Match found by username [${username}] matching Slack real_name [${user.profile?.real_name}]`,
136+
)
137+
138+
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
139+
return idMatch || emailMatch || emailContainsUsernameMatch || displayNameMatch || realNameMatch
122140
})
123141

124142
if (user) {
@@ -127,7 +145,15 @@ export class SlackClient {
127145
return user
128146
}
129147

130-
console.log('User not found, returning undefined')
148+
console.log(`No user match found after checking against [${users.length}] users`)
149+
if (userId) console.log(`Tried to match userId [${userId}] against Slack user.id fields`)
150+
if (email) console.log(`Tried to match email [${email}] against Slack user.profile.email fields`)
151+
if (username)
152+
console.log(
153+
`Tried to match username [${username}] against Slack user.profile.email (contains), display_name and real_name fields`,
154+
)
155+
156+
console.log(`Since no Slack user match found, unable to @mention user or use their profile image`)
131157
}
132158

133159
/**

src/utils/slack/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
export * from './blocks.js'
2-
export * from './slack-client.js'
2+
export * from './client.js'

0 commit comments

Comments
 (0)