Skip to content

Commit 2e8ff49

Browse files
authored
ci: auto-comment the pr-XXX image tag on PRs and linked issues (#5013)
1 parent 5b8ce5f commit 2e8ff49

1 file changed

Lines changed: 76 additions & 0 deletions

File tree

.github/workflows/dev.yml

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ on:
33
branches:
44
- master
55
pull_request:
6+
types: [opened, reopened, synchronize, ready_for_review]
67
branches:
78
- master
89
name: Push container
@@ -82,3 +83,78 @@ jobs:
8283
labels: ${{ steps.meta-alpine.outputs.labels }}
8384
# No cache-to. See the note in deploy.yml.
8485
cache-from: type=gha
86+
87+
comment:
88+
name: Comment image tag
89+
needs: buildx
90+
if: ${{ github.event_name == 'pull_request' }}
91+
runs-on: ubuntu-latest
92+
permissions:
93+
contents: read
94+
pull-requests: write
95+
issues: write
96+
steps:
97+
- name: Post pr-${{ github.event.pull_request.number }} image tag
98+
env:
99+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
100+
REPO: ${{ github.repository }}
101+
PR: ${{ github.event.pull_request.number }}
102+
HEAD_SHA: ${{ github.event.pull_request.head.sha }}
103+
IS_DRAFT: ${{ github.event.pull_request.draft }}
104+
run: |
105+
set -euo pipefail
106+
107+
short_sha="${HEAD_SHA:0:7}"
108+
pr_marker="<!-- dozzle-pr-image -->"
109+
issue_marker="<!-- dozzle-pr-image:${PR} -->"
110+
111+
run_cmd="docker run -v /var/run/docker.sock:/var/run/docker.sock -p 8080:8080 amir20/dozzle:pr-${PR}"
112+
113+
pr_body=$(printf '%s\n%s\n\n```\n%s\n```\n\n%s\n' \
114+
"$pr_marker" \
115+
"Try this PR without waiting for a release:" \
116+
"$run_cmd" \
117+
"Also on \`ghcr.io/amir20/dozzle:pr-${PR}\` and as \`amir20/dozzle:pr-${PR}-alpine\`. Rebuilt on every push, currently \`${short_sha}\`.")
118+
119+
# Sticky comment on the PR itself.
120+
existing=$(gh api "repos/${REPO}/issues/${PR}/comments" --paginate \
121+
--jq ".[] | select(.body != null and (.body | contains(\"${pr_marker}\"))) | .id" | head -1)
122+
if [ -n "$existing" ]; then
123+
gh api -X PATCH "repos/${REPO}/issues/comments/${existing}" -f body="$pr_body" > /dev/null
124+
else
125+
gh api -X POST "repos/${REPO}/issues/${PR}/comments" -f body="$pr_body" > /dev/null
126+
fi
127+
128+
if [ "$IS_DRAFT" = "true" ]; then
129+
echo "Draft PR, skipping linked issues."
130+
exit 0
131+
fi
132+
133+
# One-time comment on every issue this PR closes.
134+
linked=$(gh api "repos/${REPO}/pulls/${PR}" --jq '.body // ""' \
135+
| grep -oiE '(close[sd]?|fix(e[sd])?|resolve[sd]?)[[:space:]]*:?[[:space:]]+#[0-9]+' \
136+
| grep -oE '[0-9]+' | sort -u || true)
137+
138+
for issue in $linked; do
139+
if [ "$issue" = "$PR" ]; then
140+
continue
141+
fi
142+
kind=$(gh api "repos/${REPO}/issues/${issue}" --jq 'if .pull_request then "pr" else "issue" end' 2>/dev/null || echo "missing")
143+
if [ "$kind" != "issue" ]; then
144+
echo "#${issue} is not an issue, skipping."
145+
continue
146+
fi
147+
already=$(gh api "repos/${REPO}/issues/${issue}/comments" --paginate \
148+
--jq ".[] | select(.body != null and (.body | contains(\"${issue_marker}\"))) | .id" | head -1)
149+
if [ -n "$already" ]; then
150+
echo "#${issue} already has a pr-${PR} comment, skipping."
151+
continue
152+
fi
153+
issue_body=$(printf '%s\n%s\n\n```\n%s\n```\n\n%s\n' \
154+
"$issue_marker" \
155+
"A fix for this is in #${PR}. CI builds an image per PR, so you can test it before the next release:" \
156+
"$run_cmd" \
157+
"Report back on #${PR} if it still happens.")
158+
gh api -X POST "repos/${REPO}/issues/${issue}/comments" -f body="$issue_body" > /dev/null
159+
echo "Commented on #${issue}."
160+
done

0 commit comments

Comments
 (0)