Skip to content

Commit

Permalink
After pushing, wait for MRs to stabilize
Browse files Browse the repository at this point in the history
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).

Also:
 unit_tests/merge_request_test.py:
  MergeRequestTest:
   Removed WAIT_TIME_BEFORE_VALIDATE, etc since create_merge_requests()
   now waits for the desired state before returning.

Change-Id: Id20d9feb27076afe0603bee334badc815e5528a9
  • Loading branch information
Ahmon Dancy authored and dancysoft committed Aug 30, 2023
1 parent 9adada3 commit 3714f22
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 12 deletions.
4 changes: 4 additions & 0 deletions gerritlab/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
22 changes: 22 additions & 0 deletions gerritlab/merge_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down Expand Up @@ -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."""
Expand Down
12 changes: 0 additions & 12 deletions unit_tests/merge_request_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,6 @@
EMAIL = "[email protected]"

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)
Expand All @@ -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):
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 3714f22

Please sign in to comment.