@@ -1143,6 +1143,73 @@ def test_post_comment(self, request):
11431143 "body" : f"<!-- readthedocs-{ another_project .id } -->\n Comment from another project." ,
11441144 }
11451145
1146+ @requests_mock .Mocker (kw = "request" )
1147+ def test_post_comment_update_only (self , request ):
1148+ version = get (
1149+ Version ,
1150+ verbose_name = "1234" ,
1151+ project = self .project ,
1152+ type = EXTERNAL ,
1153+ )
1154+ build = get (
1155+ Build ,
1156+ project = self .project ,
1157+ version = version ,
1158+ )
1159+
1160+ request .post (
1161+ f"{ self .api_url } /app/installations/1111/access_tokens" ,
1162+ json = self ._get_access_token_json (),
1163+ )
1164+ request .get (
1165+ f"{ self .api_url } /repositories/{ self .remote_repository .remote_id } /issues/{ version .verbose_name } " ,
1166+ json = self ._get_pull_request_json (
1167+ number = int (version .verbose_name ),
1168+ repo_full_name = self .remote_repository .full_name ,
1169+ ),
1170+ )
1171+ request .get (
1172+ f"{ self .api_url } /repos/{ self .remote_repository .full_name } /issues/{ version .verbose_name } /comments" ,
1173+ json = [],
1174+ )
1175+ request_post_comment = request .post (
1176+ f"{ self .api_url } /repos/{ self .remote_repository .full_name } /issues/{ version .verbose_name } /comments" ,
1177+ )
1178+
1179+ service = self .installation .service
1180+
1181+ # No comments exist, so it will not create a new one.
1182+ service .post_comment (build , "Comment!" , create_new = False )
1183+ assert not request_post_comment .called
1184+
1185+ request .get (
1186+ f"{ self .api_url } /repos/{ self .remote_repository .full_name } /issues/{ version .verbose_name } /comments" ,
1187+ json = [
1188+ self ._get_comment_json (
1189+ id = 1 ,
1190+ issue_number = int (version .verbose_name ),
1191+ repo_full_name = self .remote_repository .full_name ,
1192+ user = {"login" : f"{ settings .GITHUB_APP_NAME } [bot]" },
1193+ body = f"<!-- readthedocs-{ self .project .id } -->\n Comment!" ,
1194+ ),
1195+ ],
1196+ )
1197+ request_patch_comment = request .patch (
1198+ f"{ self .api_url } /repos/{ self .remote_repository .full_name } /issues/comments/1" ,
1199+ json = {},
1200+ )
1201+
1202+ request_post_comment .reset ()
1203+
1204+ # A comment exists from the bot, so it will update it.
1205+ service .post_comment (build , "Comment!" , create_new = False )
1206+ assert not request_post_comment .called
1207+
1208+ assert request_patch_comment .called
1209+ assert request_patch_comment .last_request .json () == {
1210+ "body" : f"<!-- readthedocs-{ self .project .id } -->\n Comment!" ,
1211+ }
1212+
11461213 def test_integration_attributes (self ):
11471214 assert self .integration .is_active
11481215 assert self .integration .get_absolute_url () == "https://github.com/apps/readthedocs/installations/1111"
0 commit comments