From 92096b6bd137a8f46de9f40822fe3eabeaa55abc Mon Sep 17 00:00:00 2001 From: Andrew Grimberg Date: Mon, 9 Oct 2023 08:18:12 -0700 Subject: [PATCH] Refactor: Add annotations to lftools.jenkins Issue: RELENG-4933 Signed-off-by: Andrew Grimberg Change-Id: I94db9930f39c4421b20dc74d678c685a5b1ce0c5 --- lftools/jenkins/__init__.py | 22 +++++++++++++--------- lftools/jenkins/token.py | 13 +++++++------ 2 files changed, 20 insertions(+), 15 deletions(-) diff --git a/lftools/jenkins/__init__.py b/lftools/jenkins/__init__.py index f236ce1a..4ae4a784 100644 --- a/lftools/jenkins/__init__.py +++ b/lftools/jenkins/__init__.py @@ -1,6 +1,6 @@ # 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 @@ -8,19 +8,21 @@ # 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") @@ -37,22 +39,24 @@ def jjb_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") @@ -64,6 +68,6 @@ class Jenkins: 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 diff --git a/lftools/jenkins/token.py b/lftools/jenkins/token.py index ff323d7f..361dce36 100644 --- a/lftools/jenkins/token.py +++ b/lftools/jenkins/token.py @@ -1,6 +1,6 @@ # 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 @@ -8,6 +8,7 @@ # http://www.eclipse.org/legal/epl-v10.html ############################################################################## """Jenkins token functions.""" +from __future__ import annotations __author__ = "Thanh Ha" @@ -15,10 +16,10 @@ import logging 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 @@ -30,9 +31,9 @@ def get_token(name, url, username, password, change=False): 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.* @@ -45,5 +46,5 @@ println token.plainValue username, name ) - token = server.run_script(get_token) + token: str = str(server.run_script(get_token)) return token -- 2.16.6