1- import _ , { sortBy , unionBy , zipObject } from "lodash"
1+ import _ , { pull , sortBy , unionBy , zipObject } from "lodash"
22import { err , errAsync , ok , okAsync , Result , ResultAsync } from "neverthrow"
33import { Op , ModelStatic } from "sequelize"
44import { Sequelize } from "sequelize-typescript"
@@ -1083,28 +1083,47 @@ export default class ReviewRequestService {
10831083 const { siteName, isomerUserId } = sessionData
10841084
10851085 logger . info (
1086- `Creating comment for PR ${ pullRequestNumber } , site: ${ siteName } `
1086+ `Creating comment for PR with pullRequestNumber: ${ pullRequestNumber } , site: ${ siteName } `
10871087 )
1088- // get id of review request
1088+ // NOTE: We need to do this to ensure that users are creating comments
1089+ // for a review request that exists.
1090+ // Without this check, users can ping our backend
1091+ // to create comments for any review request,
1092+ // even if the review request doesn't exist
10891093 const reviewMeta = await this . reviewMeta . findOne ( {
1090- where : { pullRequestNumber } ,
1094+ where : {
1095+ pullRequestNumber,
1096+ reviewLink : {
1097+ // NOTE: The `/${requestId}/` that the frontend
1098+ // sends across is not actually the `reviewId`.
1099+ // This is actually the GITHUB `pullRequestNumber`
1100+ // and hence, we have to artifically construct
1101+ // the `reviewLink`.
1102+ // We need the starting `/` to prevent cases where `siteName`
1103+ // is potentially a substring of another site (eg: `moe-ny` and `moe-moe-ny`)
1104+ [ Op . endsWith ] : `/${ siteName } /review/${ pullRequestNumber } ` ,
1105+ } ,
1106+ } ,
10911107 } )
10921108
10931109 if ( reviewMeta ?. reviewId ) {
10941110 try {
10951111 return await this . reviewCommentService . createCommentForReviewRequest (
1096- reviewMeta ? .reviewId ,
1112+ reviewMeta . reviewId ,
10971113 isomerUserId ,
10981114 message
10991115 )
11001116 } catch ( e ) {
11011117 logger . error (
1102- `Error creating comment in DB for PR ${ pullRequestNumber } , site: ${ siteName } `
1118+ `Error creating comment in DB for PR ${ reviewMeta . reviewId } , site: ${ siteName } `
11031119 )
11041120 throw new DatabaseError ( "Error creating comment in DB" )
11051121 }
11061122 }
1107- logger . info ( `No review request found for PR ${ pullRequestNumber } ` )
1123+
1124+ logger . info (
1125+ `No review request found for PR with reviewId: ${ reviewMeta ?. reviewId } , pullRequestNumber: ${ pullRequestNumber } , site: ${ siteName } `
1126+ )
11081127 throw new RequestNotFoundError ( "Review Request not found" )
11091128 }
11101129
@@ -1115,17 +1134,33 @@ export default class ReviewRequestService {
11151134 ) : Promise < CommentItem [ ] > => {
11161135 const { siteName, isomerUserId : userId } = sessionData
11171136
1118- // get review request id
1137+ // NOTE: We need to do this to ensure that users are creating comments
1138+ // for a review request that exists.
1139+ // Without this check, users can ping our backend
1140+ // to create comments for any review request,
1141+ // even if the review request doesn't exist
11191142 const reviewMeta = await this . reviewMeta . findOne ( {
1120- where : { pullRequestNumber } ,
1143+ where : {
1144+ pullRequestNumber,
1145+ // NOTE: The `/${requestId}/` that the frontend
1146+ // sends across is not actually the `reviewId`.
1147+ // This is actually the GITHUB `pullRequestNumber`
1148+ // and hence, we have to artifically construct
1149+ // the `reviewLink`.
1150+ // We need the starting `/` to prevent cases where `siteName`
1151+ // is potentially a substring of another site (eg: `moe-ny` and `moe-moe-ny`)
1152+ reviewLink : {
1153+ [ Op . endsWith ] : `/${ siteName } /review/${ pullRequestNumber } ` ,
1154+ } ,
1155+ } ,
11211156 } )
11221157 if ( ! reviewMeta || ! reviewMeta . reviewId ) {
11231158 throw new RequestNotFoundError ( "Review Request not found" )
11241159 }
11251160
11261161 const comments = await this . apiService . getComments (
11271162 siteName ,
1128- pullRequestNumber
1163+ reviewMeta . pullRequestNumber
11291164 )
11301165
11311166 const requestsView = await this . reviewRequestView . findOne ( {
0 commit comments