Foldered production environments can not be attached to MRs

Problem

When a user executes a deployment job that is related to a particular merge request, GitLab creates a relationship between Deployment entry and Merge Request entry. For example, there is a .gitlab-ci.yml

deploy:
  script: echo
  environment: production
  only: [main]

When an MR has been merged, GitLab parses the commit diffs of Git between the previous deployment and the latest deployment, and if an merge commit is included in the commit history, the Deployment-MergeRequest relationship is established. This information can be fetched via List of merge requests associated with a deployment API.

However, if environments are foldred, the relationship doesn't work, for example:

deploy-web:
  script: echo
  environment: production/web

deploy-api:
  script: echo
  environment: production/api

This is because we don't allow the foldered environments to do so:

https://gitlab.com/gitlab-org/gitlab/-/blob/master/app/services/deployments/link_merge_requests_service.rb#L16-19

    def execute
      # Review apps have the environment type set (e.g. to `review`, though the
      # exact value may differ). We don't want to link merge requests to review
      # app deployments, as this is not useful.
      return if deployment.environment.environment_type

(environment_type refers the directory name of the full name. e.g. review/feature => review. production/api => production)

This is clearly a bug that it's rejecting unintentional environments as well.

Related https://gitlab.com/gitlab-org/gitlab/-/issues/343105 https://gitlab.slack.com/archives/CBZGR90A2/p1633055720211700?thread_ts=1633036546.207700&cid=CBZGR90A2

Proposal

We should check the environment tier instead for the guard clause.

    def execute
      # Review apps have the environment type set (e.g. to `review`, though the
      # exact value may differ). We don't want to link merge requests to review
      # app deployments, as this is not useful.
      return unless deployment.environment.production? || deployment.environment.staging?
Edited by Shinya Maeda