WordPress / blueprints-library

31 stars 7 forks source link

How to use files from plugin / blueprint repo in wp-cli blueprint step #99

Open leewillis77 opened 7 months ago

leewillis77 commented 7 months ago

I'm currently looking to implement a preview for one of our wp.org hosted plugins. In order to set up a meaningful demo we'd like to import some sample data using a wp-cli command. It looks like I can specify a wp-cli step, but I'm unsure what the execution context is of that command and what files it can access.

Ideally I'd be looking at providing this sort of structure in the SVN repo:

I tried referencing the file directly without any path information, as the example below, but that doesn't appear to work.

{
    "step": "wp-cli",
    "command": "wp say-what import sample-replacements.csv"
}

Is there a way to reference asset files in playground steps?

flexseth commented 7 months ago

I'm currently looking to implement a preview for one of our wp.org hosted plugins. In order to set up a meaningful demo we'd like to import some sample data using a wp-cli command. It looks like I can specify a wp-cli step, but I'm unsure what the execution context is of that command and what files it can access.

@leewillis77 - a prime example of how to use Playground!

If one of the approaches above works, please let me know which. And if possible if I could include a link to importing demo content to this example use case from the docs, that would be excellent!

leewillis77 commented 7 months ago

Hi @flexseth;

Thanks for the quick reply, but I don't think either of those examples really help.

Human Made's package is for running a WordPress import, which isn't the case here, the data is plugin-specific, so it's not done via a WordPress import. Even in the Simple User Listing plugin example while the data is not included in the blueprint.json file it looks like it's pulled from an external URL.

What I'm asking in this issue is the ability to have a way for a step (in this case a wp-cli step) to access a file in the SVN repo. I can't currently see how to do that? See the 'wp-cli' step here https://plugins.svn.wordpress.org/say-what/assets/blueprints/blueprint.json

The wp-cli command needs to access a file to read the information from, but I can't work out how to point that at https://plugins.svn.wordpress.org/say-what/assets/blueprints/sample-replacements.csv

flexseth commented 7 months ago

The wp-cli command needs to access a file to read the information from, but I can't work out how to point that at https://plugins.svn.wordpress.org/say-what/assets/blueprints/sample-replacements.csv

Interesting. I'm able to click and download the file, so it should be publicly accessible?

This may be a limitation of the WP-CLI package or how it's implemented in Playground.

Definitely a use case we are looking to get ironed out very soon, so I will be following here and also trying to work on an example myself. The only problem is I don't have anything on SVN, or really know how to use Subversion at all. :/

leewillis77 commented 7 months ago

This may be a limitation of the WP-CLI package or how it's implemented in Playground.

Maybe, since essentially wp-cli is expecting a "local" file, not a URL, so I'm not clear on how I can either a) fetch the file from a remote and then have the wp-cli step access it somehow or b) have the wp-cli step use a remote file directly.

helgatheviking commented 7 months ago

@leewillis77 My understanding on import was that it needs to be somewhere public with a CORS policy set. I couldn't tell you in the SVN repo has that or not, but in a github repo with the raw github url should be accessible via cli.

flexseth commented 7 months ago

@leewillis77 - try out the demo from @ryanwelcher's livestream

I'm working on the docs, if you're able to get a technique similar to this to work, or however you get this working, please link the blueprint you come up with!

CORS

Can totally be a "gotcha"

@helgatheviking may be correct with the Cross-Origin headers. You can try adding a PHP headers with a RunPHPWithOptionsStep step - providing the CORS headers?

{
    "steps": [
        {
            "step": "runPHP",
            "options": {
                "code": "<?php echo $_SERVER['CONTENT_TYPE']; ?>",
                "headers": {
                    "Content-type": "text/plain"
                }
            }
        }
    ]
}
leewillis77 commented 7 months ago

@helgatheviking @flexseth Thanks both for the comments, but they're not really relevant to the issue at hand - probably I'm explaining things badly, apologies.

This is not an importFile step, so neither Ryan's example nor the issue with requiring cors headers on importFile fetches are relevant. It's a wp-cli step so I either need to understand if there's a way I can write a step to fetch a remote URL into the local filesystem somewhere that I can point a subsequent wp-cli step at, or in some way allow the wp-cli step to directly access the content from a URL not a local file.

flexseth commented 7 months ago

write a step to fetch a remote URL into the local filesystem somewhere that I can point a subsequent wp-cli step at

How about with PHP?

What if you create a PHP snippet that will achieve what you need, and pass that through the runPHP step - similar to the following blueprint

In this example, Adam refactors a PHP snippet as a call to a runPHP step

changes to fetch

Otherwise, if you search the WordPress Playground repo for "fetch" - you may find more information on the changes they've been making relative to data handling.

I know there have been a lot of changes made to how fetch works behind the scenes but had to turn off email notifications because there was soo many updates 👍

adamziel commented 7 months ago

@leewillis77 here's what I would do:

{
    "steps": [
        {
             "step": "writeFile",
             "path": "/wordpress/sample-replacements.csv",
             "data": {
                 "resource": "url",
                 "url": "https://plugins.svn.wordpress.org/<YOUR PLUGIN>/assets/blueprints/sample-replacements.csv"
              }
        },
        {
            "step": "wp-cli",
            "command": "wp say-what import /wordpress/sample-replacements.csv"
        }
    ]
}

Let me know if that's helpful.

leewillis77 commented 7 months ago

Hey @adamziel;

That looks like exactly what I was asking. It won't work as-is since plugins.svn.wordpress.org doesn't set the appropriate CORS headers to allow the fetch to work. Is that something we could get changed do you think?

I can confirm that it does work if you (for example) point it at a GitHub raw URL (thanks @helgatheviking!)

adamziel commented 7 months ago

It won't work as-is since plugins.svn.wordpress.org doesn't set the appropriate CORS headers to allow the fetch to work. Is that something we could get changed do you think?

cc @tellyworth @dd32 – should plugins.svn.wordpress.org expose the CORS headers to allow using additional assets in Blueprints?

leewillis77 commented 7 months ago

The blueprint.json at https://plugins.svn.wordpress.org/say-what/assets/blueprints/ is an example of how this would be used. Ideally the resource would fetch the file from https://plugins.svn.wordpress.org/say-what/assets/blueprints/sample-replacements.csv rather than GitHub.

dd32 commented 7 months ago

should plugins.svn.wordpress.org expose the CORS headers to allow using additional assets in Blueprints?

We probably don't want plugins.svn being accessed, but the CDN in front of it at ps.w.org can probably be used (We could add some code to playground to rewrite the fetches). But it doesn't currently support CORS and it requires cache-busters be included in the urls..

Alex requested systems input back in November, and there hasn't been an outcome on whether adding CORS is possible. Private systems request: https://make.wordpress.org/systems/?p=2330