|
| 1 | +import json |
| 2 | +import locale |
| 3 | +import os |
| 4 | +import re |
| 5 | +import subprocess |
| 6 | +from collections import namedtuple |
| 7 | +from os.path import expanduser |
| 8 | + |
| 9 | +import requests |
| 10 | + |
| 11 | + |
| 12 | +Features = namedtuple( |
| 13 | + "Features", |
| 14 | + [ |
| 15 | + "title", |
| 16 | + "body", |
| 17 | + "pr_number", |
| 18 | + "files_changed", |
| 19 | + "labels", |
| 20 | + ], |
| 21 | +) |
| 22 | + |
| 23 | + |
| 24 | +def dict_to_features(dct): |
| 25 | + return Features( |
| 26 | + title=dct["title"], |
| 27 | + body=dct["body"], |
| 28 | + pr_number=dct["pr_number"], |
| 29 | + files_changed=dct["files_changed"], |
| 30 | + labels=dct["labels"], |
| 31 | + ) |
| 32 | + |
| 33 | + |
| 34 | +def features_to_dict(features): |
| 35 | + return dict(features._asdict()) |
| 36 | + |
| 37 | + |
| 38 | +def run(command): |
| 39 | + """Returns (return-code, stdout, stderr)""" |
| 40 | + p = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) |
| 41 | + output, err = p.communicate() |
| 42 | + rc = p.returncode |
| 43 | + enc = locale.getpreferredencoding() |
| 44 | + output = output.decode(enc) |
| 45 | + err = err.decode(enc) |
| 46 | + return rc, output.strip(), err.strip() |
| 47 | + |
| 48 | + |
| 49 | +def commit_body(commit_hash): |
| 50 | + cmd = f"git log -n 1 --pretty=format:%b {commit_hash}" |
| 51 | + ret, out, err = run(cmd) |
| 52 | + return out if ret == 0 else None |
| 53 | + |
| 54 | + |
| 55 | +def commit_title(commit_hash): |
| 56 | + cmd = f"git log -n 1 --pretty=format:%s {commit_hash}" |
| 57 | + ret, out, err = run(cmd) |
| 58 | + return out if ret == 0 else None |
| 59 | + |
| 60 | + |
| 61 | +def commit_files_changed(commit_hash): |
| 62 | + cmd = f"git diff-tree --no-commit-id --name-only -r {commit_hash}" |
| 63 | + ret, out, err = run(cmd) |
| 64 | + return out.split("\n") if ret == 0 else None |
| 65 | + |
| 66 | + |
| 67 | +def parse_pr_number(body, commit_hash, title): |
| 68 | + regex = r"(#[0-9]+)" |
| 69 | + matches = re.findall(regex, title) |
| 70 | + if len(matches) == 0: |
| 71 | + if "revert" not in title.lower() and "updating submodules" not in title.lower(): |
| 72 | + print(f"[{commit_hash}: {title}] Could not parse PR number, ignoring PR") |
| 73 | + return None |
| 74 | + if len(matches) > 1: |
| 75 | + print(f"[{commit_hash}: {title}] Got two PR numbers, using the last one") |
| 76 | + return matches[-1][1:] |
| 77 | + return matches[0][1:] |
| 78 | + |
| 79 | + |
| 80 | +def get_ghstack_token(): |
| 81 | + pattern = "github_oauth = (.*)" |
| 82 | + with open(expanduser("~/.ghstackrc"), "r+") as f: |
| 83 | + config = f.read() |
| 84 | + matches = re.findall(pattern, config) |
| 85 | + if len(matches) == 0: |
| 86 | + raise RuntimeError("Can't find a github oauth token") |
| 87 | + return matches[0] |
| 88 | + |
| 89 | + |
| 90 | +token = get_ghstack_token() |
| 91 | +headers = {"Authorization": f"token {token}"} |
| 92 | + |
| 93 | + |
| 94 | +def run_query(query): |
| 95 | + request = requests.post("https://api.github.com/graphql", json={"query": query}, headers=headers) |
| 96 | + if request.status_code == 200: |
| 97 | + return request.json() |
| 98 | + else: |
| 99 | + raise Exception("Query failed to run by returning code of {}. {}".format(request.status_code, query)) |
| 100 | + |
| 101 | + |
| 102 | +def gh_labels(pr_number): |
| 103 | + query = f""" |
| 104 | + {{ |
| 105 | + repository(owner: "pytorch", name: "vision") {{ |
| 106 | + pullRequest(number: {pr_number}) {{ |
| 107 | + labels(first: 10) {{ |
| 108 | + edges {{ |
| 109 | + node {{ |
| 110 | + name |
| 111 | + }} |
| 112 | + }} |
| 113 | + }} |
| 114 | + }} |
| 115 | + }} |
| 116 | + }} |
| 117 | + """ |
| 118 | + query = run_query(query) |
| 119 | + edges = query["data"]["repository"]["pullRequest"]["labels"]["edges"] |
| 120 | + return [edge["node"]["name"] for edge in edges] |
| 121 | + |
| 122 | + |
| 123 | +def get_features(commit_hash, return_dict=False): |
| 124 | + title, body, files_changed = ( |
| 125 | + commit_title(commit_hash), |
| 126 | + commit_body(commit_hash), |
| 127 | + commit_files_changed(commit_hash), |
| 128 | + ) |
| 129 | + pr_number = parse_pr_number(body, commit_hash, title) |
| 130 | + labels = [] |
| 131 | + if pr_number is not None: |
| 132 | + labels = gh_labels(pr_number) |
| 133 | + result = Features(title, body, pr_number, files_changed, labels) |
| 134 | + if return_dict: |
| 135 | + return features_to_dict(result) |
| 136 | + return result |
| 137 | + |
| 138 | + |
| 139 | +class CommitDataCache: |
| 140 | + def __init__(self, path="results/data.json"): |
| 141 | + self.path = path |
| 142 | + self.data = {} |
| 143 | + if os.path.exists(path): |
| 144 | + self.data = self.read_from_disk() |
| 145 | + |
| 146 | + def get(self, commit): |
| 147 | + if commit not in self.data.keys(): |
| 148 | + # Fetch and cache the data |
| 149 | + self.data[commit] = get_features(commit) |
| 150 | + self.write_to_disk() |
| 151 | + return self.data[commit] |
| 152 | + |
| 153 | + def read_from_disk(self): |
| 154 | + with open(self.path, "r") as f: |
| 155 | + data = json.load(f) |
| 156 | + data = {commit: dict_to_features(dct) for commit, dct in data.items()} |
| 157 | + return data |
| 158 | + |
| 159 | + def write_to_disk(self): |
| 160 | + data = {commit: features._asdict() for commit, features in self.data.items()} |
| 161 | + with open(self.path, "w") as f: |
| 162 | + json.dump(data, f) |
| 163 | + |
| 164 | + |
| 165 | +def get_commits_between(base_version, new_version): |
| 166 | + cmd = f"git merge-base {base_version} {new_version}" |
| 167 | + rc, merge_base, _ = run(cmd) |
| 168 | + assert rc == 0 |
| 169 | + |
| 170 | + # Returns a list of something like |
| 171 | + # b33e38ec47 Allow a higher-precision step type for Vec256::arange (#34555) |
| 172 | + cmd = f"git log --reverse --oneline {merge_base}..{new_version}" |
| 173 | + rc, commits, _ = run(cmd) |
| 174 | + assert rc == 0 |
| 175 | + |
| 176 | + log_lines = commits.split("\n") |
| 177 | + hashes, titles = zip(*[log_line.split(" ", 1) for log_line in log_lines]) |
| 178 | + return hashes, titles |
| 179 | + |
| 180 | + |
| 181 | +def convert_to_dataframes(feature_list): |
| 182 | + import pandas as pd |
| 183 | + |
| 184 | + df = pd.DataFrame.from_records(feature_list, columns=Features._fields) |
| 185 | + return df |
| 186 | + |
| 187 | + |
| 188 | +def main(base_version, new_version): |
| 189 | + hashes, titles = get_commits_between(base_version, new_version) |
| 190 | + |
| 191 | + cdc = CommitDataCache("data.json") |
| 192 | + for idx, commit in enumerate(hashes): |
| 193 | + if idx % 10 == 0: |
| 194 | + print(f"{idx} / {len(hashes)}") |
| 195 | + cdc.get(commit) |
| 196 | + |
| 197 | + return cdc |
| 198 | + |
| 199 | + |
| 200 | +if __name__ == "__main__": |
| 201 | + # d = get_features('2ab93592529243862ce8ad5b6acf2628ef8d0dc8') |
| 202 | + # print(d) |
| 203 | + # hashes, titles = get_commits_between("tags/v0.9.0", "fc852f3b39fe25dd8bf1dedee8f19ea04aa84c15") |
| 204 | + |
| 205 | + # Usage: change the tags below accordingly to the current release, then save the json with |
| 206 | + # cdc.write_to_disk(). |
| 207 | + # Then you can use classify_prs.py (as a notebook) |
| 208 | + # to open the json and generate the release notes semi-automatically. |
| 209 | + cdc = main("tags/v0.9.0", "fc852f3b39fe25dd8bf1dedee8f19ea04aa84c15") |
| 210 | + from IPython import embed |
| 211 | + |
| 212 | + embed() |
0 commit comments