Skip to content

Commit

Permalink
Add comment to WI when PR is opened @W-9458251@ (#123)
Browse files Browse the repository at this point in the history
* chore: empty commit to trigger heroku deploy to new stack

* feat: add comment to WI when PR is opened @W-9458251@

* chore: prettier @W-9458251@

* chore: test fix @W-9458251@
  • Loading branch information
jag-j authored Jun 14, 2021
1 parent cd95c02 commit 51bcfc6
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 1 deletion.
59 changes: 59 additions & 0 deletions api/actions/__test__/createComment.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright (c) 2021, salesforce.com, inc.
* All rights reserved.
* SPDX-License-Identifier: BSD-3-Clause
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/

const { fn } = require('../createComment');
const Gus = require('../../services/Gus');

jest.mock('../../services/Gus', () => ({
createComment: jest.fn(),
getWorkItemIdByName: jest.fn()
}));

jest.mock('../../services/Issues', () => ({
getByName: jest.fn()
}));

const req = {
body: {
pull_request: {
title: 'pull request title @W-1234567@',
html_url: 'https://github.com/Codertocat/Hello-World/pull/2',
body:
'some description\n\ndescription with workitem @W-7654321@\n\nmore description'
}
}
};

const reqWithoutWorkItem = {
body: {
pull_request: {
title: 'pull request title with no workitemId',
html_url: 'https://github.com/Codertocat/Hello-World/pull/2',
body: ''
}
}
};

describe('createChatterComment action', () => {
it('should call Issue.getName and Gus.createComment with workitem from title', async () => {
Gus.getWorkItemIdByName.mockReturnValue('a071234');
await fn(req);
expect(Gus.getWorkItemIdByName).toHaveBeenCalledWith('W-1234567');

expect(Gus.createComment).toHaveBeenCalledWith(
'A Pull Request is now open for this work item https://github.com/Codertocat/Hello-World/pull/2',
'a071234'
);
});

it('should not create work item when work item not in title', async () => {
Gus.getWorkItemIdByName.mockReset();
Gus.getWorkItemIdByName.mockReturnValue('a071234');
await fn(reqWithoutWorkItem);
expect(Gus.getWorkItemIdByName).toHaveBeenCalledTimes(0);
});
});
32 changes: 32 additions & 0 deletions api/actions/createComment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright (c) 2020, salesforce.com, inc.
* All rights reserved.
* SPDX-License-Identifier: BSD-3-Clause
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/

const Gus = require('../services/Gus');
const GithubEvents = require('../modules/GithubEvents');

module.exports = {
eventName: GithubEvents.events.PULL_REQUEST_OPENED,
fn: async function(req) {
const {
pull_request: { title, html_url: pr_url, body }
} = req.body;
const workItemInTitleOrBody = title
.concat(body)
.match('@[Ww]-\\d{6,8}@');
if (workItemInTitleOrBody) {
const workItemName = workItemInTitleOrBody[0].substring(
1,
workItemInTitleOrBody[0].length - 1
);
const issueId = await Gus.getWorkItemIdByName(workItemName);
Gus.createComment(
'A Pull Request is now open for this work item '.concat(pr_url),
issueId
);
}
}
};
7 changes: 6 additions & 1 deletion api/modules/GithubEvents/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ const events = {
ISSUE_CLOSED: 'ISSUE_CLOSED',
ISSUE_EDITED: 'ISSUE_EDITED',
ISSUE_OPENED: 'ISSUE_OPENED',
PULL_REQUEST_CLOSED: 'PULL_REQUEST_CLOSED'
PULL_REQUEST_CLOSED: 'PULL_REQUEST_CLOSED',
PULL_REQUEST_OPENED: 'PULL_REQUEST_OPENED'
};

const eventsConfig = {
Expand Down Expand Up @@ -51,6 +52,10 @@ const eventsConfig = {
[events.PULL_REQUEST_CLOSED]: {
event: 'pull_request',
action: 'closed'
},
[events.PULL_REQUEST_OPENED]: {
event: 'pull_request',
action: 'opened'
}
};

Expand Down
29 changes: 29 additions & 0 deletions api/services/Gus/createComment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright (c) 2021, salesforce.com, inc.
* All rights reserved.
* SPDX-License-Identifier: BSD-3-Clause
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/

const jsforce = require('jsforce');

module.exports = function createComment(comment, issueId) {
const conn = new jsforce.Connection();
conn.login(process.env.GUS_USERNAME, process.env.GUS_PASSWORD, err => {
if (err) {
return console.error(err);
}
conn.sobject('FeedItem').create(
{
Body: comment,
ParentId: issueId
},
(err, ret) => {
if (err || !ret.success) {
return console.error(err, ret);
}
return ret.id;
}
);
});
};
2 changes: 2 additions & 0 deletions api/services/Gus/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

const createChangelistInGus = require('./createChangelistInGus');
const createComment = require('./createComment');
const getWorkItemIdByName = require('./getWorkItemIdByName');
const resolveBuild = require('./resolveBuild');
const createWorkItemInGus = require('./createWorkItemInGus');
Expand All @@ -16,6 +17,7 @@ const getBugRecordTypeId = require('./getBugRecordTypeId');

module.exports = {
createChangelistInGus,
createComment,
getWorkItemIdByName,
resolveBuild,
createWorkItemInGus,
Expand Down

0 comments on commit 51bcfc6

Please sign in to comment.