Copilot commented on code in PR #64422:
URL: https://github.com/apache/airflow/pull/64422#discussion_r3025331096


##########
providers/git/src/airflow/providers/git/hooks/git.py:
##########
@@ -103,10 +107,26 @@ def __init__(
         self.host_proxy_cmd = extra.get("host_proxy_cmd")
         self.ssh_port: int | None = int(extra["ssh_port"]) if 
extra.get("ssh_port") else None
 
+        # GitHub App Auth Options
+        self.github_app_id = extra.get("github_app_id")
+        self.github_installation_id: int | None = (
+            int(extra["github_installation_id"]) if 
extra.get("github_installation_id") else None
+        )
+
         self.env: dict[str, str] = {}
 
         if self.key_file and self.private_key:
             raise AirflowException("Both 'key_file' and 'private_key' cannot 
be provided at the same time")
+        if (self.github_app_id and not self.github_installation_id) or (
+            not self.github_app_id and self.github_installation_id
+        ):
+            raise AirflowException(
+                "Both 'github_app_id' and 'github_installation_id' must be 
provided to use GitHub App Authentication"
+            )
+        if self.github_app_id and self.github_installation_id and 
self.private_key:
+            if not self.key_file and not self.private_key:
+                raise AirflowException("Missing inline private_key or key_file 
for GitHub App Auth")
+            self.user_name, self.auth_token = self._get_github_app_token()
         self._process_git_auth_url()

Review Comment:
   New GitHub App authentication behavior (validation of 
`github_app_id`/`github_installation_id`, key handling, and token-to-URL 
injection) is introduced here but isn’t covered by the existing 
`providers/git/tests/unit/git/hooks/test_git.py` suite. Please add unit tests 
for the success path and the key/parameter validation failures to prevent 
regressions.



##########
providers/git/src/airflow/providers/git/hooks/git.py:
##########
@@ -103,10 +107,26 @@ def __init__(
         self.host_proxy_cmd = extra.get("host_proxy_cmd")
         self.ssh_port: int | None = int(extra["ssh_port"]) if 
extra.get("ssh_port") else None
 
+        # GitHub App Auth Options
+        self.github_app_id = extra.get("github_app_id")
+        self.github_installation_id: int | None = (
+            int(extra["github_installation_id"]) if 
extra.get("github_installation_id") else None
+        )
+

Review Comment:
   `github_app_id` is taken directly from `extra` as a string, but GitHub App 
auth typically expects an integer app id (and it’s easy for users to provide a 
non-numeric value). Please validate/coerce `github_app_id` (and ensure 
`github_installation_id` is an int) and raise a clear `AirflowException` if the 
values are invalid.
   ```suggestion
           raw_github_app_id = extra.get("github_app_id")
           if raw_github_app_id is not None:
               try:
                   self.github_app_id: int | None = int(raw_github_app_id)
               except (TypeError, ValueError) as exc:
                   raise AirflowException(
                       f"Invalid 'github_app_id' value {raw_github_app_id!r}. 
It must be an integer."
                   ) from exc
           else:
               self.github_app_id = None
   
           raw_github_installation_id = extra.get("github_installation_id")
           if raw_github_installation_id is not None:
               try:
                   self.github_installation_id: int | None = 
int(raw_github_installation_id)
               except (TypeError, ValueError) as exc:
                   raise AirflowException(
                       f"Invalid 'github_installation_id' value 
{raw_github_installation_id!r}. It must be an integer."
                   ) from exc
           else:
               self.github_installation_id = None
   ```



##########
providers/git/src/airflow/providers/git/hooks/git.py:
##########
@@ -103,10 +107,26 @@ def __init__(
         self.host_proxy_cmd = extra.get("host_proxy_cmd")
         self.ssh_port: int | None = int(extra["ssh_port"]) if 
extra.get("ssh_port") else None
 
+        # GitHub App Auth Options
+        self.github_app_id = extra.get("github_app_id")
+        self.github_installation_id: int | None = (
+            int(extra["github_installation_id"]) if 
extra.get("github_installation_id") else None
+        )
+
         self.env: dict[str, str] = {}
 
         if self.key_file and self.private_key:
             raise AirflowException("Both 'key_file' and 'private_key' cannot 
be provided at the same time")
+        if (self.github_app_id and not self.github_installation_id) or (
+            not self.github_app_id and self.github_installation_id
+        ):
+            raise AirflowException(
+                "Both 'github_app_id' and 'github_installation_id' must be 
provided to use GitHub App Authentication"
+            )
+        if self.github_app_id and self.github_installation_id and 
self.private_key:
+            if not self.key_file and not self.private_key:
+                raise AirflowException("Missing inline private_key or key_file 
for GitHub App Auth")

Review Comment:
   The GitHub App auth branch only runs when `self.private_key` is set, which 
means a `key_file` (documented as acceptable in the error message) will never 
be used for GitHub App authentication. Also, the nested `if not self.key_file 
and not self.private_key` check is unreachable inside this branch. Consider 
triggering GitHub App auth when `github_app_id` + `github_installation_id` are 
provided, then require exactly one of `private_key` or `key_file` and (if 
`key_file` is used) read its contents before requesting the installation token.
   ```suggestion
           if self.github_app_id and self.github_installation_id:
               if not self.key_file and not self.private_key:
                   raise AirflowException("Missing inline private_key or 
key_file for GitHub App Auth")
               if self.key_file and not self.private_key:
                   try:
                       with open(self.key_file, "r", encoding="utf-8") as 
key_file:
                           self.private_key = key_file.read()
                   except OSError as exc:
                       raise AirflowException(
                           f"Failed to read GitHub App private key file 
{self.key_file!r}: {exc}"
                       ) from exc
   ```



##########
providers/git/src/airflow/providers/git/hooks/git.py:
##########
@@ -134,6 +154,17 @@ def _build_ssh_command(self, key_path: str | None = None) 
-> str:
 
         return " ".join(parts)
 
+    def _get_github_app_token(self):
+        from github import Auth as GithubAuth, Github as GithubClient
+
+        github_auth = GithubAuth.AppAuth(
+            app_id=self.github_app_id, private_key=self.private_key
+        ).get_installation_auth(installation_id=self.github_installation_id)

Review Comment:
   There is an import inside `_get_github_app_token()`. If this is to keep 
`PyGithub` optional, please wrap the import in `try/except ImportError` and 
raise an `AirflowException` explaining that the 
`apache-airflow-providers-git[github]` extra (or `pygithub`) must be installed 
for GitHub App authentication. If the dependency is expected to always be 
present, move the import to module scope.



##########
providers/git/src/airflow/providers/git/hooks/git.py:
##########
@@ -48,6 +48,8 @@ class GitHook(BaseHook):
     * ``ssh_config_file`` — path to a custom SSH config file.
     * ``host_proxy_cmd`` — SSH ProxyCommand string (e.g. for bastion/jump 
hosts).
     * ``ssh_port`` — non-default SSH port.
+    * ``github_app_id`` — App ID needed for GitHub App Authentication.
+    * ``github_installation_id`` — Installation ID needed for GitHub App 
Authentication.

Review Comment:
   The connection extras docstring now lists `github_app_id` / 
`github_installation_id`, but it doesn’t describe how the GitHub App private 
key is expected to be provided (reusing `private_key` vs `key_file`, and 
whether it must be PEM). Updating the docstring to explicitly state the 
required key field(s) and format will help avoid misconfiguration.
   ```suggestion
       * ``github_app_id`` — GitHub App ID used for GitHub App authentication. 
Requires the GitHub App
         private key to be provided as a PEM-encoded key via either 
``private_key`` (inline) or
         ``key_file`` (path to key file).
       * ``github_installation_id`` — GitHub App installation ID used for 
GitHub App authentication.
   ```



##########
providers/git/pyproject.toml:
##########
@@ -64,6 +64,13 @@ dependencies = [
     "GitPython>=3.1.44",
 ]
 
+# The optional dependencies should be modified in place in the generated file
+# Any change in the dependencies is preserved when the file is regenerated
+[project.optional-dependencies]
+github = [
+    "pygithub>=2.1.1",

Review Comment:
   In `pyproject.toml` most providers use the canonical package name casing for 
PyPI requirements (e.g. `PyGithub>=2.1.1` in 
`providers/github/pyproject.toml`). To match that convention and avoid 
confusion, consider changing this optional dependency from `pygithub` to 
`PyGithub` (and regenerate/update the lockfile accordingly).
   ```suggestion
       "PyGithub>=2.1.1",
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to