Skip to content
Merged
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
106 changes: 106 additions & 0 deletions .github/workflows/openbot-triage.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
name: OpenBot triage

# Tells OpenBot that an issue or a pull request was opened, so it can triage it.
# Same file in the four Dictus repositories.
#
# Security:
# - pull_request_target runs in the base repository's context with access to
# secrets, so this job never checks out and never runs code from the PR.
# - The GITHUB_TOKEN gets no permission at all; it is not used.
# - Every event value reaches the script through env, never through an
# expression inside run, so a crafted login or URL cannot inject shell.
# - The body carries no title and no body text, only identifiers.
# - The request is signed with an HMAC-SHA256 over the exact bytes sent.
# Neither the secret nor the signature is ever printed.
#
# Needs the OPENBOT_TRIAGE_WEBHOOK_SECRET organisation secret. Without it (a
# fork, or the secret removed) the job sends nothing and passes.

on:
issues:
types: [opened]
pull_request_target:
types: [opened]

permissions: {}

jobs:
notify:
runs-on: ubuntu-latest
timeout-minutes: 5
steps:
- name: Notify OpenBot
env:
EVENT_NAME: ${{ github.event_name }}
REPOSITORY: ${{ github.repository }}
NUMBER: ${{ github.event.issue.number || github.event.pull_request.number }}
AUTHOR: ${{ github.event.issue.user.login || github.event.pull_request.user.login }}
HTML_URL: ${{ github.event.issue.html_url || github.event.pull_request.html_url }}
RUN_ID: ${{ github.run_id }}
RUN_ATTEMPT: ${{ github.run_attempt }}
WEBHOOK_SECRET: ${{ secrets.OPENBOT_TRIAGE_WEBHOOK_SECRET }}
WEBHOOK_URL: https://signal.openbot.run/v1/webhooks/bbb9e817-2148-4971-8333-2fc76aa3a32a
run: |
set -euo pipefail

if [ -z "${WEBHOOK_SECRET}" ]; then
echo "OPENBOT_TRIAGE_WEBHOOK_SECRET is not set; nothing sent."
exit 0
fi

case "${EVENT_NAME}" in
issues) kind="issue" ;;
pull_request_target) kind="pull_request" ;;
*) echo "Unexpected event: ${EVENT_NAME}"; exit 1 ;;
esac

if ! [[ "${NUMBER}" =~ ^[0-9]+$ ]]; then
echo "Unexpected issue or PR number."
exit 1
fi

body_file="${RUNNER_TEMP}/openbot-triage-body.json"
jq -n -c -j \
--arg type "github.${kind}.opened" \
--arg occurredAt "$(date -u +%Y-%m-%dT%H:%M:%SZ)" \
--arg repository "${REPOSITORY}" \
--arg kind "${kind}" \
--argjson number "${NUMBER}" \
--arg author "${AUTHOR}" \
--arg url "${HTML_URL}" \
'{type: $type, occurredAt: $occurredAt, data: {repository: $repository, kind: $kind, number: $number, author: $author, url: $url}}' \
> "${body_file}"

delivery_id="gh-${RUN_ID}-${RUN_ATTEMPT}"

# Up to 3 retries, 30 s apart, only on 503. Each attempt signs again
# with a fresh timestamp, over the same body file it sends.
for attempt in 1 2 3 4; do
timestamp="$(date +%s)"
signature="sha256=$( { printf '%s.%s.' "${timestamp}" "${delivery_id}"; cat "${body_file}"; } \
| openssl dgst -sha256 -hmac "${WEBHOOK_SECRET}" -hex | awk '{print $NF}')"

status="$(curl --silent --output /dev/null --write-out '%{http_code}' \
--max-time 30 \
-X POST "${WEBHOOK_URL}" \
-H "Content-Type: application/json" \
-H "x-openbot-timestamp: ${timestamp}" \
-H "x-openbot-delivery-id: ${delivery_id}" \
-H "x-openbot-signature: ${signature}" \
--data-binary "@${body_file}" || true)"

echo "Attempt ${attempt}: HTTP ${status}"

case "${status}" in
200|202) exit 0 ;;
503)
if [ "${attempt}" -lt 4 ]; then
sleep 30
fi
;;
*) exit 1 ;;
esac
done

echo "OpenBot still returned 503 after 3 retries."
exit 1
Loading