Skip to content

fix(package): include package name in default branch decode warning - #266

Merged
swissspidy merged 1 commit into
wp-cli:mainfrom
manishsongirkar:fix/default-branch-decode-warning-sprintf
Dec 2, 2026
Merged

swissspidy merged 1 commit into
wp-cli:mainfrom
manishsongirkar:fix/default-branch-decode-warning-sprintf

Conversation

@manishsongirkar

@manishsongirkar manishsongirkar commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #265

What changed

  • src/Package_Command.php: in get_github_default_branch(), the warning for a GitHub API response that cannot be decoded as JSON is now wrapped in sprintf() with $package_name. Before, WP_CLI::warning() received a string that still had a literal %s, and it does not format messages. The two sibling warnings in the same method already use sprintf( ..., $package_name ).
  • features/package-install.feature: new scenario "Warn with the package name when the GitHub API response cannot be decoded".

Why

wp package install https://github.com/<owner>/<repo>.git printed:

Warning: Couldn't fetch default branch for package '%s' (failed to decode JSON response). Presuming default branch is 'master'.

A proxy or captive portal page that answers with HTTP 200 and a non-JSON body triggers this. Now it prints the package name, as in '<owner>/<repo>'.

About the test

  • The method is private and calls Utils\http_request() against a hard-coded api.github.com URL. So the scenario answers that request with a mock Requests transport. The transport is set through the http_request_options hook in a mock-github-api.php file.
  • The file is loaded with WP_CLI_REQUIRE. package is a protected command, so it ignores --require, but it does load files from WP_CLI_REQUIRE.
  • Only requests to https://api.github.com/repos/ are mocked. The rest of the install runs as in the existing "Try to install bad packages" scenario.
  • Without the fix, the scenario fails on the STDERR should contain step. With the fix, it passes.

Out of scope

  • The behaviour of the install after the warning (it presumes master). This is unchanged.

Test plan

Run locally with WP_CLI_PHP_ARGS cleared and GITHUB_TOKEN set, so the @github-api scenarios run too.

  • vendor/bin/run-behat-tests (all feature files): 61 scenarios, 59 passed, 2 failed.
    • The 2 failures are "Install a package with a dependency" (package-install.feature:119) and "Install a package from the wp-cli package index with a mixed-case name" (package-install.feature:787). They install real third-party packages from GitHub.
    • Both also fail on unmodified main at 6487f3a, so this change does not cause them.
  • New scenario only: passes with the fix, fails with the fix removed.
  • vendor/bin/run-php-unit-tests: 87 tests OK. The 1 PHPUnit deprecation is the same without this change.
  • phpcs on src and features, parallel-lint src, phpstan analyse: no errors.
  • Manual run of the real wp package install against a local mock of api.github.com (HTTP 200, body not json). The warning shows 'example/some-pkg' instead of '%s'.
  • Not tested: Windows, and the --insecure retry path. My local TLS mock failed on its self-signed certificate before the changed code ran. The changed branch runs after the response body is received, so it does not depend on --insecure.

The warning shown when the GitHub API response for a package's default
branch cannot be decoded as JSON was a plain string with a "%s"
placeholder, but it was never passed through sprintf(). WP_CLI::warning()
does not format its message, so users saw a literal "%s" instead of the
package name.

Wrap the message in sprintf() with $package_name, the same way as the two
sibling warnings in get_github_default_branch().

Add a behat scenario that answers api.github.com requests with a
non-JSON body through a mock Requests transport (loaded with
WP_CLI_REQUIRE, because "package" is a protected command that ignores
--require) and asserts the package name is printed. The scenario fails
without the fix.

Fixes wp-cli#265
@manishsongirkar
manishsongirkar requested a review from a team as a code owner December 2, 2026 12:22
@github-actions

github-actions Bot commented Dec 2, 2026

Copy link
Copy Markdown
Contributor

Hello! 👋

Thanks for opening this pull request! Please check out our contributing guidelines. We appreciate you taking the initiative to contribute to this project.

Contributing isn't limited to just code. We encourage you to contribute in the way that best fits your abilities, by writing tutorials, giving a demo at your local meetup, helping other users with their support questions, or revising our documentation.

Here are some useful Composer commands to get you started:

  • composer install: Install dependencies.
  • composer test: Run the full test suite.
  • composer phpcs: Check for code style violations.
  • composer phpcbf: Automatically fix code style violations.
  • composer phpunit: Run unit tests.
  • composer behat: Run behavior-driven tests.

