From 7ee3c1631d29df5743808ecbbda5423e857ac861 Mon Sep 17 00:00:00 2001 From: Ahmon Dancy Date: Tue, 29 Aug 2023 15:25:18 -0700 Subject: [PATCH] After pushing, wait for MRs to stabilize By stabililze we mean that the "changes_count" field of the MR has a value of "1". This ensures that by the time the "git lab" command terminates, MRs should be fully up-to-date. Note that this approach will have to be reevaluated if #16 is implemented (which would allow multiple commits to end up in a single MR). Change-Id: Id20d9feb27076afe0603bee334badc815e5528a9 --- gerritlab/main.py | 4 ++++ gerritlab/merge_request.py | 22 ++++++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/gerritlab/main.py b/gerritlab/main.py index 42e4f83..9de144f 100644 --- a/gerritlab/main.py +++ b/gerritlab/main.py @@ -173,6 +173,10 @@ def cancel_prev_pipelines(commit): with timing("push"): remote.push(refspec=refs_to_push, force=True) + with timing("stabilize"): + for mr in new_mrs+updated_mrs: + mr.wait_until_stable() + if len(updated_mrs) == 0 and len(new_mrs) == 0: print() warn("No updated/new MRs.\n") diff --git a/gerritlab/merge_request.py b/gerritlab/merge_request.py index 3a5a0aa..b8c797c 100644 --- a/gerritlab/merge_request.py +++ b/gerritlab/merge_request.py @@ -33,6 +33,9 @@ def __init__( self._web_url = None self._mergeable = False + self._set_data(json_data) + + def _set_data(self, json_data): if json_data is not None: for attr in json_data: setattr(self, "_{}".format(attr), json_data[attr]) @@ -157,6 +160,25 @@ def needs_update(self, commit) -> bool: self._title != title or self._description != desc.strip()) + def refresh(self): + """ + Update's this object's data using the latest info available from the server. + """ + r = global_vars.session.get("{}/{}".format(global_vars.mr_url, self._iid)) + r.raise_for_status() + self._set_data(r.json()) + + def wait_until_stable(self): + """ + Poll GitLab until the changes_count field has a value of "1". + Other values indicate that GitLab hasn't reacted to a push yet. + """ + while True: + self.refresh() + if self._changes_count == "1": + return + time.sleep(0.500) + def _get_open_merge_requests(): """Gets all open merge requests in the GitLab repo."""