How to get merge requests that mention given issue with gitlab API

Simple request to http://gitlab/api/v4/projects/XXX/issues/YYYYY

returns a bunch of attributes with "merge_requests_count": Z, field describing number of related merge requests

How I can get these merge requests using API?

More over, I try to find a way to get environment URL from a pipeline that was triggered by merge request mentioned in a given issue.

Any help?

Hi @molostoff

You need to make another API call to find the list of MRs related to the issue.

For your second question, if you are writing something to run from inside your .gitlab-ci.yml file, if a job has an environment configured, the URL should be in the predefined variable $CI_ENVIRONMENT_URL.

If you are trying to find the environment from the API, outside the pipeline itself, then I’m not so sure, but I think you need to do this:

  1. Get the MR from the API, there is some pipeline information in the JSON responses
  2. Using the pipeline ID from the previous step, use the jobs API to get all jobs from the pipeline
  3. Iterate over the jobs, and see if one of them has an environment, in which case you need its ID
  4. Then use the environment API to find the URL of the environment with the ID from the previous step (see external_url).

I haven’t tried this, so I may have missed something somewhere, but hopefully that gives you a starting point.


Thanks for the first link, but for some reason this does not work on 13.9 for any merge request even if merge_requests_count greater than 0, some MRs return a list, some - empty (but in web gui MRs are indeed referencing their issues), and I can not understand why.

The problem to me is that http://gitlab/api/v4/projects/XX/pipelines/YYYY/jobs request (step 3 in your suggestion) does not return any refs/ids to the environment while this one exists and active and referenced in MR web gui.


I’ve had a got at this now, and yes, the environment IDs are not in the jobs response. The problem is that the environments also don’t have any details about the last pipeline or job that deployed to them, or even the timestamp of the last deployment.

However, I found there is a deployments API and this does have a pipeline IID and the environment information, so perhaps you can work backwards from that?

Wow, Thanks for the right link - now I can get/filter what I need

Best regargs, Vasq

1 Like