How to deal with late required status checks?

I maintain a bot which manages merges on the behalf of developers. The bot will attempt to merge the PR once all checks and statuses are passing. The problem is that sometimes their conclusions might arrive late, therefore relying on the current statuses available through the “List commit statuses for a reference” or “List check runs for a check suite” APIs, although correct, might fail because some required status has not yet been delivered.

My first thought was to fetch all required checks’ names from the Github API and verify that they’ve all concluded before attempting the merge, although to my knowledge no such endpoint exists, which is why I’ve opted for the following “trial-and-error” approach: ignore the merge failure “Merge cannot be performed” (status code 405) if it fails because some required status is missing. So far I have found two of such cases:

For detecting those scenarios, it doesn’t feel good to rely on string matching on the error message e.g. message.starts_with("Required status check") && message.ends_with("expected.") since Github might change the contents arbitrarily. What do you think of providing a stable error status code together with the message? Example:

{
   "error": 3345,
   "message": "Required status check foo is expected."
}

or { "error": "MISSING_REQUIRED_STATUS" }, or whatever. The encoding doesn’t matter as long as the way to detect this scenario is reliable. That would allow Github to change the messages at any point in time without breaking consumers’ apps.

It’s also possible that I’m misusing those APIs, in which case I welcome suggestions for how to better work around those scenarios. I don’t know, maybe there’s an event which tells when a merge request is mergeable? That would be handy.

I found out the required status checks can be queried ahead-of-time: https://docs.github.com/en/rest/reference/repos#get-status-checks-protection