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_commitreads the commit the catalogue'sHEADnames 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 atHEADand the record saysrules=unknown. -
The full head commit.
classify_areaskeeps 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=… -->modeistestorlive, from the prefix the action label went on with, so a test-mode rating never reads as a live one.modelisunknownwhere 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.mdhas the format, andspec/mr-risk/fixtures/note.mdis 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
shais the current head; - the labels it names are still on the merge request, because a refusal takes them off and writes no note;
modeislive;- neither
rulesnorflowisunknown.
-
The flow digest.
bin/compilestamps twelve hex digits of SHA-256 intobuild_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 leavesbuild_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 saidrules=unknownuntil the catalogue's next merge. -
A reason stays on one line.
build_notetakes a backslash out of the rater's reason, becausereply_to_discussionturns a backslash before anninto 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_contentswill not overwrite, so a file already in the working directory stands in for what a step meant to write.extract_iidclears what the flow writes before the gate (mr.json,mr_diff.json,rules_commit.json,rules.json) and the gate's owngate.jsonandscopes.txt, whichplan_removalsreads when a run fails before the gate. The gate clearsanswer.txtbeside 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, andflow=its compiled digest.rules=was the commit the catalogue'sHEADnamed when the run read it, soget_commitresolvesHEAD. It returns the commit as JSON text.mode=was added after that run. A local run givesmodel=unknownandworkflow=unknown, because only a CI session sets the variables they come from.
Not verified in a run
AGENT_PLATFORM_MODEL_METADATAandDUO_WORKFLOW_WORKFLOW_IDreaching arun_commandstep 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 showrules=as a full sha.
How to set up and validate locally
make check