Skip to content

Commit b0298d8

Browse files
authored
Improve GitHub API requests efficiency (#19)
* Improve GitHub API requests efficiency * Split repositories into individual fetches
1 parent 0cf11bc commit b0298d8

2 files changed

Lines changed: 36 additions & 28 deletions

File tree

src/helpers/dataFetcher.ts

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -414,33 +414,38 @@ async function fetchPullRequests({
414414

415415
if (!orgName || !teamName) return []
416416

417-
const allTeamUsers: string[] = await gql<GraphQL_UserResponse>(
418-
getTeamUsersQuery({ orgName, teamName }),
419-
)
420-
.then((res: GraphQL_UserResponse) =>
421-
res.organization.teams.nodes[0].members.nodes.map(
422-
(user: GraphQL_User) => user.login,
423-
),
417+
let teamUsers: string[]
418+
419+
if (savedMembers && savedMembers.length > 0) {
420+
teamUsers = savedMembers
421+
} else {
422+
const allTeamUsers: string[] = await gql<GraphQL_UserResponse>(
423+
getTeamUsersQuery({ orgName, teamName }),
424424
)
425-
.catch((error: { response?: { status?: number } }) => {
426-
if (error.response?.status === 401 || error.response?.status === 403) {
427-
handleInvalidTokenError()
428-
return []
429-
} else throw error
430-
})
425+
.then((res: GraphQL_UserResponse) =>
426+
res.organization.teams.nodes[0].members.nodes.map(
427+
(user: GraphQL_User) => user.login,
428+
),
429+
)
430+
.catch((error: { response?: { status?: number } }) => {
431+
if (error.response?.status === 401 || error.response?.status === 403) {
432+
handleInvalidTokenError()
433+
return []
434+
} else throw error
435+
})
431436

432-
if (!allTeamUsers.length) {
433-
setProgress(100)
434-
return []
435-
}
437+
if (!allTeamUsers.length) {
438+
setProgress(100)
439+
return []
440+
}
436441

437-
const teamUsers =
438-
savedMembers && savedMembers.length > 0 ? savedMembers : allTeamUsers
442+
teamUsers = allTeamUsers
443+
}
439444

440445
setProgress(10)
441446

442447
let progress = 10
443-
const totalResources = teamUsers.length + (teamRepositories ? 1 : 0)
448+
const totalResources = teamUsers.length + (teamRepositories?.length ?? 0)
444449

445450
const pullRequestPromises = teamUsers.map(user =>
446451
gql<GraphQL_PullRequestsResponse>(
@@ -452,19 +457,21 @@ async function fetchPullRequests({
452457
}),
453458
)
454459

455-
teamRepositories?.length &&
460+
teamRepositories?.forEach(repo =>
456461
pullRequestPromises.push(
457462
gql<GraphQL_PullRequestsResponse>(
458463
getPullRequestsByRepositoriesQuery({
459-
repositories: teamRepositories,
464+
repository: repo,
465+
excludeAuthors: teamUsers,
460466
includeChecks,
461467
}),
462468
).then((res: GraphQL_PullRequestsResponse) => {
463469
progress += 90 / totalResources
464470
setProgress(progress)
465471
return res
466472
}),
467-
)
473+
),
474+
)
468475

469476
const rawPullRequestsData = await Promise.all(pullRequestPromises)
470477
const rawPullRequests = rawPullRequestsData

src/helpers/graphqlQueries.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -386,18 +386,19 @@ function getPullRequestsByUserQuery({
386386
}
387387

388388
type GetPullRequestsByRepositoriesQueryProps = {
389-
repositories: string[]
389+
repository: string
390+
excludeAuthors?: string[]
390391
includeChecks?: boolean
391392
}
392393
function getPullRequestsByRepositoriesQuery({
393-
repositories,
394+
repository,
395+
excludeAuthors = [],
394396
includeChecks = false,
395397
}: GetPullRequestsByRepositoriesQueryProps) {
398+
const exclusions = excludeAuthors.map(login => `-author:${login}`).join(' ')
396399
const query = `{
397400
search(
398-
query: "is:open type:pr ${repositories
399-
.map(name => `repo:${name}`)
400-
.join(' ')}"
401+
query: "is:open type:pr repo:${repository} ${exclusions}"
401402
first: 100
402403
type: ISSUE
403404
) {

0 commit comments

Comments
 (0)