Get jenkins config only with the key exists 51/15851/5
authorAnil Belur <abelur@linuxfoundation.org>
Tue, 11 Jun 2019 02:53:10 +0000 (12:53 +1000)
committerAnil Belur <abelur@linuxfoundation.org>
Tue, 11 Jun 2019 02:53:11 +0000 (12:53 +1000)
Handle config parser correctly which defaults to "[jenkins]" section
when no server is pased. This fixes the issue with checking if the key
exists in the configuration read before reading the key-value.

The issue is reproduceable by running `lftools jenkins plugins --help`
or `tox -e docs`, with the ``jenkins.ini`` file having no "[jenkins]"
section.

Use SafeConfigParser which is a saner version for parsing config files.

Issue: RELENG-2120
Change-Id: I274ac46cf7a1d03cf7c3c6fdaec6094b5e02aa42
Signed-off-by: Anil Belur <abelur@linuxfoundation.org>
lftools/jenkins/__init__.py
releasenotes/notes/fix-jenkins-config-parser-d9eb6e7068a7906a.yaml [new file with mode: 0644]

index 79e461b..c997056 100644 (file)
@@ -58,11 +58,15 @@ class Jenkins():
         if '://' not in server:
             if self.config_file:
                 log.debug('Using config from {}'.format(self.config_file))
-                config = configparser.ConfigParser()
+                config = configparser.SafeConfigParser()
                 config.read(self.config_file)
-                user = config.get(server, 'user')
-                password = config.get(server, 'password')
-                server = config.get(server, 'url')
+                if config.has_section(server):
+                    user = config.get(server, 'user')
+                    password = config.get(server, 'password')
+                    server = config.get(server, 'url')
+                else:
+                    log.error('[{}] section not found in {}'
+                              .format(server, self.config_file))
             else:
                 log.debug('jenkins_jobs.ini not found in any of the default paths.')
                 server = 'https://localhost:8080'
diff --git a/releasenotes/notes/fix-jenkins-config-parser-d9eb6e7068a7906a.yaml b/releasenotes/notes/fix-jenkins-config-parser-d9eb6e7068a7906a.yaml
new file mode 100644 (file)
index 0000000..19a7b2a
--- /dev/null
@@ -0,0 +1,40 @@
+---
+fixes:
+  - |
+    Handle config parser correctly which defaults to "[jenkins]" section
+    when no server is pased. This fixes the issue with checking if the key
+    exists in the configuration read before reading the key-value.
+
+    The issue is reproduceable by running `lftools jenkins plugins --help` or
+    `tox -e docs`, with the ``jenkins.ini`` file having no "[jenkins]" section.
+
+    .. code-block: none
+
+       Traceback (most recent call last):
+          File "/home/jenkins/.local/lftools/env/bin/lftools", line 10, in <module>
+            sys.exit(main())
+          File "/home/jenkins/.local/lftools/lftools/cli/__init__.py", line 104, in main
+            cli(obj={})
+          File "/home/jenkins/.local/lftools/env/lib/python3.7/site-packages/click/core.py", line 764, in __call__
+            return self.main(*args, **kwargs)
+          File "/home/jenkins/.local/lftools/env/lib/python3.7/site-packages/click/core.py", line 717, in main
+            rv = self.invoke(ctx)
+          File "/home/jenkins/.local/lftools/env/lib/python3.7/site-packages/click/core.py", line 1137, in invoke
+            return _process_result(sub_ctx.command.invoke(sub_ctx))
+          File "/home/jenkins/.local/lftools/env/lib/python3.7/site-packages/click/core.py", line 1134, in invoke
+            Command.invoke(self, ctx)
+          File "/home/jenkins/.local/lftools/env/lib/python3.7/site-packages/click/core.py", line 956, in invoke
+            return ctx.invoke(self.callback, **ctx.params)
+          File "/home/jenkins/.local/lftools/env/lib/python3.7/site-packages/click/core.py", line 555, in invoke
+            return callback(*args, **kwargs)
+          File "/home/jenkins/.local/lftools/env/lib/python3.7/site-packages/click/decorators.py", line 17, in new_func
+            return f(get_current_context(), *args, **kwargs)
+          File "/home/jenkins/.local/lftools/lftools/cli/jenkins/__init__.py", line 44, in jenkins_cli
+            ctx.obj['jenkins'] = Jenkins(server, user, password, config_file=conf)
+          File "/home/jenkins/.local/lftools/lftools/jenkins/__init__.py", line 63, in __init__
+            user = config.get(server, 'user')
+          File "/usr/lib64/python3.7/configparser.py", line 780, in get
+            d = self._unify_values(section, vars)
+          File "/usr/lib64/python3.7/configparser.py", line 1146, in _unify_values
+            raise NoSectionError(section) from None
+       configparser.NoSectionError: No section: 'jenkins'