diff --git a/jenkins_jobs/modules/triggers.py b/jenkins_jobs/modules/triggers.py index 46be24ad7..f72284327 100644 --- a/jenkins_jobs/modules/triggers.py +++ b/jenkins_jobs/modules/triggers.py @@ -818,6 +818,70 @@ def gitlab_merge_request(parser, xml_parent, data): XML.SubElement(ghprb, '__projectPath').text = data.get('project-path') +def gitlab(parser, xml_parent, data): + """yaml: gitlab + Makes Jenkins act like a GitlabCI server + Requires the Jenkins :jenkins-wiki:`Gitlab Plugin. + `. + + :arg bool trigger-push: Build on Push Events (default: true) + :arg bool trigger-merge-request: Build on Merge Request Events (default: + True) + :arg bool trigger-open-merge-request-push: Rebuild open Merge Requests on + Push Events (default: True) + :arg bool ci-skip: Enable [ci-skip] (default True) + :arg bool set-build-description: Set build description to build cause + (eg. Merge request or Git Push ) (default: True) + :arg bool add-note-merge-request: Add note with build status on + merge requests (default: True) + :arg bool add-vote-merge-request: Vote added to note with build status + on merge requests (default: True) + :arg bool allow-all-branches: Allow all branches (Ignoring Filtered + Branches) (default: False) + :arg list include-branches: Defined list of branches to include + (default: []) + :arg list exclude-branches: Defined list of branches to exclude + (default: []) + + Example: + + .. literalinclude:: + /../../tests/triggers/fixtures/gitlab001.yaml + """ + def _add_xml(elem, name, value): + XML.SubElement(elem, name).text = value + + gitlab = XML.SubElement( + xml_parent, 'com.dabsquared.gitlabjenkins.GitLabPushTrigger' + ) + + bool_mapping = ( + ('trigger-push', 'triggerOnPush', True), + ('trigger-merge-request', 'triggerOnMergeRequest', True), + ('trigger-open-merge-request-push', 'triggerOpenMergeRequestOnPush', + True), + ('ci-skip', 'ciSkip', True), + ('set-build-description', 'setBuildDescription', True), + ('add-note-merge-request', 'addNoteOnMergeRequest', True), + ('add-vote-merge-request', 'addVoteOnMergeRequest', True), + ('allow-all-branches', 'allowAllBranches', False), + ) + list_mapping = ( + ('include-branches', 'includeBranchesSpec', []), + ('exclude-branches', 'excludeBranchesSpec', []), + ) + + XML.SubElement(gitlab, 'spec').text = '' + + for yaml_name, xml_name, default_val in bool_mapping: + value = str(data.get(yaml_name, default_val)).lower() + _add_xml(gitlab, xml_name, value) + + for yaml_name, xml_name, default_val in list_mapping: + value = ', '.join(data.get(yaml_name, default_val)) + _add_xml(gitlab, xml_name, value) + + def build_result(parser, xml_parent, data): """yaml: build-result Configure jobB to monitor jobA build result. A build is scheduled if there diff --git a/setup.cfg b/setup.cfg index 61c124423..6c211fa73 100644 --- a/setup.cfg +++ b/setup.cfg @@ -228,6 +228,7 @@ jenkins_jobs.triggers = gerrit=jenkins_jobs.modules.triggers:gerrit github=jenkins_jobs.modules.triggers:github github-pull-request=jenkins_jobs.modules.triggers:github_pull_request + gitlab=jenkins_jobs.modules.triggers:gitlab gitlab-merge-request=jenkins_jobs.modules.triggers:gitlab_merge_request groovy-script=jenkins_jobs.modules.triggers:groovy_script ivy=jenkins_jobs.modules.triggers:ivy diff --git a/tests/triggers/fixtures/gitlab001.xml b/tests/triggers/fixtures/gitlab001.xml new file mode 100644 index 000000000..717eed400 --- /dev/null +++ b/tests/triggers/fixtures/gitlab001.xml @@ -0,0 +1,18 @@ + + + + + + true + true + true + true + true + true + true + true + master, master2, local-test + broken-test, master-foo + + + diff --git a/tests/triggers/fixtures/gitlab001.yaml b/tests/triggers/fixtures/gitlab001.yaml new file mode 100644 index 000000000..9df33b9d6 --- /dev/null +++ b/tests/triggers/fixtures/gitlab001.yaml @@ -0,0 +1,17 @@ +triggers: + - gitlab: + trigger-push: true + trigger-merge-request: true + trigger-open-merge-request-push: true + ci-skip: true + set-build-description: true + add-note-merge-request: true + add-vote-merge-request: true + allow-all-branches: true + include-branches: + - 'master' + - 'master2' + - 'local-test' + exclude-branches: + - 'broken-test' + - 'master-foo' diff --git a/tests/triggers/fixtures/gitlab002.xml b/tests/triggers/fixtures/gitlab002.xml new file mode 100644 index 000000000..bf4502fd7 --- /dev/null +++ b/tests/triggers/fixtures/gitlab002.xml @@ -0,0 +1,18 @@ + + + + + + true + true + true + true + true + true + true + false + master + feature + + + diff --git a/tests/triggers/fixtures/gitlab002.yaml b/tests/triggers/fixtures/gitlab002.yaml new file mode 100644 index 000000000..6da9bdfa0 --- /dev/null +++ b/tests/triggers/fixtures/gitlab002.yaml @@ -0,0 +1,6 @@ +triggers: + - gitlab: + include-branches: + - 'master' + exclude-branches: + - 'feature' \ No newline at end of file diff --git a/tests/triggers/fixtures/gitlab003.xml b/tests/triggers/fixtures/gitlab003.xml new file mode 100644 index 000000000..5988252bb --- /dev/null +++ b/tests/triggers/fixtures/gitlab003.xml @@ -0,0 +1,18 @@ + + + + + + true + true + true + true + true + true + true + false + master + baz, foo, fnord + + + \ No newline at end of file diff --git a/tests/triggers/fixtures/gitlab003.yaml b/tests/triggers/fixtures/gitlab003.yaml new file mode 100644 index 000000000..80ee894bc --- /dev/null +++ b/tests/triggers/fixtures/gitlab003.yaml @@ -0,0 +1,8 @@ +triggers: + - gitlab: + include-branches: + - 'master' + exclude-branches: + - 'baz' + - 'foo' + - 'fnord' \ No newline at end of file