villebro commented on a change in pull request #11469:
URL: 
https://github.com/apache/incubator-superset/pull/11469#discussion_r514102317



##########
File path: RELEASING/changelog.py
##########
@@ -0,0 +1,254 @@
+#
+# 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 re
+from dataclasses import dataclass
+from time import sleep
+from typing import Any, Dict, List, Optional, Union
+from urllib import request
+from urllib.error import HTTPError
+
+import click
+
+try:
+    import click
+except ModuleNotFoundError:
+    exit("Click is a required dependency for this script")

Review comment:
       As `click` is a requirement in `requirements/base.txt`, can't we just 
assume this is installed?

##########
File path: RELEASING/changelog.py
##########
@@ -0,0 +1,254 @@
+#
+# 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 re
+from dataclasses import dataclass
+from time import sleep
+from typing import Any, Dict, List, Optional, Union
+from urllib import request
+from urllib.error import HTTPError
+
+import click
+
+try:
+    import click
+except ModuleNotFoundError:
+    exit("Click is a required dependency for this script")
+
+
+@dataclass
+class GitLog:
+    """
+    Represents a git log entry
+    """
+
+    sha: str
+    author: str
+    time: str
+    message: str
+    pr_number: Union[int, None] = None
+
+    def __eq__(self, other: object) -> bool:
+        """ A log entry is considered equal if it has the same PR number """
+        if isinstance(other, self.__class__):
+            return other.pr_number == self.pr_number
+        return False
+
+    def __repr__(self):
+        return f"[{self.pr_number}]: {self.message} {self.time} {self.author}"
+
+
+class GitChangeLog:
+    """
+    Helper class to output a list of logs entries on a superset changelog 
format
+
+    We want to map a git author to a github login, for that we call github's 
API
+    """
+
+    def __init__(self, logs: List[GitLog]) -> None:
+        self._logs = logs
+        self._github_login_cache: Dict[str, Optional[str]] = {}
+        self._wait = 10
+
+    def _wait_github_rate_limit(self) -> None:
+        """
+        Waits for available rate limit slots on the github API
+        """
+        while True:
+            rate_limit_payload = self._fetch_github_rate_limit()
+            if rate_limit_payload["rate"]["remaining"] > 1:
+                break
+            print(f".", end="", flush=True)

Review comment:
       Nit:
   ```suggestion
               print(".", end="", flush=True)
   ```
   We should probably also add a `print()` after the `while` to not continue 
the subsequent outputs from the same line.

##########
File path: RELEASING/changelog.py
##########
@@ -0,0 +1,254 @@
+#
+# 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 re
+from dataclasses import dataclass
+from time import sleep
+from typing import Any, Dict, List, Optional, Union
+from urllib import request
+from urllib.error import HTTPError
+
+import click
+
+try:
+    import click
+except ModuleNotFoundError:
+    exit("Click is a required dependency for this script")
+
+
+@dataclass
+class GitLog:
+    """
+    Represents a git log entry
+    """
+
+    sha: str
+    author: str
+    time: str
+    message: str
+    pr_number: Union[int, None] = None
+
+    def __eq__(self, other: object) -> bool:
+        """ A log entry is considered equal if it has the same PR number """
+        if isinstance(other, self.__class__):
+            return other.pr_number == self.pr_number
+        return False
+
+    def __repr__(self):
+        return f"[{self.pr_number}]: {self.message} {self.time} {self.author}"
+
+
+class GitChangeLog:
+    """
+    Helper class to output a list of logs entries on a superset changelog 
format
+
+    We want to map a git author to a github login, for that we call github's 
API
+    """
+
+    def __init__(self, logs: List[GitLog]) -> None:
+        self._logs = logs
+        self._github_login_cache: Dict[str, Optional[str]] = {}
+        self._wait = 10
+
+    def _wait_github_rate_limit(self) -> None:
+        """
+        Waits for available rate limit slots on the github API
+        """
+        while True:
+            rate_limit_payload = self._fetch_github_rate_limit()
+            if rate_limit_payload["rate"]["remaining"] > 1:
+                break
+            print(f".", end="", flush=True)
+            sleep(self._wait)
+
+    def _fetch_github_rate_limit(self) -> Dict[str, Any]:
+        """
+        Fetches current github rate limit info
+        """
+        with request.urlopen(f"https://api.github.com/rate_limit";) as response:
+            payload = json.loads(response.read())
+        return payload
+
+    def _fetch_github_pr(self, pr_number) -> Dict[str, Any]:
+        """
+        Fetches a github PR info
+        """
+        payload = {}
+        try:
+            self._wait_github_rate_limit()
+            with request.urlopen(
+                
f"https://api.github.com/repos/apache/incubator-superset/pulls/";
+                f"{pr_number}"
+            ) as response:
+                payload = json.loads(response.read())
+        except HTTPError as ex:
+            print(f"{ex}", flush=True)
+        return payload
+
+    def _get_github_login(self, git_log: GitLog) -> Optional[str]:
+        """
+        Tries to fetch a github login (username) from a git author
+        """
+        author_name = git_log.author
+        github_login = self._github_login_cache.get(author_name)
+        if github_login:
+            return github_login
+        pr_info = self._fetch_github_pr(git_log.pr_number)
+        if pr_info:
+            github_login = pr_info["user"]["login"]
+        else:
+            github_login = author_name
+        # set cache
+        self._github_login_cache[author_name] = github_login
+        return github_login
+
+    def __repr__(self):
+        result = ""
+        for i, log in enumerate(self._logs):
+            github_login = self._get_github_login(log)
+            if not github_login:
+                github_login = log.author
+            result = result + (
+                f"- [#{log.pr_number}]"
+                
f"(https://github.com/apache/incubator-superset/pull/{log.pr_number}) "
+                f"{log.message} (@{github_login})\n"
+            )
+            print(f"\r {i}/{len(self._logs)}", end="", flush=True)
+        return result
+
+
+class GitLogs:
+    """
+    Manages git log entries from a specific branch/tag
+
+    Can compare git log entries by PR number
+    """
+
+    def __init__(self, branch_name: str) -> None:
+        self._branch_name = branch_name
+        self._logs: List[GitLog] = []
+
+    @property
+    def branch_name(self) -> str:
+        return self._branch_name
+
+    @property
+    def logs(self) -> List[GitLog]:
+        return self._logs
+
+    def fetch(self):
+        self._logs = list(map(self._parse_log, self._git_logs()))[::-1]
+
+    def diff(self, git_logs: "GitLogs") -> List[GitLog]:
+        return [log for log in git_logs.logs if log not in self._logs]
+
+    def __repr__(self):
+        return f"{self._branch_name}, Log count:{len(self._logs)}"
+
+    def _git_checkout(self):
+        os.popen(f"git checkout {self._branch_name}").read()
+
+    def _git_logs(self) -> List[str]:
+        self._git_checkout()
+        return (
+            os.popen('git --no-pager log --pretty=format:"%h|%an|%ad|%s|"')
+            .read()
+            .split("\n")
+        )
+
+    def _parse_log(self, log_item: str) -> GitLog:
+        pr_number = None
+        split_log_item = log_item.split("|")
+        # parse the PR number from the log message
+        match = re.match(".*\(\#(.*)\)", split_log_item[3])

Review comment:
       nit:
   ```suggestion
           match = re.match(".*\(\#(\d*)\)", split_log_item[3])
   ```




----------------------------------------------------------------
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:
[email protected]



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to