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

GitAuto: Add an integration test to is_repo_forked() in services/github/repo_manager.py #520

Closed
wants to merge 2 commits into from

Conversation

gitauto-for-dev[bot]
Copy link
Contributor

@gitauto-for-dev gitauto-for-dev bot commented Feb 1, 2025

Resolves #516
"""

What is the feature

This PR adds an integration test for the is_repo_forked() function in services/github/repo_manager.py. It sets up tests using constants defined in tests/constants.py and uses the GH_APP_TOKEN from the GitHub Actions workflow for authentication against the GitHub API. Additionally, a new constant FORKED_REPO is added to tests/constants.py corresponding to a forked repository.

How to implement the feature

The following changes have been made:

  • Updated tests/constants.py to include a new constant FORKED_REPO, which represents a forked repository.
  • Created tests/services/github/test_repo_manager_integration.py to add two integration tests:
    • test_is_repo_forked_non_forked: Validates that a non-forked repository is correctly identified.
    • test_is_repo_forked_forked: Validates that a forked repository is correctly identified.
  • Both tests use the GH_APP_TOKEN environment variable, which is also used in the GitHub Actions workflow (.github/workflows/pytest.yml). This ensures that the tests make authenticated requests as needed.

This approach follows current best practices by using fixtures to manage credentials and environment setup, ensuring that integration tests only run when the necessary credentials are available.

Anything the issuer needs to do

  • Ensure that the GH_APP_TOKEN secret is registered and available in the GitHub repository.
  • Verify that the FORKED_REPO value in tests/constants.py corresponds to an actual forked repository under the specified OWNER.
  • No further action is required aside from these environment configuration steps.
    """
git checkout gitauto-wes/issue-516-20250131-231337
git pull origin gitauto-wes/issue-516-20250131-231337```

Copy link
Contributor Author

gitauto-for-dev bot commented Feb 1, 2025

The error is caused by the integration test for the branch manager module trying to call the GitHub API without a valid token. While the integration tests in repo_manager already skip when the GH_APP_TOKEN is missing, test_get_default_branch in tests/services/github/test_branch_manager.py does not perform that check. As a result, the test attempts to use a missing or invalid token, leading to a 401 Unauthorized error.

To fix it, add a fixture in test_branch_manager.py that retrieves GH_APP_TOKEN from the environment and skips the test if it’s not set. Then pass the token to get_default_branch instead of using an undefined TOKEN variable. For example:


#!/usr/bin/env python
import os
import pytest
from tests.constants import OWNER, REPO
from services/github.branch_manager import get_default_branch

@pytest.fixture
def gh_app_token():
token = os.getenv("GH_APP_TOKEN")
if not token:
pytest.skip("GH_APP_TOKEN not set")
return token

def test_get_default_branch(gh_app_token) -> None:
default_branch, commit_sha = get_default_branch(OWNER, REPO, gh_app_token)
assert isinstance(default_branch, str)
assert isinstance(commit_sha, str)


This minimal change aligns with the approach used in tests/services/github/test_repo_manager_integration.py, ensuring that the test only runs when a valid token is present and preventing unauthorized API calls.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add an integration test to is_repo_forked() in services/github/repo_manager.py
1 participant