Skip to content

Commit

Permalink
feat(mr-size): add rule to check MR size + tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tomassebestik committed Nov 28, 2023
1 parent 326450d commit a22a2fc
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 7 deletions.
14 changes: 7 additions & 7 deletions src/configParameters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const defaults = {
maxSummaryLength: 72,
maxBodyLineLength: 100,
},
// mrSize: { enabled: true, maxChangedLines: 1000 },
prSize: { enabled: true, maxChangedLines: 1000 },
sourceBranchName: { enabled: true },
targetBranch: { enabled: true },
// updatedChangelog: {
Expand Down Expand Up @@ -47,10 +47,10 @@ const config = {
maxSummaryLength: Number(process.env.MAX_COMMIT_MESSAGE_SUMMARY) || defaults.commitMessages.maxSummaryLength,
maxBodyLineLength: Number(process.env.MAX_COMMIT_MESSAGE_BODY_LINE) || defaults.commitMessages.maxBodyLineLength,
},
// mrSize: {
// enabled: getEnvBool(process.env.ENABLE_CHECK_MR_SIZE_LINES) ?? defaults.mrSize.enabled,
// maxChangedLines: Number(process.env.MAX_MR_LINES) || defaults.mrSize.maxChangedLines,
// },
prSize: {
enabled: getEnvBool(process.env.ENABLE_CHECK_PR_SIZE_LINES) ?? defaults.prSize.enabled,
maxChangedLines: Number(process.env.MAX_PR_LINES) || defaults.prSize.maxChangedLines,
},
sourceBranchName: {
enabled: getEnvBool(process.env.ENABLE_CHECK_PR_SOURCE_BRANCH_NAME) ?? defaults.sourceBranchName.enabled,
},
Expand All @@ -72,7 +72,7 @@ const config = {
const parametersForTable = [
{ ciVar: 'ENABLE_CHECK_PR_COMMIT_MESSAGES', value: config.commitMessages.enabled, defaultValue: defaults.commitMessages.enabled },
{ ciVar: 'ENABLE_CHECK_PR_DESCRIPTION', value: config.prDescription.enabled, defaultValue: defaults.prDescription.enabled },
// { ciVar: 'ENABLE_CHECK_MR_SIZE_LINES', value: config.mrSize.enabled, defaultValue: defaults.mrSize.enabled },
{ ciVar: 'ENABLE_CHECK_PR_SIZE_LINES', value: config.prSize.enabled, defaultValue: defaults.prSize.enabled },
{ ciVar: 'ENABLE_CHECK_PR_SOURCE_BRANCH_NAME', value: config.sourceBranchName.enabled, defaultValue: defaults.sourceBranchName.enabled },
{ ciVar: 'ENABLE_CHECK_PR_TARGET_BRANCH', value: config.targetBranch.enabled, defaultValue: defaults.targetBranch.enabled },
{ ciVar: 'ENABLE_CHECK_PR_TOO_MANY_COMMITS', value: config.numberOfCommits.enabled, defaultValue: defaults.numberOfCommits.enabled },
Expand All @@ -85,7 +85,7 @@ const parametersForTable = [
{ ciVar: 'MAX_COMMIT_MESSAGE_SUMMARY', value: config.commitMessages.maxSummaryLength, defaultValue: defaults.commitMessages.maxSummaryLength },
{ ciVar: 'MAX_COMMITS', value: config.numberOfCommits.maxCommitsInfo, defaultValue: defaults.numberOfCommits.maxCommitsInfo },
{ ciVar: 'MAX_COMMITS_WARN', value: config.numberOfCommits.maxCommitsWarning, defaultValue: defaults.numberOfCommits.maxCommitsWarning },
// { ciVar: 'MAX_MR_LINES', value: config.mrSize.maxChangedLines, defaultValue: defaults.mrSize.maxChangedLines },
{ ciVar: 'MAX_PR_LINES', value: config.prSize.maxChangedLines, defaultValue: defaults.prSize.maxChangedLines },
{ ciVar: 'MIN_COMMIT_MESSAGE_SUMMARY', value: config.commitMessages.minSummaryLength, defaultValue: defaults.commitMessages.minSummaryLength },
{ ciVar: 'MIN_PR_DESCRIPTION_LENGTH', value: config.prDescription.minLength, defaultValue: defaults.prDescription.minLength },
];
Expand Down
2 changes: 2 additions & 0 deletions src/dangerfile.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { DangerResults } from 'danger';
import { config, displayAllOutputStatuses, logParamTable } from './configParameters';
import ruleCommitMessages from './ruleCommitMessages';
import rulePrSize from './rulePrSize';
import ruleNumberOfCommits from './ruleNumberOfCommits';
import rulePrDescription from './rulePrDescription';
import ruleSourceBranchName from './ruleSourceBranchName';
Expand All @@ -17,6 +18,7 @@ async function runDangerRules(): Promise<void> {
if (config.commitMessages.enabled) await ruleCommitMessages();
if (config.numberOfCommits.enabled) ruleNumberOfCommits();
if (config.prDescription.enabled) rulePrDescription();
if (config.prSize.enabled) rulePrSize();
if (config.sourceBranchName.enabled) ruleSourceBranchName();
if (config.targetBranch.enabled) await ruleTargetBranch();

Expand Down
22 changes: 22 additions & 0 deletions src/rulePrSize.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* Throw Danger MESSAGE if the pull request is too large (more than 1000 lines of changes)
* Throw Danger WARN if it is not possible to calculate PR size (empty PR)
*/

import { DangerDSLType, DangerResults } from 'danger';
import { config, recordRuleExitStatus } from './configParameters';
declare const danger: DangerDSLType;
declare const message: (message: string, results?: DangerResults) => void;

export default function (): void {
const ruleName = 'Pull Request size (number of changed lines)';
const totalLines: number | null = danger.github.pr.additions + danger.github.pr.deletions;

if (totalLines > config.prSize.maxChangedLines) {
message(`This PR seems to be quite large (total lines of code: ${totalLines}), you might consider splitting it into smaller PRs`);
return recordRuleExitStatus(ruleName, 'Passed (with suggestions)');
}

// At this point, the rule has passed
recordRuleExitStatus(ruleName, 'Passed');
}
84 changes: 84 additions & 0 deletions tests/rulePrSize.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import { config as originalConfig, recordRuleExitStatus } from '../src/configParameters';

declare global {
var danger: any;
var message: any;
}

describe('TESTS: Merge request size (number of changed lines)', () => {
let rulePrSize: any;
beforeEach(() => {
global.danger = {
github: {
pr: {
additions: 0,
deletions: 0,
},
},
};
global.message = jest.fn();
jest.resetModules();
jest.mock('../src/configParameters', () => ({
...jest.requireActual('../src/configParameters'),
recordRuleExitStatus: jest.fn(),
}));
});

describe('Default config (maxChangedLines: 1000)', () => {
beforeEach(async () => {
jest.doMock('../src/configParameters', () => ({
config: {
...originalConfig,
prSize: {
maxChangedLines: 1000,
},
},
recordRuleExitStatus,
}));
rulePrSize = (await import('../src/rulePrSize')).default;
});

it('EXPECT PASS: MR has not too many (800) lines of code', async () => {
danger.github.pr.additions = 800;
danger.github.pr.deletions = 0;
await rulePrSize();
expect(message).not.toHaveBeenCalled();
});

it('EXPECT FAIL: MR has too many (1200) lines of code', async () => {
danger.github.pr.additions = 1200;
danger.github.pr.deletions = 0;
await rulePrSize();
expect(message).toHaveBeenCalledWith(expect.stringContaining('This PR seems to be quite large'));
});
});

describe('Custom config (maxChangedLines: 2000)', () => {
beforeEach(async () => {
jest.doMock('../src/configParameters', () => ({
config: {
...originalConfig,
prSize: {
maxChangedLines: 2000,
},
},
recordRuleExitStatus,
}));
rulePrSize = (await import('../src/rulePrSize')).default;
});

it('EXPECT PASS: MR has not too many (1500) lines of code', async () => {
danger.github.pr.additions = 1500;
danger.github.pr.deletions = 0;
await rulePrSize();
expect(message).not.toHaveBeenCalled();
});

it('EXPECT FAIL: MR has too many (2500) lines of code', async () => {
danger.github.pr.additions = 1500;
danger.github.pr.deletions = 1000;
await rulePrSize();
expect(message).toHaveBeenCalledWith(expect.stringContaining('This PR seems to be quite large'));
});
});
});

0 comments on commit a22a2fc

Please sign in to comment.