CathyZhang0822 commented on a change in pull request #11957: [MXNET-691]Add 
LabelBot functionality -- adding labels
URL: https://github.com/apache/incubator-mxnet/pull/11957#discussion_r207108059
 
 

 ##########
 File path: mxnet-bot/LabelBotAddLabels/label_bot.py
 ##########
 @@ -0,0 +1,140 @@
+# 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
+from botocore.vendored import requests
+import re
+import logging
+import secret_manager
+
+logger = logging.getLogger()
+logger.setLevel(logging.INFO)
+logging.getLogger('boto3').setLevel(logging.CRITICAL)
+logging.getLogger('botocore').setLevel(logging.CRITICAL)
+
+# Comment: "@mxnet-label-bot, please add labels: bug, test"
+# Then, this script will recognize this comment and add labels
+secret = json.loads(secret_manager.get_secret())
+GITHUB_USER = secret["GITHUB_USER"]
+GITHUB_OAUTH_TOKEN = secret["GITHUB_OAUTH_TOKEN"]
+REPO = os.environ.get("REPO")
+AUTH = (GITHUB_USER, GITHUB_OAUTH_TOKEN)
+
+
+def tokenize(string):
+    substring = string[string.find('[')+1: string.rfind(']')] 
+    labels = [' '.join(label.split()) for label in substring.split(',')]
+    logger.info("recognize labels: {}".format(", ".join(labels)))
+    return labels
+
+
+def clean_string(raw_string, sub_string):
+    # covert all non-alphanumeric characters from raw_string to sub_string
+    cleans = re.sub("[^0-9a-zA-Z]", sub_string, raw_string)
+    return cleans.lower()
+
+
+def count_pages(obj, state='all'):
+    # This method is to count how many pages of issues/labels in total
+    # obj could be "issues"/"labels"
+    # state could be "open"/"closed"/"all", available to issues
+    assert obj in set(["issues", "labels"]), "Invalid Input!"
+    url = 'https://api.github.com/repos/{}/{}'.format(REPO, obj)
+    if obj == 'issues':
+        response = requests.get(url, {'state': state},
+                                auth=AUTH)
+    else:
+        response = requests.get(url, auth=AUTH)
+    assert response.status_code == 200, response.status_code
+    if "link" not in response.headers:
+        return 1
+    # response.headers['link'] will looks like:
+    # 
<https://api.github.com/repositories/34864402/issues?state=all&page=387>; 
rel="last"
+    # In this case we need to extrac '387' as the count of pages
+    return int(clean_string(response.headers['link'], " ").split()[-3])
+
+
+def find_notifications():
+    data = []
+    pages = count_pages("issues")
+    for page in range(1, pages+1):
+        url = 'https://api.github.com/repos/' + REPO + '/issues?page=' + 
str(page) \
+            + '&per_page=30'.format(repo=REPO)
+        response = requests.get(url,
+                                {'state': 'open',
+                                 'base': 'master',
+                                 'sort': 'created',
+                                 'direction': 'desc'},
+                                auth=AUTH)
+        for item in response.json():
+            # limit the amount of unlabeled issues per execution
+            if len(data) >= 50:
+                break
+            if "pull_request" in item:
+                continue
+            if not item['labels']:
+                if item['comments'] != 0:
+                    labels = []
+                    comments_url = 
"https://api.github.com/repos/{}/issues/{}/comments".format(REPO,item['number'])
+                    comments = requests.get(comments_url, auth=AUTH).json()
+                    for comment in comments:
+                        if "@mxnet-label-bot" in comment['body']:
+                            labels += tokenize(comment['body'])
+                            logger.info("issue: {}, comment: 
{}".format(str(item['number']),comment['body']))
+                    if labels != []:
+                        data.append({"issue": item['number'], "labels": 
labels})
+    return data
+
+
+def all_labels():
 
 Review comment:
   descriptions added
   

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to