Apache Airflow provider package for seamless GitHub integration through hooks, operators, and sensors
—
Quality
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
GitHub Hook provides authenticated connection management to GitHub API using PyGithub client. Handles authentication, connection caching, and provides access to the full GitHub API surface through the PyGithub SDK.
Main hook class for GitHub API integration that manages authentication and client lifecycle.
class GithubHook(BaseHook):
"""
Interact with GitHub API through PyGithub client.
Performs connection to GitHub and retrieves authenticated client.
Supports GitHub.com and GitHub Enterprise installations.
"""
# Class attributes
conn_name_attr: str = "github_conn_id"
default_conn_name: str = "github_default"
conn_type: str = "github"
hook_name: str = "GitHub"
def __init__(self, github_conn_id: str = "github_default", *args, **kwargs) -> None:
"""
Initialize GitHub hook and establish connection.
Automatically calls get_conn() to initialize the client connection.
Parameters:
- github_conn_id: Reference to GitHub connection ID configured in Airflow
"""
def get_conn(self) -> GithubClient:
"""
Initiate a new GitHub connection with token and hostname (for GitHub Enterprise).
Uses connection configuration to authenticate with GitHub API.
Caches client instance for reuse within the same hook instance.
Sets self.client attribute with the authenticated client.
Returns:
GithubClient: Authenticated PyGithub client instance
Raises:
AirflowException: If access token is not provided in connection
"""
@property
def client(self) -> GithubClient:
"""
Access the cached GitHub client instance.
Returns the client initialized by get_conn() method.
Returns:
GithubClient: Authenticated PyGithub client instance
"""
def test_connection(self) -> tuple[bool, str]:
"""
Test GitHub connection by attempting to retrieve user information.
Returns:
tuple[bool, str]: (success_status, message)
"""
@classmethod
def get_ui_field_behaviour(cls) -> dict:
"""
Return custom field behaviour for Airflow connection UI.
Configures which fields are hidden, relabeled, or have placeholders
in the Airflow connection form.
Returns:
dict: UI field configuration
"""from airflow.providers.github.hooks.github import GithubHook
# Create hook with default connection
hook = GithubHook()
# Get authenticated client
client = hook.get_conn()
# Use PyGithub client directly
user = client.get_user()
repos = list(user.get_repos())
# Access specific repository
repo = client.get_repo("apache/airflow")
issues = list(repo.get_issues(state='open'))# Use specific connection ID
hook = GithubHook(github_conn_id='github_prod')
client = hook.get_conn()
# Test connection
success, message = hook.test_connection()
if success:
print("Connection successful")
else:
print(f"Connection failed: {message}")# Configure connection in Airflow UI:
# - Connection Type: github
# - Connection ID: github_enterprise
# - Password: your_access_token
# - Host: https://github.enterprise.com/api/v3
hook = GithubHook(github_conn_id='github_enterprise')
client = hook.get_conn()
# Client now points to enterprise instance
enterprise_user = client.get_user()https://{hostname}/api/v3)The hook wraps PyGithub exceptions as AirflowException:
try:
hook = GithubHook()
client = hook.get_conn()
# Operations that may fail
repo = client.get_repo("nonexistent/repo")
except AirflowException as e:
# Handle Airflow-wrapped GitHub errors
print(f"GitHub operation failed: {e}")Install with Tessl CLI
npx tessl i tessl/pypi-apache-airflow-providers-github