Skip to content

Commit

Permalink
feat: add requirePound option
Browse files Browse the repository at this point in the history
checks for a hash symbol before each story or defect,
and fails if not present
  • Loading branch information
BearAlliance committed Nov 25, 2019
1 parent 737cb33 commit 032077b
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 7 deletions.
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,21 @@ This plugin:
- Provides links to stories and defects mentioned in commit messages, PR title, and PR description.
- Warns if no stories or defects are found

**Note:** Only works with Bitbucket server right now. More to come!

## API
### rally([options])

#### options

##### requirePound
Type: `Boolean`

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)

Only works with Bitbucket server right now. More to come!
## Changelog

See the GitHub [release history](https://github.com/bearalliance/danger-plugin-rally/releases).
Expand Down
41 changes: 41 additions & 0 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,47 @@ describe('rally', () => {
global.markdown = undefined;
});

describe('options', () => {
describe('requirePound', () => {
describe('when story numbers are not prefixed with a #', () => {
beforeEach(() => {
global.danger = {
bitbucket_server: {
pr: { title: 'My Test Title', description: 'some description' }
},
git: {
commits: [{ message: 'chore: do something\ncloses US1234567' }]
}
};
});
it('fails with a message', () => {
rally({ requirePound: true });
expect(global.fail)
.toHaveBeenCalledWith(`The following are referenced in the commit body, but are not prefixed by \`#\`.
- US1234567
Tools like [standard-version](https://www.npmjs.com/package/standard-version) rely on this marker to generate links to the ticket in the \`CHANGELOG\``);
});
});

describe('when story numbers are prefixed with a #', () => {
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({ requirePound: true });
expect(global.fail).not.toHaveBeenCalled();
});
});
});
});

describe('When there is no rally story in the Title, body, or commit message', () => {
beforeEach(() => {
global.danger = {
Expand Down
44 changes: 38 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@ export declare function warn(message: string): void;
export declare function fail(message: string): void;
export declare function markdown(message: string): void;

const rallyStoryPattern = /US\d{7}/g;
const rallyDefectPattern = /DE\d{6}/g;

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

function unique(array: any[]) {
Expand All @@ -19,23 +23,51 @@ function unique(array: any[]) {
});
}

function checkForPound(commitMessages) {
const poundStoryPattern = /#US\d{7}/g;
const poundDefectPattern = /#DE\d{6}/g;

const poundStories = commitMessages.match(poundStoryPattern) || [];
const poundDefects = commitMessages.match(poundDefectPattern) || [];

const stories = commitMessages.match(rallyStoryPattern) || [];
const defects = commitMessages.match(rallyDefectPattern) || [];

const storyDifference = stories.filter(x => !poundStories.includes('#' + x));
const defectDifference = defects.filter(x => !poundDefects.includes('#' + x));

const difference = [...storyDifference, ...defectDifference];

if (difference.length) {
fail(
`The following are referenced in the commit body, but are not prefixed by \`#\`.\n${difference
.map(str => `- ${str}`)
.join(
'\n'
)}\nTools like [standard-version](https://www.npmjs.com/package/standard-version) rely on this marker to generate links to the ticket in the \`CHANGELOG\``
);
}
}

/**
* tools for linking rally stories to pull requests
*/
export default function rally(
config: RallyPluginConfig = { domain: 'https://rally1.rallydev.com' }
) {
const { domain } = config;
export default function rally(config?: RallyPluginConfig) {
const defaultConfig = { domain: 'https://rally1.rallydev.com' };
const { domain, requirePound } = { ...defaultConfig, ...config };

const bbs = danger.bitbucket_server;
const prDescription = bbs.pr.description;
const prTitle = bbs.pr.title;
const rallyStoryPattern = /US\d{7}/g;
const rallyDefectPattern = /DE\d{6}/g;

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

if (requirePound) {
checkForPound(commitMessages);
}

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

0 comments on commit 032077b

Please sign in to comment.