Skip to content

Commit

Permalink
feat: add pr_number as an optional output on the action
Browse files Browse the repository at this point in the history
Signed-off-by: Jennifer Power <[email protected]>
  • Loading branch information
jpower432 committed Jul 20, 2023
1 parent 5edd331 commit 76bc57c
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 18 deletions.
2 changes: 2 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ outputs:
description: Value is "true" if changes were committed back to the repository.
commit:
description: Full hash of the created commit. Only present if the "changes" output is "true".
pr_number:
description: Number of the submitted pull request. Only present if a pull request is submitted.

runs:
using: "docker"
Expand Down
6 changes: 6 additions & 0 deletions entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,10 @@ if [ -n "$commit" ]; then
echo "commit=$commit" >> "$GITHUB_OUTPUT"
else
echo "changes=false" >> "$GITHUB_OUTPUT"
fi

pr_number=$(echo "$output" | grep "Pull Request Number:" | sed 's/.*: //')

if [ -n "$pr_number" ]; then
echo "pr_number=$pr_number" >> "$GITHUB_OUTPUT"
fi
19 changes: 12 additions & 7 deletions tests/trestlebot/test_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ def test_run(tmp_repo: Tuple[str, Repo]) -> None:
mock_push.return_value = "Mocked result"

# Test running the bot
commit_sha = bot.run(
commit_sha, pr_number = bot.run(
working_dir=repo_path,
branch="main",
commit_name="Test User",
Expand All @@ -178,6 +178,7 @@ def test_run(tmp_repo: Tuple[str, Repo]) -> None:
dry_run=False,
)
assert commit_sha != ""
assert pr_number == 0

# Verify that the commit is made
commit = next(repo.iter_commits())
Expand All @@ -202,7 +203,7 @@ def test_run_dry_run(tmp_repo: Tuple[str, Repo]) -> None:
f.write("Test content")

# Test running the bot
commit_sha = bot.run(
commit_sha, pr_number = bot.run(
working_dir=repo_path,
branch="main",
commit_name="Test User",
Expand All @@ -214,6 +215,7 @@ def test_run_dry_run(tmp_repo: Tuple[str, Repo]) -> None:
dry_run=True,
)
assert commit_sha != ""
assert pr_number == 0

# Verify that the commit is made
commit = next(repo.iter_commits())
Expand All @@ -232,7 +234,7 @@ def test_empty_commit(tmp_repo: Tuple[str, Repo]) -> None:
repo_path, repo = tmp_repo

# Test running the bot
commit_sha = bot.run(
commit_sha, pr_number = bot.run(
working_dir=repo_path,
branch="main",
commit_name="Test User",
Expand All @@ -244,6 +246,7 @@ def test_empty_commit(tmp_repo: Tuple[str, Repo]) -> None:
dry_run=True,
)
assert commit_sha == ""
assert pr_number == 0

clean(repo_path, repo)

Expand All @@ -264,7 +267,7 @@ def test_non_matching_files(tmp_repo: Tuple[str, Repo]) -> None:
json.dump(data, f, indent=4)

# Test running the bot
commit_sha = bot.run(
commit_sha, pr_number = bot.run(
working_dir=repo_path,
branch="main",
commit_name="Test User",
Expand All @@ -276,6 +279,7 @@ def test_non_matching_files(tmp_repo: Tuple[str, Repo]) -> None:
dry_run=True,
)
assert commit_sha != ""
assert pr_number == 0

# Verify that the commit is made
commit = next(repo.iter_commits())
Expand Down Expand Up @@ -303,7 +307,7 @@ def test_run_check_only(tmp_repo: Tuple[str, Repo]) -> None:
bot.RepoException,
match="Check only mode is enabled and diff detected. Manual intervention on main is required.",
):
_ = bot.run(
_, _ = bot.run(
working_dir=repo_path,
branch="main",
commit_name="Test User",
Expand All @@ -329,7 +333,7 @@ def test_run_with_provider(tmp_repo: Tuple[str, Repo]) -> None:
f.write("Test content")

mock = Mock(spec=GitProvider)
mock.create_pull_request.return_value = "10"
mock.create_pull_request.return_value = 10
mock.parse_repository.return_value = ("ns", "repo")

repo.create_remote("origin", url="git.test.com/test/repo.git")
Expand All @@ -338,7 +342,7 @@ def test_run_with_provider(tmp_repo: Tuple[str, Repo]) -> None:
mock_push.return_value = "Mocked result"

# Test running the bot
commit_sha = bot.run(
commit_sha, pr_number = bot.run(
working_dir=repo_path,
branch="test",
commit_name="Test User",
Expand All @@ -352,6 +356,7 @@ def test_run_with_provider(tmp_repo: Tuple[str, Repo]) -> None:
dry_run=False,
)
assert commit_sha != ""
assert pr_number == 10

# Verify that the commit is made
commit = next(repo.iter_commits())
Expand Down
22 changes: 12 additions & 10 deletions trestlebot/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"""This module implements functions for the Trestle Bot."""

import logging
from typing import List, Optional
from typing import List, Optional, Tuple

from git import GitCommandError
from git.repo import Repo
Expand Down Expand Up @@ -93,8 +93,8 @@ def run(
target_branch: str = "",
check_only: bool = False,
dry_run: bool = False,
) -> str:
"""Run Trestle Bot and return exit code
) -> Tuple[str, int]:
"""Run Trestle Bot and returns commit and pull request information.
Args:
working_dir: Location of the git repo
Expand All @@ -109,10 +109,12 @@ def run(
dry_run: Only complete local work. Do not push.
Returns:
A string containing the full commit sha. Defaults to "" if
there was no updates
A tuple with commit_sha and pull request number.
The commit_sha defaults to "" if there was no updates and the
pull request number default to 0 if not submitted.
"""
commit_sha: str = ""
pr_number: int = 0

# Execute bot pre-tasks before committing repository updates
if pre_tasks is not None:
Expand Down Expand Up @@ -147,7 +149,7 @@ def run(

if dry_run:
logger.info("Dry run mode is enabled. Do not push to remote.")
return commit_sha
return commit_sha, pr_number

try:
# Get the remote repository by name
Expand All @@ -168,7 +170,7 @@ def run(
namespace, repo_name = git_provider.parse_repository(remote.url)
logger.debug("Detected namespace {namespace} and {repo_name}")

git_provider.create_pull_request(
pr_number = git_provider.create_pull_request(
ns=namespace,
repo_name=repo_name,
head_branch=branch,
Expand All @@ -177,15 +179,15 @@ def run(
body="",
)

return commit_sha
return commit_sha, pr_number

except GitCommandError as e:
raise RepoException(f"Git push to {branch} failed: {e}")
except GitProviderException as e:
raise RepoException(f"Git pull request to {target_branch} failed: {e}")
else:
logger.info("Nothing to commit")
return commit_sha
return commit_sha, pr_number
else:
logger.info("Nothing to commit")
return commit_sha
return commit_sha, pr_number
6 changes: 5 additions & 1 deletion trestlebot/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ def run() -> None:
# Assume it is a successful run, if the bot
# throws an exception update the exit code accordingly
try:
commit_sha = bot.run(
commit_sha, pr_number = bot.run(
working_dir=args.working_dir,
branch=args.branch,
commit_name=args.committer_name,
Expand All @@ -260,6 +260,10 @@ def run() -> None:
if commit_sha:
print(f"Commit Hash: {commit_sha}")

# Print the pr number
if pr_number:
print(f"Pull Request Number: {pr_number}")

except Exception as e:
exit_code = handle_exception(e)

Expand Down

0 comments on commit 76bc57c

Please sign in to comment.