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