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

Jetpack core api: Refactor and reuse trait for proxying requests to WPCOM #40245

Merged
merged 16 commits into from
Nov 25, 2024

Conversation

fgiannar
Copy link
Contributor

@fgiannar fgiannar commented Nov 19, 2024

This PR refactors the existing WPCOM_REST_API_Proxy_Request_Trait so that it can be used by any shared endpoint (between self-hosted and Simple sites) that needs to forward the request to WPCOM when it runs on the Jetpack end.

It also refactors two existing endpoints to use the trait and introduces unit tests for both:

  • WP_Test_WPCOM_REST_API_V3_Endpoint_Blogging_Prompts
  • WPCOM_REST_API_V2_Endpoint_Memberships

Proposed changes:

WPCOM_REST_API_Proxy_Request_Trait:

  • Introduce proxy_request_to_wpcom to allow falling back to using the Site-level Connection if the current user is not connected
  • Update proxy_request_to_wpcom_as_user for proxying requests to WPCOM on behalf of the current user
  • Introduce proxy_request_to_wpcom_as_blog for proxying requests to WPCOM using the Site-level Connection

WP_Test_WPCOM_REST_API_V3_Endpoint_Blogging_Prompts:

  • Refactor endpoint to use the WPCOM_REST_API_Proxy_Request_Trait trait
  • Add unit tests

WPCOM_REST_API_V2_Endpoint_Memberships:

  • Refactor endpoint to use the WPCOM_REST_API_Proxy_Request_Trait trait
  • Add unit tests

