Skip to content

Commit ecfead9

Browse files
bringkingmacklinu
authored andcommitted
feat($options): Adds message format function to options (#33)
Adds a message format function to the options object that is passed the JIRA URls array and the selected emoji. The function can return a custom string to format the danger message.
1 parent 0d19e9c commit ecfead9

File tree

3 files changed

+35
-4
lines changed

3 files changed

+35
-4
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ jiraIssue({
2525
key: 'JIRA',
2626
url: 'https://myjira.atlassian.net/browse',
2727
emoji: ':paperclip:',
28+
format(emoji, jiraUrls) { // Optional Formatter
29+
return 'Some Custom Message';
30+
}
2831
})
2932
```
3033

src/index.test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,4 +110,19 @@ describe("jiraIssue()", () => {
110110
':link: <a href="https://jira.net/browse/ABC-123">ABC-123</a>, <a href="https://jira.net/browse/DEF-456">DEF-456</a>'
111111
);
112112
});
113+
it("supports a custom format function", () => {
114+
global.danger = {
115+
github: { pr: { title: "[ABC-123][DEF-456] Change some things" } }
116+
};
117+
jiraIssue({
118+
format: (emoji, jiraUrls) => {
119+
return `${emoji} JIRA Tickets: ${jiraUrls.join(", ")}`;
120+
},
121+
key: ["ABC", "DEF"],
122+
url: "https://jira.net/browse"
123+
});
124+
expect(global.message).toHaveBeenCalledWith(
125+
':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>'
126+
);
127+
});
113128
});

src/index.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,13 @@ export interface Options {
1515
* Defaults to `':link:'`.
1616
*/
1717
emoji?: string;
18+
/**
19+
* A format function to format the message
20+
* @param {string} emoji
21+
* @param {string[]} jiraUrls
22+
* @returns {string}
23+
*/
24+
format?: (emoji: string, jiraUrls: string[]) => string;
1825
}
1926

2027
const link = (href: string, text: string): string =>
@@ -53,10 +60,16 @@ export default function jiraIssue(options: Options) {
5360
jiraIssues.push(match[0]);
5461
}
5562
if (jiraIssues.length > 0) {
56-
const jiraUrls = jiraIssues
57-
.map(issue => link(resolve(ensureUrlEndsWithSlash(url), issue), issue))
58-
.join(", ");
59-
message(`${emoji} ${jiraUrls}`);
63+
const jiraUrls = jiraIssues.map(issue =>
64+
link(resolve(ensureUrlEndsWithSlash(url), issue), issue)
65+
);
66+
67+
// use custom formatter, or default
68+
if (options.format) {
69+
message(options.format(emoji, jiraUrls));
70+
} else {
71+
message(`${emoji} ${jiraUrls.join(", ")}`);
72+
}
6073
} else {
6174
warn(`Please add the JIRA issue key to the PR title (e.g. ${key}-123)`);
6275
}

0 commit comments

Comments
 (0)