How can I update/re-use a comment posted via API from pipelines?

,

I have a CI job that computes some information based on previous jobs (comparison of artifact sizes). It’s not something I could gate the whole pipeline on, because it’s more nuanced that just pass/fail. So my thought was to do something similar to what Codecov or other services do, and render a report to post as a comment on the MR that triggered the pipeline. This is possible via the “notes” API.

The problem is, I don’t want to have dozens of such comments dumped in the MR. I want to do what Codecov does, where they post a comment and then just update it when the pipeline runs again.

I can’t figure out a “lightweight” way to do this though. I assume that Codecov achieves this by storing the note ID in their infrastructure and using the modify/PUT method when the comment already exists. But I am not running a whole other service, I’m just trying to write a simple, self-contained CI job.

The only other approach I can think of is to add some very specific content to the comment that I can detect on subsequent runs ie. I use the list API to list all the comments, parse each one for the marker content, and update that one. That seems a bit fragile though, because there’s pagination to consider, and it just seems a bit overcomplicated and wasteful. (I’m leaving aside the fact that the special comment could be edited to remove the marker content, or the marker could be added to an ordinary comment, because someone would have to do that on purpose, and the worst that happens is duplication or removal of a comment. But it’s also possible.)

My basic questions are:

  1. Is there a way to store the comment ID for future runs of a pipeline, scoped to a given MR?
  2. Is there another approach to this that I haven’t thought of?

An idea: a colleague of mine needed something similar, and they ended up storing the data they wanted at the end of the merge request description, hidden inside an HTML comment.

<!-- like so -->

That’s actually amazing. I was also considering using the variables API. Unfortunately both posting MR comments and using CI variables requires a project access token, which is possible but extremely annoying (you can’t use them on the free tier, which is fine for me because they’re open source projects with the free upgrade, but they also require regenerating every year, which is yet more admin just to post metrics).