Skip to content

Commit

Permalink
Refactor: Add annotations to lftools.jenkins
Browse files Browse the repository at this point in the history
Issue: RELENG-4933
Signed-off-by: Andrew Grimberg <[email protected]>
Change-Id: I94db9930f39c4421b20dc74d678c685a5b1ce0c5
  • Loading branch information
tykeal committed Oct 9, 2023
1 parent 49d12c1 commit 92096b6
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 15 deletions.
22 changes: 13 additions & 9 deletions lftools/jenkins/__init__.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,28 @@
# 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
# which accompanies this distribution, and is available at
# 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")
Expand All @@ -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")
Expand All @@ -64,6 +68,6 @@ def __init__(self, server, user=None, password=None, config_file=None):
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
13 changes: 7 additions & 6 deletions lftools/jenkins/token.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
# 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
# which accompanies this distribution, and is available at
# http://www.eclipse.org/legal/epl-v10.html
##############################################################################
"""Jenkins token functions."""
from __future__ import annotations

__author__ = "Thanh Ha"

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
Expand All @@ -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.*
Expand All @@ -45,5 +46,5 @@ def token = t.tokenStore.generateNewToken("{}")
username, name
)

token = server.run_script(get_token)
token: str = str(server.run_script(get_token))
return token

0 comments on commit 92096b6

Please sign in to comment.