ChaiBapchya commented on a change in pull request #27:
URL: https://github.com/apache/incubator-mxnet-ci/pull/27#discussion_r460556456



##########
File path: services/lambda-label-successful-prs/CIBot.py
##########
@@ -0,0 +1,199 @@
+# 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.
+import json
+import os
+import logging
+import secret_manager
+
+from jenkinsapi.jenkins import Jenkins
+from github import Github
+
+
+class CIBot:
+    def __init__(self,
+                 repo=os.environ.get("repo"),
+                 github_personal_access_token=None,
+                 jenkins_url=os.environ.get("jenkins_url"),
+                 jenkins_username=None,
+                 jenkins_password=None,
+                 apply_secret=True):
+        """
+        Initializes the CI Bot
+        :param repo: GitHub repository that is being referenced
+        :param github_personal_access_token: GitHub authentication token 
(Personal access token)
+        :param jenkins_url: Jenkins URL
+        :param jenkins_username: Jenkins Username for JenkinsAPI
+        :param jenkins_password: Jenkins Password for JenkinsAPI
+        :param apply_secret: GitHub secret credential (Secret credential that 
is unique to a GitHub developer)
+        """
+        self.repo = repo
+        self.github_personal_access_token = github_personal_access_token
+        self.jenkins_username = jenkins_username
+        self.jenkins_password = jenkins_password
+        if apply_secret:
+            self._get_secret()
+        self.all_jobs = None
+        self.jenkins_url = jenkins_url
+        self.prs_labeled = 0
+        self.prs_already_labeled = 0
+        self.prs_status_fail = 0
+        self.prs_unknown_job = 0
+        self.prs_no_build = 0
+
+    def _get_secret(self):
+        """
+        This method is to get secret value from Secrets Manager
+        """
+        secret = json.loads(secret_manager.get_secret())
+        self.github_personal_access_token = 
secret["github_personal_access_token"]
+        self.jenkins_username = secret["jenkins_username"]
+        self.jenkins_password = secret["jenkins_password"]
+
+    def _get_github_object(self):
+        """
+        This method returns github object initialized with Github personal 
access token
+        """
+        github_obj = Github(self.github_personal_access_token)
+        return github_obj
+
+    def _find_all_open_prs(self, github_obj):
+        """
+        This method identifies all the open PRs in the given repository
+        It returns a Paginated list of Pull Request objects sorted in
+        descending order of created date i.e. latest PR first
+        :param github_obj
+        """
+        repo = github_obj.get_repo(self.repo)
+        pulls = repo.get_pulls(state='open', sort='created', direction='desc')
+        return pulls
+
+    def _get_jenkins_obj(self):
+        """
+        This method returns an object of Jenkins instantiated using username, 
password
+        """
+        return Jenkins(self.jenkins_url, username=self.jenkins_username, 
password=self.jenkins_password)
+
+    def _get_checks(self):
+        """
+        This method finds out all the checks that are currently supported as 
part of CI
+        """
+        # for now hardcoding list of checks
+        # ideally use Jenkins API to query list of checks and parse it (bit 
complicated)
+        all_checks = ['clang', 'edge', 'centos-cpu', 'centos-gpu', 
'windows-cpu', 'windows-gpu', 'miscellaneous', 'unix-cpu', 'unix-gpu', 
'website', 'sanity']

Review comment:
       I could use curl/requests to make REST API calls to GitHub [instead of 
Jenkins]. Before doing that, I wanted to verify: 
   2 reasons behind not using Jenkins API and instead stick to GitHub API:
   1. Jenkins API can be a bit restrictive. For e.g. There's a round-about way 
to get names of all the jobs ["checks" equivalent] corresponding to a branch 
[PR]. 
   2. Instead of querying Jenkins server on AWS instance for details, we can 
query GitHub servers for same info instead.
   
   Are there other compelling reasons to use GitHub API over Jenkins API? Just 
asking for my knowledge.




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

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


Reply via email to