Skip to content
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ def scripts(session):
"--cov=scripts",
"--cov-config=.coveragerc",
"--cov-report=",
"--cov-fail-under=80",
"--cov-fail-under=91",
"scripts",
*session.posargs,
)
181 changes: 102 additions & 79 deletions scripts/buildprbody.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,88 +17,111 @@
import pandas as pd
import pathlib

class ChangeType(IntEnum):
UNKNOWN = 0
DELETED = 1
ADDED = 2
CHANGED = 3


def get_commit_link(name):
"""Return a string with a link to the last commit for the given
API Name.
args:
name (str): The name of the api.
"""
from changesummary import ChangeType

SCRIPTS_DIR = pathlib.Path(__file__).parent.resolve()
CHANGE_SUMMARY_DIR = SCRIPTS_DIR / "temp"

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

file_path = pathlib.Path(directory).joinpath("{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}{2})".format(" [More details]", url, sha)
class BuildPrBody:
"""Represents the PR body which contains the change summary between 2
directories containing artifacts.
"""

return api_link
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__":
directory = pathlib.Path("temp")
dataframe = pd.read_csv("temp/allapis.dataframe")
dataframe["Version"] = dataframe["Version"].astype(str)

dataframe["Commit"] = np.vectorize(get_commit_link)(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[["Name", "Version", "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"]
)
BuildPrBody(CHANGE_SUMMARY_DIR).generate_pr_body()
65 changes: 65 additions & 0 deletions scripts/buildprbody_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# 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
#
# http://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.

"""BuildPrBody tests."""

__author__ = "[email protected] (Anthonios Partheniou)"

import pathlib
import shutil
import unittest

from buildprbody import BuildPrBody
from changesummary import ChangeType

SCRIPTS_DIR = pathlib.Path(__file__).parent.resolve()
CHANGE_SUMMARY_DIR = SCRIPTS_DIR / "test_resources" / "buildprbody_resources"

EXPECTED_PR_BODY_OUTPUT = """\
## Deleted keys were detected in the following stable discovery artifacts:
bigquery v2 https://github.com/googleapis/google-api-python-client/commit/123
cloudtasks v2 https://github.com/googleapis/google-api-python-client/commit/456

## Discovery Artifact Change Summary:
feat(bigquery): update the api https://github.com/googleapis/google-api-python-client/commit/123
feat(cloudtasks): update the api https://github.com/googleapis/google-api-python-client/commit/456
feat(drive): update the api https://github.com/googleapis/google-api-python-client/commit/789
"""


class TestBuildPrBody(unittest.TestCase):
def setUp(self):
self.buildprbody = BuildPrBody(change_summary_directory=CHANGE_SUMMARY_DIR)

def test_get_commit_uri_returns_correct_string(self):
base_uri = "https://github.com/googleapis/google-api-python-client/commit/"

expected_uri = "".join([base_uri, "123"])
result = self.buildprbody.get_commit_uri(name="bigquery")
self.assertEqual(result, expected_uri)

expected_uri = "".join([base_uri, "456"])
result = self.buildprbody.get_commit_uri(name="cloudtasks")
self.assertEqual(result, expected_uri)

expected_uri = "".join([base_uri, "789"])
result = self.buildprbody.get_commit_uri(name="drive")
self.assertEqual(result, expected_uri)

def test_generate_pr_body(self):
self.buildprbody.generate_pr_body()

with open(CHANGE_SUMMARY_DIR / "allapis.summary") as f:
pr_body = "".join(f.readlines())
self.assertEqual(pr_body, EXPECTED_PR_BODY_OUTPUT)
Loading