Skip to content

Commit

Permalink
feat(2695): Does not overwrite PR comment that from other PR job (#54)
Browse files Browse the repository at this point in the history
  • Loading branch information
itleigns authored Jun 10, 2022
1 parent d171774 commit 7367004
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 4 deletions.
9 changes: 7 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const Joi = require('joi');
const Path = require('path');
const Schema = require('screwdriver-data-schema');
const CHECKOUT_URL_REGEX = Schema.config.regex.CHECKOUT_URL;
const PR_COMMENTS_REGEX = /^.+pipelines\/(\d+)\/builds.+ ([\w-:]+)$/;
const Scm = require('screwdriver-scm-base');
const logger = require('screwdriver-logger');
const request = require('screwdriver-request');
Expand Down Expand Up @@ -895,14 +896,18 @@ class GitlabScm extends Scm {
* @param {String} config.scmUri The scmUri to get commit sha of
* @return {Promise}
*/
async _addPrComment({ comment, prNum, scmUri }) {
async _addPrComment({ comment, jobName, prNum, scmUri, pipelineId }) {
const { repoId } = getScmUriParts(scmUri);

const prComments = await this.prComments(repoId, prNum);

if (prComments) {
const botComment = prComments.comments.find(
commentObj => commentObj.author.username === this.config.username
commentObj =>
commentObj.author.username === this.config.username &&
commentObj.body.split(/\n/)[0].match(PR_COMMENTS_REGEX) &&
commentObj.body.split(/\n/)[0].match(PR_COMMENTS_REGEX)[1] === pipelineId.toString() &&
commentObj.body.split(/\n/)[0].match(PR_COMMENTS_REGEX)[2] === jobName
);

if (botComment) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
"@hapi/hoek": "^9.2.1",
"circuit-fuses": "^4.1.2",
"joi": "^17.6.0",
"screwdriver-data-schema": "^21.22.2",
"screwdriver-data-schema": "^21.24.0",
"screwdriver-logger": "^1.1.0",
"screwdriver-request": "^1.0.2",
"screwdriver-scm-base": "^7.4.0"
Expand Down
48 changes: 47 additions & 1 deletion test/data/gitlab.merge_request.comments.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,54 @@
[
{
"id": 575389132,
"type": null,
"body": "### SD Build [#133652](https://cd.screwdriver.cd/pipelines/123456/builds/133652) Job main-2",
"attachment": null,
"author": {
"id": 8615742,
"name": "sd-buildbot",
"username": "sd-buildbot",
"state": "active",
"avatar_url": "https://assets.gitlab-static.net/uploads/-/system/user/avatar/8615742/avatar.png",
"web_url": "https://gitlab.com/sd-buildbot"
},
"created_at": "2021-05-13T22:55:03.477Z",
"updated_at": "2021-05-13T23:14:33.111Z",
"system": false,
"noteable_id": 21341086,
"noteable_type": "MergeRequest",
"resolvable": false,
"confidential": false,
"noteable_iid": 1,
"commands_changes": {}
},
{
"id": 575335839,
"type": null,
"body": "### SD Build [#133652](https://cd.screwdriver.cd/pipelines/1/builds/133652) Job main",
"attachment": null,
"author": {
"id": 8615742,
"name": "sd-buildbot",
"username": "sd-buildbot",
"state": "active",
"avatar_url": "https://assets.gitlab-static.net/uploads/-/system/user/avatar/8615742/avatar.png",
"web_url": "https://gitlab.com/sd-buildbot"
},
"created_at": "2021-05-13T22:55:03.477Z",
"updated_at": "2021-05-13T23:14:33.111Z",
"system": false,
"noteable_id": 21341086,
"noteable_type": "MergeRequest",
"resolvable": false,
"confidential": false,
"noteable_iid": 1,
"commands_changes": {}
},
{
"id": 575311268,
"type": null,
"body": "**meow** EDIT *markdown*",
"body": "### SD Build [#133652](https://cd.screwdriver.cd/pipelines/123456/builds/133652) Job main",
"attachment": null,
"author": {
"id": 8615742,
Expand Down
10 changes: 10 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -811,6 +811,8 @@ describe('index', function() {
const apiUrl = 'projects/repoId/merge_requests/12345/notes';
const comment = 'this is a merge request comment';
const prNum = 12345;
const jobName = 'main';
const pipelineId = 123456;
const expectedOptions = {
url: `${prefixUrl}/${apiUrl}`,
method: 'POST',
Expand Down Expand Up @@ -844,9 +846,11 @@ describe('index', function() {
scm
.addPrComment({
comment,
jobName,
prNum,
scmUri,
token,
pipelineId,
scmContext
})
.then(result => {
Expand All @@ -865,9 +869,11 @@ describe('index', function() {
return scm
.addPrComment({
comment,
jobName,
prNum,
scmUri,
token,
pipelineId,
scmContext
})
.then(result => {
Expand Down Expand Up @@ -908,9 +914,11 @@ describe('index', function() {
return scm
.addPrComment({
comment,
jobName,
prNum,
scmUri,
token,
pipelineId,
scmContext
})
.then(data => {
Expand All @@ -936,9 +944,11 @@ describe('index', function() {
return scm
.addPrComment({
comment,
jobName,
prNum,
scmUri,
token,
pipelineId,
scmContext
})
.then(data => {
Expand Down

0 comments on commit 7367004

Please sign in to comment.