Skip to content

Commit

Permalink
feat(options): add location to options (#44)
Browse files Browse the repository at this point in the history
- location can be either the PR title or the git branch name
  • Loading branch information
neilkimmett authored and macklinu committed Oct 4, 2018
1 parent 47aba58 commit 96e6261
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 4 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ jiraIssue({
format(emoji, jiraUrls) {
// Optional Formatter
return "Some Custom Message";
}
},
location: "title" // Optional location, either 'title' or 'branch'
});
```

Expand Down
13 changes: 13 additions & 0 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,4 +125,17 @@ describe("jiraIssue()", () => {
':link: JIRA Tickets: <a href="https://jira.net/browse/ABC-123">ABC-123</a>, <a href="https://jira.net/browse/DEF-456">DEF-456</a>'
);
});
it("supports JIRA key in the git branch", () => {
global.danger = {
github: { pr: { head: { ref: "ABC-808/some-things" } } }
};
jiraIssue({
key: "ABC",
location: "branch",
url: "https://jira.net/browse"
});
expect(global.message).toHaveBeenCalledWith(
':link: <a href="https://jira.net/browse/ABC-808">ABC-808</a>'
);
});
});
32 changes: 29 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ export interface Options {
* @returns {string}
*/
format?: (emoji: string, jiraUrls: string[]) => string;
/**
* The location of the JIRA issue, either the PR title, or the git branch
* Defaults to `title`
*/
location?: "title" | "branch";
}

const link = (href: string, text: string): string =>
Expand All @@ -41,7 +46,8 @@ const ensureUrlEndsWithSlash = (url: string) => {
* to include the JIRA issue identifier in the pull request title.
*/
export default function jiraIssue(options: Options) {
const { key = "", url = "", emoji = ":link:" } = options || {};
const { key = "", url = "", emoji = ":link:", location = "title" } =
options || {};
if (!url) {
throw Error(`'url' missing - must supply JIRA installation URL`);
}
Expand All @@ -56,8 +62,26 @@ export default function jiraIssue(options: Options) {
let match;
const jiraIssues = [];
// tslint:disable-next-line:no-conditional-assignment
while ((match = jiraKeyRegex.exec(danger.github.pr.title)) != null) {
let jiraLocation;
switch (location) {
case "title": {
jiraLocation = danger.github.pr.title;
break;
}
case "branch": {
jiraLocation = danger.github.pr.head.ref;
break;
}
default: {
throw Error(
`Invalid value for 'location', must be either "title" or "branch"`
);
}
}
match = jiraKeyRegex.exec(jiraLocation);
while (match != null) {
jiraIssues.push(match[0]);
match = jiraKeyRegex.exec(jiraLocation);
}
if (jiraIssues.length > 0) {
const jiraUrls = jiraIssues.map(issue =>
Expand All @@ -71,6 +95,8 @@ export default function jiraIssue(options: Options) {
message(`${emoji} ${jiraUrls.join(", ")}`);
}
} else {
warn(`Please add the JIRA issue key to the PR title (e.g. ${key}-123)`);
warn(
`Please add the JIRA issue key to the PR ${location} (e.g. ${key}-123)`
);
}
}

0 comments on commit 96e6261

Please sign in to comment.