Commit 36486372 authored by Peter Hutterer's avatar Peter Hutterer
Browse files

ci-fairy: check (and ignore) merged MRs when checking merged requests



In the pipeline run after merging a branch, the MR itself is already in
state "merged". No point in checking things on the MR itself then (at least
not with the current checks), so just print a message and exit happily.

Since we expect a project to have a large number of merged requests we sort
those by updated_at, in the hope that our MR is somewhere at the top of the
list.
Signed-off-by: Peter Hutterer's avatarPeter Hutterer <peter.hutterer@who-t.net>
parent 88637d42
......@@ -598,6 +598,15 @@ def check_merge_request(ctx, project, merge_request_iid, junit_xml, require_allo
logger.error('Cannot find git sha, unable to search for merge request')
mr = next(iter([m for m in p.mergerequests.list(state='opened', per_page=100) if m.sha == sha]), None)
# No open MR with our sha? In the pipeline run after merging
# the MR is already in 'merged' state - too late for our checks.
if not mr:
merged = [m for m in p.mergerequests.list(state='merged', order_by='updated_at', per_page=100) if m.sha == sha]
mr = next(iter(merged), None)
if mr:
logger.info(f'Merge request !{mr.id} is already merged, skipping checks')
sys.exit(0)
if not mr:
# Not having a merge request *may* be fine so let's use a
# special exit code.
......
......@@ -70,7 +70,8 @@ def mock_gitlab(gitlab):
def mrget(mid):
return [m for m in mrs if m.id == int(mid)][0]
def mrlist(state=None, per_page=None):
def mrlist(state=None, per_page=None, order_by='created_at'):
# order_by is ignored for our tests
return [m for m in mrs if state == None or m.state == state]
project.mergerequests.list = MagicMock(side_effect=mrlist)
......@@ -750,3 +751,26 @@ def test_merge_request_fdo_upstream_repo(gitlab, caplog, gitlab_default_env):
runner = CliRunner(env=env)
result = runner.invoke(ci_fairy.ci_fairy, args)
assert result.exit_code == 0
@patch('ci_fairy.Gitlab')
def test_merge_request_already_merged(gitlab, caplog, gitlab_default_env):
args = ['-vv', 'check-merge-request', '--require-allow-collaboration']
env = gitlab_default_env
env['FDO_UPSTREAM_REPO'] = GITLAB_TEST_PROJECT_ID
env['CI_PROJECT_ID'] = '9999'
env['CI_COMMIT_SHA'] = 'dead00000002' # MRs 2 and 3 are 'merged'
# mock_gitlab sets allow_collaboration off for every second MR
gitlab, project, _ = mock_gitlab(gitlab)
runner = CliRunner(env=env)
result = runner.invoke(ci_fairy.ci_fairy, args)
assert result.exit_code == 0
assert 'Merge request !2 is already merged, skipping checks' in caplog.text
env['CI_COMMIT_SHA'] = 'dead00000003'
gitlab, project, _ = mock_gitlab(gitlab)
runner = CliRunner(env=env)
result = runner.invoke(ci_fairy.ci_fairy, args)
assert result.exit_code == 0
assert 'Merge request !3 is already merged, skipping checks' in caplog.text
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment