Deleting comment on pull-request fails
Asked by BRIAN CARLSON:

I am using a Jenkins groovy script using Curl commands to interact with Stash/Bitbucket.

I can successfully grab all the activity data as a JSON object using the /rest/api/1.0/projects/{projectKey}/repos/{repositorySlug}/pull-requests/{pullRequestId}/activities

as recommended here:

<https://community.atlassian.com/t5/Answers-Developer-Questions/Reading-pull-request-comments-via-REST-API-fails/qaq-p/523952>

```
{"size":25, "limit":25, "isLastPage":false, "values": [{"id":185562, LOTS OF INFO},{"id": #### .....}}
```

If I iterate over the values and filter on objects containing "comment" I can parse the following info.  

```
comment_obj_keys = [id, createdDate, user, action, commentAction, comment]
comment_obj_keys.getClass() = class java.util.LinkedHashSet

if comment_obj_keys.contains("comment")

id = 185562
createDate = null
user = [REMOVED]
action = COMMENTED
commentAction = ADDED
comment = [ properties:[repositoryId:1794], 
            id:16564, version:0, 
            text:Meaninful results of Jenkinsbuild, 
            author:[REMOVED], 
            createdDate:1659127730708,
            updatedDate:1659127730708, 
            comments:[], 
            tasks:[], 
            severity:NORMAL, 
            state:OPEN, 
            permittedOperations:[editable:true, transitionable:true, deletable:true]]
```

According to everything I've read. I should be able to use the id=**166564** to access the comment with the goal of ultimately deleting it. If I put the url in my brower I can see the comment.  
/rest/api/1.0/projects/{projectKey}/repos/{repositorySlug}/pull-requests/{pullRequestId}/comments/**16564**  

```
{"properties":{"repositoryId":1794},
 "id":16564,
 "version":0,
 "text":"Meaninful results of Jenkins build",
 "author":REMOVED,
 "createdDate":1659071192164,
 "updatedDate":1659071192164,
 "comments":[],
 "tasks":[],
 "severity":"NORMAL",
 "state":"OPEN",
 "permittedOperations":{"editable":false,
                        "transitionable":true,
                        "deletable":false}}
```

FYI I'm making (and trying to delete) the comment from Jenkins with the same bitbucket token. And I'm going to the link for the above results  
However, when I try to delete the comment (projectKey, repositorySlug, and pullRequestId are replaced with the actual values):  
curlCmd = "curl -s --insecure -X DELETE -H 'Authorization: REMOVED TOKEN INFO' -H 'X-Atlassian-Token: no-check' https:///rest/api/1.0/projects/{projectKey}/repos/{repositorySlug}/pull-requests/{pullRequestId}/comments/16564"  
**{"errors":\[{"context":null,"message":"You are attempting to modify a comment based on out-of-date information.", "exceptionName":"com.atlassian.bitbucket.comment.CommentOutOfDateException","currentVersion":0,"expectedVersion":-1}\]}**  
adding: comments/16564?0 yields the same result  
adding: comments/16564?-1 yields the same result  
adding: comments/16564/?-1 yields the same result  
Any ideas what the issue might be?  
Thanks!


Answers:
author: Michael Moldrup
body: For anyone else looking for the answer for this.

You will need to add "?version=x" to your URL.

So in your case:

https:///rest/api/1.0/projects/{projectKey}/repos/{repositorySlug}/pull-requests/{pullRequestId}/comments/16564?version=0

(Isn't shown in documentations.)


Tags: bitbucket-server

server

stash