Other information:

  • Have you written new tests for your changes, if applicable?
  • Have you checked the E2E test CI results, and verified that your changes do not break them?
  • Have you tested your changes on WordPress.com, if applicable (if so, you'll see a generated comment below with a script to run)?

Jetpack product discussion

p1730895681631659-slack-C05PV073SG3

Does this pull request change what data or activity we track or use?

No

Testing instructions:

  • Make sure to run the following command on your sandbox: bin/jetpack-downloader test jetpack update/forward-wpcom-request-trait
  • We'll need to test the Payments block and the Writing Prompt Block across all environments: self-hosted, WoA and Simple
  • Add a Donations Form block in a post and play around with all the related fields and block settings.
  • Confirm behaviour same as trunk
Screenshot 2024-11-20 at 13 09 01
  • Add a Writing Prompt block in a post and confirm behaviour same as trunk
Screenshot 2024-11-20 at 13 17 23

Copy link
Contributor

github-actions bot commented Nov 19, 2024

Are you an Automattician? Please test your changes on all WordPress.com environments to help mitigate accidental explosions.

  • To test on WoA, go to the Plugins menu on a WordPress.com Simple site. Click on the "Upload" button and follow the upgrade flow to be able to upload, install, and activate the Jetpack Beta plugin. Once the plugin is active, go to Jetpack > Jetpack Beta, select your plugin, and enable the update/forward-wpcom-request-trait branch.

  • To test on Simple, run the following command on your sandbox:

    bin/jetpack-downloader test jetpack update/forward-wpcom-request-trait
    

Interested in more tips and information?

  • In your local development environment, use the jetpack rsync command to sync your changes to a WoA dev blog.
  • Read more about our development workflow here: PCYsg-eg0-p2
  • Figure out when your changes will be shipped to customers here: PCYsg-eg5-p2

@github-actions github-actions bot added [Plugin] Jetpack Issues about the Jetpack plugin. https://wordpress.org/plugins/jetpack/ [Tests] Includes Tests labels Nov 19, 2024
Copy link
Contributor

Thank you for your PR!

When contributing to Jetpack, we have a few suggestions that can help us test and review your patch:

  • ✅ Include a description of your PR changes.
  • ✅ Add a "[Status]" label (In Progress, Needs Team Review, ...).
  • ✅ Add testing instructions.
  • ✅ Specify whether this PR includes any changes to data or privacy.
  • ✅ Add changelog entries to affected projects

This comment will be updated as you work on your PR and make changes. If you think that some of those checks are not needed for your PR, please explain why you think so. Thanks for cooperation 🤖


The e2e test report can be found here. Please note that it can take a few minutes after the e2e tests checks are complete for the report to be available.


Follow this PR Review Process:

  1. Ensure all required checks appearing at the bottom of this PR are passing.
  2. Choose a review path based on your changes:
    • A. Team Review: add the "[Status] Needs Team Review" label
      • For most changes, including minor cross-team impacts.
      • Example: Updating a team-specific component or a small change to a shared library.
    • B. Crew Review: add the "[Status] Needs Review" label
      • For significant changes to core functionality.
      • Example: Major updates to a shared library or complex features.
    • C. Both: Start with Team, then request Crew
      • For complex changes or when you need extra confidence.
      • Example: Refactor affecting multiple systems.
  3. Get at least one approval before merging.

Still unsure? Reach out in #jetpack-developers for guidance!


Jetpack plugin:

The Jetpack plugin has different release cadences depending on the platform:

  • WordPress.com Simple releases happen semi-continuously (PCYsg-Jjm-p2).
  • WoA releases happen weekly.
  • Releases to self-hosted sites happen monthly. The next release is scheduled for none scheduled (scheduled code freeze on undefined).

If you have any questions about the release process, please ask in the #jetpack-releases channel on Slack.

@fgiannar fgiannar added this to the Jetpack 14.1 milestone Nov 19, 2024
Copy link
Contributor

@coder-karen coder-karen left a comment

Choose a reason for hiding this comment

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

Nice work, and the extent of the tests is fantastic!

I tested functionality on all three platforms and from what I could see the two blocks worked as I'd expect.

I also left some review comments, let me know what you think (none are blockers). I reviewed from the perspective of testing and code reviewing, rather than from the point of view of understanding the best endpoint approach (following the linked Slack thread).

public function test_list_products_no_auth() {
wp_set_current_user( 0 );

$request = new WP_REST_Request( Requests::GET, '/wpcom/v2/memberships/products' );
Copy link
Contributor

Choose a reason for hiding this comment

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

Could it be worth defining some of the endpoint URLs as class constants to prevent possible issues if anything changes? such as const PRODUCTS_ENDPOINT = '/wpcom/v2/memberships/products';

Copy link
Contributor Author

Choose a reason for hiding this comment

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

TBH I'd prefer the test to fail on purpose if someone redefines the endpoint path :)

public function test_list_products_with_insufficient_permissions() {
wp_set_current_user( static::$author_id );

$request = new WP_REST_Request( Requests::GET, '/wpcom/v2/memberships/products' );
Copy link
Contributor

Choose a reason for hiding this comment

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

As most of the tests make requests, I wonder if it might be worth extracting the request logic into a helper function? A similar example is here:

protected function create_and_get_request( $route = '', $json_params = array(), $method = 'GET', $params = array() ) {

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm personally ok with repeating the new WP_REST_Request part and setting headers and body independently in each test as per out needs.
I find methods like this a bit restricting in eg adding additional headers or interacting with the WP_REST_Request object as needed, and they tend to grow over time in terms of params and custom logic.

That said, if we'd like to use a helper method like this perhaps we could do that in the future as part of refactoring the tests to use a common trait?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, perhaps if there is a need to come back and generally refactor or add more tests, it might make sense then too.

Copy link
Member

@jeherve jeherve left a comment

Choose a reason for hiding this comment

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

This is nice, thanks for working on this! And the added tests will help too.

I only have a minor question, not a blocker for me.

fgiannar and others added 2 commits November 25, 2024 10:01
Copy link
Contributor

@coder-karen coder-karen left a comment

Choose a reason for hiding this comment

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

LGTM! 👍

@fgiannar fgiannar merged commit bcf6634 into trunk Nov 25, 2024
58 checks passed
@fgiannar fgiannar deleted the update/forward-wpcom-request-trait branch November 25, 2024 11:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Plugin] Jetpack Issues about the Jetpack plugin. https://wordpress.org/plugins/jetpack/ [Tests] Includes Tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants