Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add pr_number as an optional output on the action #23

Merged
merged 1 commit into from
Aug 10, 2023
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: 2 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,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 @@ -69,4 +69,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
16 changes: 10 additions & 6 deletions tests/trestlebot/test_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,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 @@ -196,6 +196,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 Down Expand Up @@ -223,7 +224,7 @@ def test_run_dry_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 @@ -235,6 +236,7 @@ def test_run_dry_run(tmp_repo: Tuple[str, Repo]) -> None:
dry_run=True,
)
assert commit_sha != ""
assert pr_number == 0

mock_push.assert_not_called()

Expand All @@ -246,7 +248,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 @@ -258,6 +260,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 @@ -275,7 +278,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 Down Expand Up @@ -348,7 +351,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 @@ -357,7 +360,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 @@ -371,6 +374,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(
pull_request_title: str = "Automatic updates from bot",
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 @@ -113,10 +113,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 @@ -151,7 +153,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 @@ -172,7 +174,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 @@ -181,15 +183,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
8 changes: 6 additions & 2 deletions trestlebot/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,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 @@ -272,7 +272,11 @@ def run() -> None:

# Print the full commit sha
if commit_sha:
print(f"Commit Hash: {commit_sha}") # noqa
print(f"Commit Hash: {commit_sha}") # noqa: T201

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

except Exception as e:
exit_code = handle_exception(e)
Expand Down
Loading