Skip to content

Commit

Permalink
fix(pmp-api): get repository reviewers
Browse files Browse the repository at this point in the history
Get reviewers of every PR in GitHub, instead of reviewers PRs.

References #17
  • Loading branch information
wjanaszek authored and MaciejSikorski committed Jan 13, 2020
1 parent 0a4c2cc commit 623467b
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,20 @@ export class ListRepositoryReviewersHandler
) {}

async execute(query: ListRepositoryReviewersQuery): Promise<RepositoryUserStatisticsReadModel[]> {
const repositoryReviewers = await this.repositoryRepository.getRepositoryReviewers();
const repositoryReviewersWithPrs = await this.repositoryRepository.getRepositoryReviewersWithPrs();
const repositories = await this.repositoryRepository.find();
const result = await Promise.all(
repositoryReviewers.map(user =>
this.queryBus
.execute<GetUserPrsQuery, PrModel[]>(new GetUserPrsQuery(user, repositories))
.then(prs =>
Promise.all(
repositories.map(repository =>
this.prsService
.getPrsWithChanges(repository, prs)
.then(
prsWithChanges => new RepositoryUserStatisticsReadModel(user, prsWithChanges)
)
repositories.map(repository =>
Promise.all(
repositoryReviewersWithPrs.map(reviewerWithPrs =>
this.prsService
.getPrsWithChanges(repository, reviewerWithPrs.prs)
.then(
prsWithChanges =>
new RepositoryUserStatisticsReadModel(reviewerWithPrs.reviewer, prsWithChanges)
)
)
)
)
)
);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export interface GithubUserEntity {
login: string;
id: string;
id: number;
avatar_url: string;
type: 'User' | 'Organisation';
contributions: number;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { PrModel, UserModel } from '@pimp-my-pr/pmp-api/api-service/repository/domain';

export interface UserModelWithPr {
reviewer: UserModel;
prs: PrModel[];
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Mapper } from '@pimp-my-pr/pmp-api/shared/domain';
import { UserModelWithPr } from '../domain/interfaces/user-model-with-pr.interface';
import { PrModel, UserModel } from '@pimp-my-pr/pmp-api/api-service/repository/domain';
import { NotImplementedException } from '@nestjs/common';

export class CustomUserWithPrMapper implements Mapper<[UserModel, PrModel[]], UserModelWithPr> {
mapFrom(param: [UserModel, PrModel[]]): UserModelWithPr {
return {
reviewer: param[0],
prs: param[1]
};
}

mapTo(param: UserModelWithPr): [UserModel, PrModel[]] {
throw new NotImplementedException('Mapping to user model with pr format is not implemented');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import { catchRequestExceptions } from '@pimp-my-pr/pmp-api/shared/util';
import { CoreException, CoreNotFoundException } from '@pimp-my-pr/pmp-api/shared/domain';
import { GithubUserEntity } from '../domain/entities/github-user.entity';
import { GithubUserMapper } from '../mappers/github-user.mapper';
import { UserModelWithPr } from '../domain/interfaces/user-model-with-pr.interface';
import { CustomUserWithPrMapper } from '../mappers/custom-user-with-pr.mapper';

@Injectable()
export class RepositoryDataService {
Expand All @@ -32,6 +34,7 @@ export class RepositoryDataService {
true
)
};
customUserWithPrMapper = new CustomUserWithPrMapper();
prMapper = new GithubPrMapper();
repositoryMapper = new GithubRepositoryMapper();
userMapper = new GithubUserMapper();
Expand Down Expand Up @@ -83,16 +86,12 @@ export class RepositoryDataService {
.toPromise();
}

getRepositoryReviewers(): Promise<UserModel[]> {
getRepositoryReviewersWithPrs(): Promise<UserModelWithPr[]> {
const owner = this.pmpApiServiceConfigService.getRepositoryOwner();
const repositoryTitle = this.pmpApiServiceConfigService.getRepositoryTitle();

return this.getRepositoryPrsAsObservable(`${owner}/${repositoryTitle}`)
.pipe(
map(prs =>
this.removeDuplicateUsers(prs.map(pr => pr.reviewers).flatMap<UserModel>(users => users))
)
)
.pipe(map(prs => this.groupByReviewers(prs)))
.toPromise();
}

Expand All @@ -106,7 +105,20 @@ export class RepositoryDataService {
);
}

private removeDuplicateUsers(users: UserModel[]): UserModel[] {
return [...new Map(users.map(obj => [JSON.stringify(obj), obj])).values()];
private groupByReviewers(prs: PrModel[]): UserModelWithPr[] {
const result: { [id: string]: { reviewer: UserModel; prs: PrModel[] } } = {};

prs.forEach(pr =>
pr.reviewers.forEach(reviewer => {
result[reviewer.id] = {
reviewer,
prs: result[reviewer.id] ? result[reviewer.id].prs.concat(pr) : [pr]
};
})
);

return Object.keys(result).map(key =>
this.customUserWithPrMapper.mapFrom([result[key].reviewer, result[key].prs])
);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export class UserModel {
name: string;
id: string;
id: number;
avatarUrl: string;
contributions: number;
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
import {
PrChanges,
PrModel
} from '@pimp-my-pr/pmp-api/api-service/repository/domain';
import { PrChanges, PrModel } from '@pimp-my-pr/pmp-api/api-service/repository/domain';

export class PrWithChangesReadModel {
linesOfCodeToCheck: number;
createdAt: Date;

constructor(private pr: PrModel, private changes: PrChanges) {
constructor(protected pr: PrModel, protected changes: PrChanges) {
this.createdAt = pr.createdAt;
this.linesOfCodeToCheck =
changes.additions + changes.changes + changes.deletions;
this.linesOfCodeToCheck = changes.additions + changes.changes + changes.deletions;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ import { PrWithChangesReadModel } from './pr-with-changes.read-model';
import { UserModel } from '../models/user.model';

export class RepositoryUserStatisticsReadModel extends BaseRepositoryStatisticsReadModel {
private id: number;

constructor(userModel: UserModel, prsModel: PrWithChangesReadModel[]) {
super(userModel, prsModel);
this.id = userModel.id;
}
}

0 comments on commit 623467b

Please sign in to comment.