-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add comment to WI when PR is opened @W-9458251@ (#123)
* 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
Showing
5 changed files
with
128 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
); | ||
} | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters