From 88e7c9e8f08b106aa10b71846aa13167a1e4f4d4 Mon Sep 17 00:00:00 2001 From: Adela Homolova Date: Tue, 17 Dec 2019 13:36:25 +0100 Subject: [PATCH] feat: add Fat PullRequests Practice --- .../FatPullRequestsPractice.ts | 54 +++++++++++++++++++ src/practices/index.ts | 2 + 2 files changed, 56 insertions(+) create mode 100644 src/practices/LanguageIndependent/FatPullRequestsPractice.ts diff --git a/src/practices/LanguageIndependent/FatPullRequestsPractice.ts b/src/practices/LanguageIndependent/FatPullRequestsPractice.ts new file mode 100644 index 000000000..47abd0440 --- /dev/null +++ b/src/practices/LanguageIndependent/FatPullRequestsPractice.ts @@ -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 { + return true; + } + + async evaluate(ctx: PracticeContext): Promise { + 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) => pullRequest.lines?.changes > 1000); + if (fatPullRequests.length > 0) { + return PracticeEvaluationResult.notPracticing; + } + + return PracticeEvaluationResult.practicing; + } +} diff --git a/src/practices/index.ts b/src/practices/index.ts index 4c90e73a4..71e49383e 100644 --- a/src/practices/index.ts +++ b/src/practices/index.ts @@ -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 = [ @@ -53,4 +54,5 @@ export const practices = [ DoesPullRequestsPractice, CorrectCommitMessagesPractice, TimeToSolvePullRequestsPractice, + FatPullRequestsPractice, ];