Skip to content

Commit

Permalink
feat: add Fat PullRequests Practice
Browse files Browse the repository at this point in the history
  • Loading branch information
adelkahomolova committed Dec 17, 2019
1 parent 3b92dae commit 88e7c9e
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
54 changes: 54 additions & 0 deletions src/practices/LanguageIndependent/FatPullRequestsPractice.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import moment from 'moment';
import { PracticeContext } from '../../contexts/practice/PracticeContext';
import { PracticeEvaluationResult, PracticeImpact } from '../../model';
import { GitServiceUtils } from '../../services/git/GitServiceUtils';
import { DxPractice } from '../DxPracticeDecorator';
import { IPractice } from '../IPractice';
import { PullRequestState } from '../../inspectors/ICollaborationInspector';

@DxPractice({
id: 'LanguageIndependent.FatPullRequestsPractice',
name: '',
impact: PracticeImpact.medium,
suggestion: '',
reportOnlyOnce: true,
url: '',
})
export class FatPullRequestsPractice implements IPractice {
async isApplicable(): Promise<boolean> {
return true;
}

async evaluate(ctx: PracticeContext): Promise<PracticeEvaluationResult> {
if (ctx.fileInspector === undefined || ctx.collaborationInspector === undefined) {
return PracticeEvaluationResult.unknown;
}

const repoName = GitServiceUtils.getRepoName(ctx.projectComponent.repositoryPath, ctx.projectComponent.path);
const ownerAndRepoName = GitServiceUtils.getOwnerAndRepoName(repoName);

const pullRequests = await ctx.collaborationInspector.getPullRequests(ownerAndRepoName.owner, ownerAndRepoName.repoName, {
withDiffStat: true,
filter: { state: PullRequestState.all },
});

const descendingSortedPullRequests = pullRequests.items.sort(
(A, B) => new Date(B.updatedAt || B.createdAt).getTime() - new Date(A.updatedAt || A.createdAt).getTime(),
);
const daysInMilliseconds = moment.duration(30, 'days').asMilliseconds();
const newestPrDate = new Date(descendingSortedPullRequests[0].updatedAt || descendingSortedPullRequests[0].createdAt).getTime();

//get PRs which are no more than 30 days older than the newest PR
const validPullRequests = descendingSortedPullRequests.filter((val) => {
const date = new Date(val.updatedAt || val.createdAt).getTime();
return date > newestPrDate - daysInMilliseconds;
});

const fatPullRequests = validPullRequests.filter((pullRequest) => <number>pullRequest.lines?.changes > 1000);
if (fatPullRequests.length > 0) {
return PracticeEvaluationResult.notPracticing;
}

return PracticeEvaluationResult.practicing;
}
}
2 changes: 2 additions & 0 deletions src/practices/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { DoesPullRequestsPractice } from './LanguageIndependent/DoesPullRequests
import { DependenciesVersionMinorPatchLevelPractice } from './JavaScript/DependenciesVersionMinorPatchLevel';
import { CorrectCommitMessagesPractice } from './LanguageIndependent/CorrectCommitMessagesPractice';
import { TimeToSolvePullRequestsPractice } from './LanguageIndependent/TimeToSolvePullRequestsPractice';
import { FatPullRequestsPractice } from './LanguageIndependent/FatPullRequestsPractice';

// register practices here
export const practices = [
Expand Down Expand Up @@ -53,4 +54,5 @@ export const practices = [
DoesPullRequestsPractice,
CorrectCommitMessagesPractice,
TimeToSolvePullRequestsPractice,
FatPullRequestsPractice,
];

0 comments on commit 88e7c9e

Please sign in to comment.