diff --git a/.github/workflows/pr-build-live-branch.yml b/.github/workflows/pr-build-live-branch.yml new file mode 100644 index 000000000..639c7467c --- /dev/null +++ b/.github/workflows/pr-build-live-branch.yml @@ -0,0 +1,215 @@ +name: Build PR for testing + +on: + pull_request: + +concurrency: + group: ${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: true + +permissions: {} + +# Two jobs on purpose: the first one runs the pull request's own code (its build scripts and its +# dependencies' install scripts), so it gets a read-only token. The second one holds the token that +# can comment, and runs nothing from the pull request. +jobs: + build-zip-file: + name: "Build the zip file" + runs-on: ubuntu-latest + timeout-minutes: 15 + permissions: + contents: read + steps: + - name: "Checkout repository" + uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 + with: + persist-credentials: false + + - name: "Cache node_modules" + id: cache-node-modules + uses: actions/cache@d4323d4df104b026a6aa633fdb11d772146be0bf # v4.2.2 + env: + cache-name: cache-node-modules + with: + path: node_modules + key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('**/package-lock.json') }} + + - name: "Setup node version and npm cache" + uses: actions/setup-node@cdca7365b2dadb8aad0a33bc7601856ffabcc48e # v4.3.0 + with: + node-version-file: '.nvmrc' + cache: 'npm' + + - name: "Install Node dependencies" + if: steps.cache-node-modules.outputs.cache-hit != 'true' + run: npm ci --no-optional + + # The build is labelled as a pre-release of the next patch version, so it sorts above the + # current release and below the next one, and names the pull request and commit it was built from. + - name: "Get dev version" + id: get-dev-version + env: + PR_NUMBER: ${{ github.event.number }} + PR_HEAD_SHA: ${{ github.event.pull_request.head.sha }} + run: | + VERSION=$( sed -n 's/^ \* Version: //p' woocommerce-square.php | head -n1 ) + IFS=. read -r MAJOR MINOR PATCH <<<"$VERSION" + PATCH="${PATCH%%[!0-9]*}" + if [[ ! "$MAJOR" =~ ^[0-9]+$ || ! "$MINOR" =~ ^[0-9]+$ || ! "$PATCH" =~ ^[0-9]+$ ]]; then + echo "::error::Could not read the plugin version from woocommerce-square.php (found '$VERSION')." + exit 1 + fi + DEV_VERSION="${MAJOR}.${MINOR}.$(( PATCH + 1 ))-dev_PR-${PR_NUMBER}_commit-${PR_HEAD_SHA:0:7}" + echo "Current version found: $VERSION" >> "$GITHUB_STEP_SUMMARY" + echo "Dev version: $DEV_VERSION" >> "$GITHUB_STEP_SUMMARY" + echo "DEV_VERSION=$DEV_VERSION" >> "$GITHUB_OUTPUT" + + - name: "Bump dev version" + env: + DEV_VERSION: ${{ steps.get-dev-version.outputs.DEV_VERSION }} + run: | + # 'Version' header and WC_SQUARE_PLUGIN_VERSION constant in the plugin entry file. + sed -i.bak \ + -e "s/^ \* Version: .*$/ * Version: ${DEV_VERSION}/" \ + -e "s/define( 'WC_SQUARE_PLUGIN_VERSION', '[^']*' )/define( 'WC_SQUARE_PLUGIN_VERSION', '${DEV_VERSION}' )/" \ + woocommerce-square.php + + rm -f woocommerce-square.php.bak + + # Fail here rather than ship a build that still carries the release version. + grep -qF " * Version: ${DEV_VERSION}" woocommerce-square.php + grep -qF "define( 'WC_SQUARE_PLUGIN_VERSION', '${DEV_VERSION}' )" woocommerce-square.php + + - name: "Build the plugin" + run: npm run build + + # The Playground links in the PR comment download this artifact on every click, so it is kept + # for 14 days. The run ID in the name ties the links to the build of one run: a later run that + # fails or is cancelled part-way cannot change what an earlier comment's links install. + - name: "Upload the zip file as an artifact for the Playground preview" + uses: actions/upload-artifact@4cec3d8aa04e39d1a68397de0c4cd6fb9dce8ec1 # v4.6.1 + with: + name: plugins-${{ github.run_id }} + path: woocommerce-square.zip + if-no-files-found: error + overwrite: true + retention-days: 14 + + inform-zip-file: + name: "Inform the zip file" + needs: build-zip-file + runs-on: ubuntu-latest + timeout-minutes: 5 + permissions: + pull-requests: write + steps: + # WordPress Playground runs in the visitor's browser and can only fetch public URLs. + # Its artifact proxy serves this workflow's artifacts for the PR, looked up by workflow + # name - hence GITHUB_WORKFLOW rather than a literal that a rename would silently break. + # The build artifact wraps the plugin zip, so the Blueprints unzip it before installing. + # + # The proxy resolves the PR to its branch name and serves the newest build with that branch + # name, pull requests from forks included, so a link cannot promise whose code it runs. + # Never add a Blueprint that carries a secret, or a link that asks for credentials or tokens. + # + # The links are for reviewers with no development environment, so the store is set up for them: + # nothing is left to click through before the Square screens. If a pull request puts its UI + # behind an option or a feature flag, turn it on in STORE_SETUP in that same pull request. + - name: "Build the WordPress Playground preview links" + id: playground + env: + PR_NUMBER: ${{ github.event.number }} + run: | + # `read` returns non-zero when it reaches the end of the here-document, hence `|| true`. + read -r -d '' STORE_SETUP <<'PHP' || true + true ) ); + update_option( 'woocommerce_coming_soon', 'no' ); + + // Store orders the way a new store does (HPOS). WooCommerce creates the tables. + update_option( 'woocommerce_custom_orders_table_enabled', 'yes' ); + + // Square sends a new merchant to its onboarding wizard on the first admin page load. + // Mark that as done, so each link opens the screen it names. + update_option( 'wc_square_show_wizard_on_activation', true ); + + $product = new WC_Product_Simple(); + $product->set_name( 'Sample product' ); + $product->set_sku( 'sample-product' ); + $product->set_regular_price( '10' ); + $product->set_status( 'publish' ); + $product->save(); + PHP + + ARTIFACT_URL=$( jq -rn \ + --arg org "$GITHUB_REPOSITORY_OWNER" \ + --arg repo "${GITHUB_REPOSITORY#*/}" \ + --arg workflow "$GITHUB_WORKFLOW" \ + --arg pr "$PR_NUMBER" \ + --arg artifact "plugins-$GITHUB_RUN_ID" \ + '"https://playground.wordpress.net/plugin-proxy.php?org=\($org|@uri)&repo=\($repo|@uri)&workflow=\($workflow|@uri)&pr=\($pr|@uri)&artifact=\($artifact|@uri)"' ) + + BLUEPRINTS=$( jq -cn --arg artifact_url "$ARTIFACT_URL" --arg store_setup "$STORE_SETUP" ' + { + "$schema": "https://playground.wordpress.net/blueprint-schema.json", + preferredVersions: { php: "8.3", wp: "latest" }, + features: { networking: true }, + steps: [ + { step: "login", username: "admin" }, + { step: "installPlugin", pluginData: { resource: "wordpress.org/plugins", slug: "woocommerce" }, options: { activate: true } }, + { step: "runPHP", code: $store_setup }, + { step: "unzip", zipFile: { resource: "url", url: $artifact_url, caption: "Downloading the Square build for this PR" }, extractToPath: "/tmp/pr" }, + { step: "installPlugin", pluginData: { resource: "vfs", path: "/tmp/pr/woocommerce-square.zip" }, options: { activate: true } } + ] + } as $base + | { + settings: ( $base + { landingPage: "/wp-admin/admin.php?page=wc-settings&tab=square" } ), + onboarding: ( $base + { landingPage: "/wp-admin/admin.php?page=woocommerce-square-onboarding" } ) + }' ) + + { + echo "settings_url=https://playground.wordpress.net/#$( jq -rn --argjson blueprints "$BLUEPRINTS" '$blueprints.settings|tojson|@uri' )" + echo "onboarding_url=https://playground.wordpress.net/#$( jq -rn --argjson blueprints "$BLUEPRINTS" '$blueprints.onboarding|tojson|@uri' )" + } >> "$GITHUB_OUTPUT" + + - name: "Write the information for the PR" + env: + PLAYGROUND_SETTINGS_URL: ${{ steps.playground.outputs.settings_url }} + PLAYGROUND_ONBOARDING_URL: ${{ steps.playground.outputs.onboarding_url }} + PR_HEAD_SHA: ${{ github.event.pull_request.head.sha }} + COMMENT_FILE: ${{ runner.temp }}/test-the-build.md + run: | + cat > "$COMMENT_FILE" < Settings > Square. + - :sparkles: [Open the onboarding wizard](${PLAYGROUND_ONBOARDING_URL}): the same store, on the wizard a new merchant sees first. + + :information_source: Neither can connect to Square: signing in to Square has to send you back to the site, and a site that runs in your browser tab has no public address to come back to. Without a connection the store cannot take payments, sync products or receive webhooks, so the previews show what a merchant sees before connecting. Do not enter Square access tokens or other credentials in a preview. The links stop working 14 days after the last build. + + --- + + Build info: + + - Latest commit: ${PR_HEAD_SHA} + - Build time: $( date -u "+%Y-%m-%d %H:%M:%S UTC" ) + + _Note: the build is updated when a new commit is pushed to this PR._ + EOF + + cat "$COMMENT_FILE" >> "$GITHUB_STEP_SUMMARY" + + # Pull requests from forks get a read-only token, so they cannot be commented on from here. + # Their build information is in the summary of the workflow run instead. + - name: "Add/update the information as a comment in the PR" + if: github.event.pull_request.head.repo.full_name == github.repository + uses: marocchino/sticky-pull-request-comment@52423e01640425a022ef5fd42c6fb5f633a02728 # v2.9.1 + with: + header: test-the-build + path: ${{ runner.temp }}/test-the-build.md