Skip to content

Commit

Permalink
add a function to return link
Browse files Browse the repository at this point in the history
  • Loading branch information
JoeWang1127 committed May 30, 2024
1 parent 99ee537 commit 15c14f5
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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),
)
16 changes: 12 additions & 4 deletions library_generation/utils/commit_message_formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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})"

0 comments on commit 15c14f5

Please sign in to comment.