From 15c14f5bf674d2933ee9cad71365c3c006bc1e32 Mon Sep 17 00:00:00 2001 From: Joe Wang Date: Thu, 30 May 2024 15:51:36 -0400 Subject: [PATCH] add a function to return link --- .../utils/commit_message_formatter_unit_tests.py | 14 +++++++++++++- .../utils/commit_message_formatter.py | 16 ++++++++++++---- 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/library_generation/test/utils/commit_message_formatter_unit_tests.py b/library_generation/test/utils/commit_message_formatter_unit_tests.py index c7d7cfcf8d..0148214dfb 100644 --- a/library_generation/test/utils/commit_message_formatter_unit_tests.py +++ b/library_generation/test/utils/commit_message_formatter_unit_tests.py @@ -14,7 +14,10 @@ import unittest from unittest.mock import patch -from library_generation.utils.commit_message_formatter import format_commit_message +from library_generation.utils.commit_message_formatter import ( + format_commit_message, + commit_link, +) from library_generation.utils.commit_message_formatter import wrap_nested_commit from library_generation.utils.commit_message_formatter import wrap_override_commit @@ -141,3 +144,12 @@ def test_wrap_override_commit_success(self): ], wrap_override_commit(messages), ) + + def test_commit_link_success(self): + with patch("git.Commit") as mock_commit: + commit = mock_commit.return_value + commit.hexsha = "1234567abcdefg" + self.assertEqual( + "[googleapis/googleapis@1234567](https://github.com/googleapis/googleapis/commit/1234567abcdefg)", + commit_link(commit), + ) diff --git a/library_generation/utils/commit_message_formatter.py b/library_generation/utils/commit_message_formatter.py index 7d2767d6e6..85bfbc8036 100644 --- a/library_generation/utils/commit_message_formatter.py +++ b/library_generation/utils/commit_message_formatter.py @@ -59,12 +59,9 @@ def wrap_nested_commit(commit: Commit, messages: List[str]) -> List[str]: :param messages: a (multi-line) commit message, one line per item. :return: wrapped messages. """ - short_sha = commit.hexsha[:7] result = ["BEGIN_NESTED_COMMIT"] result.extend(messages) - result.append( - f"Source Link: [googleapis/googleapis@{short_sha}](https://github.com/googleapis/googleapis/commit/{commit.hexsha})" - ) + result.append(f"Source Link: {commit_link(commit)}") result.append("END_NESTED_COMMIT") return result @@ -80,3 +77,14 @@ def wrap_override_commit(messages: List[str]) -> List[str]: result.extend(messages) result.append("END_COMMIT_OVERRIDE") return result + + +def commit_link(commit: Commit) -> str: + """ + Create a link to the commit in Markdown format. + + :param commit: a GitHub commit. + :return: a link in Markdown format. + """ + short_sha = commit.hexsha[:7] + return f"[googleapis/googleapis@{short_sha}](https://github.com/googleapis/googleapis/commit/{commit.hexsha})"