Merge "Add support for URLTrigger plugin"

This commit is contained in:
Jenkins 2014-08-11 21:56:20 +00:00 committed by Gerrit Code Review
commit d81b27120b
4 changed files with 213 additions and 0 deletions

View File

@ -31,6 +31,7 @@ Example::
import xml.etree.ElementTree as XML
from jenkins_jobs.errors import JenkinsJobsException
import jenkins_jobs.modules.base
from jenkins_jobs.modules import hudson_model
import logging
@ -330,6 +331,123 @@ def pollscm(parser, xml_parent, data):
XML.SubElement(scmtrig, 'spec').text = data
def build_pollurl_content_type(xml_parent, entries, prefix,
collection_name, element_name):
namespace = 'org.jenkinsci.plugins.urltrigger.content'
content_type = XML.SubElement(
xml_parent, '{0}.{1}ContentType'.format(namespace, prefix))
if entries:
collection = XML.SubElement(content_type, collection_name)
for entry in entries:
content_entry = XML.SubElement(
collection, '{0}.{1}ContentEntry'.format(namespace, prefix))
XML.SubElement(content_entry, element_name).text = entry
def pollurl(parser, xml_parent, data):
"""yaml: pollurl
Trigger when the HTTP response from a URL changes.
Requires the Jenkins `URLTrigger Plugin.
<https://wiki.jenkins-ci.org/display/JENKINS/URLTrigger+Plugin>`_
:arg string cron: cron syntax of when to run (default '')
:arg string polling-node: Restrict where the polling should run.
(optional)
:arg list urls: List of URLs to monitor
:URL: * **url** (`str`) -- URL to monitor for changes (required)
* **proxy** (`bool`) -- Activate the Jenkins proxy (default false)
* **timeout** (`int`) -- Connect/read timeout in seconds
(default 300)
* **username** (`string`) -- User name for basic authentication
(optional)
* **password** (`string`) -- Password for basic authentication
(optional)
* **check-status** (`int`) -- Check for a specific HTTP status
code (optional)
* **check-etag** (`bool`) -- Check the HTTP ETag for changes
(default false)
* **check-date** (`bool`) -- Check the last modification date of
the URL (default false)
* **check-content** (`list`) -- List of content type changes to
monitor
:Content Type: * **simple** (`bool`) -- Trigger on any change to
the content of the URL (default false)
* **json** (`list`) -- Trigger on any change to
the listed JSON paths
* **text** (`list`) -- Trigger on any change to
the listed regular expressions
* **xml** (`list`) -- Trigger on any change to
the listed XPath expressions
Example:
.. literalinclude:: /../../tests/triggers/fixtures/pollurl001.yaml
"""
valid_content_types = {
'simple': ['Simple', '', '', []],
'json': ['JSON', 'jsonPaths', 'jsonPath', None],
'text': ['TEXT', 'regExElements', 'regEx', None],
'xml': ['XML', 'xPaths', 'xPath', None]
}
urltrig = XML.SubElement(xml_parent,
'org.jenkinsci.plugins.urltrigger.URLTrigger')
node = data.get('polling-node')
XML.SubElement(urltrig, 'spec').text = data.get('cron', '')
XML.SubElement(urltrig, 'labelRestriction').text = str(bool(node)).lower()
if node:
XML.SubElement(urltrig, 'triggerLabel').text = node
entries = XML.SubElement(urltrig, 'entries')
urls = data.get('urls', [])
if not urls:
raise JenkinsJobsException('At least one url must be provided')
for url in urls:
entry = XML.SubElement(entries,
'org.jenkinsci.plugins.urltrigger.'
'URLTriggerEntry')
XML.SubElement(entry, 'url').text = url['url']
XML.SubElement(entry, 'proxyActivated').text = \
str(url.get('proxy', False)).lower()
if 'username' in url:
XML.SubElement(entry, 'username').text = url['username']
if 'password' in url:
XML.SubElement(entry, 'password').text = url['password']
if 'check-status' in url:
XML.SubElement(entry, 'checkStatus').text = 'true'
XML.SubElement(entry, 'statusCode').text = \
str(url.get('check-status'))
else:
XML.SubElement(entry, 'checkStatus').text = 'false'
XML.SubElement(entry, 'statusCode').text = '200'
XML.SubElement(entry, 'timeout').text = \
str(url.get('timeout', 300))
XML.SubElement(entry, 'checkETag').text = \
str(url.get('check-etag', False)).lower()
XML.SubElement(entry, 'checkLastModificationDate').text = \
str(url.get('check-date', False)).lower()
check_content = url.get('check-content', [])
XML.SubElement(entry, 'inspectingContent').text = \
str(bool(check_content)).lower()
content_types = XML.SubElement(entry, 'contentTypes')
for entry in check_content:
type_name = entry.keys()[0]
if type_name not in valid_content_types:
raise JenkinsJobsException('check-content must be one of : %s'
% ', '.join(valid_content_types.
keys()))
content_type = valid_content_types.get(type_name)
if entry[type_name]:
sub_entries = content_type[3]
if sub_entries is None:
sub_entries = entry[type_name]
build_pollurl_content_type(content_types,
sub_entries,
*content_type[0:3])
def timed(parser, xml_parent, data):
"""yaml: timed
Trigger builds at certain times.

View File

@ -178,6 +178,7 @@ jenkins_jobs.triggers =
gitlab-merge-request=jenkins_jobs.modules.triggers:gitlab_merge_request
pollscm=jenkins_jobs.modules.triggers:pollscm
reverse=jenkins_jobs.modules.triggers:reverse
pollurl=jenkins_jobs.modules.triggers:pollurl
script=jenkins_jobs.modules.triggers:script
timed=jenkins_jobs.modules.triggers:timed
jenkins_jobs.wrappers =

View File

@ -0,0 +1,66 @@
<?xml version="1.0" encoding="utf-8"?>
<project>
<triggers class="vector">
<org.jenkinsci.plugins.urltrigger.URLTrigger>
<spec>* * * * *</spec>
<labelRestriction>true</labelRestriction>
<triggerLabel>label expression</triggerLabel>
<entries>
<org.jenkinsci.plugins.urltrigger.URLTriggerEntry>
<url>http://example.com/url1</url>
<proxyActivated>false</proxyActivated>
<username>username</username>
<password>sekr3t</password>
<checkStatus>true</checkStatus>
<statusCode>202</statusCode>
<timeout>442</timeout>
<checkETag>false</checkETag>
<checkLastModificationDate>true</checkLastModificationDate>
<inspectingContent>true</inspectingContent>
<contentTypes>
<org.jenkinsci.plugins.urltrigger.content.SimpleContentType/>
<org.jenkinsci.plugins.urltrigger.content.JSONContentType>
<jsonPaths>
<org.jenkinsci.plugins.urltrigger.content.JSONContentEntry>
<jsonPath>$..author</jsonPath>
</org.jenkinsci.plugins.urltrigger.content.JSONContentEntry>
<org.jenkinsci.plugins.urltrigger.content.JSONContentEntry>
<jsonPath>$.store..price</jsonPath>
</org.jenkinsci.plugins.urltrigger.content.JSONContentEntry>
</jsonPaths>
</org.jenkinsci.plugins.urltrigger.content.JSONContentType>
</contentTypes>
</org.jenkinsci.plugins.urltrigger.URLTriggerEntry>
<org.jenkinsci.plugins.urltrigger.URLTriggerEntry>
<url>http://example.com/url2</url>
<proxyActivated>true</proxyActivated>
<checkStatus>false</checkStatus>
<statusCode>200</statusCode>
<timeout>300</timeout>
<checkETag>true</checkETag>
<checkLastModificationDate>false</checkLastModificationDate>
<inspectingContent>true</inspectingContent>
<contentTypes>
<org.jenkinsci.plugins.urltrigger.content.XMLContentType>
<xPaths>
<org.jenkinsci.plugins.urltrigger.content.XMLContentEntry>
<xPath>//author</xPath>
</org.jenkinsci.plugins.urltrigger.content.XMLContentEntry>
<org.jenkinsci.plugins.urltrigger.content.XMLContentEntry>
<xPath>/store//price</xPath>
</org.jenkinsci.plugins.urltrigger.content.XMLContentEntry>
</xPaths>
</org.jenkinsci.plugins.urltrigger.content.XMLContentType>
<org.jenkinsci.plugins.urltrigger.content.TEXTContentType>
<regExElements>
<org.jenkinsci.plugins.urltrigger.content.TEXTContentEntry>
<regEx>\d+</regEx>
</org.jenkinsci.plugins.urltrigger.content.TEXTContentEntry>
</regExElements>
</org.jenkinsci.plugins.urltrigger.content.TEXTContentType>
</contentTypes>
</org.jenkinsci.plugins.urltrigger.URLTriggerEntry>
</entries>
</org.jenkinsci.plugins.urltrigger.URLTrigger>
</triggers>
</project>

View File

@ -0,0 +1,28 @@
triggers:
- pollurl:
cron: '* * * * *'
polling-node: 'label expression'
urls:
- url: 'http://example.com/url1'
proxy: false
timeout: 442
username: username
password: sekr3t
check-status: 202
check-etag: false
check-date: true
check-content:
- simple: true
- json:
- '$..author'
- '$.store..price'
- url: 'http://example.com/url2'
proxy: true
check-etag: true
check-content:
- simple: false
- xml:
- '//author'
- '/store//price'
- text:
- '\d+'