Add a parser for customer-defined MR risk assessment areas
What does this MR do and why?
Customers will define their own MR risk areas (gitlab-org#24660) in .gitlab/duo/mr-risk-rules.yaml. Each area has a name, the paths it covers, a rubric, and a floor. This MR adds Gitlab::Duo::RiskAssessment::AreasParser, which reads those areas for a project.
The rules file can be in the instance Duo template project, in the Duo template projects of the project's groups, and in the project itself. The parser reads every layer and combines the areas:
- Areas with the same name and the same paths (in any order) are the same area. When several layers define it, each definition stays separate with its own rubric, but all of them use the highest floor among them. A project cannot lower the floor of an area that its group defines.
- An invalid area (for example, one with no rubric) is kept with its errors, so the rating can skip and flag it. The other areas still apply.
- A rules file that cannot be parsed (for example, invalid YAML or
version: 2) gives no areas. The file is still returned, with its error, so the rating can report it. The other layers still apply. - Paths are glob patterns, matched with
File.fnmatch?and theFNM_PATHNAME,FNM_EXTGLOBandFNM_DOTMATCHflags, so**/*also matches dotfiles. A pattern can have at most 5 brace groups ({a,b}), becauseFile.fnmatch?expands each group and the matching time doubles with each one. An area with a pattern that has more is invalid.
Gitlab::Duo::RiskAssessment::AreasResolver uses the parser for a merge request. It returns:
- The valid areas that match at least one changed file. When the diff is larger than the diff limit, the files past the limit also count. Files that content exclusion hides from Duo do not count.
- Every invalid area, so the rating can flag it.
- The rules file of every layer, including layers with no matching area, so the rating can record which rules it used.
Generic layer lookup
A new generic resolver, Gitlab::Duo::TemplateProjectResolver, reads a file from the instance Duo template project, the Duo template projects of the groups (root group first), and the project. A project that is the template project of more than one layer is read once, in its broadest layer. A parser (a subclass of Gitlab::Duo::TemplateProjectFileParser) says which file to read, how to parse it, and how to combine the items of all layers.
Code review custom instructions (CustomInstructionsResolver) and automated rules (AutomatedRulesResolver) read files from the same layers with their own copies of this logic. Risk areas are the third feature to read these layers, so this MR adds the shared resolver instead of a third copy. It does not change the other two features. They will move to the generic resolver in a follow-up.
References
Screenshots or screen recordings
Not applicable. There are no UI changes.
How to set up and validate locally
Risk assessment areas
Nothing in the product calls the resolver yet, so use a Rails console.
-
Add
.gitlab/duo/mr-risk-rules.yamlto the default branch ofgitlab-duo/test:version: 1 areas: - name: db paths: - "db/**/*" floor: high rubric: | critical: drops a column. high: any other schema change. - name: frontend paths: - "app/assets/**/*" -
Create a merge request in
gitlab-duo/testthat changes a file underdb/and aREADME.md. -
In a Rails console, with the IID of that merge request:
project = Project.find_by_full_path('gitlab-duo/test') merge_request = project.merge_requests.find_by(iid: <iid>) result = Gitlab::Duo::RiskAssessment::AreasResolver.new(project).execute(merge_request) result.areas.map { |area| [area.layer, area.name, area.valid?, area.errors] } # => [[:project, "db", true, []], [:project, "frontend", false, ["'rubric' is missing"]]] # All areas of every layer, without the merge request filter: resolution = Gitlab::Duo::TemplateProjectResolver.new(project).resolve(Gitlab::Duo::RiskAssessment::AreasParser.new) resolution.items.map { |area| [area.layer, area.name, area.valid?, area.errors] } # => [[:project, "db", true, []], [:project, "frontend", false, ["'rubric' is missing"]]] resolution.files.map { |file| [file.layer, file.project.full_path, file.commit_sha, file.error] } # => [[:project, "gitlab-duo/test", "<head commit of the default branch>", nil]] -
Add a rules file to the group's Duo template project with two areas: a
dbarea with the samepathsas the project'sdbarea andfloor: critical, and anapiarea withpaths: ["app/api/**/*"]. Run the console snippet again, and also list the floors:result.areas.map { |area| [area.layer, area.name, area.floor] } # => [[:group, "db", "critical"], [:project, "db", "critical"], [:project, "frontend", nil]]Check that both
dbareas appear, that the project'sdbarea uses the group'scriticalfloor, and thatAreasResolverdoes not returnapi, because the merge request changes no file it matches. -
Add this area to the project's rules file:
- name: braces paths: - "{a,b}{a,b}{a,b}{a,b}{a,b}{a,b}x" rubric: "low: anything."Run the console snippet again. Check that
result.areasincludes[:project, "braces", false, ["'paths' patterns can have at most 5 brace groups"]]. -
Change
version: 1toversion: 2in the project's rules file, and run the console snippet again:resolution.files.map { |file| [file.layer, file.error] } # => [[:group, nil], [:project, "unsupported version: 2"]] result.areas.map { |area| [area.layer, area.name, area.floor] } # => [[:group, "db", "critical"]]Check that the group's areas still apply.
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.