Skip to content

Commit d3559f5

Browse files
committed
fix: logging
1 parent 8e4fba1 commit d3559f5

File tree

8 files changed

+98
-41
lines changed

8 files changed

+98
-41
lines changed

dist/index.js

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -80770,7 +80770,7 @@ const { /* homepage */ "TB": homepage, /* name */ "UU": name, /* version */ "rE"
8077080770
*/
8077180771
async function main() {
8077280772
try {
80773-
(0,_actions_core__WEBPACK_IMPORTED_MODULE_1__.debug)('Starting main...');
80773+
console.log(`Starting GitHub Notifier version [${version}]...`);
8077480774
const { githubConfig, repositoryFilter, slackConfig, withArchived, withDrafts, withPublic, withTestData, withUserMentions, } = (0,_input_parser_js__WEBPACK_IMPORTED_MODULE_10__/* .parseInputs */ .T)();
8077580775
const slack = new _utils_slack_client_js__WEBPACK_IMPORTED_MODULE_8__/* .SlackClient */ .Q(slackConfig);
8077680776
const results = await githubConfig.tokens.reduce(async (accPromise, token) => {
@@ -80789,7 +80789,7 @@ async function main() {
8078980789
});
8079080790
const org = await client.getOrg();
8079180791
const pulls = await client.getPulls({ repositories, state: _utils_github_structures_js__WEBPACK_IMPORTED_MODULE_6__/* .PullState */ .lT.Open, withDrafts });
80792-
console.log(`Found [${pulls.length}] pulls for [${org.name}]`);
80792+
console.log(`Found [${pulls.length}] ${(0,_krauters_utils__WEBPACK_IMPORTED_MODULE_2__.plural)('pull', pulls.length)} for [${org.name}]`);
8079380793
return [...acc, { client, org: org.name, pulls }];
8079480794
}
8079580795
catch (error) {
@@ -80808,6 +80808,7 @@ async function main() {
8080880808
const dedupedPulls = [...new Map(pulls.map((pull) => [`${pull.org}/${pull.repo}/${pull.number}`, pull]))].map(
8080980809
// eslint-disable-next-line @typescript-eslint/naming-convention, @typescript-eslint/no-unused-vars
8081080810
([_, pull]) => pull);
80811+
console.log(`After deduplication, there are [${dedupedPulls.length}] unique pull requests`);
8081180812
let blocks = [];
8081280813
for (const pull of dedupedPulls) {
8081380814
(0,_actions_core__WEBPACK_IMPORTED_MODULE_1__.debug)(`Building Slack blocks from pull request [${pull.number}]`);
@@ -80842,7 +80843,9 @@ async function main() {
8084280843
`/${_actions_github__WEBPACK_IMPORTED_MODULE_0__.context.payload.repository?.name}> (<${_constants_js__WEBPACK_IMPORTED_MODULE_4__/* .workflowLogsUrl */ .GM}|logs>) using <${homepage}|${name}>@<${homepage}/releases/tag/${version}|${version}>`,
8084380844
].join('')),
8084480845
];
80846+
console.log(`Posting [${blocks.length}] Slack blocks to [${slack.channels.length}] channels`);
8084580847
await slack.postMessage(header, blocks);
80848+
console.log('Successfully posted notification to Slack');
8084680849
}
8084780850
catch (error) {
8084880851
console.error(`Fatal error [${error}]`);
@@ -81022,7 +81025,7 @@ class GitHubClient {
8102281025
*/
8102381026
async getOrg() {
8102481027
if (!this.cacheOrganization) {
81025-
console.log(`Getting organization associated with current token...`);
81028+
(0,_actions_core__WEBPACK_IMPORTED_MODULE_1__.debug)(`Getting organization associated with current token...`);
8102681029
const { data } = await this.client.rest.orgs.listMembershipsForAuthenticatedUser();
8102781030
if (data.length > 1) {
8102881031
console.error(data);
@@ -81092,22 +81095,25 @@ class GitHubClient {
8109281095
* @param props Configuration for retrieving pull requests.
8109381096
*/
8109481097
async getPulls({ oldest = (0,_krauters_utils__WEBPACK_IMPORTED_MODULE_2__.snapDate)(new Date(), { months: -36, snap: _krauters_utils__WEBPACK_IMPORTED_MODULE_2__.SnapType.Month }), onlyGhReviews = false, repositories, state = _structures_js__WEBPACK_IMPORTED_MODULE_0__/* .PullState */ .lT.All, withCommits = true, withDrafts, withFilesAndChanges = true, withUser = true, }) {
81098+
const startTime = Date.now();
8109581099
const org = await this.getOrgName();
81096-
console.log('\n');
8109781100
if (state === _structures_js__WEBPACK_IMPORTED_MODULE_0__/* .PullState */ .lT.Open) {
81098-
console.log(`Getting [${state}] pulls in org [${org}]...`);
81101+
(0,_actions_core__WEBPACK_IMPORTED_MODULE_1__.debug)(`Getting [${state}] ${(0,_krauters_utils__WEBPACK_IMPORTED_MODULE_2__.plural)('pull', 2)} in org [${org}]...`);
8109981102
}
8110081103
else {
81101-
console.log(`Getting [${state}] pulls in org [${org}] that are newer than [${oldest}...`);
81104+
(0,_actions_core__WEBPACK_IMPORTED_MODULE_1__.debug)(`Getting [${state}] ${(0,_krauters_utils__WEBPACK_IMPORTED_MODULE_2__.plural)('pull', 2)} in org [${org}] that are newer than [${oldest}]...`);
8110281105
}
8110381106
const pullRequests = [];
81107+
let apiRequestCount = 0;
8110481108
for (const repo of repositories) {
8110581109
(0,_actions_core__WEBPACK_IMPORTED_MODULE_1__.debug)(`Getting [${state}] pulls in repository [${repo.name}]...`);
81110+
const repoStartTime = Date.now();
8110681111
const pulls = await this.client.paginate(this.client.rest.pulls.list, {
8110781112
owner: org,
8110881113
repo: repo.name,
8110981114
state,
8111081115
}, (response, done) => {
81116+
apiRequestCount++;
8111181117
(0,_actions_core__WEBPACK_IMPORTED_MODULE_1__.debug)(`Paginated response for repository [${repo.name}], status [${response.status}], items [${response.data.length}]`);
8111281118
const found = response.data.find((pull) => new Date(pull.created_at) < oldest);
8111381119
if (found && state !== _structures_js__WEBPACK_IMPORTED_MODULE_0__/* .PullState */ .lT.Open) {
@@ -81116,7 +81122,8 @@ class GitHubClient {
8111681122
}
8111781123
return response.data.filter((pull) => new Date(pull.created_at) > oldest);
8111881124
});
81119-
console.log(`Found [${pulls.length}] pull in repository [${repo.name}]`);
81125+
const repoDuration = (Date.now() - repoStartTime) / 1000;
81126+
(0,_actions_core__WEBPACK_IMPORTED_MODULE_1__.debug)(`Found [${pulls.length}] pull in repository [${repo.name}] in [${repoDuration.toFixed(2)}] seconds`);
8112081127
for (const pull of pulls) {
8112181128
const { base, closed_at, created_at, draft, html_url, merged_at, number, title, user } = pull;
8112281129
(0,_actions_core__WEBPACK_IMPORTED_MODULE_1__.debug)(`Processing pull [${number}]...`);
@@ -81155,6 +81162,8 @@ class GitHubClient {
8115581162
(0,_actions_core__WEBPACK_IMPORTED_MODULE_1__.debug)(`Added pull [${number}] to response`);
8115681163
}
8115781164
}
81165+
const totalDuration = (Date.now() - startTime) / 1000;
81166+
console.log(`Completed processing [${pullRequests.length}] pull requests in [${totalDuration.toFixed(2)}] seconds with [${apiRequestCount}] API calls`);
8115881167
return pullRequests;
8115981168
}
8116081169
/**
@@ -81164,7 +81173,7 @@ class GitHubClient {
8116481173
*/
8116581174
async getRepositories({ repositoryFilter = [], type = _structures_js__WEBPACK_IMPORTED_MODULE_0__/* .RepositoryType */ .vJ.All, withArchived, withPublic, }) {
8116681175
const org = await this.getOrgName();
81167-
console.log(`Getting all repositories in org [${org}]...`);
81176+
(0,_actions_core__WEBPACK_IMPORTED_MODULE_1__.debug)(`Getting all repositories in org [${org}]...`);
8116881177
const response = await this.client.paginate(this.client.rest.repos.listForOrg, {
8116981178
org,
8117081179
per_page: 100,
@@ -81180,7 +81189,7 @@ class GitHubClient {
8118081189
if (!withPublic) {
8118181190
filteredRepos = filteredRepos.filter((repo) => repo.visibility !== _structures_js__WEBPACK_IMPORTED_MODULE_0__/* .RepositoryType */ .vJ.Public);
8118281191
}
81183-
console.log(`Found [${filteredRepos.length}] repositories`);
81192+
(0,_actions_core__WEBPACK_IMPORTED_MODULE_1__.debug)(`Found [${filteredRepos.length}] repositories in org [${org}]`);
8118481193
(0,_actions_core__WEBPACK_IMPORTED_MODULE_1__.debug)(JSON.stringify(filteredRepos.map((repo) => repo.name), null, 2));
8118581194
return filteredRepos;
8118681195
}
@@ -81707,6 +81716,7 @@ var SlackAppUrl;
8170781716

8170881717
;// CONCATENATED MODULE: ./src/utils/slack/user-matchers.ts
8170981718

81719+
8171081720
function customMappingMatcher(githubUsername, slackUsername) {
8171181721
return {
8171281722
check: (user) => user.name === slackUsername ||
@@ -81791,7 +81801,7 @@ const createUserMatchers = ({ email, userId, userMappings = [], username }) => {
8179181801
return matchers;
8179281802
};
8179381803
const logFailedMatches = ({ email, userId, userMappings = [], username }, usersCount) => {
81794-
console.log(`No user match found after checking against [${usersCount}] users`);
81804+
console.log(`No user match found for [${username}] after checking against [${usersCount}] Slack ${(0,src.plural)('user', usersCount)}`);
8179581805
// Log mapping failures
8179681806
if (username && userMappings.length > 0) {
8179781807
const matchingMappings = userMappings.filter((mapping) => mapping.githubUsername === username);
@@ -81832,6 +81842,7 @@ class SlackClient {
8183281842
this.client = new dist.WebClient(token);
8183381843
this.channels = channels;
8183481844
this.userMappings = userMappings;
81845+
console.log(`Slack client initialized with [${channels.length}] ${(0,src.plural)('channel', channels.length)} and [${userMappings.length}] user ${(0,src.plural)('mapping', userMappings.length)}`);
8183581846
}
8183681847
/**
8183781848
* Ensure app name pattern.
@@ -81848,6 +81859,7 @@ class SlackClient {
8184881859
throw new Error(`Current app name [${name}] does not match the desired pattern [${pattern}]. Please update Slack app "Basic Information" > "Display Information" > "App name" to have the suffix "GitHub Notifier". Slack app url [${SlackAppUrl.Prefix}/${info?.bot?.app_id}/${SlackAppUrl.SuffixDisplayInfo}].`);
8184981860
}
8185081861
this.bot = info.bot;
81862+
console.log(`Verified Slack app name: [${name}]`);
8185181863
}
8185281864
/**
8185381865
* Get all Slack users.
@@ -81856,6 +81868,7 @@ class SlackClient {
8185681868
this.users = [];
8185781869
let cursor;
8185881870
console.log('Getting all Slack users...');
81871+
const startTime = Date.now();
8185981872
try {
8186081873
// Keep paginating until no more cursor is returned
8186181874
do {
@@ -81866,7 +81879,8 @@ class SlackClient {
8186681879
cursor = result.response_metadata?.next_cursor;
8186781880
(0,core.debug)(`Got [${result.members?.length}] users from Slack`);
8186881881
} while (cursor);
81869-
console.log(`Got a total of [${this.users.length}] active users from Slack`);
81882+
const duration = (Date.now() - startTime) / 1000;
81883+
console.log(`Got a total of [${this.users.length}] active ${(0,src.plural)('user', this.users.length)} from Slack in [${duration.toFixed(2)}] seconds`);
8187081884
return this.users;
8187181885
}
8187281886
catch (error) {
@@ -81879,6 +81893,7 @@ class SlackClient {
8187981893
*/
8188081894
async getBotInfo() {
8188181895
try {
81896+
console.log('Getting Slack bot information...');
8188281897
const authResponse = await this.client.auth.test({});
8188381898
const response = await this.client.bots.info({ bot: authResponse.bot_id });
8188481899
if (!response.ok) {
@@ -81928,10 +81943,16 @@ class SlackClient {
8192881943
* @param [channels=this.channels] Channels to post to.
8192981944
*/
8193081945
async postMessage(text, blocks, channels = this.channels) {
81946+
const startTime = Date.now();
81947+
const totalBlocks = blocks.length;
81948+
const batches = (0,src.getBatches)(blocks);
81949+
(0,core.debug)(`Preparing to post [${totalBlocks}] blocks to [${channels.length}] channels`);
8193181950
for (const channel of channels) {
8193281951
let batchNumber = 0;
81933-
for (const batch of (0,src.getBatches)(blocks)) {
81934-
console.log(`Posting batch [${batchNumber++}] to Slack channel [${channel}]...`);
81952+
for (const batch of batches) {
81953+
batchNumber++;
81954+
const batchStartTime = Date.now();
81955+
(0,core.debug)(`Posting batch [${batchNumber}] to Slack channel [${channel}]...`);
8193581956
const payload = {
8193681957
blocks: batch.items,
8193781958
channel,
@@ -81945,9 +81966,12 @@ class SlackClient {
8194581966
(0,core.debug)(JSON.stringify(payload, null, 2));
8194681967
const response = await this.client.chat.postMessage(payload);
8194781968
(0,core.debug)(JSON.stringify(response, null, 2));
81948-
console.log(`Posted batch [${batchNumber++}] to Slack channel [${channel}] with success [${response.ok}]`);
81969+
const batchDuration = (Date.now() - batchStartTime) / 1000;
81970+
console.log(`Posted batch [${batchNumber}] to Slack channel [${channel}] with success [${response.ok}] in [${batchDuration.toFixed(2)}] seconds`);
8194981971
}
8195081972
}
81973+
const totalDuration = (Date.now() - startTime) / 1000;
81974+
console.log(`Completed posting all messages to Slack in [${totalDuration.toFixed(2)}] seconds`);
8195181975
}
8195281976
}
8195381977

@@ -90605,7 +90629,7 @@ module.exports = {"version":"3.17.0"};
9060590629
/***/ 8330:
9060690630
/***/ ((module) => {
9060790631

90608-
module.exports = /*#__PURE__*/JSON.parse('{"UU":"@krauters/github-notifier","rE":"1.3.0","TB":"https://buymeacoffee.com/coltenkrauter"}');
90632+
module.exports = /*#__PURE__*/JSON.parse('{"UU":"@krauters/github-notifier","rE":"1.3.1","TB":"https://buymeacoffee.com/coltenkrauter"}');
9060990633

9061090634
/***/ })
9061190635

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: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
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.3.0",
4+
"version": "1.3.1",
55
"author": "Colten Krauter <coltenkrauter>",
66
"type": "module",
77
"homepage": "https://buymeacoffee.com/coltenkrauter",
@@ -47,7 +47,7 @@
4747
"devDependencies": {
4848
"@krauters/eslint-config": "^1.8.0",
4949
"@types/jest": "^29.5.14",
50-
"@types/node": "^22.14.1",
50+
"@types/node": "^22.15.0",
5151
"@vercel/ncc": "^0.38.3",
5252
"husky": "9.1.7",
5353
"jest": "^29.7.0",

src/app.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ const { homepage, name, version } = pkg
2323
*/
2424
async function main(): Promise<void> {
2525
try {
26-
debug('Starting main...')
26+
console.log(`Starting GitHub Notifier version [${version}]...`)
27+
2728
const {
2829
githubConfig,
2930
repositoryFilter,
@@ -55,7 +56,7 @@ async function main(): Promise<void> {
5556

5657
const org = await client.getOrg()
5758
const pulls = await client.getPulls({ repositories, state: PullState.Open, withDrafts })
58-
console.log(`Found [${pulls.length}] pulls for [${org.name}]`)
59+
console.log(`Found [${pulls.length}] ${plural('pull', pulls.length)} for [${org.name}]`)
5960

6061
return [...acc, { client, org: org.name, pulls }]
6162
} catch (error: unknown) {
@@ -85,6 +86,7 @@ async function main(): Promise<void> {
8586
// eslint-disable-next-line @typescript-eslint/naming-convention, @typescript-eslint/no-unused-vars
8687
([_, pull]) => pull,
8788
)
89+
console.log(`After deduplication, there are [${dedupedPulls.length}] unique pull requests`)
8890

8991
let blocks: KnownBlock[] = []
9092
for (const pull of dedupedPulls) {
@@ -130,7 +132,9 @@ async function main(): Promise<void> {
130132
),
131133
]
132134

135+
console.log(`Posting [${blocks.length}] Slack blocks to [${slack.channels.length}] channels`)
133136
await slack.postMessage(header, blocks)
137+
console.log('Successfully posted notification to Slack')
134138
} catch (error) {
135139
console.error(`Fatal error [${error}]`)
136140
process.exit(1)

src/utils/github/client.ts

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import {
2727
reviewText,
2828
} from './structures.js'
2929
import { debug } from '@actions/core'
30-
import { average, getHoursAgo, minutesBetweenDates, snapDate, SnapType } from '@krauters/utils'
30+
import { average, getHoursAgo, minutesBetweenDates, plural, snapDate, SnapType } from '@krauters/utils'
3131
import { ignoreFilenamesForChanges } from '../../constants.js'
3232
import { getRelativeHumanReadableAge } from '../misc.js'
3333

@@ -101,7 +101,7 @@ export class GitHubClient {
101101
*/
102102
async getOrg(): Promise<Organization> {
103103
if (!this.cacheOrganization) {
104-
console.log(`Getting organization associated with current token...`)
104+
debug(`Getting organization associated with current token...`)
105105
const { data } = await this.client.rest.orgs.listMembershipsForAuthenticatedUser()
106106

107107
if (data.length > 1) {
@@ -205,17 +205,19 @@ export class GitHubClient {
205205
withFilesAndChanges = true,
206206
withUser = true,
207207
}: GetPullsProps): Promise<Pull[]> {
208+
const startTime = Date.now()
208209
const org = await this.getOrgName()
209-
console.log('\n')
210210
if (state === PullState.Open) {
211-
console.log(`Getting [${state}] pulls in org [${org}]...`)
211+
debug(`Getting [${state}] ${plural('pull', 2)} in org [${org}]...`)
212212
} else {
213-
console.log(`Getting [${state}] pulls in org [${org}] that are newer than [${oldest}...`)
213+
debug(`Getting [${state}] ${plural('pull', 2)} in org [${org}] that are newer than [${oldest}]...`)
214214
}
215215

216216
const pullRequests = []
217+
let apiRequestCount = 0
217218
for (const repo of repositories) {
218219
debug(`Getting [${state}] pulls in repository [${repo.name}]...`)
220+
const repoStartTime = Date.now()
219221
const pulls = await this.client.paginate(
220222
this.client.rest.pulls.list,
221223
{
@@ -224,6 +226,7 @@ export class GitHubClient {
224226
state,
225227
},
226228
(response, done) => {
229+
apiRequestCount++
227230
debug(
228231
`Paginated response for repository [${repo.name}], status [${response.status}], items [${response.data.length}]`,
229232
)
@@ -237,7 +240,8 @@ export class GitHubClient {
237240
},
238241
)
239242

240-
console.log(`Found [${pulls.length}] pull in repository [${repo.name}]`)
243+
const repoDuration = (Date.now() - repoStartTime) / 1000
244+
debug(`Found [${pulls.length}] pull in repository [${repo.name}] in [${repoDuration.toFixed(2)}] seconds`)
241245
for (const pull of pulls) {
242246
const { base, closed_at, created_at, draft, html_url, merged_at, number, title, user } = pull
243247
debug(`Processing pull [${number}]...`)
@@ -286,6 +290,11 @@ export class GitHubClient {
286290
}
287291
}
288292

293+
const totalDuration = (Date.now() - startTime) / 1000
294+
console.log(
295+
`Completed processing [${pullRequests.length}] pull requests in [${totalDuration.toFixed(2)}] seconds with [${apiRequestCount}] API calls`,
296+
)
297+
289298
return pullRequests
290299
}
291300

@@ -301,7 +310,7 @@ export class GitHubClient {
301310
withPublic,
302311
}: GetRepositoriesProps): Promise<GitHubRepositories> {
303312
const org = await this.getOrgName()
304-
console.log(`Getting all repositories in org [${org}]...`)
313+
debug(`Getting all repositories in org [${org}]...`)
305314
const response = await this.client.paginate(this.client.rest.repos.listForOrg, {
306315
org,
307316
per_page: 100,
@@ -317,7 +326,7 @@ export class GitHubClient {
317326
if (!withPublic) {
318327
filteredRepos = filteredRepos.filter((repo: GitHubRepository) => repo.visibility !== RepositoryType.Public)
319328
}
320-
console.log(`Found [${filteredRepos.length}] repositories`)
329+
debug(`Found [${filteredRepos.length}] repositories in org [${org}]`)
321330
debug(
322331
JSON.stringify(
323332
filteredRepos.map((repo) => repo.name),

0 commit comments

Comments
 (0)