Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 60 additions & 2 deletions airflow/providers/jenkins/hooks/jenkins.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,40 @@
# under the License.
from __future__ import annotations

from functools import wraps
from typing import Any

import jenkins

from airflow.hooks.base import BaseHook
from airflow.utils.strings import to_boolean


def _ensure_prefixes(conn_type):
"""
Remove when provider min airflow version >= 2.5.0 since this is handled by
provider manager from that version.
"""

def dec(func):
@wraps(func)
def inner():
field_behaviors = func()
conn_attrs = {"host", "schema", "login", "password", "port", "extra"}

def _ensure_prefix(field):
if field not in conn_attrs and not field.startswith("extra__"):
return f"extra__{conn_type}__{field}"
else:
return field

if "placeholders" in field_behaviors:
placeholders = field_behaviors["placeholders"]
field_behaviors["placeholders"] = {_ensure_prefix(k): v for k, v in placeholders.items()}
return field_behaviors

return inner

return dec


class JenkinsHook(BaseHook):
Expand All @@ -31,13 +61,41 @@ class JenkinsHook(BaseHook):
conn_type = "jenkins"
hook_name = "Jenkins"

@staticmethod
def get_connection_form_widgets() -> dict[str, Any]:
"""Returns connection widgets to add to connection form"""
from flask_babel import lazy_gettext
from wtforms import BooleanField

return {
"use_https": BooleanField(
label=lazy_gettext("Use Https"),
description="Specifies whether to use https scheme. Defaults to http",
),
}

@staticmethod
@_ensure_prefixes(conn_type="jenkins")
def get_ui_field_behaviour() -> dict[str, Any]:
"""Returns custom field behaviour"""
return {
"hidden_fields": ["schema", "extra"],
"relabeling": {},
"placeholders": {
"login": "Login for the Jenkins service you would like to connect to",
"password": "Password for the Jenkins service you would like to connect too",
"host": "Host for your Jenkins server. Should NOT contain scheme (http:// or https://)",
"port": "Specify a port number",
},
}

def __init__(self, conn_id: str = default_conn_name) -> None:
super().__init__()
connection = self.get_connection(conn_id)
self.connection = connection
connection_prefix = "http"
# connection.extra contains info about using https (true) or http (false)
if to_boolean(connection.extra):
if connection.extra_dejson.get("use_https"):
connection_prefix = "https"
url = f"{connection_prefix}://{connection.host}:{connection.port}/{connection.schema}"
self.log.info("Trying to connect to %s", url)
Expand Down
4 changes: 2 additions & 2 deletions docs/apache-airflow-providers-jenkins/connections.rst
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ Host
Port
Specify a port number.

Extras (optional)
Specify whether you want to use ``http`` or ``https`` scheme by entering ``true`` to use ``https`` or ``false`` for ``http`` in extras. Default is ``http``.
Use Https
Specify whether you want to use ``https``. Default is ``http``.

When specifying the connection in environment variable you should specify
it using URI syntax.
Expand Down
8 changes: 4 additions & 4 deletions tests/providers/jenkins/hooks/test_jenkins.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ def test_client_created_default_http(self, get_connection_mock):
"""tests `init` method to validate http client creation when all parameters are passed"""
default_connection_id = "jenkins_default"

connection_host = "http://test.com"
connection_host = "test.com"
connection_port = 8080
get_connection_mock.return_value = mock.Mock(
connection_id=default_connection_id,
login="test",
password="test",
schema="",
extra="",
extra_dejson={"use_https": False},
host=connection_host,
port=connection_port,
)
Expand All @@ -53,14 +53,14 @@ def test_client_created_default_https(self, get_connection_mock):
parameters are passed"""
default_connection_id = "jenkins_default"

connection_host = "http://test.com"
connection_host = "test.com"
connection_port = 8080
get_connection_mock.return_value = mock.Mock(
connection_id=default_connection_id,
login="test",
password="test",
schema="",
extra="true",
extra_dejson={"use_https": True},
host=connection_host,
port=connection_port,
)
Expand Down