From e373622090b4dfbb17e16ca324034ac190b3233b Mon Sep 17 00:00:00 2001 From: Victor Larodiel Date: Tue, 10 Nov 2026 22:08:16 -0300 Subject: [PATCH 01/11] Add push-translations: a POT file to GlotPress, with the project and the server folder as inputs --- .github/actions/push-translations/README.md | 126 ++++++++++++++ .github/actions/push-translations/action.yml | 151 +++++++++++++++++ .../push-translations/push-translations.js | 158 ++++++++++++++++++ .../push-translations.test.js | 156 +++++++++++++++++ 4 files changed, 591 insertions(+) create mode 100644 .github/actions/push-translations/README.md create mode 100644 .github/actions/push-translations/action.yml create mode 100644 .github/actions/push-translations/push-translations.js create mode 100644 tests/actions/push-translations/push-translations.test.js diff --git a/.github/actions/push-translations/README.md b/.github/actions/push-translations/README.md new file mode 100644 index 00000000..3c70c7d3 --- /dev/null +++ b/.github/actions/push-translations/README.md @@ -0,0 +1,126 @@ +# push-translations + +Pushes a plugin's POT file to the shared GlotPress site, translations.stellarwp.com, and imports +its strings into a project. What the import printed comes back as an output. + +The Events Calendar, a project at the top level of the site: + +```yaml +- uses: stellarwp/plugin-toolbox/.github/actions/push-translations@v1 + id: translations + with: + project: the-events-calendar + pot-path: lang/the-events-calendar.pot + remote-folder: tec + host: ${{ secrets.TRANSLATIONS_DEPLOY_HOST }} + user: ${{ secrets.TRANSLATIONS_DEPLOY_USER }} + ssh-key: ${{ secrets.TRANSLATIONS_DEPLOY_SSH_KEY }} + pot-location: ${{ secrets.TRANSLATIONS_DEPLOY_POT_LOCATION }} + +- run: echo "$RESULT" + env: + RESULT: ${{ steps.translations.outputs.glotpress-result }} +``` + +GiveWP, a project under a parent, with a folder of its own on the server: + +```yaml +- uses: stellarwp/plugin-toolbox/.github/actions/push-translations@v1 + with: + project: givewp/give + pot-path: languages/give.pot + remote-folder: givewp + host: ${{ secrets.TRANSLATIONS_DEPLOY_HOST }} + user: ${{ secrets.TRANSLATIONS_DEPLOY_USER }} + ssh-key: ${{ secrets.TRANSLATIONS_DEPLOY_SSH_KEY }} + pot-location: ${{ secrets.TRANSLATIONS_DEPLOY_POT_LOCATION }} +``` + +A composite action cannot read secrets, so the caller passes them. Generate the POT before this +step: the action only sends the file. + +## What it does + +1. Checks every input and names the build: a random id of 32 hex characters. +2. Copies the POT to `//` with rsync, under the name it has in the + repository. +3. Over ssh, in `site-directory`, runs + `wp glotpress import-originals //.pot` and writes + what it prints to `bot-output/-`. +4. Reads that file back from `result-url` over https, sets it as `glotpress-result` and adds it to + the job summary. +5. Deletes the file from the server. This runs even when the import or the read failed, so the + output does not stay public. + +The build id is random, not derived from the commit, so the address of the output cannot be +guessed while the file exists. + +## What the server needs + +- The `user` can log in with `ssh-key`, and its public key is in `authorized_keys`. +- `rsync` on the server, and `` existing and writable. rsync creates + `` when it is missing, not the directories above it. +- WP-CLI as `wp` on the path of a non-interactive shell, and the GlotPress site in + `site-directory`, with the `glotpress import-originals` command. +- The project exists in GlotPress. The import does not create one. +- `site-directory/bot-output/` is served at `result-url`. The action creates the directory. + +## What it refuses + +Before anything is sent, with the input named in the message: + +- An empty `project`, `pot-path`, `remote-folder`, `host`, `user`, `ssh-key` or `pot-location`. A + secret the repository does not have arrives empty, so this is how a missing secret shows. +- Any path whose parts are not letters, digits, `.`, `_` and `-`, or that has a part starting with + `.` or `-`. That rules out spaces, quotes, `;`, `|`, `$`, backticks, `..`, `~` and a value that + would be read as a command option. +- `project` or `remote-folder` that starts with `/`. +- `pot-path` that is absolute or does not end in `.pot`. +- `pot-location` that is not absolute. +- `host` that is not a host name or an IPv4 address, and `user` that is not a plain user name. +- `result-url` that is not `https://`, or that has a query, a port or credentials. + +A refused `host`, `user` or `pot-location` is named without its value, since it comes from a +secret. + +## Inputs + +| Input | Required | Default | What it does | +|---|---|---|---| +| `project` | yes | | The GlotPress project path: `the-events-calendar`, `givewp/give` | +| `pot-path` | yes | | The POT file, relative to the workspace | +| `remote-folder` | yes | | The folder under `pot-location` the POT is copied to: `tec`, `givewp` | +| `host` | yes | | The server to connect to | +| `user` | yes | | The user to connect as | +| `ssh-key` | yes | | The private key of `user` | +| `pot-location` | yes | | The directory on the server that holds the POT folders, absolute | +| `site-directory` | no | `translations.stellarwp.com/html/` | The GlotPress site on the server, absolute or relative to the home of `user` | +| `result-url` | no | `https://translations.stellarwp.com/bot-output/` | The public URL of `bot-output/` in `site-directory` | + +A trailing `/` on a folder, a directory or the URL is optional. + +## Outputs + +| Output | Example | What it is | +|---|---|---| +| `glotpress-result` | `1 new strings added, 0 updated, 0 fuzzied, and 0 obsoleted.` | What `wp glotpress import-originals` printed. Text from the server: pass it to a script through `env`, never inside `run:` | +| `project` | `givewp/give` | The project the strings were imported into | + +## How it runs + +One [actions/github-script](https://github.com/actions/github-script) step runs +[`push-translations.js`](./push-translations.js), which checks the inputs and sets the values the +later steps use. Inputs reach it through `process.env`, never by being interpolated into it. + +The copy and the two ssh steps are third-party actions, +[burnett01/rsync-deployments](https://github.com/Burnett01/rsync-deployments) and +[appleboy/ssh-action](https://github.com/appleboy/ssh-action), pinned to a full commit SHA because +they receive the deploy key. The rsync action builds a shell command from the paths, the host and +the user, which is why all of them are checked. The remote script holds no value: the checked +values reach the remote shell as environment variables, through the ssh action's `envs`. + +The output is written to `GITHUB_OUTPUT` as a heredoc with a random delimiter, so a result of +several lines cannot add or override an output. + +`tests/actions/push-translations/push-translations.test.js` covers the check: the two shapes +above, and the refusals. Run `npm install` once, then `node --test`. diff --git a/.github/actions/push-translations/action.yml b/.github/actions/push-translations/action.yml new file mode 100644 index 00000000..ee4988c0 --- /dev/null +++ b/.github/actions/push-translations/action.yml @@ -0,0 +1,151 @@ +# Pushes a plugin's POT file to the shared GlotPress site, translations.stellarwp.com: rsync copies +# the POT to the server, `wp glotpress import-originals` imports it over ssh, and what the import +# printed comes back as an output. +# +# This action handles a deploy key and builds remote shell commands. The first step checks every +# input against an allow-list, in push-translations.js run by actions/github-script, and fails +# before anything is sent. The later steps use the values it checked. No input is interpolated into +# a script: a `run:` step and the remote `script:` read their values from the environment. +name: Push translations +description: Pushes a POT file to the GlotPress site and imports its strings into a project + +inputs: + project: + description: >- + The GlotPress project the strings are imported into, as its path on the site: + `the-events-calendar`, or `givewp/give` for a project under a parent. + required: true + pot-path: + description: >- + The POT file, relative to the workspace, e.g. `languages/give.pot`. Its name is the name it + has on the server. + required: true + remote-folder: + description: >- + The folder under `pot-location` the POT is copied to, one per product line: `tec`, `givewp`. + required: true + host: + description: 'The server to connect to. Pass a secret.' + required: true + user: + description: 'The user to connect as. Pass a secret.' + required: true + ssh-key: + description: 'The private key of `user`. Pass a secret.' + required: true + pot-location: + description: >- + The directory on the server that holds the POT folders, as an absolute path, e.g. + `/home/user/pots/`. Pass a secret. + required: true + site-directory: + description: >- + The directory of the GlotPress site on the server, where `wp` runs: absolute, or relative to + the home of `user`. + required: false + default: 'translations.stellarwp.com/html/' + result-url: + description: >- + The public https URL of the `bot-output/` folder in `site-directory`, where the import output + is read back from. + required: false + default: 'https://translations.stellarwp.com/bot-output/' + +outputs: + glotpress-result: + description: 'What `wp glotpress import-originals` printed.' + value: ${{ steps.fetch.outputs.glotpress-result }} + project: + description: 'The GlotPress project the strings were imported into.' + value: ${{ steps.check.outputs.project }} + +runs: + using: 'composite' + steps: + - name: Check the inputs and name the build + id: check + uses: actions/github-script@v9 + env: + # github-script's require() resolves against the workspace of the repository being built, + # not this action's directory, so the path has to be passed in. github.action_path is the + # documented expression for it. + ACTION_PATH: ${{ github.action_path }} + # Read from process.env rather than interpolated into the script, so an input is data and + # never part of the program. + INPUT_PROJECT: ${{ inputs.project }} + INPUT_POT_PATH: ${{ inputs.pot-path }} + INPUT_REMOTE_FOLDER: ${{ inputs.remote-folder }} + INPUT_HOST: ${{ inputs.host }} + INPUT_USER: ${{ inputs.user }} + INPUT_SSH_KEY: ${{ inputs.ssh-key }} + INPUT_POT_LOCATION: ${{ inputs.pot-location }} + INPUT_SITE_DIRECTORY: ${{ inputs.site-directory }} + INPUT_RESULT_URL: ${{ inputs.result-url }} + with: + script: | + const { runPushTranslations } = require(`${process.env.ACTION_PATH}/push-translations.js`) + await runPushTranslations({ core, env: process.env }) + + # SHA-pinned: these two actions receive the deploy key. Bump the SHA to pick up a new release. + # The rsync action builds a shell command from path, remote_path, remote_host and remote_user, + # which is why the first step checks all four. + - name: Copy the POT file to the server + uses: burnett01/rsync-deployments@796cf0d5e4b535745ce49d7429f77cf39e25ef39 # 7.0.1 + with: + switches: -avz --omit-dir-times --no-perms + path: ${{ steps.check.outputs.pot-path }} + remote_path: ${{ steps.check.outputs.remote-path }} + remote_host: ${{ inputs.host }} + remote_user: ${{ inputs.user }} + remote_key: ${{ inputs.ssh-key }} + + - name: Import the strings into GlotPress + uses: appleboy/ssh-action@25ce8cbbcb08177468c7ff7ec5cbfa236f9341e1 # v1.1.0 + env: + SITE_DIRECTORY: ${{ steps.check.outputs.site-directory }} + PROJECT: ${{ steps.check.outputs.project }} + REMOTE_POT: ${{ steps.check.outputs.remote-pot }} + RESULT_FILE: ${{ steps.check.outputs.result-file }} + with: + host: ${{ inputs.host }} + username: ${{ inputs.user }} + key: ${{ inputs.ssh-key }} + # The checked values reach the remote shell as variables, not as text of the script. + envs: SITE_DIRECTORY,PROJECT,REMOTE_POT,RESULT_FILE + script: | + set -e + cd "$SITE_DIRECTORY" + mkdir -p bot-output + wp glotpress import-originals "$PROJECT" "$REMOTE_POT" > "bot-output/$RESULT_FILE" + + - name: Read the import output + id: fetch + shell: bash + env: + RESULT_URL: ${{ steps.check.outputs.result-url }} + run: | + result=$(curl -fsS --proto '=https' --max-time 60 "$RESULT_URL") + + # A heredoc with a random delimiter: a multi-line result cannot add or override outputs. + delimiter="glotpress-$RANDOM$RANDOM$RANDOM$RANDOM" + if grep -qxF "$delimiter" <<< "$result"; then + echo "The import output holds the output delimiter. Run the job again." >&2 + exit 1 + fi + printf 'glotpress-result<<%s\n%s\n%s\n' "$delimiter" "$result" "$delimiter" >> "$GITHUB_OUTPUT" + printf '## Results from GlotPress\n\n```\n%s\n```\n' "$result" >> "$GITHUB_STEP_SUMMARY" + + # Also when the import or the read failed, so the output file does not stay public. + - name: Delete the import output from the server + if: ${{ always() && steps.check.outcome == 'success' }} + uses: appleboy/ssh-action@25ce8cbbcb08177468c7ff7ec5cbfa236f9341e1 # v1.1.0 + env: + SITE_DIRECTORY: ${{ steps.check.outputs.site-directory }} + RESULT_FILE: ${{ steps.check.outputs.result-file }} + with: + host: ${{ inputs.host }} + username: ${{ inputs.user }} + key: ${{ inputs.ssh-key }} + envs: SITE_DIRECTORY,RESULT_FILE + script: | + rm -f -- "$SITE_DIRECTORY/bot-output/$RESULT_FILE" diff --git a/.github/actions/push-translations/push-translations.js b/.github/actions/push-translations/push-translations.js new file mode 100644 index 00000000..9ea2c22d --- /dev/null +++ b/.github/actions/push-translations/push-translations.js @@ -0,0 +1,158 @@ +'use strict' + +/** + * Checks what the push-translations action was given, before anything is sent to the translations + * server, and names the file the import output is written to. + * + * The later steps of action.yml copy the POT with rsync and run `wp glotpress import-originals` + * over ssh. The rsync action builds a shell command from its inputs, and the import is a remote + * shell command, so every value that reaches either one has to match an allow-list first. Those + * steps use the outputs set here, never the raw inputs. + * + * readInputs is pure, so a test can call it with an environment. + */ + +const crypto = require('node:crypto') + +/** What was not done, to close every error message. */ +const WHAT = 'Nothing was sent to the translations server.' + +/** + * One part of a path: letters, digits, `.`, `_` and `-`, not starting with `.` or `-`. So no part + * is `..`, a hidden file or a command option, and none holds a space or a shell metacharacter. + */ +const PART = '[A-Za-z0-9_][A-Za-z0-9._-]*' + +/** One or more parts joined by `/`. */ +const PARTS = `${PART}(?:/${PART})*` + +/** + * What each input has to match, and how to say what it is. The first capture group is the value + * the action uses, so a trailing `/` is dropped. `secret` keeps the value out of the message. + */ +const RULES = { + project: { + pattern: new RegExp(`^(${PARTS})$`), + shape: 'a GlotPress project path, such as the-events-calendar or givewp/give', + }, + 'pot-path': { + pattern: new RegExp(`^((?:${PART}/)*${PART}\\.pot)$`), + shape: 'the path of a .pot file relative to the workspace, such as languages/give.pot', + }, + 'remote-folder': { + pattern: new RegExp(`^(${PARTS})/?$`), + shape: 'a folder name under pot-location, such as tec or givewp', + }, + 'site-directory': { + pattern: new RegExp(`^(/?${PARTS})/?$`), + shape: 'a directory on the server, absolute or relative to the home of user, such as translations.stellarwp.com/html/', + }, + 'result-url': { + pattern: new RegExp(`^(https://[A-Za-z0-9][A-Za-z0-9.-]*(?:/${PART})*)/?$`), + shape: 'an https URL of a folder, with no query, such as https://translations.stellarwp.com/bot-output/', + }, + host: { + pattern: /^([A-Za-z0-9][A-Za-z0-9.-]*)$/, + shape: 'a host name or an IPv4 address', + secret: true, + }, + user: { + pattern: new RegExp(`^(${PART})$`), + shape: 'a user name', + secret: true, + }, + 'pot-location': { + pattern: new RegExp(`^(/${PARTS})/?$`), + shape: 'an absolute directory on the server, such as /home/user/pots/', + secret: true, + }, + // Only handed to the rsync and ssh actions as a key, never part of a command: any text will do. + 'ssh-key': { pattern: /^([\s\S]+)$/, shape: 'a private key', secret: true }, +} + +/** A build id: 32 lowercase hex characters. */ +const BUILD_ID = /^[a-f0-9]{32}$/ + +/** + * Reads one input and checks it against its rule. + * + * @param {object} env The environment, holding the inputs as `INPUT_*`. + * @param {string} name The input's name, as action.yml spells it. + * + * @throws {Error} When the value is empty or does not match. + * + * @returns {string} The value the action uses. + */ +function readInput(env, name) { + const { pattern, shape, secret } = RULES[name] + const value = String(env[`INPUT_${name.toUpperCase().replace(/-/g, '_')}`] ?? '').trim() + + if (value === '') { + throw new Error(`${name} is empty. Pass ${shape}. ${WHAT}`) + } + + const match = pattern.exec(value) + if (!match) { + // A value from a secret is masked in the log anyway, but is not repeated here either. + throw new Error(`${name} ${secret ? '' : `'${value}' `}is not ${shape}. ${WHAT}`) + } + + return match[1] +} + +/** + * Checks every input and builds the values the later steps use. + * + * @param {object} env The environment, holding the inputs as `INPUT_*`. + * @param {string} buildId What makes the output file's name unique and hard to guess. Random by + * default; a test passes its own. + * + * @throws {Error} When an input, or the build id, is refused. + * + * @returns {Object} The step's outputs, by name. + */ +function readInputs(env, buildId = crypto.randomBytes(16).toString('hex')) { + const read = Object.fromEntries(Object.keys(RULES).map((name) => [name, readInput(env, name)])) + + if (!BUILD_ID.test(buildId)) { + throw new Error(`The build id is not 32 hex characters. ${WHAT}`) + } + + const potFile = read['pot-path'].split('/').pop() + const remotePath = `${read['pot-location']}/${read['remote-folder']}/` + const resultFile = `${potFile.slice(0, -'.pot'.length)}-${buildId}` + + // host, user and ssh-key are checked but not returned: the later steps take them from the inputs. + return { + project: read.project, + 'pot-path': read['pot-path'], + 'remote-path': remotePath, + 'remote-pot': `${remotePath}${potFile}`, + 'site-directory': read['site-directory'], + 'result-file': resultFile, + 'result-url': `${read['result-url']}/${resultFile}`, + } +} + +/** + * Runs the check and sets the outputs. + * + * @param {object} options + * @param {object} options.core The @actions/core toolkit, for the outputs and the summary. + * @param {object} options.env The environment, holding the inputs as `INPUT_*`. + * + * @throws {Error} When an input is refused. + * + * @returns {Promise} + */ +async function runPushTranslations({ core, env }) { + const outputs = readInputs(env) + + for (const [name, value] of Object.entries(outputs)) { + core.setOutput(name, value) + } + + await core.summary.addRaw(`## Translations build: \`${outputs['result-file']}\``, true).write() +} + +module.exports = { runPushTranslations, readInputs } diff --git a/tests/actions/push-translations/push-translations.test.js b/tests/actions/push-translations/push-translations.test.js new file mode 100644 index 00000000..0f09ecfb --- /dev/null +++ b/tests/actions/push-translations/push-translations.test.js @@ -0,0 +1,156 @@ +'use strict' + +const { describe, it } = require('node:test') +const assert = require('node:assert') + +const { + runPushTranslations, + readInputs, +} = require('../../../.github/actions/push-translations/push-translations.js') +const { coreWithOutputs } = require('../../support/actions.js') + +const BUILD_ID = '0123456789abcdef0123456789abcdef' + +/** What The Events Calendar passes, with the defaults of action.yml filled in. */ +const TEC = { + INPUT_PROJECT: 'the-events-calendar', + INPUT_POT_PATH: 'lang/the-events-calendar.pot', + INPUT_REMOTE_FOLDER: 'tec/', + INPUT_SITE_DIRECTORY: 'translations.stellarwp.com/html/', + INPUT_RESULT_URL: 'https://translations.stellarwp.com/bot-output/', + INPUT_HOST: 'ssh.example.com', + INPUT_USER: 'deploy', + INPUT_SSH_KEY: '-----BEGIN OPENSSH PRIVATE KEY-----\nabc\n-----END OPENSSH PRIVATE KEY-----', + INPUT_POT_LOCATION: '/home/deploy/pots/', +} + +/** What GiveWP passes: a project under a parent, and its own remote folder. */ +const GIVE = { + ...TEC, + INPUT_PROJECT: 'givewp/give', + INPUT_POT_PATH: 'languages/give.pot', + INPUT_REMOTE_FOLDER: 'givewp', +} + +describe('push-translations', () => { + it('accepts the TEC shape', () => { + assert.deepStrictEqual(readInputs(TEC, BUILD_ID), { + project: 'the-events-calendar', + 'pot-path': 'lang/the-events-calendar.pot', + 'remote-path': '/home/deploy/pots/tec/', + 'remote-pot': '/home/deploy/pots/tec/the-events-calendar.pot', + 'site-directory': 'translations.stellarwp.com/html', + 'result-file': `the-events-calendar-${BUILD_ID}`, + 'result-url': `https://translations.stellarwp.com/bot-output/the-events-calendar-${BUILD_ID}`, + }) + }) + + it('accepts the Give shape', () => { + assert.deepStrictEqual(readInputs(GIVE, BUILD_ID), { + project: 'givewp/give', + 'pot-path': 'languages/give.pot', + 'remote-path': '/home/deploy/pots/givewp/', + 'remote-pot': '/home/deploy/pots/givewp/give.pot', + 'site-directory': 'translations.stellarwp.com/html', + 'result-file': `give-${BUILD_ID}`, + 'result-url': `https://translations.stellarwp.com/bot-output/give-${BUILD_ID}`, + }) + }) + + it('trims the values, and takes a POT at the root and an absolute site directory', () => { + const read = readInputs( + { ...TEC, INPUT_POT_PATH: ' give.pot\n', INPUT_SITE_DIRECTORY: '/var/www/site', INPUT_HOST: '10.0.0.1\n' }, + BUILD_ID + ) + + assert.strictEqual(read['pot-path'], 'give.pot') + assert.strictEqual(read['site-directory'], '/var/www/site') + }) + + it('gives each run a build id of its own, 32 hex characters', () => { + const [first, second] = [readInputs(TEC), readInputs(TEC)] + + assert.match(first['result-file'], /^the-events-calendar-[a-f0-9]{32}$/) + assert.notStrictEqual(first['result-file'], second['result-file']) + }) + + it('refuses a build id that is not 32 hex characters', () => { + assert.throws(() => readInputs(TEC, 'abc; rm -rf ~'), /build id/) + }) + + const refused = { + 'a shell metacharacter in project': { INPUT_PROJECT: 'give; rm -rf ~' }, + 'a command substitution in project': { INPUT_PROJECT: 'give$(id)' }, + 'a backtick in project': { INPUT_PROJECT: 'give`id`' }, + 'a space in project': { INPUT_PROJECT: 'give wp' }, + 'a parent directory in project': { INPUT_PROJECT: '../give' }, + 'an option as project': { INPUT_PROJECT: '--path=/tmp' }, + 'an absolute project': { INPUT_PROJECT: '/givewp/give' }, + 'an empty project': { INPUT_PROJECT: '' }, + 'a pipe in remote-folder': { INPUT_REMOTE_FOLDER: 'tec|cat' }, + 'a parent directory in remote-folder': { INPUT_REMOTE_FOLDER: 'tec/../..' }, + 'an absolute remote-folder': { INPUT_REMOTE_FOLDER: '/tec' }, + 'an empty remote-folder': { INPUT_REMOTE_FOLDER: ' ' }, + 'a quote in site-directory': { INPUT_SITE_DIRECTORY: "html' && id '" }, + 'a parent directory in site-directory': { INPUT_SITE_DIRECTORY: '../html' }, + 'a home shorthand in site-directory': { INPUT_SITE_DIRECTORY: '~/html' }, + 'an empty site-directory': { INPUT_SITE_DIRECTORY: '' }, + 'a pot-path that is not a .pot': { INPUT_POT_PATH: 'languages/give.php' }, + 'a space in pot-path': { INPUT_POT_PATH: 'languages/give wp.pot' }, + 'a parent directory in pot-path': { INPUT_POT_PATH: '../../etc/give.pot' }, + 'an absolute pot-path': { INPUT_POT_PATH: '/etc/give.pot' }, + 'a metacharacter in pot-path': { INPUT_POT_PATH: 'languages/$(id).pot' }, + 'a newline inside pot-path': { INPUT_POT_PATH: 'languages/give.pot\nid.pot' }, + 'an empty pot-path': { INPUT_POT_PATH: '' }, + 'a result-url that is not https': { INPUT_RESULT_URL: 'http://translations.stellarwp.com/bot-output/' }, + 'a file result-url': { INPUT_RESULT_URL: 'file:///etc/passwd' }, + 'a result-url with a query': { INPUT_RESULT_URL: 'https://translations.stellarwp.com/bot-output/?a=b' }, + 'a result-url with credentials': { INPUT_RESULT_URL: 'https://user:pass@translations.stellarwp.com/' }, + 'an empty result-url': { INPUT_RESULT_URL: '' }, + 'a metacharacter in host': { INPUT_HOST: 'example.com;id' }, + 'an option as host': { INPUT_HOST: '-oProxyCommand=id' }, + 'an empty host': { INPUT_HOST: '' }, + 'a space in user': { INPUT_USER: 'deploy -o x' }, + 'an empty user': { INPUT_USER: undefined }, + 'an empty ssh-key': { INPUT_SSH_KEY: ' \n' }, + 'a relative pot-location': { INPUT_POT_LOCATION: 'pots/' }, + 'a parent directory in pot-location': { INPUT_POT_LOCATION: '/home/deploy/../root/' }, + 'a metacharacter in pot-location': { INPUT_POT_LOCATION: '/home/deploy/pots/ && id' }, + 'an empty pot-location': { INPUT_POT_LOCATION: '' }, + } + + for (const [name, inputs] of Object.entries(refused)) { + it(`refuses ${name}`, () => { + const input = Object.keys(inputs)[0].replace('INPUT_', '').toLowerCase().replace(/_/g, '-') + + assert.throws( + () => readInputs({ ...TEC, ...inputs }, BUILD_ID), + new RegExp(`^Error: ${input} .*Nothing was sent to the translations server\\.$`, 's') + ) + }) + } + + it('does not repeat a value that comes from a secret in the message', () => { + for (const input of ['INPUT_HOST', 'INPUT_USER', 'INPUT_POT_LOCATION']) { + assert.throws( + () => readInputs({ ...TEC, [input]: 's3cret value' }, BUILD_ID), + (error) => !error.message.includes('s3cret') + ) + } + }) + + it('sets the validated values as outputs and names the build in the summary', async (t) => { + const { core, outputs, summary } = await coreWithOutputs(t) + + await runPushTranslations({ core, env: GIVE }) + + const set = outputs() + assert.strictEqual(set.project, 'givewp/give') + assert.strictEqual(set['remote-pot'], '/home/deploy/pots/givewp/give.pot') + assert.match(set['result-file'], /^give-[a-f0-9]{32}$/) + assert.strictEqual(set['result-url'], `https://translations.stellarwp.com/bot-output/${set['result-file']}`) + assert.ok(summary().includes(set['result-file'])) + // The key and the host are never outputs: the later steps take them from the inputs. + assert.ok(!Object.values(set).some((value) => value.includes('PRIVATE KEY') || value.includes('ssh.example.com'))) + }) +}) From eb9fec3a55e98ca7750957ddbc27a50fc5c963ae Mon Sep 17 00:00:00 2001 From: Victor Larodiel Date: Tue, 10 Nov 2026 22:12:20 -0300 Subject: [PATCH 02/11] push-translations reads the import output in its script, not in a bash step --- .github/actions/push-translations/action.yml | 20 ++++------ .../push-translations/push-translations.js | 38 +++++++++++++++++- .../push-translations.test.js | 39 +++++++++++++++++++ 3 files changed, 83 insertions(+), 14 deletions(-) diff --git a/.github/actions/push-translations/action.yml b/.github/actions/push-translations/action.yml index ee4988c0..684d7169 100644 --- a/.github/actions/push-translations/action.yml +++ b/.github/actions/push-translations/action.yml @@ -5,7 +5,7 @@ # This action handles a deploy key and builds remote shell commands. The first step checks every # input against an allow-list, in push-translations.js run by actions/github-script, and fails # before anything is sent. The later steps use the values it checked. No input is interpolated into -# a script: a `run:` step and the remote `script:` read their values from the environment. +# a script: the remote `script:` reads its values from the environment. name: Push translations description: Pushes a POT file to the GlotPress site and imports its strings into a project @@ -120,20 +120,14 @@ runs: - name: Read the import output id: fetch - shell: bash + uses: actions/github-script@v9 env: + ACTION_PATH: ${{ github.action_path }} RESULT_URL: ${{ steps.check.outputs.result-url }} - run: | - result=$(curl -fsS --proto '=https' --max-time 60 "$RESULT_URL") - - # A heredoc with a random delimiter: a multi-line result cannot add or override outputs. - delimiter="glotpress-$RANDOM$RANDOM$RANDOM$RANDOM" - if grep -qxF "$delimiter" <<< "$result"; then - echo "The import output holds the output delimiter. Run the job again." >&2 - exit 1 - fi - printf 'glotpress-result<<%s\n%s\n%s\n' "$delimiter" "$result" "$delimiter" >> "$GITHUB_OUTPUT" - printf '## Results from GlotPress\n\n```\n%s\n```\n' "$result" >> "$GITHUB_STEP_SUMMARY" + with: + script: | + const { runReadResult } = require(`${process.env.ACTION_PATH}/push-translations.js`) + await runReadResult({ core, env: process.env }) # Also when the import or the read failed, so the output file does not stay public. - name: Delete the import output from the server diff --git a/.github/actions/push-translations/push-translations.js b/.github/actions/push-translations/push-translations.js index 9ea2c22d..fdf3ef47 100644 --- a/.github/actions/push-translations/push-translations.js +++ b/.github/actions/push-translations/push-translations.js @@ -155,4 +155,40 @@ async function runPushTranslations({ core, env }) { await core.summary.addRaw(`## Translations build: \`${outputs['result-file']}\``, true).write() } -module.exports = { runPushTranslations, readInputs } +/** + * Reads what the import printed back from the site, and sets it as the `glotpress-result` output. + * + * The URL was built by the check step from checked values. It is checked again for https here, + * since the request is made with whatever this step is handed. + * + * @param {object} options + * @param {object} options.core The @actions/core toolkit, for the output and the summary. + * @param {object} options.env The environment, holding `RESULT_URL`. + * @param {Function} options.fetchImpl The fetch to use, the runtime's by default. + * + * @throws {Error} When the URL is not https, or the site does not answer 200. + * + * @returns {Promise} + */ +async function runReadResult({ core, env, fetchImpl = globalThis.fetch }) { + const url = String(env.RESULT_URL ?? '') + + if (!url.startsWith('https://')) { + throw new Error('The import output has no https URL to be read from. The import ran, and its output was not read.') + } + + const response = await fetchImpl(url, { redirect: 'error', signal: AbortSignal.timeout(60000) }) + + if (!response.ok) { + throw new Error( + `Reading the import output answered ${response.status}. The import ran, and its output was not read.` + ) + } + + const result = (await response.text()).trim() + + core.setOutput('glotpress-result', result) + await core.summary.addHeading('Results from GlotPress', 2).addCodeBlock(result).write() +} + +module.exports = { runPushTranslations, runReadResult, readInputs } diff --git a/tests/actions/push-translations/push-translations.test.js b/tests/actions/push-translations/push-translations.test.js index 0f09ecfb..4013c734 100644 --- a/tests/actions/push-translations/push-translations.test.js +++ b/tests/actions/push-translations/push-translations.test.js @@ -5,6 +5,7 @@ const assert = require('node:assert') const { runPushTranslations, + runReadResult, readInputs, } = require('../../../.github/actions/push-translations/push-translations.js') const { coreWithOutputs } = require('../../support/actions.js') @@ -154,3 +155,41 @@ describe('push-translations', () => { assert.ok(!Object.values(set).some((value) => value.includes('PRIVATE KEY') || value.includes('ssh.example.com'))) }) }) + +describe('runReadResult', () => { + /** A fetch that answers once and records the URL it was asked for. */ + const fetchOf = (status, body) => { + const urls = [] + + return { urls, fetchImpl: async (url) => (urls.push(url), new Response(body, { status })) } + } + + it('sets what the import printed as the output, several lines included', async (t) => { + const { core, outputs, summary } = await coreWithOutputs(t) + const { urls, fetchImpl } = fetchOf(200, '3 new strings added\n1 string obsoleted\n') + + await runReadResult({ core, env: { RESULT_URL: 'https://example.com/bot-output/give-abc' }, fetchImpl }) + + assert.deepStrictEqual(urls, ['https://example.com/bot-output/give-abc']) + assert.strictEqual(outputs()['glotpress-result'], '3 new strings added\n1 string obsoleted') + assert.match(summary(), /3 new strings added/) + }) + + it('fails when the site does not answer 200, and sets no output', async (t) => { + const { core, outputs } = await coreWithOutputs(t) + + await assert.rejects( + runReadResult({ core, env: { RESULT_URL: 'https://example.com/x' }, fetchImpl: fetchOf(404, 'no').fetchImpl }), + /answered 404/ + ) + assert.deepStrictEqual(outputs(), {}) + }) + + it('refuses a URL that is not https before any request', async (t) => { + const { core } = await coreWithOutputs(t) + const { urls, fetchImpl } = fetchOf(200, 'x') + + await assert.rejects(runReadResult({ core, env: { RESULT_URL: 'http://example.com/x' }, fetchImpl }), /no https URL/) + assert.deepStrictEqual(urls, []) + }) +}) From c7f0fc68844ab55360d2f23057e5e869723d248d Mon Sep 17 00:00:00 2001 From: Victor Larodiel Date: Tue, 10 Nov 2026 22:12:36 -0300 Subject: [PATCH 03/11] push-translations README: the second script step reads the import output --- .github/actions/push-translations/README.md | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/.github/actions/push-translations/README.md b/.github/actions/push-translations/README.md index 3c70c7d3..e298138a 100644 --- a/.github/actions/push-translations/README.md +++ b/.github/actions/push-translations/README.md @@ -108,9 +108,10 @@ A trailing `/` on a folder, a directory or the URL is optional. ## How it runs -One [actions/github-script](https://github.com/actions/github-script) step runs -[`push-translations.js`](./push-translations.js), which checks the inputs and sets the values the -later steps use. Inputs reach it through `process.env`, never by being interpolated into it. +Two [actions/github-script](https://github.com/actions/github-script) steps run +[`push-translations.js`](./push-translations.js). The first checks the inputs and sets the values +the later steps use. The second reads the import output back over https and sets it as the +`glotpress-result` output. Inputs reach both through `process.env`, never by being interpolated. The copy and the two ssh steps are third-party actions, [burnett01/rsync-deployments](https://github.com/Burnett01/rsync-deployments) and @@ -119,8 +120,5 @@ they receive the deploy key. The rsync action builds a shell command from the pa the user, which is why all of them are checked. The remote script holds no value: the checked values reach the remote shell as environment variables, through the ssh action's `envs`. -The output is written to `GITHUB_OUTPUT` as a heredoc with a random delimiter, so a result of -several lines cannot add or override an output. - -`tests/actions/push-translations/push-translations.test.js` covers the check: the two shapes -above, and the refusals. Run `npm install` once, then `node --test`. +`tests/actions/push-translations/push-translations.test.js` covers the check, with the two shapes +above and the refusals, and the read of the import output. Run `npm install` once, then `node --test`. From b7a20e46defc286a903e6333a8d7307bb3bf629e Mon Sep 17 00:00:00 2001 From: Victor Larodiel Date: Mon, 16 Nov 2026 13:00:20 -0300 Subject: [PATCH 04/11] README lists the ported release actions --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index eef3fd16..ab13927d 100644 --- a/README.md +++ b/README.md @@ -119,7 +119,7 @@ Three drawers in the toolbox. You can use any one without the others. | Layer | Lives in | What's in it | |---|---|---| -| Composite actions | `.github/actions/` | Small single-purpose steps with no opinions: `setup-slic`, `setup-bun`, `setup-node`, `prepare-matrix`, `check-php-changes`, `smart-checkout`, `process-changelog`, `generate-pot`, `zip-name`, `release-zip`, `changed-files`, `skip-drafts`, `download-plugins` | +| Composite actions | `.github/actions/` | Small single-purpose steps with no opinions: `setup-slic`, `setup-bun`, `setup-node`, `prepare-matrix`, `check-php-changes`, `smart-checkout`, `process-changelog`, `generate-pot`, `push-translations`, `zip-name`, `release-zip`, `changed-files`, `skip-drafts`, `download-plugins` | | Reusable workflows | `.github/workflows/` | Full jobs built from the actions (standards, analysis, tests, zip, release prep). Most repos will only touch this layer. | | File sync | `templates/` + `.github/sync.yml` | Files that must exist in the target repo, grouped by brand and repo type | @@ -171,6 +171,7 @@ Each phase ends with something people can actually use. - [x] Add `release-merge`: the release branch into the base branch, the base back into develop, each step safe to rerun and a conflict reported with a `MERGE_CONFLICT` line. - [x] Add `release-publish`: the GitHub release for a version at a commit, with a title, notes from a file and assets uploaded before the release is public, skipped when it already exists. The complete deploy workflow to copy, both actions with the build between them, is `templates/workflows/release.yml`. - [x] Add `zip-name` and `release-zip`: the zip's name before the build, with a pre-release label (`dev`, `alpha`, `beta`, `rc`) and an optional build number, then `pup build`, `check`, `i18n` and `package` for that version. What `stellarwp/github-actions`' `zip.yml` does, as two actions so the bucket can be asked for the name first. The caller to copy, with the S3 check, the upload, the Slack notice and the artifact around them, is `templates/workflows/zip.yml`. +- [x] Port `push-translations` from TEC: a POT file to GlotPress, with the GlotPress project and the server folder as inputs. - [ ] Turn every hard-coded repo name, slug, or branch pattern into an input. - [ ] Self-checks: actionlint everything, unit test anything with real logic. - [ ] Tag `v1.0.0`. From a010499b92a150a999e90c2cd45ab045b99ab9b7 Mon Sep 17 00:00:00 2001 From: Victor Larodiel Date: Mon, 16 Nov 2026 14:20:14 -0300 Subject: [PATCH 05/11] push-translations connects with the host and user its check step trimmed and checked --- .github/actions/push-translations/action.yml | 12 +++++----- .../push-translations/push-translations.js | 7 ++++-- .../push-translations.test.js | 23 +++++++++++++++++-- 3 files changed, 32 insertions(+), 10 deletions(-) diff --git a/.github/actions/push-translations/action.yml b/.github/actions/push-translations/action.yml index 684d7169..be21c8dc 100644 --- a/.github/actions/push-translations/action.yml +++ b/.github/actions/push-translations/action.yml @@ -95,8 +95,8 @@ runs: switches: -avz --omit-dir-times --no-perms path: ${{ steps.check.outputs.pot-path }} remote_path: ${{ steps.check.outputs.remote-path }} - remote_host: ${{ inputs.host }} - remote_user: ${{ inputs.user }} + remote_host: ${{ steps.check.outputs.host }} + remote_user: ${{ steps.check.outputs.user }} remote_key: ${{ inputs.ssh-key }} - name: Import the strings into GlotPress @@ -107,8 +107,8 @@ runs: REMOTE_POT: ${{ steps.check.outputs.remote-pot }} RESULT_FILE: ${{ steps.check.outputs.result-file }} with: - host: ${{ inputs.host }} - username: ${{ inputs.user }} + host: ${{ steps.check.outputs.host }} + username: ${{ steps.check.outputs.user }} key: ${{ inputs.ssh-key }} # The checked values reach the remote shell as variables, not as text of the script. envs: SITE_DIRECTORY,PROJECT,REMOTE_POT,RESULT_FILE @@ -137,8 +137,8 @@ runs: SITE_DIRECTORY: ${{ steps.check.outputs.site-directory }} RESULT_FILE: ${{ steps.check.outputs.result-file }} with: - host: ${{ inputs.host }} - username: ${{ inputs.user }} + host: ${{ steps.check.outputs.host }} + username: ${{ steps.check.outputs.user }} key: ${{ inputs.ssh-key }} envs: SITE_DIRECTORY,RESULT_FILE script: | diff --git a/.github/actions/push-translations/push-translations.js b/.github/actions/push-translations/push-translations.js index fdf3ef47..bc67240b 100644 --- a/.github/actions/push-translations/push-translations.js +++ b/.github/actions/push-translations/push-translations.js @@ -7,7 +7,7 @@ * The later steps of action.yml copy the POT with rsync and run `wp glotpress import-originals` * over ssh. The rsync action builds a shell command from its inputs, and the import is a remote * shell command, so every value that reaches either one has to match an allow-list first. Those - * steps use the outputs set here, never the raw inputs. + * steps use the outputs set here, never the raw inputs, except ssh-key, which is only ever a key. * * readInputs is pure, so a test can call it with an environment. */ @@ -122,10 +122,13 @@ function readInputs(env, buildId = crypto.randomBytes(16).toString('hex')) { const remotePath = `${read['pot-location']}/${read['remote-folder']}/` const resultFile = `${potFile.slice(0, -'.pot'.length)}-${buildId}` - // host, user and ssh-key are checked but not returned: the later steps take them from the inputs. + // The later steps connect with host and user as checked here, trimmed. ssh-key is not returned: + // it is handed to those steps as a key, never placed in a command. return { project: read.project, 'pot-path': read['pot-path'], + host: read.host, + user: read.user, 'remote-path': remotePath, 'remote-pot': `${remotePath}${potFile}`, 'site-directory': read['site-directory'], diff --git a/tests/actions/push-translations/push-translations.test.js b/tests/actions/push-translations/push-translations.test.js index 4013c734..4bc5a079 100644 --- a/tests/actions/push-translations/push-translations.test.js +++ b/tests/actions/push-translations/push-translations.test.js @@ -38,6 +38,8 @@ describe('push-translations', () => { assert.deepStrictEqual(readInputs(TEC, BUILD_ID), { project: 'the-events-calendar', 'pot-path': 'lang/the-events-calendar.pot', + host: 'ssh.example.com', + user: 'deploy', 'remote-path': '/home/deploy/pots/tec/', 'remote-pot': '/home/deploy/pots/tec/the-events-calendar.pot', 'site-directory': 'translations.stellarwp.com/html', @@ -50,6 +52,8 @@ describe('push-translations', () => { assert.deepStrictEqual(readInputs(GIVE, BUILD_ID), { project: 'givewp/give', 'pot-path': 'languages/give.pot', + host: 'ssh.example.com', + user: 'deploy', 'remote-path': '/home/deploy/pots/givewp/', 'remote-pot': '/home/deploy/pots/givewp/give.pot', 'site-directory': 'translations.stellarwp.com/html', @@ -68,6 +72,19 @@ describe('push-translations', () => { assert.strictEqual(read['site-directory'], '/var/www/site') }) + it('returns host and user without the newlines around them, which the later steps connect with', () => { + const read = readInputs({ ...TEC, INPUT_HOST: '\nssh.example.com\n', INPUT_USER: ' deploy\n' }, BUILD_ID) + + assert.strictEqual(read.host, 'ssh.example.com') + assert.strictEqual(read.user, 'deploy') + }) + + it('refuses a newline inside host or user', () => { + for (const input of ['INPUT_HOST', 'INPUT_USER']) { + assert.throws(() => readInputs({ ...TEC, [input]: 'example\nid' }, BUILD_ID), /Nothing was sent/) + } + }) + it('gives each run a build id of its own, 32 hex characters', () => { const [first, second] = [readInputs(TEC), readInputs(TEC)] @@ -151,8 +168,10 @@ describe('push-translations', () => { assert.match(set['result-file'], /^give-[a-f0-9]{32}$/) assert.strictEqual(set['result-url'], `https://translations.stellarwp.com/bot-output/${set['result-file']}`) assert.ok(summary().includes(set['result-file'])) - // The key and the host are never outputs: the later steps take them from the inputs. - assert.ok(!Object.values(set).some((value) => value.includes('PRIVATE KEY') || value.includes('ssh.example.com'))) + assert.strictEqual(set.host, 'ssh.example.com') + assert.strictEqual(set.user, 'deploy') + // The key is never an output: the later steps take it from the input. + assert.ok(!Object.values(set).some((value) => value.includes('PRIVATE KEY'))) }) }) From 5dbff60e259e615a5b74586fa839d2c9c4542296 Mon Sep 17 00:00:00 2001 From: Victor Larodiel Date: Mon, 16 Nov 2026 14:20:29 -0300 Subject: [PATCH 06/11] push-translations README: the calling job runs one push per project at a time with a concurrency group --- .github/actions/push-translations/README.md | 20 ++++++++++++++++++++ .github/actions/push-translations/action.yml | 3 +++ 2 files changed, 23 insertions(+) diff --git a/.github/actions/push-translations/README.md b/.github/actions/push-translations/README.md index e298138a..8e90ec62 100644 --- a/.github/actions/push-translations/README.md +++ b/.github/actions/push-translations/README.md @@ -39,6 +39,26 @@ GiveWP, a project under a parent, with a folder of its own on the server: A composite action cannot read secrets, so the caller passes them. Generate the POT before this step: the action only sends the file. +## One run at a time per project + +Every run copies the POT to the same path on the server, `//.pot`, +and imports that path into the same project. Two runs at once can import each other's file, or +import an older POT after a newer one. A composite action cannot set `concurrency`, so the job that +uses it does, with a group named after the project: + +```yaml +jobs: + translations: + runs-on: ubuntu-latest + concurrency: + group: push-translations-givewp-give + cancel-in-progress: false +``` + +GitHub runs the jobs of a group one at a time and keeps only the newest waiting one, so the last +import is from the newest run. A group holds jobs of one repository only, so push a project +from one repository. + ## What it does 1. Checks every input and names the build: a random id of 32 hex characters. diff --git a/.github/actions/push-translations/action.yml b/.github/actions/push-translations/action.yml index be21c8dc..699705a8 100644 --- a/.github/actions/push-translations/action.yml +++ b/.github/actions/push-translations/action.yml @@ -6,6 +6,9 @@ # input against an allow-list, in push-translations.js run by actions/github-script, and fails # before anything is sent. The later steps use the values it checked. No input is interpolated into # a script: the remote `script:` reads its values from the environment. +# +# Every run copies the POT to the same path on the server, so the calling job sets a `concurrency` +# group for the project; the README shows one. name: Push translations description: Pushes a POT file to the GlotPress site and imports its strings into a project From b3f15426aa4409009aa44bececf49366ff0e9446 Mon Sep 17 00:00:00 2001 From: Victor Larodiel Date: Mon, 16 Nov 2026 21:29:30 -0300 Subject: [PATCH 07/11] push-translations README: a concurrency group stops imports overlapping, not an older POT importing last --- .github/actions/push-translations/README.md | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/.github/actions/push-translations/README.md b/.github/actions/push-translations/README.md index 8e90ec62..31c5e2ec 100644 --- a/.github/actions/push-translations/README.md +++ b/.github/actions/push-translations/README.md @@ -55,9 +55,11 @@ jobs: cancel-in-progress: false ``` -GitHub runs the jobs of a group one at a time and keeps only the newest waiting one, so the last -import is from the newest run. A group holds jobs of one repository only, so push a project -from one repository. +GitHub runs the jobs of a group one at a time, so two imports never overlap. It does not order them +by commit: a job that reaches the group later runs later, even when it holds an older POT, and of +the jobs waiting only the one that arrived last is kept. Run the push from the commit you release, +after the steps that generate the POT, so the last job to reach the group holds the newest POT. A +group holds jobs of one repository only, so push a project from one repository. ## What it does From d1ddbef18375ec27525da481381cae2b47ec59cd Mon Sep 17 00:00:00 2001 From: Victor Larodiel Date: Tue, 24 Nov 2026 19:54:50 -0300 Subject: [PATCH 08/11] pot-path defaults from the plugin header, and the result file stays out of the job summary --- .github/actions/push-translations/README.md | 24 +++-- .github/actions/push-translations/action.yml | 8 +- .../push-translations/push-translations.js | 40 +++++-- .github/lib/plugin-header.js | 100 ++++++++++++++++++ .../push-translations.test.js | 52 ++++++++- tests/lib/plugin-header.test.js | 76 +++++++++++++ 6 files changed, 280 insertions(+), 20 deletions(-) create mode 100644 .github/lib/plugin-header.js create mode 100644 tests/lib/plugin-header.test.js diff --git a/.github/actions/push-translations/README.md b/.github/actions/push-translations/README.md index 31c5e2ec..92492cc7 100644 --- a/.github/actions/push-translations/README.md +++ b/.github/actions/push-translations/README.md @@ -10,7 +10,6 @@ The Events Calendar, a project at the top level of the site: id: translations with: project: the-events-calendar - pot-path: lang/the-events-calendar.pot remote-folder: tec host: ${{ secrets.TRANSLATIONS_DEPLOY_HOST }} user: ${{ secrets.TRANSLATIONS_DEPLOY_USER }} @@ -28,7 +27,6 @@ GiveWP, a project under a parent, with a folder of its own on the server: - uses: stellarwp/plugin-toolbox/.github/actions/push-translations@v1 with: project: givewp/give - pot-path: languages/give.pot remote-folder: givewp host: ${{ secrets.TRANSLATIONS_DEPLOY_HOST }} user: ${{ secrets.TRANSLATIONS_DEPLOY_USER }} @@ -39,6 +37,11 @@ GiveWP, a project under a parent, with a folder of its own on the server: A composite action cannot read secrets, so the caller passes them. Generate the POT before this step: the action only sends the file. +With `pot-path` empty, the POT is `/.pot`, read from the header of the +plugin file: the first PHP file at the top of the workspace with a `Plugin Name` header in its first +8 KiB. Leading and trailing slashes on `Domain Path` are dropped, so `/languages/` gives +`languages/give.pot`. Pass `pot-path` for a POT that is somewhere else or named differently. + ## One run at a time per project Every run copies the POT to the same path on the server, `//.pot`, @@ -71,11 +74,12 @@ group holds jobs of one repository only, so push a project from one repository. what it prints to `bot-output/-`. 4. Reads that file back from `result-url` over https, sets it as `glotpress-result` and adds it to the job summary. -5. Deletes the file from the server. This runs even when the import or the read failed, so the - output does not stay public. +5. Deletes the file from the server. This runs even when the import or the read failed. -The build id is random, not derived from the commit, so the address of the output cannot be -guessed while the file exists. +The file is public while it exists, between steps 3 and 5. Its address appears in the run log, in +the environment of the import, read and delete steps, so anyone who can read the log can fetch it +in that time. It holds only what the import printed: counts of the strings added, updated, fuzzied +and obsoleted. ## What the server needs @@ -91,13 +95,15 @@ guessed while the file exists. Before anything is sent, with the input named in the message: -- An empty `project`, `pot-path`, `remote-folder`, `host`, `user`, `ssh-key` or `pot-location`. A +- An empty `project`, `remote-folder`, `host`, `user`, `ssh-key` or `pot-location`. A secret the repository does not have arrives empty, so this is how a missing secret shows. - Any path whose parts are not letters, digits, `.`, `_` and `-`, or that has a part starting with `.` or `-`. That rules out spaces, quotes, `;`, `|`, `$`, backticks, `..`, `~` and a value that would be read as a command option. - `project` or `remote-folder` that starts with `/`. -- `pot-path` that is absolute or does not end in `.pot`. +- `pot-path` that is absolute or does not end in `.pot`, given or read from the plugin header. +- An empty `pot-path` when no plugin file is found, or its header has no `Text Domain` or no + `Domain Path`. - `pot-location` that is not absolute. - `host` that is not a host name or an IPv4 address, and `user` that is not a plain user name. - `result-url` that is not `https://`, or that has a query, a port or credentials. @@ -110,7 +116,7 @@ secret. | Input | Required | Default | What it does | |---|---|---|---| | `project` | yes | | The GlotPress project path: `the-events-calendar`, `givewp/give` | -| `pot-path` | yes | | The POT file, relative to the workspace | +| `pot-path` | no | `/.pot` from the plugin header | The POT file, relative to the workspace | | `remote-folder` | yes | | The folder under `pot-location` the POT is copied to: `tec`, `givewp` | | `host` | yes | | The server to connect to | | `user` | yes | | The user to connect as | diff --git a/.github/actions/push-translations/action.yml b/.github/actions/push-translations/action.yml index 699705a8..c587b6bd 100644 --- a/.github/actions/push-translations/action.yml +++ b/.github/actions/push-translations/action.yml @@ -21,8 +21,10 @@ inputs: pot-path: description: >- The POT file, relative to the workspace, e.g. `languages/give.pot`. Its name is the name it - has on the server. - required: true + has on the server. Empty means `/.pot` from the header of the plugin + file at the top of the workspace. + required: false + default: '' remote-folder: description: >- The folder under `pot-location` the POT is copied to, one per product line: `tec`, `givewp`. @@ -132,7 +134,7 @@ runs: const { runReadResult } = require(`${process.env.ACTION_PATH}/push-translations.js`) await runReadResult({ core, env: process.env }) - # Also when the import or the read failed, so the output file does not stay public. + # Also when the import or the read failed, so the output file does not stay on the server. - name: Delete the import output from the server if: ${{ always() && steps.check.outcome == 'success' }} uses: appleboy/ssh-action@25ce8cbbcb08177468c7ff7ec5cbfa236f9341e1 # v1.1.0 diff --git a/.github/actions/push-translations/push-translations.js b/.github/actions/push-translations/push-translations.js index bc67240b..e3d57b51 100644 --- a/.github/actions/push-translations/push-translations.js +++ b/.github/actions/push-translations/push-translations.js @@ -14,6 +14,8 @@ const crypto = require('node:crypto') +const { readPluginHeader } = require('../../lib/plugin-header.js') + /** What was not done, to close every error message. */ const WHAT = 'Nothing was sent to the translations server.' @@ -100,19 +102,46 @@ function readInput(env, name) { return match[1] } +/** + * Where the POT is when `pot-path` is empty: `/.pot`, from the header of + * the plugin file at the top of the workspace. The result is checked like a given `pot-path`. + * + * @param {object} env The environment, holding `GITHUB_WORKSPACE`. + * + * @throws {Error} When no plugin file is found, or its header has no Text Domain or Domain Path. + * + * @returns {string} The POT's path, relative to the workspace. + */ +function defaultPotPath(env) { + const header = readPluginHeader(env.GITHUB_WORKSPACE || '.') + const pass = `Pass pot-path. ${WHAT}` + + if (header === null) { + throw new Error(`pot-path is empty and no PHP file at the top of the workspace has a Plugin Name header. ${pass}`) + } + for (const [value, name] of [[header.textDomain, 'Text Domain'], [header.domainPath, 'Domain Path']]) { + if (!value) { + throw new Error(`pot-path is empty and ${header.file} has no ${name} header. ${pass}`) + } + } + + return `${header.domainPath}/${header.textDomain}.pot` +} + /** * Checks every input and builds the values the later steps use. * * @param {object} env The environment, holding the inputs as `INPUT_*`. - * @param {string} buildId What makes the output file's name unique and hard to guess. Random by - * default; a test passes its own. + * @param {string} buildId What makes the output file's name unique. Random by default; a test + * passes its own. * * @throws {Error} When an input, or the build id, is refused. * * @returns {Object} The step's outputs, by name. */ function readInputs(env, buildId = crypto.randomBytes(16).toString('hex')) { - const read = Object.fromEntries(Object.keys(RULES).map((name) => [name, readInput(env, name)])) + const withPot = { ...env, INPUT_POT_PATH: String(env.INPUT_POT_PATH ?? '').trim() || defaultPotPath(env) } + const read = Object.fromEntries(Object.keys(RULES).map((name) => [name, readInput(withPot, name)])) if (!BUILD_ID.test(buildId)) { throw new Error(`The build id is not 32 hex characters. ${WHAT}`) @@ -141,7 +170,7 @@ function readInputs(env, buildId = crypto.randomBytes(16).toString('hex')) { * Runs the check and sets the outputs. * * @param {object} options - * @param {object} options.core The @actions/core toolkit, for the outputs and the summary. + * @param {object} options.core The @actions/core toolkit, for the outputs. * @param {object} options.env The environment, holding the inputs as `INPUT_*`. * * @throws {Error} When an input is refused. @@ -151,11 +180,10 @@ function readInputs(env, buildId = crypto.randomBytes(16).toString('hex')) { async function runPushTranslations({ core, env }) { const outputs = readInputs(env) + // Not written to the job summary: the result file's name is the address of the import output. for (const [name, value] of Object.entries(outputs)) { core.setOutput(name, value) } - - await core.summary.addRaw(`## Translations build: \`${outputs['result-file']}\``, true).write() } /** diff --git a/.github/lib/plugin-header.js b/.github/lib/plugin-header.js new file mode 100644 index 00000000..fba74b44 --- /dev/null +++ b/.github/lib/plugin-header.js @@ -0,0 +1,100 @@ +'use strict' + +/** + * The header of a plugin's main file, shared by the toolbox actions. Loaded by a path relative to + * each action's directory, like workspace.js. + * + * The main file is found the way `wp i18n make-pot` finds it: the PHP files at the top of the + * directory, read up to their first 8 KiB, and the first with a `Plugin Name` header wins. The + * files are read in name order. + */ + +const fs = require('node:fs') +const path = require('node:path') + +/** How much of a file make-pot reads for its header, and so how much this reads. */ +const HEADER_BYTES = 8192 + +/** + * Reads one header field the way WordPress does: the line may start with spaces, tabs, `/`, `*`, + * `#` or `@`, the name matches in any case, and the value ends before a `*\/` or `?>`. + * + * @param {string} text The start of the file. + * @param {string} name The header name, e.g. `Text Domain`. + * + * @returns {string|null} The value, or null when the header is missing or empty. + */ +function readField(text, name) { + const escaped = name.replace(/[.*+?^${}()|[\]\\]/g, '\\$&') + const match = text.match(new RegExp(`^[ \\t/*#@]*${escaped}:(.*)$`, 'mi')) + const value = match ? match[1].replace(/\s*(?:\*\/|\?>).*/, '').trim() : '' + + return value === '' ? null : value +} + +/** + * Reads the start of a file, with CR-only line endings made LF. + * + * @param {string} file The file. + * + * @returns {string} Its first 8 KiB. + */ +function readStart(file) { + const fd = fs.openSync(file, 'r') + try { + const buffer = Buffer.alloc(HEADER_BYTES) + const bytes = fs.readSync(fd, buffer, 0, HEADER_BYTES, 0) + + return buffer.toString('utf8', 0, bytes).replace(/\r/g, '\n') + } finally { + fs.closeSync(fd) + } +} + +/** + * Finds the plugin's main file in a directory and reads its `Text Domain` and `Domain Path`. + * + * @param {string} dir The plugin directory. + * + * @throws {Error} When the directory or a file exists but cannot be read. + * + * @returns {{file: string, textDomain: string|null, domainPath: string|null}|null} The main file's + * name, its text domain, and its domain path without leading and trailing slashes, a + * missing header as null; or null when no top-level PHP file has a `Plugin Name` header, + * or the directory does not exist. + */ +function readPluginHeader(dir) { + let entries + try { + entries = fs.readdirSync(dir, { withFileTypes: true }) + } catch (error) { + if (error.code === 'ENOENT' || error.code === 'ENOTDIR') { + return null + } + + throw error + } + + const files = entries + .filter((entry) => entry.isFile() && entry.name.endsWith('.php')) + .map((entry) => entry.name) + .sort() + + for (const file of files) { + const text = readStart(path.join(dir, file)) + + if (readField(text, 'Plugin Name') !== null) { + const domainPath = readField(text, 'Domain Path') + + return { + file, + textDomain: readField(text, 'Text Domain'), + domainPath: domainPath === null ? null : domainPath.replace(/^\/+|\/+$/g, ''), + } + } + } + + return null +} + +module.exports = { readPluginHeader } diff --git a/tests/actions/push-translations/push-translations.test.js b/tests/actions/push-translations/push-translations.test.js index 4bc5a079..a45ae69e 100644 --- a/tests/actions/push-translations/push-translations.test.js +++ b/tests/actions/push-translations/push-translations.test.js @@ -2,6 +2,9 @@ const { describe, it } = require('node:test') const assert = require('node:assert') +const fs = require('node:fs') +const os = require('node:os') +const path = require('node:path') const { runPushTranslations, @@ -157,7 +160,7 @@ describe('push-translations', () => { } }) - it('sets the validated values as outputs and names the build in the summary', async (t) => { + it('sets the validated values as outputs, and leaves the result file out of the summary', async (t) => { const { core, outputs, summary } = await coreWithOutputs(t) await runPushTranslations({ core, env: GIVE }) @@ -167,7 +170,7 @@ describe('push-translations', () => { assert.strictEqual(set['remote-pot'], '/home/deploy/pots/givewp/give.pot') assert.match(set['result-file'], /^give-[a-f0-9]{32}$/) assert.strictEqual(set['result-url'], `https://translations.stellarwp.com/bot-output/${set['result-file']}`) - assert.ok(summary().includes(set['result-file'])) + assert.ok(!summary().includes(set['result-file'].slice(-32))) assert.strictEqual(set.host, 'ssh.example.com') assert.strictEqual(set.user, 'deploy') // The key is never an output: the later steps take it from the input. @@ -212,3 +215,48 @@ describe('runReadResult', () => { assert.deepStrictEqual(urls, []) }) }) + +describe('the pot-path default', () => { + /** + * A workspace holding one plugin file with the given header lines. + * + * @param {string} header The header lines, one per line. + * + * @returns {string} The workspace directory. + */ + function workspace(header) { + const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'push-translations-')) + fs.writeFileSync(path.join(dir, 'give.php'), `/.pot from the plugin header when pot-path is empty', () => { + const env = { ...GIVE, INPUT_POT_PATH: '', GITHUB_WORKSPACE: workspace(' * Text Domain: give\n * Domain Path: /languages/') } + + assert.strictEqual(readInputs(env, BUILD_ID)['pot-path'], 'languages/give.pot') + }) + + it('is not used when pot-path is given', () => { + const env = { ...GIVE, GITHUB_WORKSPACE: workspace(' * Text Domain: other\n * Domain Path: /lang') } + + assert.strictEqual(readInputs(env, BUILD_ID)['pot-path'], 'languages/give.pot') + }) + + const refused = { + 'no plugin file': null, + 'no Text Domain': ' * Domain Path: /languages', + 'no Domain Path': ' * Text Domain: give', + 'a Text Domain that is not a file name': ' * Text Domain: give $(id)\n * Domain Path: /languages', + } + + for (const [name, header] of Object.entries(refused)) { + it(`refuses an empty pot-path when the header has ${name}`, () => { + const dir = header === null ? fs.mkdtempSync(path.join(os.tmpdir(), 'push-translations-')) : workspace(header) + + assert.throws( + () => readInputs({ ...GIVE, INPUT_POT_PATH: '', GITHUB_WORKSPACE: dir }, BUILD_ID), + /^Error: pot-path .*Nothing was sent to the translations server\.$/s + ) + }) + } +}) diff --git a/tests/lib/plugin-header.test.js b/tests/lib/plugin-header.test.js new file mode 100644 index 00000000..08e1197d --- /dev/null +++ b/tests/lib/plugin-header.test.js @@ -0,0 +1,76 @@ +'use strict' + +const { describe, it, beforeEach, afterEach } = require('node:test') +const assert = require('node:assert/strict') +const fs = require('node:fs') +const os = require('node:os') +const path = require('node:path') + +const { readPluginHeader } = require('../../.github/lib/plugin-header.js') + +describe('readPluginHeader', () => { + let dir + + const write = (name, body) => { + fs.mkdirSync(path.dirname(path.join(dir, name)), { recursive: true }) + fs.writeFileSync(path.join(dir, name), body) + } + + beforeEach(() => { + dir = fs.mkdtempSync(path.join(os.tmpdir(), 'plugin-header-')) + }) + + afterEach(() => { + fs.rmSync(dir, { recursive: true, force: true }) + }) + + it('reads the Text Domain and Domain Path of the top-level PHP file with a Plugin Name', () => { + write('helpers.php', ' { + write('learndash.php', ' { + write('plugin.php', ' { + write('plugin.php', ' { + write('b.php', ' { + write('plugin.php', ` { + write('plugin.php', ' { + assert.equal(readPluginHeader(path.join(dir, 'nowhere')), null) + }) +}) From e41755fecffee0f044251273251ecdeba4732f68 Mon Sep 17 00:00:00 2001 From: Victor Larodiel Date: Tue, 24 Nov 2026 20:16:58 -0300 Subject: [PATCH 09/11] The build id is masked in the run log, and an empty pot-path needs a Domain Path below the root --- .github/actions/push-translations/README.md | 8 ++--- .../push-translations/push-translations.js | 23 ++++++++---- .github/lib/plugin-header.js | 5 ++- .../push-translations.test.js | 36 +++++++++++++++---- 4 files changed, 52 insertions(+), 20 deletions(-) diff --git a/.github/actions/push-translations/README.md b/.github/actions/push-translations/README.md index 92492cc7..90a9dbe4 100644 --- a/.github/actions/push-translations/README.md +++ b/.github/actions/push-translations/README.md @@ -76,10 +76,10 @@ group holds jobs of one repository only, so push a project from one repository. the job summary. 5. Deletes the file from the server. This runs even when the import or the read failed. -The file is public while it exists, between steps 3 and 5. Its address appears in the run log, in -the environment of the import, read and delete steps, so anyone who can read the log can fetch it -in that time. It holds only what the import printed: counts of the strings added, updated, fuzzied -and obsoleted. +The file is public while it exists, between steps 3 and 5. Its name ends in a random build id, +which the action masks before any step uses it, so the run log shows `***` in its place and the +job summary does not name the file. It holds only what the import printed: counts of the strings +added, updated, fuzzied and obsoleted. ## What the server needs diff --git a/.github/actions/push-translations/push-translations.js b/.github/actions/push-translations/push-translations.js index e3d57b51..7df1ee52 100644 --- a/.github/actions/push-translations/push-translations.js +++ b/.github/actions/push-translations/push-translations.js @@ -113,16 +113,21 @@ function readInput(env, name) { * @returns {string} The POT's path, relative to the workspace. */ function defaultPotPath(env) { - const header = readPluginHeader(env.GITHUB_WORKSPACE || '.') const pass = `Pass pot-path. ${WHAT}` + if (!env.GITHUB_WORKSPACE) { + throw new Error(`pot-path is empty and GITHUB_WORKSPACE is not set, so no plugin header can be read. ${pass}`) + } + const header = readPluginHeader(env.GITHUB_WORKSPACE) if (header === null) { throw new Error(`pot-path is empty and no PHP file at the top of the workspace has a Plugin Name header. ${pass}`) } - for (const [value, name] of [[header.textDomain, 'Text Domain'], [header.domainPath, 'Domain Path']]) { - if (!value) { - throw new Error(`pot-path is empty and ${header.file} has no ${name} header. ${pass}`) - } + if (!header.textDomain) { + throw new Error(`pot-path is empty and ${header.file} has no Text Domain header. ${pass}`) + } + // `Domain Path: /` names the plugin's root, which comes back empty once its slashes are dropped. + if (!header.domainPath) { + throw new Error(`pot-path is empty and ${header.file} has no Domain Path header, or it names the plugin's root. ${pass}`) } return `${header.domainPath}/${header.textDomain}.pot` @@ -170,7 +175,7 @@ function readInputs(env, buildId = crypto.randomBytes(16).toString('hex')) { * Runs the check and sets the outputs. * * @param {object} options - * @param {object} options.core The @actions/core toolkit, for the outputs. + * @param {object} options.core The @actions/core toolkit, for the mask and the outputs. * @param {object} options.env The environment, holding the inputs as `INPUT_*`. * * @throws {Error} When an input is refused. @@ -180,7 +185,11 @@ function readInputs(env, buildId = crypto.randomBytes(16).toString('hex')) { async function runPushTranslations({ core, env }) { const outputs = readInputs(env) - // Not written to the job summary: the result file's name is the address of the import output. + // The build id makes the import output's public address. Masked before any output carries it, + // so the runner prints *** in its place in every later log line, the env of the ssh steps + // included. It is not written to the job summary either. + core.setSecret(outputs['result-file'].slice(-32)) + for (const [name, value] of Object.entries(outputs)) { core.setOutput(name, value) } diff --git a/.github/lib/plugin-header.js b/.github/lib/plugin-header.js index fba74b44..adbcbfae 100644 --- a/.github/lib/plugin-header.js +++ b/.github/lib/plugin-header.js @@ -4,9 +4,8 @@ * The header of a plugin's main file, shared by the toolbox actions. Loaded by a path relative to * each action's directory, like workspace.js. * - * The main file is found the way `wp i18n make-pot` finds it: the PHP files at the top of the - * directory, read up to their first 8 KiB, and the first with a `Plugin Name` header wins. The - * files are read in name order. + * The main file is the first PHP file at the top of the directory, in name order, with a + * `Plugin Name` header in its first 8 KiB. */ const fs = require('node:fs') diff --git a/tests/actions/push-translations/push-translations.test.js b/tests/actions/push-translations/push-translations.test.js index a45ae69e..22acd193 100644 --- a/tests/actions/push-translations/push-translations.test.js +++ b/tests/actions/push-translations/push-translations.test.js @@ -1,6 +1,6 @@ 'use strict' -const { describe, it } = require('node:test') +const { afterEach, describe, it } = require('node:test') const assert = require('node:assert') const fs = require('node:fs') const os = require('node:os') @@ -160,17 +160,28 @@ describe('push-translations', () => { } }) - it('sets the validated values as outputs, and leaves the result file out of the summary', async (t) => { + it('masks the build id, sets the validated values as outputs, and leaves the summary empty', async (t) => { const { core, outputs, summary } = await coreWithOutputs(t) - await runPushTranslations({ core, env: GIVE }) + // core is a module namespace, so its calls are recorded through a wrapper. + const calls = [] + const recording = { + ...core, + setSecret: (value) => calls.push(['secret', value]) && core.setSecret(value), + setOutput: (name, value) => calls.push(['output', name]) && core.setOutput(name, value), + } + + await runPushTranslations({ core: recording, env: GIVE }) const set = outputs() + // The build id is masked before any output carries it, so the log shows *** in its place. + assert.deepStrictEqual(calls[0], ['secret', set['result-file'].slice(-32)]) + assert.strictEqual(calls.filter(([kind]) => kind === 'secret').length, 1) assert.strictEqual(set.project, 'givewp/give') assert.strictEqual(set['remote-pot'], '/home/deploy/pots/givewp/give.pot') assert.match(set['result-file'], /^give-[a-f0-9]{32}$/) assert.strictEqual(set['result-url'], `https://translations.stellarwp.com/bot-output/${set['result-file']}`) - assert.ok(!summary().includes(set['result-file'].slice(-32))) + assert.strictEqual(summary(), '') assert.strictEqual(set.host, 'ssh.example.com') assert.strictEqual(set.user, 'deploy') // The key is never an output: the later steps take it from the input. @@ -217,6 +228,18 @@ describe('runReadResult', () => { }) describe('the pot-path default', () => { + const dirs = [] + + afterEach(() => { + dirs.splice(0).forEach((dir) => fs.rmSync(dir, { recursive: true, force: true })) + }) + + /** An empty temporary directory, removed after the test. */ + const tempDir = () => { + dirs.push(fs.mkdtempSync(path.join(os.tmpdir(), 'push-translations-'))) + return dirs.at(-1) + } + /** * A workspace holding one plugin file with the given header lines. * @@ -225,7 +248,7 @@ describe('the pot-path default', () => { * @returns {string} The workspace directory. */ function workspace(header) { - const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'push-translations-')) + const dir = tempDir() fs.writeFileSync(path.join(dir, 'give.php'), ` { 'no plugin file': null, 'no Text Domain': ' * Domain Path: /languages', 'no Domain Path': ' * Text Domain: give', + 'the root as its Domain Path': ' * Text Domain: give\n * Domain Path: /', 'a Text Domain that is not a file name': ' * Text Domain: give $(id)\n * Domain Path: /languages', } for (const [name, header] of Object.entries(refused)) { it(`refuses an empty pot-path when the header has ${name}`, () => { - const dir = header === null ? fs.mkdtempSync(path.join(os.tmpdir(), 'push-translations-')) : workspace(header) + const dir = header === null ? tempDir() : workspace(header) assert.throws( () => readInputs({ ...GIVE, INPUT_POT_PATH: '', GITHUB_WORKSPACE: dir }, BUILD_ID), From ccefe763de62411e48a17c1b0d090e2619ea7b68 Mon Sep 17 00:00:00 2001 From: Victor Larodiel Date: Tue, 24 Nov 2026 20:50:09 -0300 Subject: [PATCH 10/11] The examples and messages name no brand --- .github/actions/push-translations/README.md | 22 +++++++++---------- .github/actions/push-translations/action.yml | 6 ++--- .../push-translations/push-translations.js | 6 ++--- tests/lib/plugin-header.test.js | 6 ++--- 4 files changed, 20 insertions(+), 20 deletions(-) diff --git a/.github/actions/push-translations/README.md b/.github/actions/push-translations/README.md index 90a9dbe4..c63c2450 100644 --- a/.github/actions/push-translations/README.md +++ b/.github/actions/push-translations/README.md @@ -3,14 +3,14 @@ Pushes a plugin's POT file to the shared GlotPress site, translations.stellarwp.com, and imports its strings into a project. What the import printed comes back as an output. -The Events Calendar, a project at the top level of the site: +A project at the top level of the site: ```yaml - uses: stellarwp/plugin-toolbox/.github/actions/push-translations@v1 id: translations with: - project: the-events-calendar - remote-folder: tec + project: my-plugin + remote-folder: my-brand host: ${{ secrets.TRANSLATIONS_DEPLOY_HOST }} user: ${{ secrets.TRANSLATIONS_DEPLOY_USER }} ssh-key: ${{ secrets.TRANSLATIONS_DEPLOY_SSH_KEY }} @@ -21,13 +21,13 @@ The Events Calendar, a project at the top level of the site: RESULT: ${{ steps.translations.outputs.glotpress-result }} ``` -GiveWP, a project under a parent, with a folder of its own on the server: +A project under a parent project: ```yaml - uses: stellarwp/plugin-toolbox/.github/actions/push-translations@v1 with: - project: givewp/give - remote-folder: givewp + project: my-brand/my-plugin + remote-folder: my-brand host: ${{ secrets.TRANSLATIONS_DEPLOY_HOST }} user: ${{ secrets.TRANSLATIONS_DEPLOY_USER }} ssh-key: ${{ secrets.TRANSLATIONS_DEPLOY_SSH_KEY }} @@ -40,7 +40,7 @@ step: the action only sends the file. With `pot-path` empty, the POT is `/.pot`, read from the header of the plugin file: the first PHP file at the top of the workspace with a `Plugin Name` header in its first 8 KiB. Leading and trailing slashes on `Domain Path` are dropped, so `/languages/` gives -`languages/give.pot`. Pass `pot-path` for a POT that is somewhere else or named differently. +`languages/my-plugin.pot` for a `my-plugin` Text Domain. Pass `pot-path` for a POT that is somewhere else or named differently. ## One run at a time per project @@ -54,7 +54,7 @@ jobs: translations: runs-on: ubuntu-latest concurrency: - group: push-translations-givewp-give + group: push-translations-my-plugin cancel-in-progress: false ``` @@ -115,9 +115,9 @@ secret. | Input | Required | Default | What it does | |---|---|---|---| -| `project` | yes | | The GlotPress project path: `the-events-calendar`, `givewp/give` | +| `project` | yes | | The GlotPress project path: `my-plugin`, or `my-brand/my-plugin` under a parent | | `pot-path` | no | `/.pot` from the plugin header | The POT file, relative to the workspace | -| `remote-folder` | yes | | The folder under `pot-location` the POT is copied to: `tec`, `givewp` | +| `remote-folder` | yes | | The folder under `pot-location` the POT is copied to, e.g. `my-brand` | | `host` | yes | | The server to connect to | | `user` | yes | | The user to connect as | | `ssh-key` | yes | | The private key of `user` | @@ -132,7 +132,7 @@ A trailing `/` on a folder, a directory or the URL is optional. | Output | Example | What it is | |---|---|---| | `glotpress-result` | `1 new strings added, 0 updated, 0 fuzzied, and 0 obsoleted.` | What `wp glotpress import-originals` printed. Text from the server: pass it to a script through `env`, never inside `run:` | -| `project` | `givewp/give` | The project the strings were imported into | +| `project` | `my-brand/my-plugin` | The project the strings were imported into | ## How it runs diff --git a/.github/actions/push-translations/action.yml b/.github/actions/push-translations/action.yml index c587b6bd..cfc1df24 100644 --- a/.github/actions/push-translations/action.yml +++ b/.github/actions/push-translations/action.yml @@ -16,18 +16,18 @@ inputs: project: description: >- The GlotPress project the strings are imported into, as its path on the site: - `the-events-calendar`, or `givewp/give` for a project under a parent. + `my-plugin`, or `my-brand/my-plugin` for a project under a parent. required: true pot-path: description: >- - The POT file, relative to the workspace, e.g. `languages/give.pot`. Its name is the name it + The POT file, relative to the workspace, e.g. `languages/my-plugin.pot`. Its name is the name it has on the server. Empty means `/.pot` from the header of the plugin file at the top of the workspace. required: false default: '' remote-folder: description: >- - The folder under `pot-location` the POT is copied to, one per product line: `tec`, `givewp`. + The folder under `pot-location` the POT is copied to, one per product line, e.g. `my-brand`. required: true host: description: 'The server to connect to. Pass a secret.' diff --git a/.github/actions/push-translations/push-translations.js b/.github/actions/push-translations/push-translations.js index 7df1ee52..b77d6385 100644 --- a/.github/actions/push-translations/push-translations.js +++ b/.github/actions/push-translations/push-translations.js @@ -35,15 +35,15 @@ const PARTS = `${PART}(?:/${PART})*` const RULES = { project: { pattern: new RegExp(`^(${PARTS})$`), - shape: 'a GlotPress project path, such as the-events-calendar or givewp/give', + shape: 'a GlotPress project path, such as my-plugin or my-brand/my-plugin', }, 'pot-path': { pattern: new RegExp(`^((?:${PART}/)*${PART}\\.pot)$`), - shape: 'the path of a .pot file relative to the workspace, such as languages/give.pot', + shape: 'the path of a .pot file relative to the workspace, such as languages/my-plugin.pot', }, 'remote-folder': { pattern: new RegExp(`^(${PARTS})/?$`), - shape: 'a folder name under pot-location, such as tec or givewp', + shape: 'a folder name under pot-location, such as my-brand', }, 'site-directory': { pattern: new RegExp(`^(/?${PARTS})/?$`), diff --git a/tests/lib/plugin-header.test.js b/tests/lib/plugin-header.test.js index 08e1197d..4bb55c77 100644 --- a/tests/lib/plugin-header.test.js +++ b/tests/lib/plugin-header.test.js @@ -26,13 +26,13 @@ describe('readPluginHeader', () => { it('reads the Text Domain and Domain Path of the top-level PHP file with a Plugin Name', () => { write('helpers.php', ' { - write('learndash.php', ' Date: Tue, 1 Dec 2026 15:24:21 -0300 Subject: [PATCH 11/11] Add working-directory to push-translations Review asked for a working-directory input like generate-pot and update-wp-version have, so a workflow can pass generate-pot's pot-path output straight on. That output is relative to generate-pot's working-directory, and push-translations read pot-path, and the plugin header for the empty-pot-path default, from the workspace root only. pot-path and the plugin header are now read in working-directory, and the copy step gets the POT's path from the workspace. The directory has to be inside the workspace, relative or absolute: the copy step is a container that reads $GITHUB_WORKSPACE/, and its path is checked against the same allow-list as the other paths. Empty is the workspace. The README shows the two actions together and says when an empty pot-path names the same file as generate-pot. --- .github/actions/push-translations/README.md | 38 +++++++++++- .github/actions/push-translations/action.yml | 11 +++- .../push-translations/push-translations.js | 59 +++++++++++++++---- .../push-translations.test.js | 55 +++++++++++++++++ 4 files changed, 149 insertions(+), 14 deletions(-) diff --git a/.github/actions/push-translations/README.md b/.github/actions/push-translations/README.md index c63c2450..14a54800 100644 --- a/.github/actions/push-translations/README.md +++ b/.github/actions/push-translations/README.md @@ -38,10 +38,42 @@ A composite action cannot read secrets, so the caller passes them. Generate the step: the action only sends the file. With `pot-path` empty, the POT is `/.pot`, read from the header of the -plugin file: the first PHP file at the top of the workspace with a `Plugin Name` header in its first +plugin file: the first PHP file at the top of the checkout with a `Plugin Name` header in its first 8 KiB. Leading and trailing slashes on `Domain Path` are dropped, so `/languages/` gives `languages/my-plugin.pot` for a `my-plugin` Text Domain. Pass `pot-path` for a POT that is somewhere else or named differently. +## After generate-pot + +[generate-pot](../generate-pot) reports the POT it wrote as its `pot-path` output, relative to its +`working-directory`. Pass both on, so this action sends that file: + +```yaml +- id: pot + uses: stellarwp/plugin-toolbox/.github/actions/generate-pot@v1 + with: + working-directory: plugin + +- if: steps.pot.outputs.changed == 'yes' + uses: stellarwp/plugin-toolbox/.github/actions/push-translations@v1 + with: + working-directory: plugin + pot-path: ${{ steps.pot.outputs.pot-path }} + project: my-plugin + remote-folder: my-brand + host: ${{ secrets.TRANSLATIONS_DEPLOY_HOST }} + user: ${{ secrets.TRANSLATIONS_DEPLOY_USER }} + ssh-key: ${{ secrets.TRANSLATIONS_DEPLOY_SSH_KEY }} + pot-location: ${{ secrets.TRANSLATIONS_DEPLOY_POT_LOCATION }} +``` + +An empty `pot-path` here names the same file only when generate-pot ran with its `domain` and +`source-path` inputs unset: this action reads the `Text Domain` of the plugin file at the top of the +checkout, and takes no such inputs. + +`working-directory` is a directory inside the workspace, relative to it or absolute. `pot-path` and +the plugin file are read there. The copy step runs in a container that sees only the workspace, so a +checkout outside it is refused. + ## One run at a time per project Every run copies the POT to the same path on the server, `//.pot`, @@ -102,6 +134,7 @@ Before anything is sent, with the input named in the message: would be read as a command option. - `project` or `remote-folder` that starts with `/`. - `pot-path` that is absolute or does not end in `.pot`, given or read from the plugin header. +- `working-directory` that is not a directory, or is outside the workspace. - An empty `pot-path` when no plugin file is found, or its header has no `Text Domain` or no `Domain Path`. - `pot-location` that is not absolute. @@ -116,7 +149,8 @@ secret. | Input | Required | Default | What it does | |---|---|---|---| | `project` | yes | | The GlotPress project path: `my-plugin`, or `my-brand/my-plugin` under a parent | -| `pot-path` | no | `/.pot` from the plugin header | The POT file, relative to the workspace | +| `pot-path` | no | `/.pot` from the plugin header | The POT file, relative to the checkout | +| `working-directory` | no | the workspace | Where the repository is checked out: a directory inside the workspace, relative to it or absolute, e.g. `plugin` | | `remote-folder` | yes | | The folder under `pot-location` the POT is copied to, e.g. `my-brand` | | `host` | yes | | The server to connect to | | `user` | yes | | The user to connect as | diff --git a/.github/actions/push-translations/action.yml b/.github/actions/push-translations/action.yml index cfc1df24..16fb352b 100644 --- a/.github/actions/push-translations/action.yml +++ b/.github/actions/push-translations/action.yml @@ -20,9 +20,15 @@ inputs: required: true pot-path: description: >- - The POT file, relative to the workspace, e.g. `languages/my-plugin.pot`. Its name is the name it + The POT file, relative to the checkout, e.g. `languages/my-plugin.pot`. Its name is the name it has on the server. Empty means `/.pot` from the header of the plugin - file at the top of the workspace. + file at the top of the checkout. + required: false + default: '' + working-directory: + description: >- + Where the repository is checked out: a directory inside the workspace, relative to it or + absolute, e.g. `plugin`. Empty is the workspace. required: false default: '' remote-folder: @@ -79,6 +85,7 @@ runs: # never part of the program. INPUT_PROJECT: ${{ inputs.project }} INPUT_POT_PATH: ${{ inputs.pot-path }} + INPUT_WORKING_DIRECTORY: ${{ inputs.working-directory }} INPUT_REMOTE_FOLDER: ${{ inputs.remote-folder }} INPUT_HOST: ${{ inputs.host }} INPUT_USER: ${{ inputs.user }} diff --git a/.github/actions/push-translations/push-translations.js b/.github/actions/push-translations/push-translations.js index b77d6385..a43dc585 100644 --- a/.github/actions/push-translations/push-translations.js +++ b/.github/actions/push-translations/push-translations.js @@ -13,8 +13,10 @@ */ const crypto = require('node:crypto') +const path = require('node:path') const { readPluginHeader } = require('../../lib/plugin-header.js') +const { readWorkingDirectory } = require('../../lib/workspace.js') /** What was not done, to close every error message. */ const WHAT = 'Nothing was sent to the translations server.' @@ -39,7 +41,7 @@ const RULES = { }, 'pot-path': { pattern: new RegExp(`^((?:${PART}/)*${PART}\\.pot)$`), - shape: 'the path of a .pot file relative to the workspace, such as languages/my-plugin.pot', + shape: 'the path of a .pot file relative to the checkout, such as languages/my-plugin.pot', }, 'remote-folder': { pattern: new RegExp(`^(${PARTS})/?$`), @@ -102,25 +104,60 @@ function readInput(env, name) { return match[1] } +/** + * Reads `working-directory`: where the repository is checked out. Empty is the workspace. + * + * The copy step runs in a container that sees only the workspace, and builds a shell command from + * the POT's path. So the checkout has to be inside the workspace, and its path from there is + * checked like the other paths. + * + * @param {object} env The environment, holding `INPUT_WORKING_DIRECTORY` and `GITHUB_WORKSPACE`. + * + * @throws {Error} When it is not a directory, is outside the workspace, or is not plain names. + * + * @returns {{dir: string|undefined, prefix: string}} The checkout, and what goes before a path + * relative to it to make the path relative to the workspace: empty, or ending in `/`. + */ +function readCheckout(env) { + const given = String(env.INPUT_WORKING_DIRECTORY ?? '').trim() + if (given === '') { + return { dir: env.GITHUB_WORKSPACE, prefix: '' } + } + + // The step runs in the workspace, like every step of a composite action. + const workspace = env.GITHUB_WORKSPACE || process.cwd() + const dir = readWorkingDirectory(given, { base: workspace, what: WHAT }) + const relative = path.relative(workspace, dir).split(path.sep).join('/') + + if (relative !== '' && !new RegExp(`^${PARTS}$`).test(relative)) { + throw new Error( + `working-directory '${given}' is not a directory inside the workspace, such as plugin: each part of its ` + + `path takes letters, digits, ., _ and -, and does not start with . or -. ${WHAT}` + ) + } + + return { dir, prefix: relative === '' ? '' : `${relative}/` } +} + /** * Where the POT is when `pot-path` is empty: `/.pot`, from the header of - * the plugin file at the top of the workspace. The result is checked like a given `pot-path`. + * the plugin file at the top of the checkout. The result is checked like a given `pot-path`. * - * @param {object} env The environment, holding `GITHUB_WORKSPACE`. + * @param {string|undefined} dir The checkout; undefined when `GITHUB_WORKSPACE` is not set. * * @throws {Error} When no plugin file is found, or its header has no Text Domain or Domain Path. * - * @returns {string} The POT's path, relative to the workspace. + * @returns {string} The POT's path, relative to the checkout. */ -function defaultPotPath(env) { +function defaultPotPath(dir) { const pass = `Pass pot-path. ${WHAT}` - if (!env.GITHUB_WORKSPACE) { + if (!dir) { throw new Error(`pot-path is empty and GITHUB_WORKSPACE is not set, so no plugin header can be read. ${pass}`) } - const header = readPluginHeader(env.GITHUB_WORKSPACE) + const header = readPluginHeader(dir) if (header === null) { - throw new Error(`pot-path is empty and no PHP file at the top of the workspace has a Plugin Name header. ${pass}`) + throw new Error(`pot-path is empty and no PHP file at the top of the checkout has a Plugin Name header. ${pass}`) } if (!header.textDomain) { throw new Error(`pot-path is empty and ${header.file} has no Text Domain header. ${pass}`) @@ -145,7 +182,8 @@ function defaultPotPath(env) { * @returns {Object} The step's outputs, by name. */ function readInputs(env, buildId = crypto.randomBytes(16).toString('hex')) { - const withPot = { ...env, INPUT_POT_PATH: String(env.INPUT_POT_PATH ?? '').trim() || defaultPotPath(env) } + const { dir, prefix } = readCheckout(env) + const withPot = { ...env, INPUT_POT_PATH: String(env.INPUT_POT_PATH ?? '').trim() || defaultPotPath(dir) } const read = Object.fromEntries(Object.keys(RULES).map((name) => [name, readInput(withPot, name)])) if (!BUILD_ID.test(buildId)) { @@ -160,7 +198,8 @@ function readInputs(env, buildId = crypto.randomBytes(16).toString('hex')) { // it is handed to those steps as a key, never placed in a command. return { project: read.project, - 'pot-path': read['pot-path'], + // Relative to the workspace, which is where the copy step reads it from. + 'pot-path': `${prefix}${read['pot-path']}`, host: read.host, user: read.user, 'remote-path': remotePath, diff --git a/tests/actions/push-translations/push-translations.test.js b/tests/actions/push-translations/push-translations.test.js index 22acd193..56ac8503 100644 --- a/tests/actions/push-translations/push-translations.test.js +++ b/tests/actions/push-translations/push-translations.test.js @@ -265,6 +265,61 @@ describe('the pot-path default', () => { assert.strictEqual(readInputs(env, BUILD_ID)['pot-path'], 'languages/give.pot') }) + describe('with working-directory', () => { + /** + * A workspace with one plugin at its top and another checked out in `plugin/`. + * + * @returns {string} The workspace directory. + */ + function workspaceWithCheckout() { + const dir = workspace(' * Text Domain: root\n * Domain Path: /lang') + fs.mkdirSync(path.join(dir, 'plugin')) + fs.writeFileSync( + path.join(dir, 'plugin', 'give.php'), + ' { + const env = { ...GIVE, INPUT_WORKING_DIRECTORY: 'plugin', GITHUB_WORKSPACE: workspaceWithCheckout() } + const read = readInputs(env, BUILD_ID) + + assert.strictEqual(read['pot-path'], 'plugin/languages/give.pot') + assert.strictEqual(read['remote-pot'], '/home/deploy/pots/givewp/give.pot') + assert.strictEqual(read['result-file'], `give-${BUILD_ID}`) + }) + + it('reads the plugin header in the checkout when pot-path is empty', () => { + const env = { ...GIVE, INPUT_POT_PATH: '', INPUT_WORKING_DIRECTORY: 'plugin', GITHUB_WORKSPACE: workspaceWithCheckout() } + + assert.strictEqual(readInputs(env, BUILD_ID)['pot-path'], 'plugin/languages/give.pot') + }) + + it('takes ./, a trailing /, an absolute path inside the workspace, and . for the workspace', () => { + const dir = workspaceWithCheckout() + const potPath = (given) => + readInputs({ ...GIVE, INPUT_WORKING_DIRECTORY: given, GITHUB_WORKSPACE: dir }, BUILD_ID)['pot-path'] + + assert.strictEqual(potPath(' ./plugin/\n'), 'plugin/languages/give.pot') + assert.strictEqual(potPath(path.join(dir, 'plugin')), 'plugin/languages/give.pot') + assert.strictEqual(potPath('.'), 'languages/give.pot') + }) + + it('refuses a working-directory that is missing, outside the workspace, or not plain names', () => { + const dir = workspaceWithCheckout() + fs.mkdirSync(path.join(dir, 'my plugin')) + + for (const given of ['missing', '..', tempDir(), 'my plugin']) { + assert.throws( + () => readInputs({ ...GIVE, INPUT_WORKING_DIRECTORY: given, GITHUB_WORKSPACE: dir }, BUILD_ID), + /^Error: working-directory .*Nothing was sent to the translations server\.$/s, + given + ) + } + }) + }) + const refused = { 'no plugin file': null, 'no Text Domain': ' * Domain Path: /languages',