如何通过API(HTTP/REST或SSH)丢弃Gerrit特定评审的草稿评论?
Alright, let’s walk through exactly how to discard draft review comments in Gerrit using either the HTTP REST API or SSH API—no web interface clicks needed, just clean command-line or scriptable calls.
First, you’ll need to get the ID of the draft comment you want to delete. Gerrit’s REST API makes this straightforward:
Fetch all draft comments for a specific change
Send aGETrequest to the draft comments endpoint for your target change. Replace{change-id}with either the numeric change ID, change number, or full reference (likerefs/changes/45/12345/3).curl -u your-username:your-auth-token https://your-gerrit-domain.com/a/changes/{change-id}/draft_comments- Note: Use an HTTP auth token instead of your password if you have 2FA enabled—you can generate one in Gerrit’s settings under HTTP Credentials.
- The response will be a JSON array of draft comments, each with an
idfield (this is the value you’ll need for deletion).
Delete the specific draft comment
Use aDELETErequest targeting the draft comment’s ID. Replace{draft-comment-id}with the ID you pulled from the previous step:curl -X DELETE -u your-username:your-auth-token https://your-gerrit-domain.com/a/changes/{change-id}/draft_comments/{draft-comment-id}A successful deletion will return a
204 No Contentstatus code.
If you prefer using Gerrit’s SSH interface (which is often more straightforward for command-line workflows), here’s how to do it:
List draft comments for the change
Connect to your Gerrit instance via SSH and run thedraft-comments listcommand with your target change ID:ssh -p 29418 your-username@your-gerrit-domain.com gerrit draft-comments list {change-id}This will output a list of draft comments with their IDs, along with details like the file path and comment text.
Delete the draft comment
Use thedraft-comments deletecommand with both the change ID and draft comment ID:ssh -p 29418 your-username@your-gerrit-domain.com gerrit draft-comments delete {change-id} {draft-comment-id}You’ll get a confirmation message like
Deleted draft comment {draft-comment-id} from change {change-id}if the operation succeeds.
Quick Notes
- Make sure you have the necessary permissions: you can only delete draft comments that you created, or if you have administrative privileges for the change.
- For both APIs,
{change-id}can be any valid identifier for the review (numeric ID, change number, or full ref path—Gerrit will resolve it automatically).
内容的提问来源于stack exchange,提问作者sorin

