Skip to content

Conversation

@shenxianpeng
Copy link
Contributor

@shenxianpeng shenxianpeng commented Nov 5, 2025

closes #323

Summary by CodeRabbit

  • Bug Fixes

    • Improved branch-name detection in CI: the system now checks CI environment variables as fallbacks and reliably defaults to "HEAD" when needed, improving workflow robustness.
  • Tests

    • Added and updated tests to cover fallback behaviors and priority order for branch-name resolution, ensuring consistent behavior across environments.

@shenxianpeng shenxianpeng requested a review from a team as a code owner November 5, 2025 16:42
@shenxianpeng shenxianpeng added the enhancement New feature or request label Nov 5, 2025
@netlify
Copy link

netlify bot commented Nov 5, 2025

Deploy Preview for commit-check ready!

Name Link
🔨 Latest commit 9b5586c
🔍 Latest deploy log https://app.netlify.com/projects/commit-check/deploys/690bc5d8a789ed000844292f
😎 Deploy Preview https://deploy-preview-322--commit-check.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Nov 5, 2025

Walkthrough

Reworked get_branch_name to prefer git output when non-empty, otherwise fall back to environment variables in order: GITHUB_HEAD_REF, GITHUB_REF_NAME, then "HEAD". Added tests covering the fallback paths and precedence.

Changes

Cohort / File(s) Summary
Branch name detection change
commit_check/util.py
Added import os. Modified get_branch_name() to use git output if non-empty; otherwise check GITHUB_HEAD_REF, then GITHUB_REF_NAME, then return "HEAD".
Tests for branch fallback behavior
tests/util_test.py
Updated and added tests to mock cmd_output and os.getenv verifying: git returns non-empty, fallback to GITHUB_HEAD_REF, fallback to GITHUB_REF_NAME, final fallback to "HEAD", and precedence where GITHUB_HEAD_REF overrides GITHUB_REF_NAME.

Sequence Diagram

sequenceDiagram
    participant Caller
    participant get_branch_name
    participant git as "git (cmd_output)"
    participant env as "os.getenv"

    rect rgb(242,247,255)
    Note over Caller,git: Primary lookup
    Caller->>get_branch_name: call get_branch_name()
    get_branch_name->>git: cmd_output(["git","branch","--show-current"])
    git-->>get_branch_name: output (non-empty)
    get_branch_name-->>Caller: return git output
    end

    rect rgb(255,249,240)
    Note over get_branch_name,env: Fallback when git output empty
    Caller->>get_branch_name: call get_branch_name()
    get_branch_name->>git: cmd_output(["git","branch","--show-current"])
    git-->>get_branch_name: output (empty or error)
    get_branch_name->>env: getenv("GITHUB_HEAD_REF")
    env-->>get_branch_name: value or None
    alt has GITHUB_HEAD_REF
        get_branch_name-->>Caller: return GITHUB_HEAD_REF
    else
        get_branch_name->>env: getenv("GITHUB_REF_NAME")
        env-->>get_branch_name: value or None
        alt has GITHUB_REF_NAME
            get_branch_name-->>Caller: return GITHUB_REF_NAME
        else
            get_branch_name-->>Caller: return "HEAD"
        end
    end
    end
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

  • Focus points:
    • Verify correct handling of empty string vs exception from cmd_output.
    • Confirm environment fallback precedence and that tests properly mock cmd_output and os.getenv.

Poem

🐰 I sniffed the branches, quiet or loud,
When git went mute I asked the cloud.
First head_ref, then ref_name in sight,
If none replied, I settled on HEAD tonight. 🥕

Pre-merge checks and finishing touches

✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and specifically describes the main change: enabling commit-check to work without requiring the ref parameter in actions/checkout.
Linked Issues check ✅ Passed The changes directly address issue #323 by implementing fallback mechanisms to environment variables (GITHUB_HEAD_REF, GITHUB_REF_NAME) when git commands fail, allowing commit-check to function without actions/checkout specifying ref.
Out of Scope Changes check ✅ Passed All changes are focused on the get_branch_name function and corresponding tests to support the fallback behavior, remaining within the scope of enabling commit-check without requiring ref in actions/checkout.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch feature/support-github-user-to-get-env

📜 Recent review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 832e7e9 and 9b5586c.

