Skip to content

Refactor collecting-PR script for release note #1951

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Nov 2, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 17 additions & 16 deletions scripts/retrieve_prs.py → tools/retrieve_prs.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
"""Collect the PRs between two specified tags or commits and
output the commit titles, PR numbers, and labels in a json file.
Usage: python tools/release_notes/retrieve_prs.py tags/v0.10.0 \
18685a517ae68353b05b9a0ede5343df31525c76 --file data.json
"""
import argparse
import json
import re
import sys
import argparse
import subprocess
from collections import namedtuple
from os.path import expanduser

import requests


Features = namedtuple(
"Features",
[
Expand All @@ -19,10 +24,7 @@


def _run_cmd(cmd):
try:
return subprocess.check_output(cmd).strip()
except Exception:
return None
return subprocess.check_output(cmd).decode('utf-8').strip()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: This would fail if the commit message is not encoded with utf-8. (i.e. if user configures their git with non-uff commit message https://www.git-tower.com/help/guides/faq-and-tips/faq/encoding/windows)

The script needs to handle such case, while logging the failure and keep processing the rest of commits.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. We need a way to detect the correct encoding from the result of check_output.
  2. The for loop in the main function has to handle error properly and notify the user.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will follow-up the case in the future PR.



def commit_title(commit_hash):
Expand Down Expand Up @@ -57,11 +59,9 @@ def get_ghstack_token():


def run_query(query):
request = requests.post("https://github.com/api/graphql", json={"query": query}, headers=headers)
if request.status_code == 200:
return request.json()
else:
raise Exception("Query failed to run by returning code of {}. {}".format(request.status_code, query))
response = requests.post("https://github.com/api/graphql", json={"query": query}, headers=headers)
response.raise_for_status()
return response.json()


def gh_labels(pr_number):
Expand Down Expand Up @@ -108,8 +108,11 @@ def get_commits_between(base_version, new_version):
return hashes, titles


def _parse_args(args):
parser = argparse.ArgumentParser()
def _parse_args(args=None):
parser = argparse.ArgumentParser(
description=__doc__,
formatter_class=argparse.RawTextHelpFormatter,
)
parser.add_argument("base_version", type=str, help="starting tag or commit (exclusive)")
parser.add_argument("new_version", type=str, help="final tag or commit (inclusive)")
parser.add_argument("--file", type=str, default="data.json", help="output json file")
Expand All @@ -131,6 +134,4 @@ def _main(args):


if __name__ == "__main__":
# Usage: python scripts/release_notes/retrieve_prs.py tags/v0.10.0 \
# 18685a517ae68353b05b9a0ede5343df31525c76 --file data.json
_main(_parse_args(sys.argv[1:]))
_main(_parse_args())