|
17 | 17 | import pandas as pd
|
18 | 18 | import pathlib
|
19 | 19 |
|
20 |
| -class ChangeType(IntEnum): |
21 |
| - UNKNOWN = 0 |
22 |
| - DELETED = 1 |
23 |
| - ADDED = 2 |
24 |
| - CHANGED = 3 |
25 |
| - |
26 |
| - |
27 |
| -def get_commit_link(name): |
28 |
| - """Return a string with a link to the last commit for the given |
29 |
| - API Name. |
30 |
| - args: |
31 |
| - name (str): The name of the api. |
32 |
| - """ |
| 20 | +from changesummary import ChangeType |
| 21 | + |
| 22 | +SCRIPTS_DIR = pathlib.Path(__file__).parent.resolve() |
| 23 | +CHANGE_SUMMARY_DIR = SCRIPTS_DIR / "temp" |
33 | 24 |
|
34 |
| - url = "https://github.com/googleapis/google-api-python-client/commit/" |
35 |
| - sha = None |
36 |
| - api_link = "" |
37 | 25 |
|
38 |
| - file_path = pathlib.Path(directory).joinpath("{0}.sha".format(name)) |
39 |
| - if file_path.is_file(): |
40 |
| - with open(file_path, "r") as f: |
41 |
| - sha = f.readline().rstrip() |
42 |
| - if sha: |
43 |
| - api_link = "[{0}]({1}{2})".format(" [More details]", url, sha) |
| 26 | +class BuildPrBody: |
| 27 | + """Represents the PR body which contains the change summary between 2 |
| 28 | + directories containing artifacts. |
| 29 | + """ |
44 | 30 |
|
45 |
| - return api_link |
| 31 | + def __init__(self, change_summary_directory): |
| 32 | + """Initializes an instance of a BuildPrBody. |
| 33 | +
|
| 34 | + Args: |
| 35 | + change_summary_directory (str): The relative path to the directory |
| 36 | + which contains the change summary output. |
| 37 | + """ |
| 38 | + self._change_summary_directory = change_summary_directory |
| 39 | + |
| 40 | + def get_commit_uri(self, name): |
| 41 | + """Return a uri to the last commit for the given API Name. |
| 42 | +
|
| 43 | + Args: |
| 44 | + name (str): The name of the api. |
| 45 | + """ |
| 46 | + |
| 47 | + url = "https://github.com/googleapis/google-api-python-client/commit/" |
| 48 | + sha = None |
| 49 | + api_link = "" |
| 50 | + |
| 51 | + file_path = pathlib.Path(self._change_summary_directory) / "{0}.sha".format(name) |
| 52 | + if file_path.is_file(): |
| 53 | + with open(file_path, "r") as f: |
| 54 | + sha = f.readline().rstrip() |
| 55 | + if sha: |
| 56 | + api_link = "{0}{1}".format(url, sha) |
| 57 | + |
| 58 | + return api_link |
| 59 | + |
| 60 | + def generate_pr_body(self): |
| 61 | + """ |
| 62 | + Generates a PR body given an input file `'allapis.dataframe'` and |
| 63 | + writes it to disk with file name `'allapis.summary'`. |
| 64 | + """ |
| 65 | + directory = pathlib.Path(self._change_summary_directory) |
| 66 | + dataframe = pd.read_csv(directory / "allapis.dataframe") |
| 67 | + dataframe["Version"] = dataframe["Version"].astype(str) |
| 68 | + |
| 69 | + dataframe["Commit"] = np.vectorize(self.get_commit_uri)(dataframe["Name"]) |
| 70 | + |
| 71 | + stable_and_breaking = ( |
| 72 | + dataframe[ |
| 73 | + dataframe["IsStable"] & (dataframe["ChangeType"] == ChangeType.DELETED) |
| 74 | + ][["Name", "Version", "Commit"]] |
| 75 | + .drop_duplicates() |
| 76 | + .agg(" ".join, axis=1) |
| 77 | + .values |
| 78 | + ) |
| 79 | + |
| 80 | + prestable_and_breaking = ( |
| 81 | + dataframe[ |
| 82 | + (dataframe["IsStable"] == False) |
| 83 | + & (dataframe["ChangeType"] == ChangeType.DELETED) |
| 84 | + ][["Name", "Version", "Commit"]] |
| 85 | + .drop_duplicates() |
| 86 | + .agg(" ".join, axis=1) |
| 87 | + .values |
| 88 | + ) |
| 89 | + |
| 90 | + all_apis = ( |
| 91 | + dataframe[["Summary", "Commit"]] |
| 92 | + .drop_duplicates() |
| 93 | + .agg(" ".join, axis=1) |
| 94 | + .values |
| 95 | + ) |
| 96 | + |
| 97 | + with open(directory / "allapis.summary", "w") as f: |
| 98 | + if len(stable_and_breaking) > 0: |
| 99 | + f.writelines( |
| 100 | + [ |
| 101 | + "## Deleted keys were detected in the following stable discovery artifacts:\n", |
| 102 | + "\n".join(stable_and_breaking), |
| 103 | + "\n\n", |
| 104 | + ] |
| 105 | + ) |
| 106 | + |
| 107 | + if len(prestable_and_breaking) > 0: |
| 108 | + f.writelines( |
| 109 | + [ |
| 110 | + "## Deleted keys were detected in the following pre-stable discovery artifacts:\n", |
| 111 | + "\n".join(prestable_and_breaking), |
| 112 | + "\n\n", |
| 113 | + ] |
| 114 | + ) |
| 115 | + |
| 116 | + if len(all_apis) > 0: |
| 117 | + f.writelines( |
| 118 | + [ |
| 119 | + "## Discovery Artifact Change Summary:\n", |
| 120 | + "\n".join(all_apis), |
| 121 | + "\n", |
| 122 | + ] |
| 123 | + ) |
46 | 124 |
|
47 | 125 |
|
48 | 126 | if __name__ == "__main__":
|
49 |
| - directory = pathlib.Path("temp") |
50 |
| - dataframe = pd.read_csv("temp/allapis.dataframe") |
51 |
| - dataframe["Version"] = dataframe["Version"].astype(str) |
52 |
| - |
53 |
| - dataframe["Commit"] = np.vectorize(get_commit_link)(dataframe["Name"]) |
54 |
| - |
55 |
| - stable_and_breaking = ( |
56 |
| - dataframe[ |
57 |
| - dataframe["IsStable"] |
58 |
| - & (dataframe["ChangeType"] == ChangeType.DELETED) |
59 |
| - ][["Name", "Version", "Commit"]] |
60 |
| - .drop_duplicates() |
61 |
| - .agg("".join, axis=1) |
62 |
| - .values |
63 |
| - ) |
64 |
| - |
65 |
| - prestable_and_breaking = ( |
66 |
| - dataframe[ |
67 |
| - (dataframe["IsStable"] == False) |
68 |
| - & (dataframe["ChangeType"] == ChangeType.DELETED) |
69 |
| - ][["Name", "Version", "Commit"]] |
70 |
| - .drop_duplicates() |
71 |
| - .agg("".join, axis=1) |
72 |
| - .values |
73 |
| - ) |
74 |
| - |
75 |
| - all_apis = ( |
76 |
| - dataframe[["Name", "Version", "Commit"]] |
77 |
| - .drop_duplicates() |
78 |
| - .agg("".join, axis=1) |
79 |
| - .values |
80 |
| - ) |
81 |
| - |
82 |
| - with open(directory / "allapis.summary", "w") as f: |
83 |
| - if len(stable_and_breaking) > 0: |
84 |
| - f.writelines( |
85 |
| - [ |
86 |
| - "## Deleted keys were detected in the following stable discovery artifacts:\n", |
87 |
| - "\n".join(stable_and_breaking), |
88 |
| - "\n\n", |
89 |
| - ] |
90 |
| - ) |
91 |
| - |
92 |
| - if len(prestable_and_breaking) > 0: |
93 |
| - f.writelines( |
94 |
| - [ |
95 |
| - "## Deleted keys were detected in the following pre-stable discovery artifacts:\n", |
96 |
| - "\n".join(prestable_and_breaking), |
97 |
| - "\n\n", |
98 |
| - ] |
99 |
| - ) |
100 |
| - |
101 |
| - if len(all_apis) > 0: |
102 |
| - f.writelines( |
103 |
| - ["## Discovery Artifact Change Summary:\n", "\n".join(all_apis), "\n"] |
104 |
| - ) |
| 127 | + BuildPrBody(CHANGE_SUMMARY_DIR).generate_pr_body() |
0 commit comments