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:
Get the MR from the API, there is some pipeline information in the JSON responses
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?