Cannot delete build using delete_build

Bug #1781381 reported by dale_schaffer
32
This bug affects 7 people
Affects Status Importance Assigned to Milestone
Python Jenkins
Fix Released
Undecided
Unassigned

Bug Description

Requirement already up-to-date: python-jenkins in .XXXXXXX/venv/lib/python3.6/site-packages (1.0.2)

In line 357 of __init__.py (function maybe_add_crumb) the code fails on req.headers[self.crumb['crumbRequestField']] = self.crumb['crumb']

at this point i have a value for self.crumb and req.headers is b''

the error i get is "bytes' object does not support item assignment"

trace

 File "XXXXXX.py", line 9, in <module>
    j.delete_build(r"Whale Watchers/Project Whale Watcher QA - Grouped", k)
  File "XXXXXX/venv/lib/python3.6/site-packages/jenkins/__init__.py", line 1354, in delete_build
    self._build_url(DELETE_BUILD, locals()), b''))
  File "XXXXXX/venv/lib/python3.6/site-packages/jenkins/__init__.py", line 540, in jenkins_open
    return self.jenkins_request(req, add_crumb, resolve_auth).text
  File "XXXXXX/venv/lib/python3.6/site-packages/jenkins/__init__.py", line 556, in jenkins_request
    self.maybe_add_crumb(req)
  File "XXXXXX/venv/lib/python3.6/site-packages/jenkins/__init__.py", line 357, in maybe_add_crumb
    req.headers[self.crumb['crumbRequestField']] = self.crumb['crumb']
TypeError: 'bytes' object does not support item assignment

Revision history for this message
Mike Winter (suederat) wrote :
Download full text (3.3 KiB)

 my cmd2.Cmd wrapper with a workaround, simply replaces b'' w/ {}

     def do_deletebuild(self, args):
          """delete build numbers

          WIP, fails on maybe_add_crumb
          https://<email address hidden>/msg00533.html
          """
          if hasattr(self, 'jobs'):
              _jobs = self.jobs
          else:
              # this is the path for nested jobs
              # e.g. Release_3.3/GOLD/PERFORMANCE/EDGE1000 property lastBuild
              _jobs = args[:1]
              args = args[1:]
          if isinstance(_jobs[0], str):
              _jobs[0] = dict(name=_jobs[0])
          jobname = _jobs[0]['name']
          try:
              numbers = int(args[0])
              if numbers < 0:
                  # TODO all but N newest
                  pass
              numbers = range(numbers, numbers + 1)
          except ValueError:
              numbers = args[0].split('-')
              numbers = range(int(numbers[0]), int(numbers[1]) + 1)
              # delete a range

          for number in numbers:
              folder_url, short_name = self.server._get_job_folder(jobname)
              url = self.server._build_url(jenkins.DELETE_BUILD, locals())
              ...

Read more...

Revision history for this message
Mike Winter (suederat) wrote :

suggest editing library to replace all b'' with {} in all jenkins_open calls.

Revision history for this message
Robbert Korving (robkorv) wrote :

For some reason the `req.headers` in `maybe_add_crumb` are in bytes when `delete_build` is called with python3.

I fixed this with the following diff:

```diff
--- /home/robkorv/.local/lib/python3.5/site-packages/jenkins/__init__.py (Selection)
+++ (clipboard)
@@ -10,5 +10,5 @@
                 self.crumb = False
             else:
                 self.crumb = json.loads(response)
- if self.crumb:
+ if self.crumb and isinstance(req.headers, dict):
             req.headers[self.crumb['crumbRequestField']] = self.crumb['crumb']

```

Revision history for this message
Éamonn Ó Nualláin (eamonn-o-nuallain) wrote :

I had the same issue and Robbert's (robkorv) fix worked for me.

I'm using Windows10 so the fix was applied to

c:\Users\user\appdata\Local\programs\Python\Python38-32\Lib\site-packages\jenkins\__init__.py

Revision history for this message
Gildas Fargeas (gfargeas) wrote :

It seems more appropriate to update the request object instead. With your patch, you'll ignore added headers(including the crumb).
To prevent this, just change the request instanciation for the delete_build call (wipeout_job_workspace has the same call so it fixes that as well):

diff --git a/jenkins/__init__.py b/jenkins/__init__.py
index 620ce14..0691f2a 100755
--- a/jenkins/__init__.py
+++ b/jenkins/__init__.py
@@ -1469,7 +1469,7 @@ class Jenkins(object):
         """
         folder_url, short_name = self._get_job_folder(name)
         self.jenkins_open(requests.Request('POST',
- self._build_url(DELETE_BUILD, locals()), b''))
+ self._build_url(DELETE_BUILD, locals())))

     def wipeout_job_workspace(self, name):
         """Wipe out workspace for given Jenkins job.
@@ -1479,7 +1479,7 @@ class Jenkins(object):
         folder_url, short_name = self._get_job_folder(name)
         self.jenkins_open(requests.Request('POST',
                           self._build_url(WIPEOUT_JOB_WORKSPACE,
- locals()), b''))
+ locals())))

     def get_running_builds(self):
         '''Return list of running builds.

Revision history for this message
Kartik Tanwar (aqeelasks) wrote : [Python-jenkins-developers] [Bug 1781381] Re: Cannot delete build using delete_build

Hi Glidas,
I was using the python-jenkins wrapper, and i still cant use the
delete_build() command as it shows the error: byte cant be assigned any
item. How to solve this problem? i dont want to change anything in the
__init__.py as the project i am working on will be distributed globally and
its not physically possible to change the __init__.py file of jenkins in
each of the server.
Since this is almost an year old issue, did the developer made the
correction?
please let me know.
Thanks and regards.

Kartik Tanwar

Revision history for this message
Christian Henz (chrhenz) wrote (last edit ):

The fix for this seems completely trivial (see patch), quite strange that this has not been fixed in five years.

Revision history for this message
OpenStack Infra (hudson-openstack) wrote : Fix proposed to python-jenkins (master)

Fix proposed to branch: master
Review: https://review.opendev.org/c/jjb/python-jenkins/+/901965

Changed in python-jenkins:
status: New → In Progress
Revision history for this message
OpenStack Infra (hudson-openstack) wrote : Fix merged to python-jenkins (master)

Reviewed: https://review.opendev.org/c/jjb/python-jenkins/+/901965
Committed: https://opendev.org/jjb/python-jenkins/commit/6b757a7c364d641fd314975daffb412024e95c64
Submitter: "Zuul (22348)"
Branch: master

commit 6b757a7c364d641fd314975daffb412024e95c64
Author: Christian Henz <email address hidden>
Date: Tue Nov 28 16:35:22 2023 +0100

    Do not initialize Request headers with bytestring.

    Closes-Bug: 1781381
    Change-Id: I14fcf165988bfe5fadf680d31152327eccbab5f5

Changed in python-jenkins:
status: In Progress → Fix Released
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Duplicates of this bug

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.