# SPDX-License-Identifier: EPL-1.0
##############################################################################
-# Copyright (c) 2018 The Linux Foundation and others.
+# Copyright (c) 2018, 2023 The Linux Foundation and others.
#
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Eclipse Public License v1.0
# http://www.eclipse.org/legal/epl-v10.html
##############################################################################
"""Jenkins."""
+from __future__ import annotations
__author__ = "Thanh Ha"
import logging
import os
+from typing import Optional
import jenkins
from six.moves import configparser
-log = logging.getLogger(__name__)
+log: logging.Logger = logging.getLogger(__name__)
-def jjb_ini():
+def jjb_ini() -> str | None:
"""Return jenkins_jobs.ini file location if it exists, None otherwise."""
global_conf = "/etc/jenkins_jobs/jenkins_jobs.ini"
user_conf = os.path.join(os.path.expanduser("~"), ".config", "jenkins_jobs", "jenkins_jobs.ini")
return conf
-JJB_INI = jjb_ini()
+JJB_INI: str | None = jjb_ini()
class Jenkins:
"""lftools Jenkins object."""
- def __init__(self, server, user=None, password=None, config_file=None):
+ def __init__(
+ self, server: str, user: Optional[str] = None, password: Optional[str] = None, config_file: Optional[str] = None
+ ) -> None:
"""Initialize a Jenkins object."""
- self.config_file = config_file
+ self.config_file: Optional[str] = config_file
if not self.config_file:
self.config_file = JJB_INI
if "://" not in server:
if self.config_file:
log.debug("Using config from {}".format(self.config_file))
- config = configparser.SafeConfigParser()
+ config: configparser.SafeConfigParser = configparser.SafeConfigParser()
config.read(self.config_file)
if config.has_section(server):
user = config.get(server, "user")
log.debug("jenkins_jobs.ini not found in any of the default paths.")
server = "https://localhost:8080"
- self.server = jenkins.Jenkins(server, username=user, password=password)
+ self.server: jenkins.Jenkins = jenkins.Jenkins(server, username=user, password=password) # type: ignore
- self.url = server
+ self.url: str = server
# SPDX-License-Identifier: EPL-1.0
##############################################################################
-# Copyright (c) 2018 The Linux Foundation and others.
+# Copyright (c) 2018, 2023 The Linux Foundation and others.
#
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Eclipse Public License v1.0
# http://www.eclipse.org/legal/epl-v10.html
##############################################################################
"""Jenkins token functions."""
+from __future__ import annotations
__author__ = "Thanh Ha"
import jenkins
-log = logging.getLogger(__name__)
+log: logging.Logger = logging.getLogger(__name__)
-def get_token(name, url, username, password, change=False):
+def get_token(name: str, url: str, username: str, password: str, change: bool = False) -> str:
"""Get API token.
This function uses the global username / password for Jenkins from
else:
log.debug("Fetching Jenkins API token from {}".format(url))
- server = jenkins.Jenkins(url, username=username, password=password)
+ server: jenkins.Jenkins = jenkins.Jenkins(url, username=username, password=password) # type: ignore
- get_token = """
+ get_token: str = """
import hudson.model.*
import jenkins.model.*
import jenkins.security.*
username, name
)
- token = server.run_script(get_token)
+ token: str = str(server.run_script(get_token))
return token