Commit Graph

192 Commits

Author SHA1 Message Date
Christian Henz 6b757a7c36 Do not initialize Request headers with bytestring.
Closes-Bug: 1781381
Change-Id: I14fcf165988bfe5fadf680d31152327eccbab5f5
2023-11-28 22:34:36 +01:00
Christoph Erhardt 876a2a928c Fix compatibility with setuptools >= 66
The Python world, including the `setuptools` package, has migrated to
the versioning scheme defined in PEP 440. Unfortunately, the versioning
of Jenkins plugins is less strict than that. As a consequence,
`pkg_resources.parse_version()` now rejects versions as invalid.

Fix `PluginVersion` by salvaging the implementation of the old
`LegacyVersion` class, which used to be part of the `packaging` library
prior to version 22.0. The code is licensed under a 2-clause BSD
licence.

For compatibility with older Python versions, remove type annotations
from the copied code.

Change-Id: Iaa057dcd81620fed861cb11bed534ff7f9ab3f32
Signed-off-by: Christoph Erhardt <ce+ubuntuone@sicherha.de>
2023-10-19 15:42:27 +00:00
Guillaume DeMengin 5c44d7c9ec get_build_artifact_as_bytes to support non-json files
new function get_build_artifact_as_bytes to replace get_build_artifact (incompatible with artifacts not json-formatted)
+ use a stream to download binary artifacts

all artifacts are returned as bytes to avoid encoding issues
and an exception NotFoundException is raised in case of missing artifact

Closes-Bug: #1973243
Change-Id: I24ce4ecd854f8a19ed4d760404adb7d1ac6b5509
2023-10-14 14:52:56 +00:00
Vsevolod Fedorov a8f0d8cfda STORY 2010790: Handle 'all' view name when requested 'All'
Jenkins version 2.387.1 or earlier can return 'all' as view name
when requested is 'All'. Add workaround for that.

Change-Id: I6b5327a5f969607ca4838a13bbac838f44e76ccb
2023-08-19 10:06:25 +00:00
Kenyon Ralph 5a98031aee WrappedSession: Fix URL to requests issue 3829
The repo moved on GitHub and the old URL returns error 404.

Change-Id: Ida67cdfad02accc917fcd4646cc1efaff01bcc5f
2023-08-03 11:30:10 -07:00
Ken Dreyer ba9f06e0c2 support urllib3 newer DEFAULT_TIMEOUT
urllib3 1.x used to accept socket._GLOBAL_DEFAULT_TIMEOUT as a sentinal
object to mean "no configured timeout".

In urllib3 2.x, urllib3 uses its own _DEFAULT_TIMEOUT sentinal object,
and it rejects socket._GLOBAL_DEFAULT_TIMEOUT.

Assign our own DEFAULT_TIMEOUT constant to the newer object if it
exists, and fall back to the old behavior on old urllib3 versions.

Co-authored-by: Vsevolod Fedorov <vsevolod.fedorov@gmail.com>
Closes-Bug: #2018567
Change-Id: Ic626ba0e8ed79eec3a63ffab6cc02f91aa545ab1
2023-07-17 14:53:37 +02:00
Michael Still d1e4696495 Handle new master naming in Jenkins post v2.307.
Closes-Bug: #1943402
Change-Id: I6789f6af325f2c104adb91ac7159e3734f71029f
2021-12-28 17:28:15 +11:00
Jim Wisniewski dc2b9447de Use fullname in get_job_info_regex
Use full job name in `get_job_info_regex` instead of just the name, so
that matches will work in folders as well.

Closes-Bug: 1685575
Change-Id: I97ba8ca004387b376a511ba52fc9fdd2aeb98a63
2021-12-14 23:39:03 +00:00
JP Sullivan 570a143c74 Allow build number to be a string
When retreiving data from builds, the build number can have a string portion.
This is for matrix jobs that define a label to retrieve one sub-build of the
matrix job.

The URL structure is:
https://<jenkins_server>/job/>job_name>/<number>/label=<configuration>/

Allowing a string for build number allows for it to be specifed as, e.g.:
1/label=amd64

And whilst being somewhat a workaround, works without introducing complexity
and modifying function calls for extra parameters.

Change-Id: Ic409df6e20e85d6a417c6490472b3729f1f20cd2
2020-11-23 10:40:27 +00:00
JP Sullivan 4aab17d404 Add retrieval of stages and artifacts
Enable calls to retreive artifacts from Jenkins and also to pull
the stages data from the wfapi endpoint.

