Allow subgroups to set a Duo template project

What does this MR do and why?

Only a top-level group can pick the project that holds Duo Code Review custom instructions and exclusion rules. Teams that own a subgroup, but not the top-level group, have to copy the same .gitlab/duo/mr-review-instructions.yaml into every project, and every rule change means editing all of those files.

With this MR, a subgroup Owner can pick a Duo template project too. The project must be in the subgroup or one of its subgroups, so a subgroup cannot pull instructions from a parent or sibling group. Code Review already reads the template project of every parent group, so a subgroup's instructions are added after its parents' and never replace them. Exclusion rules from a subgroup's template project apply the same way.

The epic also proposes a cap on the total size of instructions from all group levels. That cap is not part of this MR.

References

Screenshots or screen recordings

Before After
A subgroup's Settings > General > GitLab Duo features has no Customize code review section subgroup-after

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. In gitlab-duo, create a subgroup review-team with two projects: review-template and app.

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

    instructions:
      - name: Subgroup rule
        fileFilters: ["**/*.rb"]
        instructions: Check that every new Ruby class has a short class comment.

    In app, add the same file with a project rule:

    instructions:
      - name: Project rule
        fileFilters: ["**/*.rb"]
        instructions: Check that every new public Ruby method has a spec.
  3. Go to review-team's Settings > General > GitLab Duo features > Customize code review. The Source project list has only review-team/app and review-team/review-template. Select review-template and select Save changes.

  4. Open an MR in review-team/app that adds a Ruby file, for example app/models/widget.rb with a class that has one public method. Add GitLabDuo as a reviewer.

  5. 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 the instructions of gitlab-duo's template project first (if it has one), then Subgroup rule, then Project rule. The MR has review comments that start with According to custom instructions in 'Subgroup rule' and According to custom instructions in 'Project rule'.

  6. Try to set a project outside the subgroup:

    curl -s -X PUT -H "PRIVATE-TOKEN: $TOKEN" -H 'Content-Type: application/json' -d '{"duo_template_project_id": <gitlab-duo/test id>}' "http://gdk.test:3000/api/v4/groups/<review-team id>"

    It returns 400 with Duo template project does not belong to this group.

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