Skip to content

Commit

Permalink
feat: include trailers in PR body (#526)
Browse files Browse the repository at this point in the history
* feat: include trailers in PR body

for the two trailers we expect merge-on-green bot to consume.

* fix: update golden log

* chore: trailers arg defaults to empty
  • Loading branch information
SurferJeffAtGoogle authored May 6, 2020
1 parent 756bc4d commit 558bb0d
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 7 deletions.
47 changes: 45 additions & 2 deletions autosynth/change_pusher.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

from autosynth import git, github
from autosynth.log import logger
import re


class AbstractPullRequest(ABC):
Expand Down Expand Up @@ -82,12 +83,13 @@ def push_changes(
self, commit_count: int, branch: str, pr_title: str, synth_log: str = ""
) -> AbstractPullRequest:
git.push_changes(branch)
trailers = _collect_trailers(commit_count)

pr = self._gh.create_pull_request(
self._repository,
branch=branch,
title=pr_title,
body=build_pr_body(synth_log),
body=build_pr_body(synth_log, trailers),
)

# args.synth_path (and api: * labels) only exist in monorepos
Expand Down Expand Up @@ -129,6 +131,8 @@ def push_changes(
subprocess.check_call(["git", "checkout", branch]) # Probably redundant.
with tempfile.NamedTemporaryFile() as message_file:
# Collect the commit messages into a temporary file.
message_file.write("changes triggered by multiple versions\n\n".encode())
message_file.flush()
subprocess.run(
["git", "log", f"-{commit_count}", "--format=* %s%n%b"],
stdout=message_file,
Expand All @@ -148,7 +152,7 @@ def check_if_pr_already_exists(self, branch) -> bool:
return self.inner_change_pusher.check_if_pr_already_exists(branch)


def build_pr_body(synth_log: str):
def build_pr_body(synth_log: str, trailers: str = ""):
"""Composes the pull request body with the synth_log.
If synth_log is empty, then creates link to kokoro build log.
Expand All @@ -171,4 +175,43 @@ def build_pr_body(synth_log: str):
This PR was generated using Autosynth. :rainbow:
{build_log_text}
{trailers}
""".strip()


def _collect_trailers(commit_count: int, git_dir: typing.Optional[str] = None) -> str:
"""Collects the trailers from recent commits in the repo.
Only collects the two trailers we're interested in.
Arguments:
commit_count {int} -- Number of commits to collect trailers from.
Keyword Arguments:
git_dir {typing.Optional[str]} -- directory of git repo (default: {None})
Returns:
str -- The trailer lines from the recent commits.
"""
text = subprocess.run(
["git", "log", f"-{commit_count}", "--pretty=%b"],
universal_newlines=True,
check=True,
stdout=subprocess.PIPE,
cwd=git_dir,
).stdout
return _parse_trailers(text)


def _parse_trailers(text: str) -> str:
"""Parses and returns trailers in the text.
Arguments:
text {str} -- commit body text with trailers
Returns:
str -- the trailer lines.
"""
lines = text.splitlines()
trailer = re.compile(r"\s*(Source-Link:|PiperOrigin-RevId:).*")
return "\n".join([line for line in lines if trailer.match(line)])
35 changes: 34 additions & 1 deletion tests/test_change_pusher.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from autosynth.change_pusher import build_pr_body
from autosynth.change_pusher import build_pr_body, _parse_trailers
from integration_tests import util


Expand All @@ -39,3 +39,36 @@ def test_build_pr_body_with_synth_log_and_kokoro_build_id():
pr_body.find("https://source.cloud.google.com/results/invocations/42") == -1
)
assert pr_body.find(synth_log) > -1


def test_build_pr_body_with_synth_trailers():
synth_log = "synth log"
pr_body = build_pr_body(synth_log, "a: b\nc: d")
assert pr_body.find("a: b") > -1
assert pr_body.find("c: d") > -1


def test_parse_trailers():
text = """
Source-Author: Google APIs <noreply@google.com>
Source-Date: Mon Apr 13 12:05:23 2020 -0700
Source-Repo: googleapis/googleapis
Source-Sha: 4d61e1cb40184a7ad63ef37b1813f6608718674a
Source-Link: https://github.com/googleapis/googleapis/commit/4d61e1cb40184a7ad63ef37b1813f6608718674a
* Removing erroneous comment, a la https://github.com/googleapis/java-speech/pull/103
PiperOrigin-RevId: 296332968
Source-Author: Google APIs <noreply@google.com>
Source-Date: Thu Feb 20 17:19:15 2020 -0800
Source-Repo: googleapis/googleapis
Source-Sha: 17567c4a1ef0a9b50faa87024d66f8acbb561089
Source-Link: https://github.com/googleapis/googleapis/commit/17567c4a1ef0a9b50faa87024d66f8acbb561089
* changes without context
autosynth cannot find the source of changes triggered by earlier changes in this
repository, or by version upgrades to tools such as linters.
""".strip()
trailers = _parse_trailers(text)
golden_trailers = "Source-Link: https://github.com/googleapis/googleapis/commit/4d61e1cb40184a7ad63ef37b1813f6608718674a\nPiperOrigin-RevId: 296332968\nSource-Link: https://github.com/googleapis/googleapis/commit/17567c4a1ef0a9b50faa87024d66f8acbb561089"
assert trailers == golden_trailers
10 changes: 6 additions & 4 deletions tests/testdata/mock-synth-golden-squash-prs.log
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
commit 3c25ad4bca383a3248d5f05abc87e0e33e9dbd6d
commit b2feb453f021a09e486919dfe79980c38125a22e
Author: Jeffrey Rennie <rennie@google.com>
Date: Wed Apr 22 12:37:51 2020 -0700
Date: Wed May 6 09:15:08 2020 -0700

changes triggered by multiple versions

* Wrote c-more to c.txt.

* Wrote c to c.txt.
Expand All @@ -27,9 +29,9 @@ index 0000000..0794b8a
+c-more
\ No newline at end of file

commit ec0d8367aa2e53f951625078ac87d432bbebd057
commit 3a3e7a2e74a010875449e9d0df16d2a1a9d1e612
Author: Jeffrey Rennie <rennie@google.com>
Date: Wed Apr 22 12:37:47 2020 -0700
Date: Wed May 6 09:15:04 2020 -0700

Added Readme

Expand Down

0 comments on commit 558bb0d

Please sign in to comment.