Skip to content

Commit 6a88422

Browse files
authored
chore: Add conventional commits to PR body of discovery document update (#1314)
1 parent bae2ce9 commit 6a88422

18 files changed

+488
-18410
lines changed

noxfile.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ def scripts(session):
105105
"--cov=scripts",
106106
"--cov-config=.coveragerc",
107107
"--cov-report=",
108-
"--cov-fail-under=80",
108+
"--cov-fail-under=91",
109109
"scripts",
110110
*session.posargs,
111111
)

scripts/buildprbody.py

Lines changed: 102 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -17,88 +17,111 @@
1717
import pandas as pd
1818
import pathlib
1919

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"
3324

34-
url = "https://github.com/googleapis/google-api-python-client/commit/"
35-
sha = None
36-
api_link = ""
3725

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+
"""
4430

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+
)
46124

47125

48126
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()

scripts/buildprbody_test.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# Copyright 2021 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
"""BuildPrBody tests."""
16+
17+
__author__ = "[email protected] (Anthonios Partheniou)"
18+
19+
import pathlib
20+
import shutil
21+
import unittest
22+
23+
from buildprbody import BuildPrBody
24+
from changesummary import ChangeType
25+
26+
SCRIPTS_DIR = pathlib.Path(__file__).parent.resolve()
27+
CHANGE_SUMMARY_DIR = SCRIPTS_DIR / "test_resources" / "buildprbody_resources"
28+
29+
EXPECTED_PR_BODY_OUTPUT = """\
30+
## Deleted keys were detected in the following stable discovery artifacts:
31+
bigquery v2 https://github.com/googleapis/google-api-python-client/commit/123
32+
cloudtasks v2 https://github.com/googleapis/google-api-python-client/commit/456
33+
34+
## Discovery Artifact Change Summary:
35+
feat(bigquery): update the api https://github.com/googleapis/google-api-python-client/commit/123
36+
feat(cloudtasks): update the api https://github.com/googleapis/google-api-python-client/commit/456
37+
feat(drive): update the api https://github.com/googleapis/google-api-python-client/commit/789
38+
"""
39+
40+
41+
class TestBuildPrBody(unittest.TestCase):
42+
def setUp(self):
43+
self.buildprbody = BuildPrBody(change_summary_directory=CHANGE_SUMMARY_DIR)
44+
45+
def test_get_commit_uri_returns_correct_string(self):
46+
base_uri = "https://github.com/googleapis/google-api-python-client/commit/"
47+
48+
expected_uri = "".join([base_uri, "123"])
49+
result = self.buildprbody.get_commit_uri(name="bigquery")
50+
self.assertEqual(result, expected_uri)
51+
52+
expected_uri = "".join([base_uri, "456"])
53+
result = self.buildprbody.get_commit_uri(name="cloudtasks")
54+
self.assertEqual(result, expected_uri)
55+
56+
expected_uri = "".join([base_uri, "789"])
57+
result = self.buildprbody.get_commit_uri(name="drive")
58+
self.assertEqual(result, expected_uri)
59+
60+
def test_generate_pr_body(self):
61+
self.buildprbody.generate_pr_body()
62+
63+
with open(CHANGE_SUMMARY_DIR / "allapis.summary") as f:
64+
pr_body = "".join(f.readlines())
65+
self.assertEqual(pr_body, EXPECTED_PR_BODY_OUTPUT)

0 commit comments

Comments
 (0)