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

fix(IDX): run git commands in a different dir #83

Merged
merged 5 commits into from
Dec 9, 2024
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
1 change: 1 addition & 0 deletions .github/workflows/repo_policies.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,4 @@ jobs:
REPO: ${{ github.event.repository.name }}
MERGE_BASE_SHA: ${{ github.event.pull_request.base.sha }}
BRANCH_HEAD_SHA: ${{ github.event.pull_request.head.sha }}
REPO_PATH: current-repo
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import subprocess
from typing import Optional

import github3

Expand All @@ -11,12 +12,13 @@
"GH_TOKEN",
"GH_ORG",
"REPO",
"REPO_PATH",
"MERGE_BASE_SHA",
"BRANCH_HEAD_SHA",
]


def get_changed_files(merge_base_sha: str, branch_head_sha: str) -> list[str]:
def get_changed_files(merge_base_sha: str, branch_head_sha: str, repo_path: Optional[str] = None) -> list[str]:
"""
Compares the files changed in the current branch to the merge base.
"""
Expand All @@ -25,6 +27,7 @@ def get_changed_files(merge_base_sha: str, branch_head_sha: str) -> list[str]:
["git", "diff", "--name-only", commit_range],
capture_output=True,
text=True,
cwd=repo_path,
)
if result.returncode != 0:
raise RuntimeError(f"git diff failed with exit code {result.returncode}: {result.stderr}")
Expand Down Expand Up @@ -61,8 +64,9 @@ def check_if_pr_is_blocked(env_vars: dict) -> None:
"""
gh = github3.login(token=env_vars["GH_TOKEN"])
repo = gh.repository(owner=env_vars["GH_ORG"], repository=env_vars["REPO"])
repo_path = env_vars["REPO_PATH"]
changed_files = get_changed_files(
env_vars["MERGE_BASE_SHA"], env_vars["BRANCH_HEAD_SHA"]
env_vars["MERGE_BASE_SHA"], env_vars["BRANCH_HEAD_SHA"], repo_path
)
config = get_approved_files_config(repo)
approved_files = get_approved_files(config)
Expand Down
7 changes: 5 additions & 2 deletions reusable_workflows/tests/test_repo_policies.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ def test_get_changed_files(mock_subprocess_run):
["git", "diff", "--name-only", "merge_base_sha..branch_head_sha"],
capture_output=True,
text=True,
cwd=None,
)


Expand Down Expand Up @@ -74,6 +75,7 @@ def test_pr_is_blocked_false(gh_login, get_approved_files_config, get_changed_fi
"GH_TOKEN": "token",
"GH_ORG": "org",
"REPO": "repo",
"REPO_PATH": "path",
"MERGE_BASE_SHA": "base",
"BRANCH_HEAD_SHA": "head",
}
Expand All @@ -89,7 +91,7 @@ def test_pr_is_blocked_false(gh_login, get_approved_files_config, get_changed_fi

check_if_pr_is_blocked(env_vars)

get_changed_files.assert_called_once_with("base", "head")
get_changed_files.assert_called_once_with("base", "head", "path")
get_approved_files_config.assert_called_once_with(repo)


Expand All @@ -103,6 +105,7 @@ def test_pr_is_blocked_true(gh_login, get_approved_files_config, get_changed_fil
"GH_TOKEN": "token",
"GH_ORG": "org",
"REPO": "repo",
"REPO_PATH": "path",
"MERGE_BASE_SHA": "base",
"BRANCH_HEAD_SHA": "head",
}
Expand All @@ -119,7 +122,7 @@ def test_pr_is_blocked_true(gh_login, get_approved_files_config, get_changed_fil
with pytest.raises(SystemExit):
check_if_pr_is_blocked(env_vars)

get_changed_files.assert_called_once_with("base", "head")
get_changed_files.assert_called_once_with("base", "head", "path")
get_approved_files_config.assert_called_once_with(repo)


Expand Down
Loading