How to POST a note to an Issue and close this issue

Viewed 1036

I found an issue and get it's details with:

GET https://localhost/api/v4/projects/2779/issues/2

I can add a new_comment to the issue by:

POST https://localhost/api/v4/projects/2779/issues/2/notes?body=new_comment

how to close this issue at once with adding comment?

I've tried only to close without a comment by:

PUT https://localhost/api/v4/projects/2779/issues/2?state_event=close" and it works but I have to use to request first POST to comment and then PUT to close it.

2 Answers

with adding comment

The Issue "Closing issues automatically" section mentions:

If a commit message or merge request description contains text matching a defined pattern, all issues referenced in the matched text are closed.
This happens when the commit is pushed to a project’s default branch, or when a commit or merge request is merged into it.

For example, if Closes #4, #6, Related to #5 is included in a Merge Request description, issues #4 and #6 are closed automatically when the MR is merged, but not #5.

So "comments" are related to commits or MR (Merge Requests), not comment on the issue itself.

The Edit issue API proposes the following call to close an issue:

curl --request PUT --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.example.com/api/v4/projects/4/issues/85?state_event=close"

That should be pretty similar to what you have tried, except you need the PAT (Personal Access Token) with the right scope (api).

As the OP found out, the PUT is effective only after posting a comment on said issue: Create new issue note

POST /projects/:id/issues/:issue_iid/notes

You can add \n/close to the end of your comment to close the issue with a single API call while adding a comment.

POST https://localhost/api/v4/projects/2779/issues/2/notes?body=new_comment\n/close

This works because /close is one of the quick actions.

Related