-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(mr-size): add rule to check MR size + tests
- Loading branch information
1 parent
326450d
commit a22a2fc
Showing
4 changed files
with
115 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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')); | ||
}); | ||
}); | ||
}); |