Change-Id: I59eb403d54416ed45a918d98fc2f43c3c02441d0
2020-04-23 16:15:38 +01:00
Zuul 9e66e45f0a Merge "Include placeholder tasks in get_running_builds" 2020-03-04 02:58:33 +00:00
Pascal Hofmann 19e7f7395a Include placeholder tasks in get_running_builds
Pipeline jobs in in Jenkins 2.x appear as placeholder tasks for their
entire lifetime when queried via the API. The fix for #1659787
introduced completly ignoring placeholder tasks. Thus pipeline jobs are
never included in the list returned by get_running_builds.

This commit will change the behaviour of get_running_builds as follows:
  - Placeholder tasks will be included: So running pipeline jobs
    will be included.
  - Builds which do not have a build number yet will be ignored:
    This prevents the exception described in #1659787.

Change-Id: I30351ec78d6186b0674e3c08be00076b0ce2333e
Closes-Bug: #1750372
Related-Bug: #1659787
2019-12-25 08:43:46 +01:00
Jeff Schroeder 9bf7f81509
Fix reconfig_credential() to send the xml data
Previously, reconfig_credential() passed empty `data` with
`Content-Length: 0` to the credentials configuration rather
than the credentials data.

This patch fixes the issue by passing the config_xml data to
the configuration API to properly update the credential.

Change-Id: Idef50f5a31d55991698b6217f55f15a9308b8526
2019-12-16 09:32:50 -05:00
Zuul 36c99d3436 Merge "new method: create_folder (with tests)" 2019-12-16 14:08:18 +00:00
Marcos Diez d6d510b59d new method: create_folder (with tests)
Change-Id: Ib21398c98d853aed4f4eea50270164e501fd18ce
2019-12-15 23:57:11 -03:00
Marcos Diez f222797332 new method: check_jenkinsfile_syntax (with tests)
Change-Id: I77a04c95ee3e4d9184bb418853ae414957bf0280
2019-12-15 23:41:38 -03:00
Marcos Diez 722a303f9a upsert_job
Change-Id: I1f389baf1c530b6e04860569deb5aca51bf781c2
2019-09-17 10:12:27 -03:00
Zuul 35f660a8b9 Merge "When updating jobs the response body may be empty" 2019-07-23 15:12:05 +00:00
Frank Lichtenheld 190fbbb5ad Make get_job_info fetch_all_builds work with jobs in folders
Make sure to use fullName from the API results to correctly
compute the URL for the additional API request.

Change-Id: I7ca1f2e15a6c40883ba376b182c2c991ca76e8b2
Closes-Bug:#1828051
2019-05-07 17:23:14 +02:00
Jan Zerebecki a2c6f6a8ff
When updating jobs the response body may be empty
Other specific requests may need a body. Thus remove the general check
for an empty body and add a more specific one where needed.

I noticed this because when updating a Jenkins job with Jenkins Job
Builder I got an EmptyResponseException even though updating worked
correctly.

Change-Id: Ie144e6fb0b922a681e6c65ffbe5120da72a495f0
2019-01-07 21:52:19 +01:00
Tomas Janousek b5a8b7035e Request multiple folder levels at once in get_all_jobs
On our Jenkins instance with almost a hundred folders, JJB update
stalls for quite a while because it calls get_all_jobs. When invoked
locally at the Jenkins master, it's a matter of seconds, on a fast
broadband link and VPN, it's 2 minutes, and on a train it's easily 10
minutes.

But there's trick!

    curl \
        --show-error --silent --fail \
        --user : --negotiate \
        --get \
        --data-urlencode \
            tree=jobs\[url\,name\,jobs\[url\,name\,jobs\[url\,name\,\
            jobs\[url\,name\,jobs\[url\,name\,jobs\[url\,name\,\
            jobs\[url\,name\,jobs\[url\,name\,jobs\[url\,name\,\
            jobs\[url\,name\,jobs\]\]\]\]\]\]\]\]\]\]
        https://jenkins.example.com/api/json

This returns almost instantly.

And it gets better: if we fail to correctly guess the nesting level
necessary, Jenkins returns

    …, "jobs": [{}, {}, …], …

so we can easily detect that we need to recurse deeper.

Change-Id: I7268259149e4bc8939c512a112c7e6ec1908224f
2018-10-17 18:06:56 +02:00
Tomas Janousek 49d6c92d0c Clean up job/folder path handling
Avoid repeatedly serializing and splitting the root path component of
jobs being added to the list to be processed and returned.

