Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jira Branch Name Convention Support #45

Merged
merged 4 commits into from
Oct 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions docs/pr-body/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

In order for Captain's log to pick up any issues addressed in a pull request, you must include them in the pull request body.

**Note:** If you are using Jira, Captain's Log supports the branch name convention outlined [here](https://confluence.atlassian.com/jirasoftwarecloud/referencing-issues-in-your-development-work-777002789.html). You may include something like `myusername/JIRA-123` or `JIRA-123` as your branch name, and Captain's Log will pick that up along with all issues in the body.

### How to get Captain's Log to pick up an issue

For example:

![ ](../static/pr-example.png)

The linking of _#34_ in the pull request will automatically get picked up by Captain's Log.
The linking of _#34_ in the pull request will automatically get picked up by Captain's Log.

The same goes for all of the github keywords, like "closes #34" (etc.).

Expand All @@ -24,11 +26,11 @@ This PR addresses concerns brought up in [JIRA-234](https://jira.mycompany.com/b
This PR addresses concerns brought up in https://jira.mycompany.com/browse/JIRA-234.
```

### How to allow Captain's Log to explicitly ignore an issue
### How to allow Captain's Log to explicitly ignore an issue

There are many reasons that you would want to ignore an issue. One of the most common ones is when you have worked on multiple "sub-tasks" or "sub-tickets" that all roll into a single issue. It can be helpful to reference these in a pull request, especially if they close issues, like the github syntax allows you to do. However, you may just want Captain's Log to reference the "parent" issue on release.
There are many reasons that you would want to ignore an issue. One of the most common ones is when you have worked on multiple "sub-tasks" or "sub-tickets" that all roll into a single issue. It can be helpful to reference these in a pull request, especially if they close issues, like the github syntax allows you to do. However, you may just want Captain's Log to reference the "parent" issue on release.

Ignoring an issue or multiple issues is as easy as placing these issues inside of a Captain's Log _"ignore block"_. It is a simple HTML comment block with two special key words, `icl` and `ecl`. This stands for `ignore captain's log` and `end captain's log` (ignore), respectively.
Ignoring an issue or multiple issues is as easy as placing these issues inside of a Captain's Log _"ignore block"_. It is a simple HTML comment block with two special key words, `icl` and `ecl`. This stands for `ignore captain's log` and `end captain's log` (ignore), respectively.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just check the rich diff, these seem to be whitespace cleanups by the editor.


Place this in your pull request template, or copy and paste it on a pr basis:

Expand Down
12 changes: 10 additions & 2 deletions src/facades/ReleaseCommunicationFacade.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,15 @@ class ReleaseCommunication {
}, []);

if (nonSquashedPRs.length) {
const prNumbersByCommit = await Promise.all(nonSquashedPRs.map(async commit => searchIssuesByCommitHandler(commit)));
let prNumbersByCommit = [];

// Should allow execution to continue for repositories that use a different merge method.
try {
prNumbersByCommit = await Promise.all(nonSquashedPRs.map(async commit => searchIssuesByCommitHandler(commit)));
} catch (e) {
console.log('Unable to retreive issues by commit. Check if API limit was exceeded.', e);
}

// There should only ever be one issue for a commit
prNumbersByCommit.forEach(pr => pullRequestNumbers.push(idx(pr, _ => _.items[0].number)));
}
Expand All @@ -99,7 +107,7 @@ class ReleaseCommunication {
const stripIgnoreTickets = body.replace(STRIP_IGNORE_TICKETS, '');
const noCommentBody = stripIgnoreTickets.replace(PR_TEMPLATE_COMMENT_REGEX, '');

const ticketGroups = await ticketFinder(noCommentBody);
const ticketGroups = await ticketFinder({ ...pr, body: noCommentBody });

return {
number: pr.number,
Expand Down
28 changes: 23 additions & 5 deletions src/utils/ticketFinder/__tests__/ticketFinder.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const {

describe('ticketFinder', () => {
it('should find tickets for all finders given a message body', async () => {
const tickets = await ticketFinder(mockBodyWithTickets);
const tickets = await ticketFinder({ body: mockBodyWithTickets });

expect(tickets).toEqual({
github: {
Expand All @@ -28,22 +28,40 @@ describe('ticketFinder', () => {
});

it('should find Jira tickets in brackets in given a message body', async () => {
const tickets = await ticketFinder(mockBodyWithTicketsInBrackets);
const tickets = await ticketFinder({ body: mockBodyWithTicketsInBrackets });

expect(tickets).toEqual({
github: { name: 'github', tickets: [] },
jira: { name: 'jira', tickets: [{ name: 'CAT-123' }] },
});
});

it('should find Jira ticket when there are tickets in the body and in the branch name', async () => {
const tickets = await ticketFinder({ body: mockBodyWithTicketsInBrackets, head: { ref: 'JIRA-123' } });

expect(tickets).toEqual({
github: { name: 'github', tickets: [] },
jira: { name: 'jira', tickets: [{ name: 'CAT-123' }, { name: 'JIRA-123' }] },
});
});

it('should find Jira ticket when there are tickets in the branch name only', async () => {
const tickets = await ticketFinder({ body: mockBodyWithNothing, head: { ref: 'therynamo/JIRA-123' } });

expect(tickets).toEqual({
github: { name: 'github', tickets: [] },
jira: { name: 'jira', tickets: [{ name: 'JIRA-123' }] },
});
});

it('should not find things that look almost like tickets in given a message body', async () => {
const tickets = await ticketFinder(mockBodyWithTicketLikeThings);
const tickets = await ticketFinder({ body: mockBodyWithTicketLikeThings });

expect(tickets).toEqual({ github: { name: 'github', tickets: [] }, jira: { name: 'jira', tickets: [] } });
});

it('should find multiple tickets for all finders given a message body', async () => {
const tickets = await ticketFinder(mockBodyWithMultipleTickets);
const tickets = await ticketFinder({ body: mockBodyWithMultipleTickets });

expect(tickets).toEqual({
github: {
Expand All @@ -68,7 +86,7 @@ describe('ticketFinder', () => {
});

it('should handle no tickets in a body', async () => {
const tickets = await ticketFinder(mockBodyWithNothing);
const tickets = await ticketFinder({ body: mockBodyWithNothing });

expect(tickets).toEqual({ github: { name: 'github', tickets: [] }, jira: { name: 'jira', tickets: [] } });
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ describe('github finder', () => {
],
name: 'github',
};
expect(githubFinder(body)).toEqual(expectation);
expect(githubFinder({ body })).toEqual(expectation);
});

it('should return no tickets if there are no ticket types', () => {
Expand Down
4 changes: 3 additions & 1 deletion src/utils/ticketFinder/finders/github/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ const GITHUB_CLOSE_REGEX = new RegExp(CLOSE_ISSUE_SYNTAX, 'gm');
const GITHUB_CLOSE_NUMBER_REGEX = new RegExp(CLOSE_SYNTAX_NUMBER, 'gm');
const GITHUB_CLOSE_PROJECT_REGEX = new RegExp(CLOSE_SYNTAX_PROJECT, 'gm');

const githubFinder = (body) => {
const githubFinder = (pr) => {
const { body = '' } = pr;

const fullUrls = uniq(groupFinder(GITHUB_URL_REGEX, body) || []);
const closeSyntaxUrls = uniq(groupFinder(GITHUB_CLOSE_REGEX, body) || []);

Expand Down
17 changes: 13 additions & 4 deletions src/utils/ticketFinder/finders/jira/index.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,24 @@
const idx = require('idx');
const { uniq } = require('lodash');
const nconf = require('nconf');

const { groupFinder } = require('../../../');

const regex = nconf.get('regex') || /(?:\[|https:\/\/jira\..*\.com\/browse\/)([[A-Z][A-Z0-9]*-[1-9][0-9]*)\]?/;
const JIRA_REGEX = new RegExp(regex, 'g');
const bodyRegex = nconf.get('regex') || /(?:\[|https:\/\/jira\..*\.com\/browse\/)([[A-Z][A-Z0-9]*-[1-9][0-9]*)\]?/;
const branchRegex = /([[A-Z][A-Z0-9]*-[1-9][0-9]*)\]?/;

const JIRA_REGEX = new RegExp(bodyRegex, 'g');
const JIRA_BRANCH_REGEX = new RegExp(branchRegex, 'g');

const jiraFinder = (pr) => {
const { body = '' } = pr;

const jiraFinder = (body) => {
const jiraTickets = uniq(groupFinder(JIRA_REGEX, body) || []);

const formattedTickets = jiraTickets.map(ticket => ({
const branchName = idx(pr, _ => _.head.ref) || '';
const branchTicket = uniq(groupFinder(JIRA_BRANCH_REGEX, branchName) || []);

const formattedTickets = [...jiraTickets, ...branchTicket].map(ticket => ({
name: ticket,
}));

Expand Down
8 changes: 4 additions & 4 deletions src/utils/ticketFinder/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,16 @@ const getFinderFunctions = async () => {
};

/**
* Finds tickets in pull request bodies based on pre-defined regexs
* @param {String} body pull request body
* Finds tickets in pull request based on pre-defined regexs
* @param {String} pr pull request response
* @return {Promise} resolves to return an object with separated buckets that have matches
*/
const ticketFinder = async (body) => {
const ticketFinder = async (pr) => {
const finders = await getFinderFunctions();
const ticketMap = {};

finders.forEach((finder) => {
const { name = null, tickets } = finder(body);
const { name = null, tickets } = finder(pr);

if (!ticketMap[name]) {
ticketMap[name] = {};
Expand Down