Skip to content

Gardener - Notify Slack #1595

Gardener - Notify Slack

Gardener - Notify Slack #1595

name: Gardener - Notify Slack
# Runs after `Gardener - Notify Event` completes. Posts a linked title to Slack,
# and for Dependabot-opened PRs also applies the `devtools-gardener` label
# (so Dependabot PRs show up in the gardener flow without a human having
# to label them).
#
# The workflow_run trigger runs this job in the default-branch context with
# full GITHUB_TOKEN permissions and Actions secret access — this is what
# lets it succeed for Dependabot-opened PRs, where the upstream event
# workflow can't label or reach secrets directly. Labels applied here via
# GITHUB_TOKEN do not trigger another `labeled` run (loop prevention), so
# Slack only gets one post per PR.
on:
workflow_run:
workflows: ['Gardener - Notify Event']
types: [completed]
permissions:
contents: read
issues: write
pull-requests: write
actions: read
jobs:
notify:
# `conclusion == success` also covers runs where the capture job was
# skipped by its `if` gate (no matching label, etc.) — in that case
# no artifact was uploaded, so the download step below no-ops.
if: github.event.workflow_run.conclusion == 'success'
runs-on: ubuntu-latest
steps:
- name: Download event payload
id: download
continue-on-error: true
uses: actions/download-artifact@v4
with:
name: gardener-event
run-id: ${{ github.event.workflow_run.id }}
github-token: ${{ secrets.GITHUB_TOKEN }}
- name: Add devtools-gardener label to Dependabot PR
if: steps.download.outcome == 'success'
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GH_REPO: ${{ github.repository }}
run: |
ACTION=$(jq -r '.action' event.json)
LOGIN=$(jq -r '.pull_request.user.login // ""' event.json)
# Only the Dependabot-opened path needs labeling here. On manual
# `labeled` events the label is already on the PR/issue.
if [ "$ACTION" != "opened" ] || [ "$LOGIN" != "dependabot[bot]" ]; then
exit 0
fi
NUMBER=$(jq -r '.pull_request.number' event.json)
gh pr edit "$NUMBER" --add-label devtools-gardener
- name: Post to Slack
if: steps.download.outcome == 'success'
continue-on-error: true
env:
SLACK_BOT_TOKEN: ${{ secrets.SLACK_GARDENER_BOT_TOKEN }}
SLACK_CHANNEL_ID: ${{ vars.GARDENER_SLACK_CHANNEL_ID }}
run: |
jq \
--arg channel "$SLACK_CHANNEL_ID" \
'
def escape: gsub("&";"&amp;") | gsub("<";"&lt;") | gsub(">";"&gt;");
(.issue // .pull_request) as $i
| { channel: $channel,
text: "<\($i.html_url)|\($i.title | escape)>",
unfurl_links: false,
unfurl_media: false }
' event.json | curl -sf -X POST \
-H "Authorization: Bearer $SLACK_BOT_TOKEN" \
-H 'Content-type: application/json; charset=utf-8' \
-d @- https://slack.com/api/chat.postMessage