You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by GitBox <gi...@apache.org> on 2022/01/26 17:20:12 UTC

[GitHub] [airflow] pateash commented on a change in pull request #21076: Adding GitHub provider

pateash commented on a change in pull request #21076:
URL: https://github.com/apache/airflow/pull/21076#discussion_r792868988



##########
File path: airflow/providers/github/sensors/github.py
##########
@@ -0,0 +1,157 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+from typing import Any, Callable, Optional
+
+from github import GithubException
+
+from airflow import AirflowException
+from airflow.providers.github.operators.github import GithubOperator
+from airflow.sensors.base import BaseSensorOperator
+from airflow.utils.context import Context
+
+
+class GithubSensor(BaseSensorOperator):
+    """
+    Base GithubSensor which can monitor for any change.
+
+    :param github_conn_id: reference to a pre-defined Github Connection
+    :type github_conn_id: str
+    :param method_name: method name from PyGithub to be executed
+    :type method_name: str
+    :param method_params: parameters for the method method_name
+    :type method_params: dict
+    :param result_processor: function that return boolean and act as a sensor response
+    :type result_processor: function
+    """
+
+    def __init__(
+        self,
+        *,
+        method_name: str,
+        github_conn_id: str = 'github_default',
+        method_params: Optional[dict] = None,
+        result_processor: Optional[Callable] = None,
+        **kwargs,
+    ) -> None:
+        super().__init__(**kwargs)
+        self.github_conn_id = github_conn_id
+        self.result_processor = None
+        if result_processor is not None:
+            self.result_processor = result_processor
+        self.method_name = method_name
+        self.method_params = method_params
+        self.github_operator = GithubOperator(
+            task_id=self.task_id,
+            github_conn_id=self.github_conn_id,
+            github_method=self.method_name,
+            github_method_args=self.method_params,
+            result_processor=self.result_processor,
+        )
+
+    def poke(self, context: Context) -> bool:
+        return self.github_operator.execute(context=context)
+
+
+class BaseGithubRepositorySensor(GithubSensor):

Review comment:
       Something like this, result_processor must return **boolean** for sensor to work.
   
   
   ```
    github_tag_sensor = GithubSensor(
               task_id='github_sensor_test',
               method_name='method_name',
               method_params={},
               result_processor=lambda x: x == 'x',
               timeout=60,
               poke_interval=10,
               dag=self.dag,
           )
   
   ```




-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org