Attempting to split a string path using 'job' introduces a bug if a job
ever happens to also use the name 'job' as part of its name. This could
be fixed by using root.split('/job/') but it still seems hackish.

Maintain the root path to the folder in the hierarchy as a list of path
components, add to it when descending and store the result of joining
it with '/job/' as a separate variable to avoid needing to re-parse.

Change-Id: I074ad74ad935c50716141a79822ca84999734c3f
2018-10-17 18:06:56 +02:00
Zuul afa1e05e74 Merge "Check for 'Location' header in the response" 2018-09-21 14:11:48 +00:00
Zuul ede93173e3 Merge "Add folder credential support" 2018-09-21 13:46:39 +00:00
Aigars Mahinovs 811a6cffc8 Allow adding extra HTTP headers to Jenkins requests
In some network setups Jenkins may be hidden behind complex reverse
proxy setups that require additional custom headers to be set on
each request in order to pass them trough

Allow providing such headers using JENKINS_API_EXTRA_HEADERS environment
variable

Change-Id: If071c5c707f916ba5f4f2c371ec600b7476bf723
2018-09-12 13:42:23 +02:00
Thanh Ha 93515ae07d
Revert "detect and respect http redirects"
This reverts commit 4150a83d45.

Change-Id: I972404ff936cbd3c0fe164dcbaa186d5e530736c
2018-08-24 13:12:38 -04:00
Thanh Ha f68b2fb9d6
Revert "Avoid empty body failure on HEAD requests"
This reverts commit 5e8dfb9065.

Change-Id: Ia82e11b65c0d175f2d3b517e43176213eb1a4ae0
2018-08-24 13:12:37 -04:00
Zuul 437d5de4e9 Merge "Fix item being ignored in get_info" 2018-08-13 14:10:04 +00:00
Sorin Sbarnea 5e8dfb9065 Avoid empty body failure on HEAD requests
HTTP specification states clearly that HEAD requests
should have no body, so we should not fail on these.

Change-Id: I9d2418f26c26b2a40f6bafaf86ed6640fbf83b0c
2018-08-07 17:53:14 +01:00
Tomas Janousek fc953ee505 Fix item being ignored in get_info
This, I think, would've caused get_all_jobs to loop indefinitely if the
Jenkins instance has folders. Am I really the first to have caught this?

Change-Id: I4158b0d068135de08a3337486d8a4b41d8b7cda6
2018-08-04 19:05:15 +02:00
joelee 857235b7fa Add folder credential support
implement functions to manage credential in Jenkins Folder
job with rest api provided by :
https://wiki.jenkins.io/display/JENKINS/Credentials+Plugin

Change-Id: I9bcc3943e4ec705fe8705ad1d457b6fd4ad1024a
2018-08-03 06:39:13 +03:00
Sorin Sbarnea 4150a83d45
detect and respect http redirects
Detects if API url is redirected and corrects it in order
to avoid doubling number of requests and even failing
POST ones because of having them silently converted by
requests from POST to GET.

Displays a warning to the user when redirection occurs
because almost always this means that they are using an
old endpoint, like a non secured one.

Change-Id: I7387bf150dad307342f9a6a91afbae32859bc82e
Signed-off-by: Sorin Sbarnea <ssbarnea@redhat.com>
2018-07-16 17:56:19 +01:00
Richard Felkl 4e64f0f880 Fix run_script method.
Existing code assumes the script sent by the user is terminated with a newline.
Appending magic string without '\n' before print() command results in a syntactically
incorrect Groovy code.
This patch ensures that '\n' is added between user's provided script and appended
magic string.

Change-Id: I39021f1058e7434d487efb4bc7aca18416b2c08e
2018-07-10 15:40:07 +02:00
Dennis Dmitriev f420d6de01 Check for 'Location' header in the response
If the keep_alive is not available because of using reverse
proxy, the header 'Content-Lenght' may not be available,
causing the exception:

 Error communicating with server[...]: empty response

However, if 'Location' header is present, the response is valid.

Add unit tests:
- build_job must pass even if no 'content-lenght'
  in response header
- build_job must fail if no 'location' in response header

Change-Id: I4da6dd19f9d8302a76652a3686a9377f9a2503a6
Closes-Bug:#1775047
2018-06-25 12:59:38 +03:00
Sorin Sbarnea e5e7a097b1
Adopt use of pre-commit hooks
- rename pep8 target to more generic linters
- migrates flake8 execution to pre-commit
- adds yaml linting
- hardening flake8 rules
- setting max-line-lengh to 120 instead of ignoring it.
- bumped minimal sphinx version
- configured sphinx with warnings as errors

