From a5391f55172786be5b71e84e426e6962fa083b67 Mon Sep 17 00:00:00 2001 From: David Seveloff Date: Sun, 29 Nov 2026 13:54:44 +0200 Subject: [PATCH 1/3] feat: add slack-find-user, slack-send-message and set-commit-status actions Co-Authored-By: Claude Sonnet 5.5 --- actions/set-commit-status/README.md | 34 +++++ actions/set-commit-status/action.yml | 46 ++++++ actions/set-commit-status/package.json | 9 ++ .../set-commit-status/set-commit-status.sh | 29 ++++ .../set-commit-status.test.sh | 100 +++++++++++++ actions/slack-find-user/README.md | 47 ++++++ actions/slack-find-user/action.yml | 55 +++++++ actions/slack-find-user/find-user.sh | 103 +++++++++++++ actions/slack-find-user/find-user.test.sh | 129 ++++++++++++++++ actions/slack-find-user/package.json | 9 ++ actions/slack-send-message/README.md | 52 +++++++ actions/slack-send-message/action.yml | 68 +++++++++ actions/slack-send-message/package.json | 9 ++ actions/slack-send-message/send-message.sh | 93 ++++++++++++ .../slack-send-message/send-message.test.sh | 138 ++++++++++++++++++ 15 files changed, 921 insertions(+) create mode 100644 actions/set-commit-status/README.md create mode 100644 actions/set-commit-status/action.yml create mode 100644 actions/set-commit-status/package.json create mode 100644 actions/set-commit-status/set-commit-status.sh create mode 100644 actions/set-commit-status/set-commit-status.test.sh create mode 100644 actions/slack-find-user/README.md create mode 100644 actions/slack-find-user/action.yml create mode 100644 actions/slack-find-user/find-user.sh create mode 100644 actions/slack-find-user/find-user.test.sh create mode 100644 actions/slack-find-user/package.json create mode 100644 actions/slack-send-message/README.md create mode 100644 actions/slack-send-message/action.yml create mode 100644 actions/slack-send-message/package.json create mode 100644 actions/slack-send-message/send-message.sh create mode 100644 actions/slack-send-message/send-message.test.sh diff --git a/actions/set-commit-status/README.md b/actions/set-commit-status/README.md new file mode 100644 index 000000000..7b4076899 --- /dev/null +++ b/actions/set-commit-status/README.md @@ -0,0 +1,34 @@ +# Set commit status + +Sets a commit status on a SHA. Useful when a check runs in one repository and +reports its result on a commit in another one. + +The token needs commit statuses write access to the target repository. Inputs +are validated (full SHA, known state) before the API call, and the description +is cut to the 140 characters GitHub accepts. + +## Usage + +```yaml +- name: Mark pending + uses: elementor/elementor-editor-github-actions/actions/set-commit-status@main + with: + github-token: ${{ steps.app.outputs.token }} + repository: elementor/elementor + sha: ${{ steps.payload.outputs.sha }} + context: ci/code-checks + state: pending + description: Running +``` + +## Inputs + +| Input | Required | Default | Notes | +| -------------- | -------- | -------------------- | ------------------------------------------ | +| `github-token` | yes | | Commit statuses write access | +| `sha` | yes | | Full 40 character SHA | +| `state` | yes | | `pending`, `success`, `failure` or `error` | +| `context` | yes | | For example `ci/code-checks` | +| `description` | no | | Cut to 140 characters | +| `target-url` | no | current workflow run | URL the status links to | +| `repository` | no | `github.repository` | `owner/name` that owns the commit | diff --git a/actions/set-commit-status/action.yml b/actions/set-commit-status/action.yml new file mode 100644 index 000000000..46cb6dacc --- /dev/null +++ b/actions/set-commit-status/action.yml @@ -0,0 +1,46 @@ +name: Set Commit Status +description: >- + Sets a commit status (pending, success, failure or error) on a SHA, optionally linking to a URL. + Use it to report the result of a check that runs outside the repository that owns the commit. + +inputs: + github-token: + description: Token with commit statuses write access to the repository + required: true + sha: + description: Full commit SHA to set the status on + required: true + state: + description: pending, success, failure or error + required: true + context: + description: Status context, for example ci/code-checks + required: true + description: + description: Short description, cut to 140 characters + required: false + default: '' + target-url: + description: URL the status links to. Defaults to the current workflow run + required: false + default: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }} + repository: + description: Repository in owner/name form that owns the commit + required: false + default: ${{ github.repository }} + +runs: + using: composite + steps: + - name: Set commit status + shell: bash + env: + GH_TOKEN: ${{ inputs.github-token }} + REPOSITORY: ${{ inputs.repository }} + SHA: ${{ inputs.sha }} + STATE: ${{ inputs.state }} + CONTEXT: ${{ inputs.context }} + DESCRIPTION: ${{ inputs.description }} + TARGET_URL: ${{ inputs.target-url }} + ACTION_PATH: ${{ github.action_path }} + run: bash "$ACTION_PATH/set-commit-status.sh" diff --git a/actions/set-commit-status/package.json b/actions/set-commit-status/package.json new file mode 100644 index 000000000..dde7647f0 --- /dev/null +++ b/actions/set-commit-status/package.json @@ -0,0 +1,9 @@ +{ + "name": "@elementor/set-commit-status", + "author": "Elementor Team", + "license": "GPL-3.0-or-later", + "private": true, + "scripts": { + "test": "bash set-commit-status.test.sh" + } +} diff --git a/actions/set-commit-status/set-commit-status.sh b/actions/set-commit-status/set-commit-status.sh new file mode 100644 index 000000000..ad161c8b2 --- /dev/null +++ b/actions/set-commit-status/set-commit-status.sh @@ -0,0 +1,29 @@ +#!/usr/bin/env bash +# Sets a commit status on a SHA. Inputs are validated before they reach the API +# call, and the description is cut to the 140 characters GitHub accepts. + +set -euo pipefail + +REPOSITORY="${REPOSITORY:-}" +SHA="${SHA:-}" +STATE="${STATE:-}" +CONTEXT="${CONTEXT:-}" +DESCRIPTION="${DESCRIPTION:-}" +TARGET_URL="${TARGET_URL:-}" + +fail() { + echo "::error::$1" + exit 1 +} + +[[ "$REPOSITORY" =~ ^[A-Za-z0-9_.-]+/[A-Za-z0-9_.-]+$ ]] || fail "repository must be owner/name." +[[ "$SHA" =~ ^[0-9a-f]{40}$ ]] || fail "sha must be a full 40 character commit SHA." +[[ "$STATE" =~ ^(error|failure|pending|success)$ ]] || fail "state must be error, failure, pending or success." +[[ -n "$CONTEXT" ]] || fail "context is required." + +args=(-f "state=${STATE}" -f "context=${CONTEXT}" -f "description=${DESCRIPTION:0:140}") +if [[ -n "$TARGET_URL" ]]; then + args+=(-f "target_url=${TARGET_URL}") +fi + +gh api "repos/${REPOSITORY}/statuses/${SHA}" "${args[@]}" > /dev/null diff --git a/actions/set-commit-status/set-commit-status.test.sh b/actions/set-commit-status/set-commit-status.test.sh new file mode 100644 index 000000000..dd3347ea9 --- /dev/null +++ b/actions/set-commit-status/set-commit-status.test.sh @@ -0,0 +1,100 @@ +#!/usr/bin/env bash +# Test suite for set-commit-status.sh +# Run with: bash set-commit-status.test.sh + +set -euo pipefail + +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +SCRIPT="${SCRIPT_DIR}/set-commit-status.sh" +FAILURES=0 +TESTS_RUN=0 + +assert_equals() { + TESTS_RUN=$((TESTS_RUN + 1)) + if [ "$1" = "$2" ]; then + echo " ✓ $3" + else + echo " ✗ $3" + echo " Expected: $1" + echo " Got: $2" + FAILURES=$((FAILURES + 1)) + fi +} + +assert_contains() { + TESTS_RUN=$((TESTS_RUN + 1)) + if grep -qF -- "$2" <<< "$1"; then + echo " ✓ $3" + else + echo " ✗ $3" + echo " Missing: $2" + FAILURES=$((FAILURES + 1)) + fi +} + +assert_not_contains() { + TESTS_RUN=$((TESTS_RUN + 1)) + if grep -qF -- "$2" <<< "$1"; then + echo " ✗ $3" + echo " Unexpected: $2" + FAILURES=$((FAILURES + 1)) + else + echo " ✓ $3" + fi +} + +WORK="$(mktemp -d)" +trap 'rm -rf "$WORK"' EXIT + +mkdir -p "$WORK/bin" +cat > "$WORK/bin/gh" <<'STUB' +#!/usr/bin/env bash +echo "$*" > "$STUB_LOG" +STUB +chmod +x "$WORK/bin/gh" +export STUB_LOG="$WORK/log" + +SHA="0123456789abcdef0123456789abcdef01234567" + +# Runs the script with the gh stub. Prints "exit=". +run() { + : > "$STUB_LOG" + set +e + env PATH="$WORK/bin:$PATH" REPOSITORY=elementor/elementor SHA="$SHA" STATE=pending CONTEXT=ci/code-checks "$@" bash "$SCRIPT" > /dev/null 2>&1 + local code=$? + set -e + echo "exit=${code}" +} + +echo "setting a status" + +out="$(run DESCRIPTION=Running TARGET_URL=https://example.com/run/1)" +log="$(cat "$STUB_LOG")" +assert_equals "exit=0" "$out" "succeeds with valid input" +assert_contains "$log" "api repos/elementor/elementor/statuses/${SHA}" "calls the statuses endpoint for the SHA" +assert_contains "$log" 'state=pending' "sends the state" +assert_contains "$log" 'context=ci/code-checks' "sends the context" +assert_contains "$log" 'target_url=https://example.com/run/1' "sends the target URL" + +run > /dev/null +assert_not_contains "$(cat "$STUB_LOG")" 'target_url' "omits the target URL when empty" + +long="$(printf 'a%.0s' {1..200})" +run DESCRIPTION="$long" > /dev/null +expected="$(printf 'a%.0s' {1..140})" +assert_contains "$(cat "$STUB_LOG")" "description=${expected}" "cuts the description to 140 characters" +assert_not_contains "$(cat "$STUB_LOG")" "description=${expected}a" "does not keep more than 140 characters" + +echo "validation" + +assert_equals "exit=1" "$(run REPOSITORY='elementor')" "rejects a repository without an owner" +assert_equals "exit=1" "$(run SHA=abc123)" "rejects a short SHA" +assert_equals "exit=1" "$(run STATE=done)" "rejects an unknown state" +assert_equals "exit=1" "$(run CONTEXT=)" "rejects an empty context" + +echo "" +echo "Tests run: ${TESTS_RUN}, failures: ${FAILURES}" + +if [ "$FAILURES" -gt 0 ]; then + exit 1 +fi diff --git a/actions/slack-find-user/README.md b/actions/slack-find-user/README.md new file mode 100644 index 000000000..f48c73d64 --- /dev/null +++ b/actions/slack-find-user/README.md @@ -0,0 +1,47 @@ +# Slack find user + +Resolves a Slack user ID from a GitHub login or an email. Pair it with +[slack-send-message](../slack-send-message/README.md) to DM someone. + +A GitHub login is mapped to an email through the organization SAML identity, +then through the verified domain emails. Both queries need org-level access, so +pass a `github-token` that has it (for example a classic PAT with `read:org` +that is authorized for SAML SSO). The default workflow token usually cannot +read them, and the step then reports the user as not found with a warning. + +A user that cannot be found does not fail the step. Check `found`. + +## Usage + +```yaml +- name: Find the PR author in Slack + id: user + uses: elementor/elementor-editor-github-actions/actions/slack-find-user@main + with: + slack-token: ${{ secrets.SLACK_BOT_TOKEN }} + github-token: ${{ secrets.ORG_LOOKUP_TOKEN }} + github-login: ${{ github.event.pull_request.user.login }} +``` + +## Inputs + +| Input | Required | Default | Notes | +| -------------- | -------- | ------------------------- | --------------------------------- | +| `slack-token` | yes | | Bot token with `users:read.email` | +| `github-login` | no | | Ignored when `email` is passed | +| `email` | no | | Skips the GitHub lookup | +| `github-org` | no | `github.repository_owner` | Organization for the email lookup | +| `github-token` | no | `github.token` | Token for the email lookup | + +One of `github-login` or `email` is required. + +## Outputs + +| Output | Notes | +| --------- | -------------------------------------------- | +| `found` | `true` or `false` | +| `user-id` | Slack user ID, empty when not found | +| `email` | Email used for the Slack lookup | +| `reason` | Why the user was not found, empty when found | + +Slack API errors other than `users_not_found` fail the step. diff --git a/actions/slack-find-user/action.yml b/actions/slack-find-user/action.yml new file mode 100644 index 000000000..67780af4c --- /dev/null +++ b/actions/slack-find-user/action.yml @@ -0,0 +1,55 @@ +name: Slack Find User +description: >- + Resolves a Slack user ID from a GitHub login or an email. A login is mapped to an email through the + organization SAML identity or verified domain emails, which needs a token with org read access. + A user that is not found does not fail the step: check the "found" output. + +inputs: + slack-token: + description: Slack bot token with users:read.email + required: true + github-login: + description: GitHub login to look up. Ignored when email is passed + required: false + default: '' + email: + description: Email to look up in Slack. Skips the GitHub lookup + required: false + default: '' + github-org: + description: Organization used to read the SAML identity and verified domain emails of the login + required: false + default: ${{ github.repository_owner }} + github-token: + description: Token for the GitHub email lookup. The SAML identity query needs org-level access, so the default token usually cannot read it + required: false + default: ${{ github.token }} + +outputs: + found: + description: '"true" when a Slack user was found, otherwise "false"' + value: ${{ steps.find.outputs.found }} + user-id: + description: Slack user ID, empty when not found + value: ${{ steps.find.outputs.user-id }} + email: + description: Email used for the Slack lookup, empty when none was found + value: ${{ steps.find.outputs.email }} + reason: + description: Why the user was not found, empty when found + value: ${{ steps.find.outputs.reason }} + +runs: + using: composite + steps: + - name: Find Slack user + id: find + shell: bash + env: + SLACK_TOKEN: ${{ inputs.slack-token }} + GITHUB_LOGIN: ${{ inputs.github-login }} + EMAIL: ${{ inputs.email }} + GITHUB_ORG: ${{ inputs.github-org }} + GH_TOKEN: ${{ inputs.github-token }} + ACTION_PATH: ${{ github.action_path }} + run: bash "$ACTION_PATH/find-user.sh" diff --git a/actions/slack-find-user/find-user.sh b/actions/slack-find-user/find-user.sh new file mode 100644 index 000000000..4fb6cf0a8 --- /dev/null +++ b/actions/slack-find-user/find-user.sh @@ -0,0 +1,103 @@ +#!/usr/bin/env bash +# Resolves a Slack user ID from a GitHub login or from an email. A GitHub login +# is first mapped to an email through the organization SAML identity, then +# through the verified domain emails. A user that cannot be found is not an +# error: the outputs say so and the step succeeds with a warning. + +set -euo pipefail + +SLACK_API="${SLACK_API:-https://slack.com/api}" +GITHUB_LOGIN="${GITHUB_LOGIN:-}" +EMAIL="${EMAIL:-}" +GITHUB_ORG="${GITHUB_ORG:-}" + +fail() { + echo "::error::$1" + exit 1 +} + +output() { + echo "$1=$2" >> "${GITHUB_OUTPUT:-/dev/null}" +} + +not_found() { + echo "::warning::$1" + output found false + output user-id "" + output email "$EMAIL" + output reason "$1" + exit 0 +} + +lookup_email_from_login() { + local login="$1" org="$2" err found="" + err="$(mktemp)" + + set +e + # shellcheck disable=SC2016 + found="$(gh api graphql -f login="$login" -f org="$org" -f query=' + query ($login: String!, $org: String!) { + organization(login: $org) { + samlIdentityProvider { + externalIdentities(first: 1, login: $login) { + nodes { samlIdentity { nameId } } + } + } + } + } + ' --jq '.data.organization.samlIdentityProvider.externalIdentities.nodes[0].samlIdentity.nameId // empty' 2>> "$err")" + if [[ -z "$found" ]]; then + # shellcheck disable=SC2016 + found="$(gh api graphql -f login="$login" -f org="$org" -f query=' + query ($login: String!, $org: String!) { + user(login: $login) { + organizationVerifiedDomainEmails(login: $org) + } + } + ' --jq '.data.user.organizationVerifiedDomainEmails[0] // empty' 2>> "$err")" + fi + set -e + + if [[ -z "$found" && -s "$err" ]]; then + echo "::warning::GitHub email lookup returned errors: $(head -c 300 "$err" | tr '\n' ' ')" >&2 + fi + rm -f "$err" + printf '%s' "$found" +} + +if [[ -z "$EMAIL" ]]; then + [[ -n "$GITHUB_LOGIN" ]] || fail "Pass github-login or email." + [[ "$GITHUB_LOGIN" =~ ^[A-Za-z0-9][A-Za-z0-9-]*$ ]] || not_found "Invalid GitHub login." + [[ "$GITHUB_ORG" =~ ^[A-Za-z0-9][A-Za-z0-9-]*$ ]] || fail "github-org is required to look up an email from a login." + EMAIL="$(lookup_email_from_login "$GITHUB_LOGIN" "$GITHUB_ORG")" +fi + +[[ "$EMAIL" =~ ^[^@[:space:]]+@[^@[:space:]]+\.[^@[:space:]]+$ ]] || { + EMAIL="" + not_found "No email found for ${GITHUB_LOGIN:-the given user}." +} + +: "${SLACK_TOKEN:?slack-token is required}" + +response="$(curl -sS --fail-with-body --retry 3 -G \ + -H "Authorization: Bearer ${SLACK_TOKEN}" \ + --data-urlencode "email=${EMAIL}" \ + "${SLACK_API}/users.lookupByEmail")" || fail "Slack users.lookupByEmail request failed." + +ok="$(jq -r '.ok // empty' <<< "$response")" +user_id="$(jq -r '.user.id // empty' <<< "$response")" +error="$(jq -r '.error // empty' <<< "$response")" + +if [[ "$ok" == "true" && "$user_id" =~ ^[UW][A-Z0-9]+$ ]]; then + output found true + output user-id "$user_id" + output email "$EMAIL" + output reason "" + exit 0 +fi + +if [[ "$error" == "users_not_found" || -z "$error" ]]; then + not_found "No Slack user found for ${EMAIL}." +fi + +fail "Slack users.lookupByEmail failed: ${error}" diff --git a/actions/slack-find-user/find-user.test.sh b/actions/slack-find-user/find-user.test.sh new file mode 100644 index 000000000..36f188ed0 --- /dev/null +++ b/actions/slack-find-user/find-user.test.sh @@ -0,0 +1,129 @@ +#!/usr/bin/env bash +# Test suite for find-user.sh +# Run with: bash find-user.test.sh + +set -euo pipefail + +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +SCRIPT="${SCRIPT_DIR}/find-user.sh" +FAILURES=0 +TESTS_RUN=0 + +assert_equals() { + TESTS_RUN=$((TESTS_RUN + 1)) + if [ "$1" = "$2" ]; then + echo " ✓ $3" + else + echo " ✗ $3" + echo " Expected: $1" + echo " Got: $2" + FAILURES=$((FAILURES + 1)) + fi +} + +assert_contains() { + TESTS_RUN=$((TESTS_RUN + 1)) + if grep -qF -- "$2" <<< "$1"; then + echo " ✓ $3" + else + echo " ✗ $3" + echo " Missing: $2" + FAILURES=$((FAILURES + 1)) + fi +} + +assert_not_contains() { + TESTS_RUN=$((TESTS_RUN + 1)) + if grep -qF -- "$2" <<< "$1"; then + echo " ✗ $3" + echo " Unexpected: $2" + FAILURES=$((FAILURES + 1)) + else + echo " ✓ $3" + fi +} + +WORK="$(mktemp -d)" +trap 'rm -rf "$WORK"' EXIT + +mkdir -p "$WORK/bin" +cat > "$WORK/bin/gh" <<'STUB' +#!/usr/bin/env bash +echo "gh" >> "$STUB_LOG" +if [ -n "${STUB_GH_ERROR:-}" ]; then + echo "$STUB_GH_ERROR" >&2 + exit 1 +fi +printf '%s' "${STUB_GH_EMAIL:-}" +STUB +cat > "$WORK/bin/curl" <<'STUB' +#!/usr/bin/env bash +echo "curl $*" >> "$STUB_LOG" +response="${STUB_SLACK_RESPONSE:-}" +if [ -z "$response" ]; then + response='{"ok":false,"error":"users_not_found"}' +fi +echo "$response" +STUB +chmod +x "$WORK/bin/gh" "$WORK/bin/curl" +export STUB_LOG="$WORK/log" + +# Runs the script with the stubs. Prints the outputs, then "exit=". +run() { + : > "$STUB_LOG" + : > "$WORK/out" + set +e + env PATH="$WORK/bin:$PATH" SLACK_TOKEN=x GITHUB_ORG=elementor GITHUB_OUTPUT="$WORK/out" "$@" bash "$SCRIPT" > "$WORK/stdout" 2>&1 + local code=$? + set -e + cat "$WORK/out" + echo "exit=${code}" +} + +FOUND='{"ok":true,"user":{"id":"U123ABC"}}' + +echo "lookup by email" + +out="$(run EMAIL=dev@example.com STUB_SLACK_RESPONSE="$FOUND")" +assert_contains "$out" 'found=true' "finds a Slack user by email" +assert_contains "$out" 'user-id=U123ABC' "returns the Slack user ID" +assert_not_contains "$(cat "$STUB_LOG")" 'gh' "does not call GitHub when an email is passed" + +out="$(run EMAIL=dev@example.com)" +assert_contains "$out" 'found=false' "reports found=false for users_not_found" +assert_contains "$out" 'exit=0' "does not fail when the user is not found" + +out="$(run EMAIL=dev@example.com STUB_SLACK_RESPONSE='{"ok":false,"error":"invalid_auth"}')" +assert_contains "$out" 'exit=1' "fails on other Slack errors" + +echo "lookup by GitHub login" + +out="$(run GITHUB_LOGIN=octo-dev STUB_GH_EMAIL=dev@example.com STUB_SLACK_RESPONSE="$FOUND")" +assert_contains "$out" 'user-id=U123ABC' "maps a login to an email and finds the user" +assert_contains "$out" 'email=dev@example.com' "returns the email that was used" + +out="$(run GITHUB_LOGIN=octo-dev)" +assert_contains "$out" 'found=false' "reports found=false when no email is found" +assert_not_contains "$(cat "$STUB_LOG")" 'curl' "skips Slack when there is no email" + +out="$(run GITHUB_LOGIN=octo-dev STUB_GH_ERROR='Resource not accessible by integration')" +assert_contains "$out" 'exit=0' "does not fail when the GitHub lookup is denied" +assert_contains "$(cat "$WORK/stdout")" 'Resource not accessible' "warns with the GitHub error" + +echo "validation" + +out="$(run GITHUB_LOGIN='bad login; rm')" +assert_contains "$out" 'found=false' "rejects an invalid login" + +out="$(run)" +assert_contains "$out" 'exit=1' "fails when neither login nor email is passed" + +out="$(run GITHUB_LOGIN=octo-dev GITHUB_ORG=)" +assert_contains "$out" 'exit=1' "fails when a login is passed without an organization" + +echo "" +echo "Tests run: ${TESTS_RUN}, failures: ${FAILURES}" + +if [ "$FAILURES" -gt 0 ]; then + exit 1 +fi diff --git a/actions/slack-find-user/package.json b/actions/slack-find-user/package.json new file mode 100644 index 000000000..80a669b5d --- /dev/null +++ b/actions/slack-find-user/package.json @@ -0,0 +1,9 @@ +{ + "name": "@elementor/slack-find-user", + "author": "Elementor Team", + "license": "GPL-3.0-or-later", + "private": true, + "scripts": { + "test": "bash find-user.test.sh" + } +} diff --git a/actions/slack-send-message/README.md b/actions/slack-send-message/README.md new file mode 100644 index 000000000..0c29e8154 --- /dev/null +++ b/actions/slack-send-message/README.md @@ -0,0 +1,52 @@ +# Slack send message + +Posts a message to a Slack channel, or to a DM when `channel` is a user ID. +Untrusted text (for example content derived from a PR diff) can be escaped, +truncated and wrapped in a code block so it cannot trigger mentions or links. + +To DM someone, resolve the user ID first with +[slack-find-user](../slack-find-user/README.md). + +## Usage + +```yaml +- name: Find the PR author in Slack + id: user + uses: elementor/elementor-editor-github-actions/actions/slack-find-user@main + with: + slack-token: ${{ secrets.SLACK_BOT_TOKEN }} + github-login: ${{ github.event.pull_request.user.login }} + +- name: Send findings + if: steps.user.outputs.found == 'true' + uses: elementor/elementor-editor-github-actions/actions/slack-send-message@main + with: + slack-token: ${{ secrets.SLACK_BOT_TOKEN }} + channel: ${{ steps.user.outputs.user-id }} + title: 'Checks found items on ${{ github.event.pull_request.html_url }}' + text-file: findings.txt + code-block: 'true' +``` + +## Inputs + +| Input | Required | Default | Notes | +| ------------- | -------- | ------- | ----------------------------------------------------- | +| `slack-token` | yes | | Bot token with `chat:write`, plus `im:write` for DMs | +| `channel` | yes | | Channel ID (`C…`, `G…`, `D…`) or user ID (`U…`, `W…`) | +| `text` | no | | Message body | +| `text-file` | no | | File with the body, takes precedence over `text` | +| `title` | no | | Trusted first line, posted as is | +| `code-block` | no | `false` | Wrap the body in a code block | +| `escape` | no | `true` | Escape `&`, `<`, `>` in the body | +| `max-length` | no | `3500` | Body is cut to this length and ends with an ellipsis | +| `dry-run` | no | `false` | Print the payload and do not post | + +One of `text` or `text-file` is required. + +## Outputs + +`ts` and `channel` of the posted message. + +Requests use `--fail-with-body --retry 3`. A Slack `ok: false` response fails +the step with the Slack error name. diff --git a/actions/slack-send-message/action.yml b/actions/slack-send-message/action.yml new file mode 100644 index 000000000..8d6e54ea3 --- /dev/null +++ b/actions/slack-send-message/action.yml @@ -0,0 +1,68 @@ +name: Slack Send Message +description: >- + Posts a message to a Slack channel, or to a DM when the target is a user ID. Text can be escaped, + truncated and wrapped in a code block, so untrusted content cannot trigger mentions or links. + Resolve a user ID from a GitHub login or an email with the slack-find-user action. + +inputs: + slack-token: + description: Slack bot token with chat:write (and im:write to open DMs) + required: true + channel: + description: Slack channel ID (C…, G…, D…) or user ID (U…, W…). A user ID opens a DM first + required: true + text: + description: Message body. Use text-file for longer content + required: false + default: '' + text-file: + description: Path to a file with the message body. Takes precedence over text + required: false + default: '' + title: + description: Trusted first line, posted as is (mrkdwn allowed). Never put untrusted content here + required: false + default: '' + code-block: + description: When "true", wrap the body in a code block + required: false + default: 'false' + escape: + description: When "true", escape &, < and > in the body so mentions and links are not rendered + required: false + default: 'true' + max-length: + description: Maximum body length in characters. Longer bodies are cut and end with an ellipsis + required: false + default: '3500' + dry-run: + description: When "true", print the payload and do not post it + required: false + default: 'false' + +outputs: + ts: + description: Timestamp of the posted message + value: ${{ steps.send.outputs.ts }} + channel: + description: ID of the channel the message was posted to + value: ${{ steps.send.outputs.channel }} + +runs: + using: composite + steps: + - name: Send Slack message + id: send + shell: bash + env: + SLACK_TOKEN: ${{ inputs.slack-token }} + CHANNEL: ${{ inputs.channel }} + TEXT: ${{ inputs.text }} + TEXT_FILE: ${{ inputs.text-file }} + TITLE: ${{ inputs.title }} + CODE_BLOCK: ${{ inputs.code-block }} + ESCAPE: ${{ inputs.escape }} + MAX_LENGTH: ${{ inputs.max-length }} + DRY_RUN: ${{ inputs.dry-run }} + ACTION_PATH: ${{ github.action_path }} + run: bash "$ACTION_PATH/send-message.sh" diff --git a/actions/slack-send-message/package.json b/actions/slack-send-message/package.json new file mode 100644 index 000000000..88a91c487 --- /dev/null +++ b/actions/slack-send-message/package.json @@ -0,0 +1,9 @@ +{ + "name": "@elementor/slack-send-message", + "author": "Elementor Team", + "license": "GPL-3.0-or-later", + "private": true, + "scripts": { + "test": "bash send-message.test.sh" + } +} diff --git a/actions/slack-send-message/send-message.sh b/actions/slack-send-message/send-message.sh new file mode 100644 index 000000000..5c25e15cc --- /dev/null +++ b/actions/slack-send-message/send-message.sh @@ -0,0 +1,93 @@ +#!/usr/bin/env bash +# Posts a message to a Slack channel or, when the target is a user ID, to a DM +# with that user. Untrusted text can be escaped, truncated and wrapped in a code +# block so that it cannot trigger mentions or links. + +set -euo pipefail + +SLACK_API="${SLACK_API:-https://slack.com/api}" +CHANNEL="${CHANNEL:-}" +TITLE="${TITLE:-}" +TEXT="${TEXT:-}" +TEXT_FILE="${TEXT_FILE:-}" +CODE_BLOCK="${CODE_BLOCK:-false}" +ESCAPE="${ESCAPE:-true}" +MAX_LENGTH="${MAX_LENGTH:-3500}" +DRY_RUN="${DRY_RUN:-false}" + +fail() { + echo "::error::$1" + exit 1 +} + +output() { + echo "$1=$2" >> "${GITHUB_OUTPUT:-/dev/null}" +} + +[[ "$CHANNEL" =~ ^[CGDUW][A-Z0-9]+$ ]] || fail "channel must be a Slack channel or user ID." +[[ "$CODE_BLOCK" == "true" || "$CODE_BLOCK" == "false" ]] || fail "code-block must be true or false." +[[ "$ESCAPE" == "true" || "$ESCAPE" == "false" ]] || fail "escape must be true or false." +[[ "$DRY_RUN" == "true" || "$DRY_RUN" == "false" ]] || fail "dry-run must be true or false." +[[ "$MAX_LENGTH" =~ ^[0-9]+$ ]] || fail "max-length must be a number." + +if [[ -n "$TEXT_FILE" ]]; then + [[ -f "$TEXT_FILE" ]] || fail "text-file does not exist." + body="$(cat "$TEXT_FILE")" +else + body="$TEXT" +fi +[[ -n "$body" ]] || fail "Pass text or text-file." + +build_payload() { + local channel="$1" + jq -cn \ + --arg channel "$channel" \ + --arg title "$TITLE" \ + --arg body "$body" \ + --argjson escape "$ESCAPE" \ + --argjson code "$CODE_BLOCK" \ + --argjson max "$MAX_LENGTH" ' + (if ($body | length) > $max then ($body | .[0:$max]) + "…" else $body end) as $cut + | ($cut + | if $escape then gsub("&"; "&") | gsub("<"; "<") | gsub(">"; ">") else . end + | if $code then gsub("```"; "ʼʼʼ") else . end) as $safe + | { + channel: $channel, + text: ( + (if $title != "" then $title + "\n" else "" end) + + (if $code then "```" + $safe + "```" else $safe end) + ) + }' +} + +if [[ "$DRY_RUN" == "true" ]]; then + build_payload "$CHANNEL" + exit 0 +fi + +: "${SLACK_TOKEN:?slack-token is required}" + +# Calls a Slack API method and stores the response in REPLY. It is not run in a +# subshell, so a failure reaches the workflow log as an error annotation. +slack_call() { + local method="$1" payload="$2" + REPLY="$(printf '%s' "$payload" | curl -sS --fail-with-body --retry 3 -X POST \ + -H "Authorization: Bearer ${SLACK_TOKEN}" \ + -H "Content-type: application/json; charset=utf-8" \ + --data-binary @- \ + "${SLACK_API}/${method}")" || fail "Slack ${method} request failed." + if [[ "$(jq -r '.ok // empty' <<< "$REPLY")" != "true" ]]; then + fail "Slack ${method} failed: $(jq -r '.error // "unknown error"' <<< "$REPLY")" + fi +} + +target="$CHANNEL" +if [[ "$CHANNEL" =~ ^[UW] ]]; then + slack_call conversations.open "$(jq -cn --arg users "$CHANNEL" '{users:$users}')" + target="$(jq -r '.channel.id // empty' <<< "$REPLY")" + [[ -n "$target" ]] || fail "Slack did not return a DM channel." +fi + +slack_call chat.postMessage "$(build_payload "$target")" +output ts "$(jq -r '.ts // empty' <<< "$REPLY")" +output channel "$(jq -r '.channel // empty' <<< "$REPLY")" diff --git a/actions/slack-send-message/send-message.test.sh b/actions/slack-send-message/send-message.test.sh new file mode 100644 index 000000000..d2fe746f2 --- /dev/null +++ b/actions/slack-send-message/send-message.test.sh @@ -0,0 +1,138 @@ +#!/usr/bin/env bash +# Test suite for send-message.sh +# Run with: bash send-message.test.sh + +set -euo pipefail + +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +SCRIPT="${SCRIPT_DIR}/send-message.sh" +FAILURES=0 +TESTS_RUN=0 + +assert_equals() { + TESTS_RUN=$((TESTS_RUN + 1)) + if [ "$1" = "$2" ]; then + echo " ✓ $3" + else + echo " ✗ $3" + echo " Expected: $1" + echo " Got: $2" + FAILURES=$((FAILURES + 1)) + fi +} + +assert_contains() { + TESTS_RUN=$((TESTS_RUN + 1)) + if grep -qF -- "$2" <<< "$1"; then + echo " ✓ $3" + else + echo " ✗ $3" + echo " Missing: $2" + FAILURES=$((FAILURES + 1)) + fi +} + +assert_not_contains() { + TESTS_RUN=$((TESTS_RUN + 1)) + if grep -qF -- "$2" <<< "$1"; then + echo " ✗ $3" + echo " Unexpected: $2" + FAILURES=$((FAILURES + 1)) + else + echo " ✓ $3" + fi +} + +WORK="$(mktemp -d)" +trap 'rm -rf "$WORK"' EXIT + +# Runs the script in dry-run mode and prints the payload text. +dry_text() { + env DRY_RUN=true CHANNEL=C123ABC "$@" bash "$SCRIPT" | jq -r '.text' +} + +echo "escaping and formatting" + +out="$(dry_text TEXT='hi & done')" +assert_equals 'hi <!channel> <https://evil|click> & done' "$out" "escapes &, < and >" + +out="$(dry_text ESCAPE=false TEXT='')" +assert_equals '' "$out" "keeps text as is when escape is false" + +out="$(dry_text CODE_BLOCK=true TEXT='a ```b``` c')" +assert_equals '```a ʼʼʼbʼʼʼ c```' "$out" "wraps in a code block and defuses fences" + +out="$(dry_text TITLE='Found items' TEXT='body')" +assert_equals $'Found items\nbody' "$out" "puts the trusted title on the first line" + +out="$(dry_text MAX_LENGTH=5 TEXT='abcdefghij')" +assert_equals 'abcde…' "$out" "truncates long text with an ellipsis" + +out="$(dry_text MAX_LENGTH=4 TEXT='<<<<<<')" +assert_equals '<<<<…' "$out" "truncates before escaping so entities stay intact" + +printf 'from a file' > "$WORK/body.txt" +out="$(dry_text TEXT_FILE="$WORK/body.txt" TEXT='ignored')" +assert_equals 'from a file' "$out" "text-file takes precedence over text" + +echo "validation" + +for bad in 'CHANNEL=general' 'CODE_BLOCK=yes' 'ESCAPE=1' 'MAX_LENGTH=abc' 'TEXT='; do + set +e + env DRY_RUN=true CHANNEL=C123ABC TEXT=x "$bad" bash "$SCRIPT" > /dev/null 2>&1 + code=$? + set -e + assert_equals "1" "$code" "rejects ${bad}" +done + +echo "posting" + +mkdir -p "$WORK/bin" +cat > "$WORK/bin/curl" <<'STUB' +#!/usr/bin/env bash +url="${@: -1}" +method="${url##*/}" +printf '%s ' "$method" >> "$STUB_LOG" +cat >> "$STUB_LOG" +printf '\n' >> "$STUB_LOG" +case "$method" in + conversations.open) echo '{"ok":true,"channel":{"id":"D999DM"}}' ;; + chat.postMessage) + response="${STUB_POST_RESPONSE:-}" + if [ -z "$response" ]; then + response='{"ok":true,"ts":"1.2","channel":"C123ABC"}' + fi + echo "$response" + ;; +esac +STUB +chmod +x "$WORK/bin/curl" + +export STUB_LOG="$WORK/log" +: > "$STUB_LOG" +out_file="$WORK/out" +PATH="$WORK/bin:$PATH" SLACK_TOKEN=x CHANNEL=C123ABC TEXT=hello GITHUB_OUTPUT="$out_file" bash "$SCRIPT" +log="$(cat "$STUB_LOG")" +assert_contains "$log" 'chat.postMessage {"channel":"C123ABC","text":"hello"}' "posts to a channel" +assert_not_contains "$log" 'conversations.open' "does not open a DM for a channel" +assert_contains "$(cat "$out_file")" 'ts=1.2' "writes the ts output" + +: > "$STUB_LOG" +PATH="$WORK/bin:$PATH" SLACK_TOKEN=x CHANNEL=U123ABC TEXT=hello bash "$SCRIPT" +log="$(cat "$STUB_LOG")" +assert_contains "$log" 'conversations.open {"users":"U123ABC"}' "opens a DM for a user ID" +assert_contains "$log" 'chat.postMessage {"channel":"D999DM","text":"hello"}' "posts to the DM channel" + +set +e +STUB_POST_RESPONSE='{"ok":false,"error":"channel_not_found"}' PATH="$WORK/bin:$PATH" SLACK_TOKEN=x CHANNEL=C123ABC TEXT=hello bash "$SCRIPT" > "$WORK/err" 2>&1 +code=$? +set -e +assert_equals "1" "$code" "fails when Slack returns ok false" +assert_contains "$(cat "$WORK/err")" 'channel_not_found' "reports the Slack error name" + +echo "" +echo "Tests run: ${TESTS_RUN}, failures: ${FAILURES}" + +if [ "$FAILURES" -gt 0 ]; then + exit 1 +fi From f7d0d4208d5e325bc388545228e2d36d881c89df Mon Sep 17 00:00:00 2001 From: David Seveloff Date: Sun, 29 Nov 2026 14:11:37 +0200 Subject: [PATCH 2/3] fix: sync package-lock with the new action workspaces Co-Authored-By: Claude Sonnet 5.5 --- package-lock.json | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/package-lock.json b/package-lock.json index af45f0397..02f67bc57 100644 --- a/package-lock.json +++ b/package-lock.json @@ -89,6 +89,10 @@ "tsup": "^8.5.0" } }, + "actions/set-commit-status": { + "name": "@elementor/set-commit-status", + "license": "GPL-3.0-or-later" + }, "actions/setup-elementor-env": { "name": "@elementor/setup-elementor-env", "license": "GPL-3.0-or-later", @@ -115,6 +119,14 @@ "tsup": "^8.5.0" } }, + "actions/slack-find-user": { + "name": "@elementor/slack-find-user", + "license": "GPL-3.0-or-later" + }, + "actions/slack-send-message": { + "name": "@elementor/slack-send-message", + "license": "GPL-3.0-or-later" + }, "actions/sync-release-branches": { "name": "@elementor/sync-release-branches", "license": "GPL-3.0-or-later" @@ -751,6 +763,10 @@ "resolved": "actions/run-lighthouse-tests", "link": true }, + "node_modules/@elementor/set-commit-status": { + "resolved": "actions/set-commit-status", + "link": true + }, "node_modules/@elementor/setup-elementor-env": { "resolved": "actions/setup-elementor-env", "link": true @@ -759,6 +775,14 @@ "resolved": "actions/setup-wp-env", "link": true }, + "node_modules/@elementor/slack-find-user": { + "resolved": "actions/slack-find-user", + "link": true + }, + "node_modules/@elementor/slack-send-message": { + "resolved": "actions/slack-send-message", + "link": true + }, "node_modules/@elementor/sync-release-branches": { "resolved": "actions/sync-release-branches", "link": true From 78e90d5c1f3cd0c577c3ed2573375a28b5574e56 Mon Sep 17 00:00:00 2001 From: David Seveloff Date: Sun, 29 Nov 2026 14:47:54 +0200 Subject: [PATCH 3/3] refactor: drop slack-find-user and accept @username in slack-send-message The organization already has map-github-to-slack in elementor-tests-configuration, so it stays the single source for the GitHub to Slack mapping. Co-Authored-By: Claude Sonnet 5.5 --- actions/slack-find-user/README.md | 47 ------- actions/slack-find-user/action.yml | 55 -------- actions/slack-find-user/find-user.sh | 103 -------------- actions/slack-find-user/find-user.test.sh | 129 ------------------ actions/slack-find-user/package.json | 9 -- actions/slack-send-message/README.md | 39 +++--- actions/slack-send-message/action.yml | 3 +- actions/slack-send-message/send-message.sh | 4 +- .../slack-send-message/send-message.test.sh | 8 +- package-lock.json | 5 +- 10 files changed, 27 insertions(+), 375 deletions(-) delete mode 100644 actions/slack-find-user/README.md delete mode 100644 actions/slack-find-user/action.yml delete mode 100644 actions/slack-find-user/find-user.sh delete mode 100644 actions/slack-find-user/find-user.test.sh delete mode 100644 actions/slack-find-user/package.json diff --git a/actions/slack-find-user/README.md b/actions/slack-find-user/README.md deleted file mode 100644 index f48c73d64..000000000 --- a/actions/slack-find-user/README.md +++ /dev/null @@ -1,47 +0,0 @@ -# Slack find user - -Resolves a Slack user ID from a GitHub login or an email. Pair it with -[slack-send-message](../slack-send-message/README.md) to DM someone. - -A GitHub login is mapped to an email through the organization SAML identity, -then through the verified domain emails. Both queries need org-level access, so -pass a `github-token` that has it (for example a classic PAT with `read:org` -that is authorized for SAML SSO). The default workflow token usually cannot -read them, and the step then reports the user as not found with a warning. - -A user that cannot be found does not fail the step. Check `found`. - -## Usage - -```yaml -- name: Find the PR author in Slack - id: user - uses: elementor/elementor-editor-github-actions/actions/slack-find-user@main - with: - slack-token: ${{ secrets.SLACK_BOT_TOKEN }} - github-token: ${{ secrets.ORG_LOOKUP_TOKEN }} - github-login: ${{ github.event.pull_request.user.login }} -``` - -## Inputs - -| Input | Required | Default | Notes | -| -------------- | -------- | ------------------------- | --------------------------------- | -| `slack-token` | yes | | Bot token with `users:read.email` | -| `github-login` | no | | Ignored when `email` is passed | -| `email` | no | | Skips the GitHub lookup | -| `github-org` | no | `github.repository_owner` | Organization for the email lookup | -| `github-token` | no | `github.token` | Token for the email lookup | - -One of `github-login` or `email` is required. - -## Outputs - -| Output | Notes | -| --------- | -------------------------------------------- | -| `found` | `true` or `false` | -| `user-id` | Slack user ID, empty when not found | -| `email` | Email used for the Slack lookup | -| `reason` | Why the user was not found, empty when found | - -Slack API errors other than `users_not_found` fail the step. diff --git a/actions/slack-find-user/action.yml b/actions/slack-find-user/action.yml deleted file mode 100644 index 67780af4c..000000000 --- a/actions/slack-find-user/action.yml +++ /dev/null @@ -1,55 +0,0 @@ -name: Slack Find User -description: >- - Resolves a Slack user ID from a GitHub login or an email. A login is mapped to an email through the - organization SAML identity or verified domain emails, which needs a token with org read access. - A user that is not found does not fail the step: check the "found" output. - -inputs: - slack-token: - description: Slack bot token with users:read.email - required: true - github-login: - description: GitHub login to look up. Ignored when email is passed - required: false - default: '' - email: - description: Email to look up in Slack. Skips the GitHub lookup - required: false - default: '' - github-org: - description: Organization used to read the SAML identity and verified domain emails of the login - required: false - default: ${{ github.repository_owner }} - github-token: - description: Token for the GitHub email lookup. The SAML identity query needs org-level access, so the default token usually cannot read it - required: false - default: ${{ github.token }} - -outputs: - found: - description: '"true" when a Slack user was found, otherwise "false"' - value: ${{ steps.find.outputs.found }} - user-id: - description: Slack user ID, empty when not found - value: ${{ steps.find.outputs.user-id }} - email: - description: Email used for the Slack lookup, empty when none was found - value: ${{ steps.find.outputs.email }} - reason: - description: Why the user was not found, empty when found - value: ${{ steps.find.outputs.reason }} - -runs: - using: composite - steps: - - name: Find Slack user - id: find - shell: bash - env: - SLACK_TOKEN: ${{ inputs.slack-token }} - GITHUB_LOGIN: ${{ inputs.github-login }} - EMAIL: ${{ inputs.email }} - GITHUB_ORG: ${{ inputs.github-org }} - GH_TOKEN: ${{ inputs.github-token }} - ACTION_PATH: ${{ github.action_path }} - run: bash "$ACTION_PATH/find-user.sh" diff --git a/actions/slack-find-user/find-user.sh b/actions/slack-find-user/find-user.sh deleted file mode 100644 index 4fb6cf0a8..000000000 --- a/actions/slack-find-user/find-user.sh +++ /dev/null @@ -1,103 +0,0 @@ -#!/usr/bin/env bash -# Resolves a Slack user ID from a GitHub login or from an email. A GitHub login -# is first mapped to an email through the organization SAML identity, then -# through the verified domain emails. A user that cannot be found is not an -# error: the outputs say so and the step succeeds with a warning. - -set -euo pipefail - -SLACK_API="${SLACK_API:-https://slack.com/api}" -GITHUB_LOGIN="${GITHUB_LOGIN:-}" -EMAIL="${EMAIL:-}" -GITHUB_ORG="${GITHUB_ORG:-}" - -fail() { - echo "::error::$1" - exit 1 -} - -output() { - echo "$1=$2" >> "${GITHUB_OUTPUT:-/dev/null}" -} - -not_found() { - echo "::warning::$1" - output found false - output user-id "" - output email "$EMAIL" - output reason "$1" - exit 0 -} - -lookup_email_from_login() { - local login="$1" org="$2" err found="" - err="$(mktemp)" - - set +e - # shellcheck disable=SC2016 - found="$(gh api graphql -f login="$login" -f org="$org" -f query=' - query ($login: String!, $org: String!) { - organization(login: $org) { - samlIdentityProvider { - externalIdentities(first: 1, login: $login) { - nodes { samlIdentity { nameId } } - } - } - } - } - ' --jq '.data.organization.samlIdentityProvider.externalIdentities.nodes[0].samlIdentity.nameId // empty' 2>> "$err")" - if [[ -z "$found" ]]; then - # shellcheck disable=SC2016 - found="$(gh api graphql -f login="$login" -f org="$org" -f query=' - query ($login: String!, $org: String!) { - user(login: $login) { - organizationVerifiedDomainEmails(login: $org) - } - } - ' --jq '.data.user.organizationVerifiedDomainEmails[0] // empty' 2>> "$err")" - fi - set -e - - if [[ -z "$found" && -s "$err" ]]; then - echo "::warning::GitHub email lookup returned errors: $(head -c 300 "$err" | tr '\n' ' ')" >&2 - fi - rm -f "$err" - printf '%s' "$found" -} - -if [[ -z "$EMAIL" ]]; then - [[ -n "$GITHUB_LOGIN" ]] || fail "Pass github-login or email." - [[ "$GITHUB_LOGIN" =~ ^[A-Za-z0-9][A-Za-z0-9-]*$ ]] || not_found "Invalid GitHub login." - [[ "$GITHUB_ORG" =~ ^[A-Za-z0-9][A-Za-z0-9-]*$ ]] || fail "github-org is required to look up an email from a login." - EMAIL="$(lookup_email_from_login "$GITHUB_LOGIN" "$GITHUB_ORG")" -fi - -[[ "$EMAIL" =~ ^[^@[:space:]]+@[^@[:space:]]+\.[^@[:space:]]+$ ]] || { - EMAIL="" - not_found "No email found for ${GITHUB_LOGIN:-the given user}." -} - -: "${SLACK_TOKEN:?slack-token is required}" - -response="$(curl -sS --fail-with-body --retry 3 -G \ - -H "Authorization: Bearer ${SLACK_TOKEN}" \ - --data-urlencode "email=${EMAIL}" \ - "${SLACK_API}/users.lookupByEmail")" || fail "Slack users.lookupByEmail request failed." - -ok="$(jq -r '.ok // empty' <<< "$response")" -user_id="$(jq -r '.user.id // empty' <<< "$response")" -error="$(jq -r '.error // empty' <<< "$response")" - -if [[ "$ok" == "true" && "$user_id" =~ ^[UW][A-Z0-9]+$ ]]; then - output found true - output user-id "$user_id" - output email "$EMAIL" - output reason "" - exit 0 -fi - -if [[ "$error" == "users_not_found" || -z "$error" ]]; then - not_found "No Slack user found for ${EMAIL}." -fi - -fail "Slack users.lookupByEmail failed: ${error}" diff --git a/actions/slack-find-user/find-user.test.sh b/actions/slack-find-user/find-user.test.sh deleted file mode 100644 index 36f188ed0..000000000 --- a/actions/slack-find-user/find-user.test.sh +++ /dev/null @@ -1,129 +0,0 @@ -#!/usr/bin/env bash -# Test suite for find-user.sh -# Run with: bash find-user.test.sh - -set -euo pipefail - -SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" -SCRIPT="${SCRIPT_DIR}/find-user.sh" -FAILURES=0 -TESTS_RUN=0 - -assert_equals() { - TESTS_RUN=$((TESTS_RUN + 1)) - if [ "$1" = "$2" ]; then - echo " ✓ $3" - else - echo " ✗ $3" - echo " Expected: $1" - echo " Got: $2" - FAILURES=$((FAILURES + 1)) - fi -} - -assert_contains() { - TESTS_RUN=$((TESTS_RUN + 1)) - if grep -qF -- "$2" <<< "$1"; then - echo " ✓ $3" - else - echo " ✗ $3" - echo " Missing: $2" - FAILURES=$((FAILURES + 1)) - fi -} - -assert_not_contains() { - TESTS_RUN=$((TESTS_RUN + 1)) - if grep -qF -- "$2" <<< "$1"; then - echo " ✗ $3" - echo " Unexpected: $2" - FAILURES=$((FAILURES + 1)) - else - echo " ✓ $3" - fi -} - -WORK="$(mktemp -d)" -trap 'rm -rf "$WORK"' EXIT - -mkdir -p "$WORK/bin" -cat > "$WORK/bin/gh" <<'STUB' -#!/usr/bin/env bash -echo "gh" >> "$STUB_LOG" -if [ -n "${STUB_GH_ERROR:-}" ]; then - echo "$STUB_GH_ERROR" >&2 - exit 1 -fi -printf '%s' "${STUB_GH_EMAIL:-}" -STUB -cat > "$WORK/bin/curl" <<'STUB' -#!/usr/bin/env bash -echo "curl $*" >> "$STUB_LOG" -response="${STUB_SLACK_RESPONSE:-}" -if [ -z "$response" ]; then - response='{"ok":false,"error":"users_not_found"}' -fi -echo "$response" -STUB -chmod +x "$WORK/bin/gh" "$WORK/bin/curl" -export STUB_LOG="$WORK/log" - -# Runs the script with the stubs. Prints the outputs, then "exit=". -run() { - : > "$STUB_LOG" - : > "$WORK/out" - set +e - env PATH="$WORK/bin:$PATH" SLACK_TOKEN=x GITHUB_ORG=elementor GITHUB_OUTPUT="$WORK/out" "$@" bash "$SCRIPT" > "$WORK/stdout" 2>&1 - local code=$? - set -e - cat "$WORK/out" - echo "exit=${code}" -} - -FOUND='{"ok":true,"user":{"id":"U123ABC"}}' - -echo "lookup by email" - -out="$(run EMAIL=dev@example.com STUB_SLACK_RESPONSE="$FOUND")" -assert_contains "$out" 'found=true' "finds a Slack user by email" -assert_contains "$out" 'user-id=U123ABC' "returns the Slack user ID" -assert_not_contains "$(cat "$STUB_LOG")" 'gh' "does not call GitHub when an email is passed" - -out="$(run EMAIL=dev@example.com)" -assert_contains "$out" 'found=false' "reports found=false for users_not_found" -assert_contains "$out" 'exit=0' "does not fail when the user is not found" - -out="$(run EMAIL=dev@example.com STUB_SLACK_RESPONSE='{"ok":false,"error":"invalid_auth"}')" -assert_contains "$out" 'exit=1' "fails on other Slack errors" - -echo "lookup by GitHub login" - -out="$(run GITHUB_LOGIN=octo-dev STUB_GH_EMAIL=dev@example.com STUB_SLACK_RESPONSE="$FOUND")" -assert_contains "$out" 'user-id=U123ABC' "maps a login to an email and finds the user" -assert_contains "$out" 'email=dev@example.com' "returns the email that was used" - -out="$(run GITHUB_LOGIN=octo-dev)" -assert_contains "$out" 'found=false' "reports found=false when no email is found" -assert_not_contains "$(cat "$STUB_LOG")" 'curl' "skips Slack when there is no email" - -out="$(run GITHUB_LOGIN=octo-dev STUB_GH_ERROR='Resource not accessible by integration')" -assert_contains "$out" 'exit=0' "does not fail when the GitHub lookup is denied" -assert_contains "$(cat "$WORK/stdout")" 'Resource not accessible' "warns with the GitHub error" - -echo "validation" - -out="$(run GITHUB_LOGIN='bad login; rm')" -assert_contains "$out" 'found=false' "rejects an invalid login" - -out="$(run)" -assert_contains "$out" 'exit=1' "fails when neither login nor email is passed" - -out="$(run GITHUB_LOGIN=octo-dev GITHUB_ORG=)" -assert_contains "$out" 'exit=1' "fails when a login is passed without an organization" - -echo "" -echo "Tests run: ${TESTS_RUN}, failures: ${FAILURES}" - -if [ "$FAILURES" -gt 0 ]; then - exit 1 -fi diff --git a/actions/slack-find-user/package.json b/actions/slack-find-user/package.json deleted file mode 100644 index 80a669b5d..000000000 --- a/actions/slack-find-user/package.json +++ /dev/null @@ -1,9 +0,0 @@ -{ - "name": "@elementor/slack-find-user", - "author": "Elementor Team", - "license": "GPL-3.0-or-later", - "private": true, - "scripts": { - "test": "bash find-user.test.sh" - } -} diff --git a/actions/slack-send-message/README.md b/actions/slack-send-message/README.md index 0c29e8154..e820a3243 100644 --- a/actions/slack-send-message/README.md +++ b/actions/slack-send-message/README.md @@ -4,43 +4,36 @@ Posts a message to a Slack channel, or to a DM when `channel` is a user ID. Untrusted text (for example content derived from a PR diff) can be escaped, truncated and wrapped in a code block so it cannot trigger mentions or links. -To DM someone, resolve the user ID first with -[slack-find-user](../slack-find-user/README.md). - ## Usage ```yaml -- name: Find the PR author in Slack - id: user - uses: elementor/elementor-editor-github-actions/actions/slack-find-user@main - with: - slack-token: ${{ secrets.SLACK_BOT_TOKEN }} - github-login: ${{ github.event.pull_request.user.login }} - - name: Send findings - if: steps.user.outputs.found == 'true' uses: elementor/elementor-editor-github-actions/actions/slack-send-message@main with: slack-token: ${{ secrets.SLACK_BOT_TOKEN }} - channel: ${{ steps.user.outputs.user-id }} + channel: ${{ steps.map_user.outputs.slack_user }} title: 'Checks found items on ${{ github.event.pull_request.html_url }}' text-file: findings.txt code-block: 'true' ``` +To DM someone, map their GitHub login to Slack first. The organization already has +the `map-github-to-slack` action in `elementor-tests-configuration` for that, and it +returns the `@username` this action accepts as `channel`. + ## Inputs -| Input | Required | Default | Notes | -| ------------- | -------- | ------- | ----------------------------------------------------- | -| `slack-token` | yes | | Bot token with `chat:write`, plus `im:write` for DMs | -| `channel` | yes | | Channel ID (`C…`, `G…`, `D…`) or user ID (`U…`, `W…`) | -| `text` | no | | Message body | -| `text-file` | no | | File with the body, takes precedence over `text` | -| `title` | no | | Trusted first line, posted as is | -| `code-block` | no | `false` | Wrap the body in a code block | -| `escape` | no | `true` | Escape `&`, `<`, `>` in the body | -| `max-length` | no | `3500` | Body is cut to this length and ends with an ellipsis | -| `dry-run` | no | `false` | Print the payload and do not post | +| Input | Required | Default | Notes | +| ------------- | -------- | ------- | ------------------------------------------------------------------ | +| `slack-token` | yes | | Bot token with `chat:write`, plus `im:write` for DMs | +| `channel` | yes | | Channel ID (`C…`, `G…`, `D…`), user ID (`U…`, `W…`) or `@username` | +| `text` | no | | Message body | +| `text-file` | no | | File with the body, takes precedence over `text` | +| `title` | no | | Trusted first line, posted as is | +| `code-block` | no | `false` | Wrap the body in a code block | +| `escape` | no | `true` | Escape `&`, `<`, `>` in the body | +| `max-length` | no | `3500` | Body is cut to this length and ends with an ellipsis | +| `dry-run` | no | `false` | Print the payload and do not post | One of `text` or `text-file` is required. diff --git a/actions/slack-send-message/action.yml b/actions/slack-send-message/action.yml index 8d6e54ea3..94e339d01 100644 --- a/actions/slack-send-message/action.yml +++ b/actions/slack-send-message/action.yml @@ -2,14 +2,13 @@ name: Slack Send Message description: >- Posts a message to a Slack channel, or to a DM when the target is a user ID. Text can be escaped, truncated and wrapped in a code block, so untrusted content cannot trigger mentions or links. - Resolve a user ID from a GitHub login or an email with the slack-find-user action. inputs: slack-token: description: Slack bot token with chat:write (and im:write to open DMs) required: true channel: - description: Slack channel ID (C…, G…, D…) or user ID (U…, W…). A user ID opens a DM first + description: Slack channel ID (C…, G…, D…), user ID (U…, W…) or @username. A user ID opens a DM first; an @username is passed to Slack as is required: true text: description: Message body. Use text-file for longer content diff --git a/actions/slack-send-message/send-message.sh b/actions/slack-send-message/send-message.sh index 5c25e15cc..a36234db1 100644 --- a/actions/slack-send-message/send-message.sh +++ b/actions/slack-send-message/send-message.sh @@ -1,6 +1,6 @@ #!/usr/bin/env bash # Posts a message to a Slack channel or, when the target is a user ID, to a DM -# with that user. Untrusted text can be escaped, truncated and wrapped in a code +# with that user. An @username is passed to Slack as is. Untrusted text can be escaped, truncated and wrapped in a code # block so that it cannot trigger mentions or links. set -euo pipefail @@ -24,7 +24,7 @@ output() { echo "$1=$2" >> "${GITHUB_OUTPUT:-/dev/null}" } -[[ "$CHANNEL" =~ ^[CGDUW][A-Z0-9]+$ ]] || fail "channel must be a Slack channel or user ID." +[[ "$CHANNEL" =~ ^([CGDUW][A-Z0-9]+|@[A-Za-z0-9._-]+)$ ]] || fail "channel must be a Slack channel ID, user ID or @username." [[ "$CODE_BLOCK" == "true" || "$CODE_BLOCK" == "false" ]] || fail "code-block must be true or false." [[ "$ESCAPE" == "true" || "$ESCAPE" == "false" ]] || fail "escape must be true or false." [[ "$DRY_RUN" == "true" || "$DRY_RUN" == "false" ]] || fail "dry-run must be true or false." diff --git a/actions/slack-send-message/send-message.test.sh b/actions/slack-send-message/send-message.test.sh index d2fe746f2..4a994d4b3 100644 --- a/actions/slack-send-message/send-message.test.sh +++ b/actions/slack-send-message/send-message.test.sh @@ -77,7 +77,7 @@ assert_equals 'from a file' "$out" "text-file takes precedence over text" echo "validation" -for bad in 'CHANNEL=general' 'CODE_BLOCK=yes' 'ESCAPE=1' 'MAX_LENGTH=abc' 'TEXT='; do +for bad in 'CHANNEL=general' 'CHANNEL=@bad name' 'CHANNEL=@' 'CODE_BLOCK=yes' 'ESCAPE=1' 'MAX_LENGTH=abc' 'TEXT='; do set +e env DRY_RUN=true CHANNEL=C123ABC TEXT=x "$bad" bash "$SCRIPT" > /dev/null 2>&1 code=$? @@ -123,6 +123,12 @@ log="$(cat "$STUB_LOG")" assert_contains "$log" 'conversations.open {"users":"U123ABC"}' "opens a DM for a user ID" assert_contains "$log" 'chat.postMessage {"channel":"D999DM","text":"hello"}' "posts to the DM channel" +: > "$STUB_LOG" +PATH="$WORK/bin:$PATH" SLACK_TOKEN=x CHANNEL=@octo.dev TEXT=hello bash "$SCRIPT" +log="$(cat "$STUB_LOG")" +assert_contains "$log" 'chat.postMessage {"channel":"@octo.dev","text":"hello"}' "posts to an @username as is" +assert_not_contains "$log" 'conversations.open' "does not open a DM for an @username" + set +e STUB_POST_RESPONSE='{"ok":false,"error":"channel_not_found"}' PATH="$WORK/bin:$PATH" SLACK_TOKEN=x CHANNEL=C123ABC TEXT=hello bash "$SCRIPT" > "$WORK/err" 2>&1 code=$? diff --git a/package-lock.json b/package-lock.json index 02f67bc57..453970dfc 100644 --- a/package-lock.json +++ b/package-lock.json @@ -121,6 +121,7 @@ }, "actions/slack-find-user": { "name": "@elementor/slack-find-user", + "extraneous": true, "license": "GPL-3.0-or-later" }, "actions/slack-send-message": { @@ -775,10 +776,6 @@ "resolved": "actions/setup-wp-env", "link": true }, - "node_modules/@elementor/slack-find-user": { - "resolved": "actions/slack-find-user", - "link": true - }, "node_modules/@elementor/slack-send-message": { "resolved": "actions/slack-send-message", "link": true