Skip to content

Commit

Permalink
feat: add onlyBody option
Browse files Browse the repository at this point in the history
allows user to enforce story references only the commit body
  • Loading branch information
BearAlliance committed Dec 6, 2019
1 parent 05acb70 commit 40adee4
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 4 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ Default: `false`
Fails if story or defect numbers are not prefixed with `#` in the commit body.
This useful if you are generating ticket links with [standard-version](https://www.npmjs.com/package/standard-version) or [semantic-release](https://www.npmjs.com/package/semantic-release)

##### bodyOnly
Type: `Boolean`

Default: `false`

Fails if story or defect numbers mentioned in the commit header, rather than the body.

##### domain
Type: `String`

Expand Down
56 changes: 56 additions & 0 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,62 @@ describe('rally', () => {
});

describe('options', () => {
describe('bodyOnly', () => {
describe('when stories are in the commit heading', () => {
beforeEach(() => {
global.danger = {
bitbucket_server: {
pr: { title: 'My Test Title', description: 'some description' }
},
git: {
commits: [
{ message: 'chore: do something #US1234567\ncloses #US1234567' }
]
}
};
});
it('fails with a message', () => {
rally({ bodyOnly: true });
expect(global.fail).toHaveBeenCalledWith(
'Story and Defect references should go in the commit body, not the title'
);
});
});

describe('when stories are in the commit body', () => {
beforeEach(() => {
global.danger = {
bitbucket_server: {
pr: { title: 'My Test Title', description: 'some description' }
},
git: {
commits: [{ message: 'chore: do something\ncloses #US1234567' }]
}
};
});
it('does not fail', () => {
rally({ bodyOnly: true });
expect(global.fail).not.toHaveBeenCalled();
});
});

describe('when there is no body', () => {
beforeEach(() => {
global.danger = {
bitbucket_server: {
pr: { title: 'My Test Title', description: 'some description' }
},
git: {
commits: [{ message: 'chore: do something' }]
}
};
});
it('does not fail', () => {
rally({ bodyOnly: true });
expect(global.fail).not.toHaveBeenCalled();
});
});
});
describe('requirePound', () => {
describe('when story numbers are not prefixed with a #', () => {
beforeEach(() => {
Expand Down
30 changes: 26 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ const rallyDefectPattern = /DE\d{6}/g;

export interface RallyPluginConfig {
domain?: string;
requirePound: boolean;
requirePound?: boolean;
bodyOnly?: boolean;
}

function unique(array: any[]) {
Expand All @@ -23,6 +24,20 @@ function unique(array: any[]) {
});
}

function checkBody(commitMessages) {
commitMessages.forEach(commitMessage => {
const commitTitle = commitMessage.split('\n')[0];
if (
commitTitle.match(rallyStoryPattern) ||
commitTitle.match(rallyDefectPattern)
) {
fail(
'Story and Defect references should go in the commit body, not the title'
);
}
});
}

function checkForPound(commitMessages) {
const poundStoryPattern = /#US\d{7}/g;
const poundDefectPattern = /#DE\d{6}/g;
Expand Down Expand Up @@ -53,20 +68,27 @@ function checkForPound(commitMessages) {
* tools for linking rally stories to pull requests
*/
export default function rally(config?: RallyPluginConfig) {
const defaultConfig = { domain: 'https://rally1.rallydev.com' };
const { domain, requirePound } = { ...defaultConfig, ...config };
const defaultConfig: RallyPluginConfig = {
domain: 'https://rally1.rallydev.com',
requirePound: false,
bodyOnly: false
};
const { domain, requirePound, bodyOnly } = { ...defaultConfig, ...config };

const bbs = danger.bitbucket_server;
const prDescription = bbs.pr.description;
const prTitle = bbs.pr.title;

const commitMessages = danger.git.commits
.map(commit => commit.message)
.join(' ');
.join('\n');

if (requirePound) {
checkForPound(commitMessages);
}
if (bodyOnly) {
checkBody(danger.git.commits.map(commit => commit.message));
}

const storyNumbers = (prDescription + prTitle + commitMessages).match(
rallyStoryPattern
Expand Down

0 comments on commit 40adee4

Please sign in to comment.