From a202e0110b0d9c49d1fd8adc02a0d19a6e64dbfd Mon Sep 17 00:00:00 2001 From: Ian Zelikman Date: Tue, 22 Dec 2015 13:36:36 -0500 Subject: [PATCH] Adding Build Keeper plugin. Change-Id: If0b840884186e0e3dc649076a994e99ba38796c7 --- jenkins_jobs/modules/wrappers.py | 73 +++++++++++++++++++ tests/wrappers/fixtures/build-keeper0001.xml | 11 +++ tests/wrappers/fixtures/build-keeper0001.yaml | 5 ++ tests/wrappers/fixtures/build-keeper0002.xml | 10 +++ tests/wrappers/fixtures/build-keeper0002.yaml | 4 + tests/wrappers/fixtures/build-keeper0003.xml | 11 +++ tests/wrappers/fixtures/build-keeper0003.yaml | 4 + tests/wrappers/fixtures/build-keeper0004.xml | 11 +++ tests/wrappers/fixtures/build-keeper0004.yaml | 4 + 9 files changed, 133 insertions(+) create mode 100644 tests/wrappers/fixtures/build-keeper0001.xml create mode 100644 tests/wrappers/fixtures/build-keeper0001.yaml create mode 100644 tests/wrappers/fixtures/build-keeper0002.xml create mode 100644 tests/wrappers/fixtures/build-keeper0002.yaml create mode 100644 tests/wrappers/fixtures/build-keeper0003.xml create mode 100644 tests/wrappers/fixtures/build-keeper0003.yaml create mode 100644 tests/wrappers/fixtures/build-keeper0004.xml create mode 100644 tests/wrappers/fixtures/build-keeper0004.yaml diff --git a/jenkins_jobs/modules/wrappers.py b/jenkins_jobs/modules/wrappers.py index 6d12000d3..c0536a78b 100644 --- a/jenkins_jobs/modules/wrappers.py +++ b/jenkins_jobs/modules/wrappers.py @@ -331,6 +331,79 @@ def ansicolor(parser, xml_parent, data): XML.SubElement(cwrapper, 'colorMapName').text = colormap +def build_keeper(parser, xml_parent, data): + """yaml: build-keeper + Keep builds based on specific policy. + Requires the Jenkins :jenkins-wiki:`Build Keeper Plugin + `. + + :arg str policy: Policy to keep builds. + + :policy values: + * **by-day** + * **keep-since** + * **build-number** + * **keep-first-failed** + :arg int build-period: Number argument to calculate build to keep, + depends on the policy. (default 0) + :arg bool dont-keep-failed: Flag to indicate if to keep failed builds. + (default False) + :arg int number-of-fails: number of consecutive failed builds in order + to mark first as keep forever, only applies to keep-first-failed + policy (default 0) + + Example: + + .. literalinclude:: /../../tests/wrappers/fixtures/build-keeper0001.yaml + + .. literalinclude:: /../../tests/wrappers/fixtures/build-keeper0002.yaml + + """ + + root = XML.SubElement(xml_parent, + 'org.jenkins__ci.plugins.build__keeper.BuildKeeper') + + valid_policies = ('by-day', 'keep-since', 'build-number', + 'keep-first-failed') + policy = data.get('policy') + build_period = str(data.get('build-period', 0)) + dont_keep_failed = str(data.get('dont-keep-failed', False)).lower() + + if policy == 'by-day': + policy_element = XML.SubElement(root, + 'policy', + {'class': 'org.jenkins_ci.plugins.' + 'build_keeper.ByDayPolicy'}) + XML.SubElement(policy_element, 'buildPeriod').text = build_period + XML.SubElement(policy_element, + 'dontKeepFailed').text = dont_keep_failed + elif policy == 'keep-since': + policy_element = XML.SubElement(root, + 'policy', + {'class': 'org.jenkins_ci.plugins.' + 'build_keeper.KeepSincePolicy'}) + XML.SubElement(policy_element, 'buildPeriod').text = build_period + XML.SubElement(policy_element, + 'dontKeepFailed').text = dont_keep_failed + elif policy == 'build-number': + policy_element = XML.SubElement(root, + 'policy', + {'class': 'org.jenkins_ci.plugins.' + 'build_keeper.BuildNumberPolicy'}) + XML.SubElement(policy_element, 'buildPeriod').text = build_period + XML.SubElement(policy_element, + 'dontKeepFailed').text = dont_keep_failed + elif policy == 'keep-first-failed': + policy_element = XML.SubElement(root, + 'policy', + {'class': 'org.jenkins_ci.plugins.' + 'build_keeper.KeepFirstFailedPolicy'}) + XML.SubElement(policy_element, 'numberOfFails').text = str( + data.get('number-of-fails', 0)) + else: + InvalidAttributeError('policy', policy, valid_policies) + + def live_screenshot(parser, xml_parent, data): """yaml: live-screenshot Show live screenshots of running jobs in the job list. diff --git a/tests/wrappers/fixtures/build-keeper0001.xml b/tests/wrappers/fixtures/build-keeper0001.xml new file mode 100644 index 000000000..df0c5c2af --- /dev/null +++ b/tests/wrappers/fixtures/build-keeper0001.xml @@ -0,0 +1,11 @@ + + + + + + 10 + true + + + + diff --git a/tests/wrappers/fixtures/build-keeper0001.yaml b/tests/wrappers/fixtures/build-keeper0001.yaml new file mode 100644 index 000000000..2acb434ee --- /dev/null +++ b/tests/wrappers/fixtures/build-keeper0001.yaml @@ -0,0 +1,5 @@ +wrappers: + - build-keeper: + policy: 'by-day' + build-period: 10 + dont-keep-failed: true diff --git a/tests/wrappers/fixtures/build-keeper0002.xml b/tests/wrappers/fixtures/build-keeper0002.xml new file mode 100644 index 000000000..edd33e2f5 --- /dev/null +++ b/tests/wrappers/fixtures/build-keeper0002.xml @@ -0,0 +1,10 @@ + + + + + + 1 + + + + diff --git a/tests/wrappers/fixtures/build-keeper0002.yaml b/tests/wrappers/fixtures/build-keeper0002.yaml new file mode 100644 index 000000000..cab0f59c3 --- /dev/null +++ b/tests/wrappers/fixtures/build-keeper0002.yaml @@ -0,0 +1,4 @@ +wrappers: + - build-keeper: + policy: 'keep-first-failed' + number-of-fails: 1 diff --git a/tests/wrappers/fixtures/build-keeper0003.xml b/tests/wrappers/fixtures/build-keeper0003.xml new file mode 100644 index 000000000..679569d2a --- /dev/null +++ b/tests/wrappers/fixtures/build-keeper0003.xml @@ -0,0 +1,11 @@ + + + + + + 1 + false + + + + diff --git a/tests/wrappers/fixtures/build-keeper0003.yaml b/tests/wrappers/fixtures/build-keeper0003.yaml new file mode 100644 index 000000000..d274d5308 --- /dev/null +++ b/tests/wrappers/fixtures/build-keeper0003.yaml @@ -0,0 +1,4 @@ +wrappers: + - build-keeper: + policy: 'keep-since' + build-period: 1 diff --git a/tests/wrappers/fixtures/build-keeper0004.xml b/tests/wrappers/fixtures/build-keeper0004.xml new file mode 100644 index 000000000..ae30e548b --- /dev/null +++ b/tests/wrappers/fixtures/build-keeper0004.xml @@ -0,0 +1,11 @@ + + + + + + 1 + false + + + + diff --git a/tests/wrappers/fixtures/build-keeper0004.yaml b/tests/wrappers/fixtures/build-keeper0004.yaml new file mode 100644 index 000000000..6dff17a16 --- /dev/null +++ b/tests/wrappers/fixtures/build-keeper0004.yaml @@ -0,0 +1,4 @@ +wrappers: + - build-keeper: + policy: 'build-number' + build-period: 1