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 2023/01/02 21:40:42 UTC

[GitHub] [airflow] uranusjr commented on a diff in pull request #28659: check jenkins result state

uranusjr commented on code in PR #28659:
URL: https://github.com/apache/airflow/pull/28659#discussion_r1060194539


##########
airflow/providers/jenkins/hooks/jenkins.py:
##########
@@ -40,21 +39,34 @@ def __init__(self, conn_id: str = default_conn_name) -> None:
         # connection.extra contains info about using https (true) or http (false)
         if to_boolean(connection.extra):
             connection_prefix = "https"
-        url = f"{connection_prefix}://{connection.host}:{connection.port}"
+        url = f"{connection_prefix}://{connection.host}:{connection.port}/{connection.schema}"
         self.log.info("Trying to connect to %s", url)
         self.jenkins_server = jenkins.Jenkins(url, connection.login, connection.password)
 
     def get_jenkins_server(self) -> jenkins.Jenkins:
         """Get jenkins server"""
         return self.jenkins_server
 
+    def get_latest_build_number(self, job_name) -> int:
+        try:
+            self.log.info("Build number not specified, getting latest build info from Jenkins")
+            job_info = self.jenkins_server.get_job_info(job_name)
+            return job_info["lastBuild"]["number"]
+        except jenkins.JenkinsException as err:
+            raise AirflowException(f"Jenkins call failed with error : {err}")

Review Comment:
   Do not catch this, let the Jenkins error bubble. (Same for other functions.)



##########
airflow/providers/jenkins/sensors/jenkins.py:
##########
@@ -42,21 +43,28 @@ def __init__(
         jenkins_connection_id: str,
         job_name: str,
         build_number: int | None = None,
+        target_states: Iterable[str],
         **kwargs,
     ):
         super().__init__(**kwargs)
         self.job_name = job_name
         self.build_number = build_number
         self.jenkins_connection_id = jenkins_connection_id
+        self.target_states = target_states or ["SUCCESS", "FAILED"]
 
     def poke(self, context: Context) -> bool:
         self.log.info("Poking jenkins job %s", self.job_name)
         hook = JenkinsHook(self.jenkins_connection_id)
-        is_building = hook.get_build_building_state(self.job_name, self.build_number)
+        build_number = self.build_number or hook.get_latest_build_number(self.job_name)
+        is_building = hook.get_build_building_state(self.job_name, build_number)
 
         if is_building:
             self.log.info("Build still ongoing!")
             return False
-        else:
-            self.log.info("Build is finished.")
+
+        build_result = hook.get_build_result(self.job_name, build_number)
+        self.log.info(f"Build is finished, result is {build_result}.")
+        if build_result in self.target_states:
             return True
+        else:
+            raise AirflowException("The build result does not meet the target states.")

Review Comment:
   Can this be more specific? Can the user do something to fix this, or is this an impossible situation the user should tell the Jenkins admin?



-- 
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