To run a single Behat test, you can use the following command:

# Run all tests in a single file
composer behat features/some-feature.feature

# Run only a specific scenario (where 123 is the line number of the "Scenario:" title)
composer behat features/some-feature.feature:123

You can find a list of all available Behat steps in our handbook.

@coderabbitai

coderabbitai Bot commented Dec 2, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The JSON-decode failure warning now includes the package name instead of a literal %s. A feature scenario tests the warning when GitHub returns invalid JSON. The fallback remains master.

Changes: GitHub branch warning

Layer / File(s) Summary
Format and verify the decode-failure warning
src/Package_Command.php, features/package-install.feature
The warning formats the message with the repository name. A feature scenario mocks a successful HTTP response containing invalid JSON and checks that the warning names the package, states that master is presumed, and does not contain the literal %s placeholder.

Estimated code review effort: 2 (Simple) | ~8 minutes

Change: Bug fix · Severity of issue fixed: Low

Suggested reviewers: schlessera, swissspidy

Merge Risk: Minimal · up to a7a4c

This fixes a warning that printed a literal placeholder instead of the package name when GitHub returned an undecodable response. Install behavior is unchanged, and a test covers the case, so the risk of merging is minimal.

Security architecture risk: Minimal · up to a7a4c

WP-CLI now names the package when it cannot decode GitHub’s response. It still falls back to the same branch, and package installation behavior is unchanged.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the change: the default branch decode warning in get_github_default_branch() now includes the package name. The raw summary and objectives confirm this is the main change, which fixes …
Linked Issues check Passed Issue #265 requires the default-branch JSON-decode warning in get_github_default_branch() to print the package name instead of a literal '%s'. The suggested fix is sprintf() with $package_name, matchi…
Out of Scope Changes check Passed The changes cover only the warning fix in src/Package_Command.php and a Behat scenario in features/package-install.feature. Both match issue #265. The PR description also names a test fixture, mock-gi…
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Full details: Title check

Explanation

The title names the change: the default branch decode warning in get_github_default_branch() now includes the package name. The raw summary and objectives confirm this is the main change, which fixes the literal %s placeholder in that warning. The title uses the conventional fix(package) prefix and is specific and concise.

Full details: Linked Issues check

Explanation

Issue #265 requires the default-branch JSON-decode warning in get_github_default_branch() to print the package name instead of a literal '%s'. The suggested fix is sprintf() with $package_name, matching the sibling warnings. The PR description and the change summary both state that the warning now uses sprintf( ..., $package_name ). The new Behat scenario 'Warn with the package name when the GitHub API response cannot be decoded' mocks HTTP 200 with a non-JSON body and asserts the package name in STDERR. The author reports that the scenario fails without the fix and passes with it. The issue does not require a change to the 'master' fallback, and the PR leaves that behaviour unchanged. Limitation: the diff tool was unavailable and the head file read was truncated before the method, so the edited lines were not read directly. This verdict relies on the PR description and change summary.

Full details: Out of Scope Changes check

Explanation

The changes cover only the warning fix in src/Package_Command.php and a Behat scenario in features/package-install.feature. Both match issue #265. The PR description also names a test fixture, mock-github-api.php, loaded through WP_CLI_REQUIRE. That fixture supports the new scenario, so it is a legitimate test change. The change summary does not list this file, and the diff tool was unavailable, so its contents were not verified. No unrelated change is demonstrated.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 79bbbda6-cc41-43b3-ace3-5df7c388ca2a
Commits

Reviewing files that changed from the base of the PR and between 6487f3a and a7a4cd5.

Files selected for processing: 2

  • features/package-install.feature
  • src/Package_Command.php

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.

@github-actions github-actions Bot added bug command:package-install Related to 'package install' command scope:testing Related to testing labels Dec 2, 2026
@codecov

codecov Bot commented Dec 2, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 0% with 5 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
src/Package_Command.php 0.00% 5 Missing ⚠️

📢 Thoughts on this report? Let us know!

@swissspidy swissspidy left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Good catch, thank you!

@swissspidy
swissspidy merged commit 609a393 into wp-cli:main Dec 2, 2026
67 of 68 checks passed
@github-actions github-actions Bot added this to the 4.0.1 milestone Dec 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug command:package-install Related to 'package install' command scope:testing Related to testing

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Literal "%s" in warning when default branch JSON response cannot be decoded

2 participants