Change-Id: I4c598c07297ae6a2160ba6754cec860adbfeb6f2
Signed-off-by: Sorin Sbarnea <ssbarnea@redhat.com>
2018-06-22 13:37:30 +01:00
Nickolas Pohilets f7fa4ce059
Adds support for executing Groovy scripts on jenkins nodes
Change-Id: I4c9d266073c3ba2a85f2c58a44afc2f041bf37f1
Signed-off-by: Sorin Sbarnea <ssbarnea@redhat.com>
2018-06-17 19:15:13 +01:00
Jan Priessnitz 44d211b90f
Allow use of unicode job names
Closes bug: #1713481
Signed-off-by: Jan Priessnitz <jan.priessnitz@gooddata.com>

Change-Id: I8a1ff4bf0f7711ab9c9b76f538411a854244283c
2018-06-17 15:28:46 +01:00
Zuul 1c4870c20b Merge "Update URLS that require depth or tree filters" 2018-06-16 21:29:43 +00:00
Thanh Ha 7ffb27c7d5
Fix run_scripts() API
Resolve regression caused by I5369a0d35be4bf8b3b197a51e60aba21b5742cc7
preventing run_scripts() from successfully running against the remote
Jenkins instance.

Switching data to a dictionary and passing the script string without
quote() appears to allow the script to successfully run.

Add a magic string ')]}.' as an indicator of if the groovy script
successfully passed or failed. In theory if the groovy script failed
perhaps due to syntax error or some other reason the magic string
will not be printed and we can assume failure.

Change-Id: Ibaffb768ea82c76b44ec5a6cccde8563afe1783f
Signed-off-by: Thanh Ha <zxiiro@linux.com>
2018-06-15 11:37:46 -04:00
Darragh Bailey 45529fba54 Ensure timeout exceptions are re-raised during auth attempts
Timeouts are different to auth failures, make sure to re-raise them
while attempting multiple authentications.

Change-Id: I56c9580be9a90f5cda7349a148d467e6ff4e9270
2018-06-01 10:48:54 +01:00
Zuul a3c3672a6f Merge "Fix: send proxy as empty dict" 2018-05-31 18:50:49 +00:00
JP Sullivan f1f97ab4bf Update URLS that require depth or tree filters
Some API endpoints require the use of a filter or they will respond
with HTTP error code 418 I'm a teapot.

This was seen on CloudBees Jenkins Enterprise 2.107.2.1-rolling.

Adding a depth filter will ensure the API calls will succeed.

Change-Id: Ib4d6a251bf3a024a76081b2fc83baa7839ad4015
2018-05-28 10:17:26 +01:00
Zuul 892cd41f94 Merge "Update to _response_handler" 2018-05-19 21:42:19 +00:00
Brad Brown a11edaca52 Fix: send proxy as empty dict
Change-Id: Ie713479ef5a468a22bc71f220166b438a91d5877
2018-05-01 06:46:18 -05:00
JP Sullivan 827f28a223 Add functions to retrieve more data from Jenkins
If tests results are added to builds, or environment variables are injected
into a job, there are separate API calls to retrieve that data.  Add functions
to do so.

Change-Id: Ie0a51839d527db6d440719eeb44cd1cc89644d06
2018-04-25 13:30:29 +01:00
JP Sullivan 5033405db7 Update to _response_handler
Account for situations where no Content-Length or Transfer-Encoding
headers are present in a response.

The length of the response.content could still indicate a response was
given.  The earlier raise_for_status() should catch bad HTTP responses
that return content.

Closes-bug: #1766660
Change-Id: I6739e26a039a898982f5cdc96d19d6992ed37737
2018-04-25 12:36:20 +01:00
Zuul 5c913ca185 Merge "add get_queue_item() method" 2018-04-17 19:27:23 +00:00
Zuul 27201edd51 Merge "build_job: return queue item identifier" 2018-04-17 19:08:58 +00:00
Zuul ca86352248 Merge "add jenkins_request() method" 2018-04-17 18:45:22 +00:00
Ken Dreyer 284c3d37b7 add get_queue_item() method
Pass in a queue ID number to discover a job's status, and possibly a job
URL.

Change-Id: I20541ec49cc30e5c74a6c596e02b3f42b2567fa5
Closes-Bug: #1724932
2018-04-17 12:27:32 -06:00