From 3030c00ba2116a08c69e88dcc6097b6d90f2ed47 Mon Sep 17 00:00:00 2001 From: Oliver Smith Date: Tue, 5 Mar 2019 08:56:47 +0100 Subject: [PATCH] CI: check_mr_settings.py: fix check for same repo (!255) Do not crash when a MR was made from the same repository, because the "allow_maintainer_to_push" key does not exist in the GitLab API's output. Check first if the "source_project_id" is the same as the "target_project_id", and if it is, do not try to access "allow_maintainer_to_push" at all. --- .gitlab-ci/check_mr_settings.py | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/.gitlab-ci/check_mr_settings.py b/.gitlab-ci/check_mr_settings.py index 445f6d03f..412de33d3 100755 --- a/.gitlab-ci/check_mr_settings.py +++ b/.gitlab-ci/check_mr_settings.py @@ -77,16 +77,27 @@ def get_mr_settings(path): exit(1) -def check_allow_push(settings): - """ :returns: True when maintainers are allowed to push to the branch - (what we want!), False otherwise """ - if "allow_maintainer_to_push" not in settings: - print("ERROR: missing 'allow_maintainer_to_push' key in settings!") +def settings_read(settings, key): + if key not in settings: + print("ERROR: missing '" + key + "' key in settings!") print("Here are the whole settings for debugging:") print("---") print(settings) exit(1) - return settings["allow_maintainer_to_push"] + return settings[key] + + +def check_allow_push(settings): + """ :returns: True when maintainers are allowed to push to the branch + (what we want!), False otherwise """ + + # Check if source branch is in same repository + source = settings_read(settings, "source_project_id") + target = settings_read(settings, "target_project_id") + if source == target: + return True + + return settings_read(settings, "allow_maintainer_to_push") def main():