diff --git a/gerritlab/main.py b/gerritlab/main.py index bc0fb15..6d66e11 100644 --- a/gerritlab/main.py +++ b/gerritlab/main.py @@ -181,6 +181,10 @@ def create_merge_requests(repo, remote, local_branch): 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.""" diff --git a/unit_tests/merge_request_test.py b/unit_tests/merge_request_test.py index 0d5243f..8aa7ba4 100755 --- a/unit_tests/merge_request_test.py +++ b/unit_tests/merge_request_test.py @@ -20,11 +20,6 @@ EMAIL = "yaoyuannnn@gmail.com" class MergeRequestTest(unittest.TestCase): - # GitLab seems to need a little bit of time before the commits of the - # MRs get updated, after new commits are pushed to the branches. This is - # the time (in seconds) we'll wait before we validate the MRs. - WAIT_TIME_BEFORE_VALIDATE = 10 - def setUp(self): self._test_project_dir = os.path.join( repo_path, GITLAB_TEST_PROJECT_PATH) @@ -40,7 +35,6 @@ def setUp(self): self._local_branch = LOCAL_BRANCH self._test_repo.git.checkout(self._local_branch) global_vars.ci_mode = True - self._wait_before_validate = True self._mrs = [] def tearDown(self): @@ -116,12 +110,6 @@ def validate_mr(mr, commit, target_branch): self._mrs.append( merge_request.get_merge_request(self._remote, source_branch)) - # Wait some time before the validation, in order for GitLab to update - # the MRs after seeing new commits. - if self._wait_before_validate: - time.sleep(MergeRequestTest.WAIT_TIME_BEFORE_VALIDATE) - self._wait_before_validate = False - for idx, (mr, commit) in enumerate(zip(self._mrs, commits)): validate_mr( mr, commit, global_vars.global_target_branch if idx == 0 else