Make the MR Risk Labeler's note the record of its rating

What does this MR do and why?

gitlab-org/gitlab#638502 row 7 lists what each merge request's rating has to show: "rating, reason, commit rated, rules version, model used, bot approval". It adds: "We have all but rules version and model today." This MR adds both, and the full commit rated. It is item 3 of gitlab-org/gitlab#638236.

The last line of the MR Risk Labeler's internal note becomes the record of its rating. triage-ops will key gitlab-bot's approvals on that record, pinned to the commit it rated. So the note has to say what was rated, by what, and whether the labels it names are live.

  • One catalogue commit per run. fetch_rules_commit reads the commit the catalogue's HEAD names before the rules are fetched, and the rules, the rubrics and the upstream check are all read at it. If the commit will not read, they are read at HEAD and the record says rules=unknown.

  • The full head commit. classify_areas keeps all 40 characters. The note still shows 8.

  • The record. The note gains a line naming the diff's commit, the rules' commit, the flow's digest, the model and the session. It ends with a line for a program to read:

    <!-- mr-risk:v1 sha=… risk=… action=… mode=… areas=… rules=… flow=… model=… workflow=… -->
    • mode is test or live, from the prefix the action label went on with, so a test-mode rating never reads as a live one.
    • model is unknown where the variable is missing. GitLab sends no metadata when no model is set, so a step cannot tell the default model from a variable that never reached it.
    • Every value is matched against a pattern before it is written, and a verdict the line cannot state whole gets no line.

    "The record" in docs/specs/mr-risk.md has the format, and spec/mr-risk/fixtures/note.md is a whole note.

  • When a reader acts on a record. "The record" also says when a reader may act on one:

    • it is in an internal note the flow's service account wrote, read as the note was created, because a maintainer can edit the note afterwards;
    • it is in the last such note, and sha is the current head;
    • the labels it names are still on the merge request, because a refusal takes them off and writes no note;
    • mode is live;
    • neither rules nor flow is unknown.
  • The flow digest. bin/compile stamps twelve hex digits of SHA-256 into build_note's command. The hash is over the rendered definition with its scripts inflated, so every machine reaches the same one. A change to a step, a script or a prompt changes it. A comment or an added consumer does not, so onboarding a project leaves build_note's command alone.

  • The catalogue commit's id is matched in get_commit's response rather than parsed. The tool returns the commit as JSON text, and Duo's filters cut that text short at an unclosed HTML comment in the commit's title. It then stopped parsing, and every record said rules=unknown until the catalogue's next merge.

  • A reason stays on one line. build_note takes a backslash out of the rater's reason, because reply_to_discussion turns a backslash before an n into a line break. The spec says a reason is escaped and kept to one line; that now holds for a reply as well as a new note.

  • Cleared files. create_file_with_contents will not overwrite, so a file already in the working directory stands in for what a step meant to write. extract_iid clears what the flow writes before the gate (mr.json, mr_diff.json, rules_commit.json, rules.json) and the gate's own gate.json and scopes.txt, which plan_removals reads when a run fails before the gate. The gate clears answer.txt beside the rubric and upstream responses.

flows/mr-risk.yml has a 40,790-byte definition, 170 under the catalog's limit.

A run on this merge request

A local run of an earlier version of this branch's flow on this merge request (glab duo cli run --flow-config, run by the author) ended its note with a record.

  • sha= was the merge request's head at the time, and flow= its compiled digest.
  • rules= was the commit the catalogue's HEAD named when the run read it, so get_commit resolves HEAD. It returns the commit as JSON text.
  • mode= was added after that run. A local run gives model=unknown and workflow=unknown, because only a CI session sets the variables they come from.

Not verified in a run

  • AGENT_PLATFORM_MODEL_METADATA and DUO_WORKFLOW_WORKFLOW_ID reaching a run_command step in a CI session.
    • The platform's code shows the CI job sets both.
    • Whether the sandboxed executor passes them on to a command is an open question in the spec.
    • The first notes after the tag show both.
  • The record's shape as it is now, the digest of the rendered definition, the id matched in get_commit's text and the added clears. The specs and the golden note cover them, and the first CI note should show rules= as a full sha.

How to set up and validate locally

make check

🤖 Generated with Claude Code

Edited by Connor Sanders

Merge request reports

Loading
Loading