pythongitlabgitlab-apipython-gitlab

Gitlab-Python Api. After creating MR cant get actual MR status info


Im creating MR with python-gitlab. After creation i need to check for mr_conflicts and pipeline state.

project = gl.projects.get(PROJECT_ID, lazy=False)
create_mr = project.mergerequests.create({'id': PROJECT_ID,
                                      'title': 'Testing multiMR',
                                      'source_branch': SOURCE_BRANCH,
                                      'target_branch': TARGET_BRANCH,
                                      'labels': [LABEL]})
mr_iid = getattr(create_mr, 'iid')

time.sleep(3)

get_mr = project.mergerequests.get(mr_iid)
mr_conflict = getattr(get_mr, 'has_conflicts')
mr_pipeline = getattr(get_mr, 'pipeline', 'No pipeline')

But I dont get actual info with this requests! I have answer with data like MR was created just now and has no info about coflicts or pipelines.

Same time if i start another script from different console only for checking MR status i got right actual information.

Another moment. If in the script with MR creation i run cycle

n = 4
while n > 0: 
  
  time.sleep(1)
  n -= 1
  project.refresh()
  get_mr = project.mergerequests.get(mr_iid)
  mr_conflict = get_mr.has_conflicts
  mr_pipeline = getattr(get_mr, 'pipeline', 'No pipeline')

still no updated info.

How can i get actual info about MR inside script?


Solution

  • The attributes of an MR object will not refresh/update themselves. You must request the information with the API again (create a new object)

    my_mr = project.mergerequests.create(...)
    mr_iid = my_mr.iid
    # later
    ...
    # get current data from the server
    mr_info = project.mergerequests.get(mr_iid) 
    print(mr_info.attributes)