Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions actions/set-commit-status/README.md
Original file line number Diff line number Diff line change
@@ -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 |
46 changes: 46 additions & 0 deletions actions/set-commit-status/action.yml
Original file line number Diff line number Diff line change
@@ -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"
9 changes: 9 additions & 0 deletions actions/set-commit-status/package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
29 changes: 29 additions & 0 deletions actions/set-commit-status/set-commit-status.sh
Original file line number Diff line number Diff line change
@@ -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
100 changes: 100 additions & 0 deletions actions/set-commit-status/set-commit-status.test.sh
Original file line number Diff line number Diff line change
@@ -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=<code>".
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
45 changes: 45 additions & 0 deletions actions/slack-send-message/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# 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.

## Usage

```yaml
- name: Send findings
uses: elementor/elementor-editor-github-actions/actions/slack-send-message@main
with:
slack-token: ${{ secrets.SLACK_BOT_TOKEN }}
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…`), 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.

## 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.
67 changes: 67 additions & 0 deletions actions/slack-send-message/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
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.

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…), 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
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"
9 changes: 9 additions & 0 deletions actions/slack-send-message/package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
Loading
Loading