()
| 21 | private projects: Model<ProjectReferenceEntity, "repositories">[]; |
| 22 | |
| 23 | public async find(): Promise<Model<ContributorEntity, "repositories">[]> { |
| 24 | // flatten repositories into one array |
| 25 | const repositories = this.projects.reduce<RepositoryEntity[]>( |
| 26 | (repositories, project) => [...repositories, ...project.repositories], |
| 27 | [], |
| 28 | ); |
| 29 | |
| 30 | // we first store them in a Record (object with id as keys) so we can uniquify them easily |
| 31 | const contributorsRecord: Record< |
| 32 | string, |
| 33 | Model<ContributorEntity, "repositories"> & { contributions: number } |
| 34 | > = {}; |
| 35 | |
| 36 | // get contributors from all the repos we have |
| 37 | await Promise.all( |
| 38 | repositories.map(async ({ provider, owner, repository }) => { |
| 39 | const committers = await this.githubService.listRepositoryContributors({ |
| 40 | owner, |
| 41 | repo: repository, |
| 42 | }); |
| 43 | committers.forEach(({ avatar_url: avatarUrl, id, login, contributions }) => { |
| 44 | const uuid = `${provider}/${id}`; |
| 45 | // add new contributor if doesn't exists |
| 46 | if (!contributorsRecord[uuid]) { |
| 47 | contributorsRecord[`${provider}/${id}`] = { |
| 48 | id: `${provider}/${id}`, |
| 49 | avatarUrl, |
| 50 | username: login, |
| 51 | repositories: [{ provider, owner, repository }], |
| 52 | contributions, |
| 53 | }; |
| 54 | } else { |
| 55 | // if exists, increment commit counts |
| 56 | contributorsRecord[uuid].contributions += contributions; |
| 57 | // add repository if doesn't exists |
| 58 | if ( |
| 59 | !contributorsRecord[uuid].repositories.some( |
| 60 | (r) => r.provider === provider && r.owner === owner && r.repository === repository, |
| 61 | ) |
| 62 | ) { |
| 63 | contributorsRecord[uuid].repositories.push({ |
| 64 | provider, |
| 65 | owner, |
| 66 | repository, |
| 67 | }); |
| 68 | } |
| 69 | } |
| 70 | }); |
| 71 | }), |
| 72 | ); |
| 73 | |
| 74 | return Object.keys(contributorsRecord) |
| 75 | .sort((a, b) => contributorsRecord[b].contributions - contributorsRecord[a].contributions) // sort contributors by their commits count |
| 76 | .map((id) => { |
| 77 | // eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 78 | const { contributions, ...contributor } = contributorsRecord[id]; |
| 79 | return contributor; |
| 80 | }); |
no outgoing calls
no test coverage detected