📒 Files selected for processing (1)
  • tests/util_test.py (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • tests/util_test.py
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (18)
  • GitHub Check: install (3.14, ubuntu-24.04)
  • GitHub Check: install (3.12, macos-latest)
  • GitHub Check: install (3.12, windows-latest)
  • GitHub Check: install (3.11, macos-latest)
  • GitHub Check: install (3.14, windows-latest)
  • GitHub Check: install (3.12, ubuntu-24.04)
  • GitHub Check: install (3.14, macos-latest)
  • GitHub Check: install (3.13, ubuntu-24.04)
  • GitHub Check: install (3.13, windows-latest)
  • GitHub Check: install (3.11, ubuntu-24.04)
  • GitHub Check: install (3.9, ubuntu-24.04)
  • GitHub Check: install (3.10, macos-latest)
  • GitHub Check: install (3.11, windows-latest)
  • GitHub Check: install (3.9, windows-latest)
  • GitHub Check: install (3.10, windows-latest)
  • GitHub Check: install (3.9, macos-latest)
  • GitHub Check: install (3.10, ubuntu-24.04)
  • GitHub Check: Run benchmarks

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@codecov
Copy link

codecov bot commented Nov 5, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 87.31%. Comparing base (106ea31) to head (9b5586c).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #322      +/-   ##
==========================================
+ Coverage   87.26%   87.31%   +0.05%     
==========================================
  Files           8        8              
  Lines         683      686       +3     
==========================================
+ Hits          596      599       +3     
  Misses         87       87              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@codspeed-hq
Copy link

codspeed-hq bot commented Nov 5, 2025

CodSpeed Performance Report

Merging #322 will not alter performance

Comparing feature/support-github-user-to-get-env (9b5586c) with main (106ea31)

Summary

✅ 156 untouched
🆕 4 new
⏩ 85 skipped1

Benchmarks breakdown

Benchmark BASE HEAD Change
🆕 test_get_branch_name_fallback_github_head_ref N/A 951.8 µs N/A
🆕 test_get_branch_name_fallback_github_ref_name N/A 954.4 µs N/A
🆕 test_get_branch_name_fallback_head N/A 1.5 ms N/A
🆕 test_get_branch_name_fallback_priority N/A 954 µs N/A

Footnotes

  1. 85 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (1)
commit_check/util.py (1)

56-61: This change correctly enables branch detection in GitHub Actions and will help resolve issue #156.

The env var fallback chain (GITHUB_HEAD_REF → GITHUB_REF_NAME → "HEAD") is the right approach. The issue occurs when branch name detection fails—validation rules can't trigger without a correct branch name, preventing the exit code mechanism from working. Your change enables that by properly detecting branch names in GitHub Actions environments.

Consider adding documentation for the GitHub Actions-specific behavior:

 def get_branch_name() -> str:
     """Identify current branch name.
+    
+    Falls back to GitHub Actions environment variables when git command returns
+    no output: GITHUB_HEAD_REF (for pull requests) or GITHUB_REF_NAME (for other events).
     
     .. note::
         With Git 2.22 and above supports `git branch --show-current`
         Please open an issue at https://github.com/commit-check/commit-check/issues
         if you encounter any issue.

     :returns: A `str` describing the current branch name.
     """
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 106ea31 and 7d8b8eb.

📒 Files selected for processing (1)
  • commit_check/util.py (2 hunks)
🧰 Additional context used
🧠 Learnings (3)
📓 Common learnings
Learnt from: CR
Repo: commit-check/commit-check PR: 0
File: .github/copilot-instructions.md:0-0
Timestamp: 2025-10-03T10:28:06.793Z
Learning: Applies to commit_check/branch.py : Allow special branches: master, main, HEAD, PR-*
Learnt from: CR
Repo: commit-check/commit-check PR: 0
File: .github/copilot-instructions.md:0-0
Timestamp: 2025-10-03T10:28:06.793Z
Learning: Applies to commit_check/branch.py : Enforce Conventional Branch prefixes: bugfix/, feature/, release/, hotfix/, task/, chore/
📚 Learning: 2025-10-03T10:28:06.793Z
Learnt from: CR
Repo: commit-check/commit-check PR: 0
File: .github/copilot-instructions.md:0-0
Timestamp: 2025-10-03T10:28:06.793Z
Learning: Applies to commit_check/branch.py : Allow special branches: master, main, HEAD, PR-*

Applied to files:

  • commit_check/util.py
📚 Learning: 2025-10-03T10:28:06.793Z
Learnt from: CR
Repo: commit-check/commit-check PR: 0
File: .github/copilot-instructions.md:0-0
Timestamp: 2025-10-03T10:28:06.793Z
Learning: Applies to commit_check/branch.py : Enforce Conventional Branch prefixes: bugfix/, feature/, release/, hotfix/, task/, chore/

Applied to files:

  • commit_check/util.py
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (3)
  • GitHub Check: install (3.13, windows-latest)
  • GitHub Check: install (3.9, windows-latest)
  • GitHub Check: Run benchmarks
🔇 Additional comments (1)
commit_check/util.py (1)

8-8: LGTM - Required import added.

The os module import is necessary for accessing environment variables.

@shenxianpeng shenxianpeng changed the title feat: Fallback to environment variables (GitHub Actions) feat: support commit-check without requiring ref in actions/checkout Nov 5, 2025
@shenxianpeng shenxianpeng added the github_actions Pull requests that update GitHub Actions code label Nov 5, 2025
@shenxianpeng shenxianpeng force-pushed the feature/support-github-user-to-get-env branch from 7d8b8eb to 6768e32 Compare November 5, 2025 21:27
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (1)
tests/util_test.py (1)

44-82: Good test coverage for the main fallback scenarios.

The new tests properly verify environment variable fallbacks when cmd_output returns empty string, covering GITHUB_HEAD_REF, GITHUB_REF_NAME, HEAD, and priority order.

However, consider adding test coverage for the CalledProcessError case with environment variables. Currently, the existing test test_get_branch_name_with_exception (lines 29-42) only verifies that an empty string is returned when an exception is raised, but doesn't verify that environment variables are checked in that scenario.

Add a test like this to improve coverage:

@pytest.mark.benchmark
def test_get_branch_name_exception_with_env_fallback(self, mocker):
    """Test fallback to environment variables when cmd_output raises exception."""
    mocker.patch(
        "commit_check.util.cmd_output",
        side_effect=CalledProcessError(1, "git")
    )
    mocker.patch(
        "commit_check.util.os.getenv",
        lambda key: "feature-branch" if key == "GITHUB_HEAD_REF" else None,
    )
    assert get_branch_name() == "feature-branch"

Note: This test will pass only after applying the fix suggested in commit_check/util.py lines 56-64.

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 7d8b8eb and 6768e32.

📒 Files selected for processing (2)
  • commit_check/util.py (2 hunks)
  • tests/util_test.py (1 hunks)
🧰 Additional context used
📓 Path-based instructions (1)
tests/**

📄 CodeRabbit inference engine (.github/copilot-instructions.md)

Ensure tests run via pytest -v and cover commit, branch, author, and CLI behaviors

Files:

  • tests/util_test.py
🧠 Learnings (3)
📓 Common learnings
Learnt from: CR
Repo: commit-check/commit-check PR: 0
File: .github/copilot-instructions.md:0-0
Timestamp: 2025-10-03T10:28:06.793Z
Learning: Applies to commit_check/commit.py : Support optional scope in commits, e.g., feat(api): add endpoint
Learnt from: CR
Repo: commit-check/commit-check PR: 0
File: .github/copilot-instructions.md:0-0
Timestamp: 2025-10-03T10:28:06.793Z
Learning: Applies to commit_check/commit.py : Support breaking change notation with ! (e.g., feat!: breaking change)
Learnt from: CR
Repo: commit-check/commit-check PR: 0
File: .github/copilot-instructions.md:0-0
Timestamp: 2025-10-03T10:28:06.793Z
Learning: Applies to tests/** : Ensure tests run via pytest -v and cover commit, branch, author, and CLI behaviors
Learnt from: CR
Repo: commit-check/commit-check PR: 0
File: .github/copilot-instructions.md:0-0
Timestamp: 2025-10-03T10:28:06.793Z
Learning: Applies to commit_check/branch.py : Allow special branches: master, main, HEAD, PR-*
📚 Learning: 2025-10-03T10:28:06.793Z
Learnt from: CR
Repo: commit-check/commit-check PR: 0
File: .github/copilot-instructions.md:0-0
Timestamp: 2025-10-03T10:28:06.793Z
Learning: Applies to tests/** : Ensure tests run via pytest -v and cover commit, branch, author, and CLI behaviors

Applied to files:

  • tests/util_test.py
📚 Learning: 2025-10-03T10:28:06.793Z
Learnt from: CR
Repo: commit-check/commit-check PR: 0
File: .github/copilot-instructions.md:0-0
Timestamp: 2025-10-03T10:28:06.793Z
Learning: Applies to commit_check/branch.py : Allow special branches: master, main, HEAD, PR-*

Applied to files:

  • commit_check/util.py
🧬 Code graph analysis (1)
tests/util_test.py (1)
commit_check/util.py (1)
  • get_branch_name (44-64)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (17)
  • GitHub Check: install (3.9, ubuntu-24.04)
  • GitHub Check: install (3.14, windows-latest)
  • GitHub Check: install (3.13, ubuntu-24.04)
  • GitHub Check: install (3.13, macos-latest)
  • GitHub Check: install (3.12, windows-latest)
  • GitHub Check: install (3.14, ubuntu-24.04)
  • GitHub Check: install (3.14, macos-latest)
  • GitHub Check: install (3.13, windows-latest)
  • GitHub Check: install (3.10, ubuntu-24.04)
  • GitHub Check: install (3.11, ubuntu-24.04)
  • GitHub Check: install (3.11, windows-latest)
  • GitHub Check: install (3.9, windows-latest)
  • GitHub Check: install (3.12, macos-latest)
  • GitHub Check: install (3.11, macos-latest)
  • GitHub Check: install (3.10, macos-latest)
  • GitHub Check: install (3.10, windows-latest)
  • GitHub Check: Run benchmarks
🔇 Additional comments (1)
commit_check/util.py (1)

8-8: LGTM!

The os import is correctly added to support the environment variable fallback logic.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (1)
commit_check/util.py (1)

56-65: Excellent: Past review concern fully addressed.

The environment variable fallback is now correctly positioned outside the try-except block (lines 60-64), ensuring it executes in both scenarios:

  • When cmd_output returns an empty string
  • When CalledProcessError is raised (line 57-58 sets branch_name = "")

The fallback priority (GITHUB_HEAD_REF → GITHUB_REF_NAME → "HEAD") is appropriate for GitHub Actions workflows.

Minor observation: The cmd_output function (lines 134-148) doesn't use check=True, so CalledProcessError may not actually be raised in practice. The exception handler is defensive coding and harmless.

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 6768e32 and 832e7e9.

📒 Files selected for processing (2)
  • commit_check/util.py (2 hunks)
  • tests/util_test.py (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • tests/util_test.py
🧰 Additional context used
🧠 Learnings (3)
📓 Common learnings
Learnt from: CR
Repo: commit-check/commit-check PR: 0
File: .github/copilot-instructions.md:0-0
Timestamp: 2025-10-03T10:28:06.793Z
Learning: Applies to commit_check/commit.py : Support optional scope in commits, e.g., feat(api): add endpoint
Learnt from: CR
Repo: commit-check/commit-check PR: 0
File: .github/copilot-instructions.md:0-0
Timestamp: 2025-10-03T10:28:06.793Z
Learning: Applies to commit_check/commit.py : Support breaking change notation with ! (e.g., feat!: breaking change)
Learnt from: CR
Repo: commit-check/commit-check PR: 0
File: .github/copilot-instructions.md:0-0
Timestamp: 2025-10-03T10:28:06.793Z
Learning: Applies to commit_check/branch.py : Allow special branches: master, main, HEAD, PR-*
Learnt from: CR
Repo: commit-check/commit-check PR: 0
File: .github/copilot-instructions.md:0-0
Timestamp: 2025-10-03T10:28:06.793Z
Learning: Applies to commit_check/branch.py : Enforce Conventional Branch prefixes: bugfix/, feature/, release/, hotfix/, task/, chore/
📚 Learning: 2025-10-03T10:28:06.793Z
Learnt from: CR
Repo: commit-check/commit-check PR: 0
File: .github/copilot-instructions.md:0-0
Timestamp: 2025-10-03T10:28:06.793Z
Learning: Applies to commit_check/branch.py : Allow special branches: master, main, HEAD, PR-*

Applied to files:

  • commit_check/util.py
📚 Learning: 2025-10-03T10:28:06.793Z
Learnt from: CR
Repo: commit-check/commit-check PR: 0
File: .github/copilot-instructions.md:0-0
Timestamp: 2025-10-03T10:28:06.793Z
Learning: Applies to commit_check/branch.py : Enforce Conventional Branch prefixes: bugfix/, feature/, release/, hotfix/, task/, chore/

Applied to files:

  • commit_check/util.py
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: Run benchmarks
🔇 Additional comments (1)
commit_check/util.py (1)

8-8: LGTM: Import required for environment variable access.

The os import is necessary for the os.getenv() calls in the fallback logic.

@sonarqubecloud
Copy link

sonarqubecloud bot commented Nov 5, 2025

@shenxianpeng shenxianpeng merged commit 187c3b6 into main Nov 5, 2025
36 checks passed
@shenxianpeng shenxianpeng deleted the feature/support-github-user-to-get-env branch November 5, 2025 21:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request github_actions Pull requests that update GitHub Actions code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support commit-check without requiring ref in actions/checkout

2 participants