Resolve review instructions with TemplateProjectResolver

What does this MR do and why?

Duo Code Review custom instructions are read from the instance, group, and project layers. That lookup had its own copy of the logic that risk assessment now gets from Gitlab::Duo::TemplateProjectResolver. The copy had some issues:

  • A project that is also its group's (or the instance's) template project had its instructions sent twice.
  • A YAML mistake in a customer's instructions file was reported to error tracking as an application error.

This MR moves custom instructions to the shared resolver, with a parser for .gitlab/duo/mr-review-instructions.yaml. A file that is not valid YAML, or that has no list of instructions, is skipped and logged as a warning instead of being sent to error tracking. The other layers still apply. Other errors in a file are still sent to error tracking, as before.

The order of group layers is now root group first, the same as risk assessment. Only a top-level group can set a Duo template project, so this does not change what users see.

References

  • Gitlab::Duo::TemplateProjectResolver was added for risk assessment in !271468 (merged).

Screenshots or screen recordings

Not applicable: there is no UI change.

How to set up and validate locally

Use a GDK with GitLab Duo set up for gitlab-duo/test, so that Duo Code Review runs when you add GitLabDuo as a reviewer.

  1. Make sure the gitlab-duo group has no Duo template project: in Settings > General > GitLab Duo features > Customize code review, the Source project is empty.

  2. In gitlab-duo/test, add .gitlab/duo/mr-review-instructions.yaml on the default branch:

    instructions:
      - name: Project Ruby rule
        fileFilters: ["**/*.rb"]
        instructions: Check that every new public Ruby method has a spec.
  3. Open an MR in gitlab-duo/test that adds a Ruby file, for example app/models/ci_check_one.rb with a class that has one public method. Add GitLabDuo as a reviewer.

  4. When the review finishes, open the review session from the MR's "started review session" note. In the session's review context, custom_instructions_summary lists only Project Ruby rule. The review comments for the rule start with According to custom instructions in 'Project Ruby rule'.

  5. Create a project gitlab-duo/review-template with .gitlab/duo/mr-review-instructions.yaml on its default branch:

    instructions:
      - name: Group Ruby rule
        fileFilters: ["**/*.rb"]
        instructions: Check that every new Ruby class has a short class comment.
  6. In gitlab-duo, go to Settings > General > GitLab Duo features > Customize code review, select gitlab-duo/review-template as the Source project, and select Save changes.

  7. Open a new MR in gitlab-duo/test that adds another Ruby file, and add GitLabDuo as a reviewer.

  8. When the review finishes, open its session. custom_instructions_summary lists Group Ruby rule and Project Ruby rule, and the MR has review comments for both rules.

MR acceptance checklist

Evaluate this MR against the MR acceptance checklist. It helps you analyze changes to reduce risks in quality, performance, reliability, security, and maintainability.

Edited by Wanderson Policarpo

Merge request reports

Loading
Loading