Skip to content

Commit 10a1e11

Browse files
authored
[ci] Enabled CI failure bot
1 parent a102dda commit 10a1e11

File tree

1 file changed

+83
-0
lines changed

1 file changed

+83
-0
lines changed
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
name: CI Failure Bot
2+
3+
on:
4+
workflow_run:
5+
workflows: ["OpenWISP Monitoring CI Build"]
6+
types:
7+
- completed
8+
9+
permissions:
10+
pull-requests: write
11+
actions: read
12+
contents: read
13+
14+
concurrency:
15+
group: ci-failure-${{ github.repository }}-${{ github.event.workflow_run.pull_requests[0].number || github.event.workflow_run.head_branch }}
16+
cancel-in-progress: true
17+
18+
jobs:
19+
find-pr:
20+
runs-on: ubuntu-latest
21+
if: ${{ github.event.workflow_run.conclusion == 'failure' }}
22+
outputs:
23+
pr_number: ${{ steps.pr.outputs.number }}
24+
pr_author: ${{ steps.pr.outputs.author }}
25+
steps:
26+
- name: Find PR Number
27+
id: pr
28+
env:
29+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
30+
REPO: ${{ github.repository }}
31+
PR_NUMBER_PAYLOAD: ${{ github.event.workflow_run.pull_requests[0].number }}
32+
EVENT_HEAD_SHA: ${{ github.event.workflow_run.head_sha }}
33+
run: |
34+
emit_pr() {
35+
local pr_number="$1"
36+
local pr_author
37+
pr_author=$(gh pr view "$pr_number" --repo "$REPO" --json author --jq '.author.login' 2>/dev/null || echo "")
38+
if [ -z "$pr_author" ]; then
39+
echo "::warning::Could not fetch PR author for PR #$pr_number"
40+
fi
41+
echo "number=$pr_number" >> "$GITHUB_OUTPUT"
42+
echo "author=$pr_author" >> "$GITHUB_OUTPUT"
43+
}
44+
PR_NUMBER="$PR_NUMBER_PAYLOAD"
45+
if [ -n "$PR_NUMBER" ]; then
46+
echo "Found PR #$PR_NUMBER from workflow payload."
47+
emit_pr "$PR_NUMBER"
48+
exit 0
49+
fi
50+
HEAD_SHA="$EVENT_HEAD_SHA"
51+
echo "Payload empty. Searching for PR via Commits API..."
52+
PR_NUMBER=$(gh api repos/$REPO/commits/$HEAD_SHA/pulls -q '.[0].number' 2>/dev/null || true)
53+
if [ -n "$PR_NUMBER" ] && [ "$PR_NUMBER" != "null" ]; then
54+
echo "Found PR #$PR_NUMBER using Commits API."
55+
emit_pr "$PR_NUMBER"
56+
exit 0
57+
fi
58+
echo "API lookup failed/empty. Scanning open PRs for matching head SHA..."
59+
PR_NUMBER=$(gh pr list --repo "$REPO" --state open --limit 100 --json number,headRefOid --jq ".[] | select(.headRefOid == \"$HEAD_SHA\") | .number" | head -n 1)
60+
if [ -n "$PR_NUMBER" ]; then
61+
echo "Found PR #$PR_NUMBER by scanning open PRs."
62+
emit_pr "$PR_NUMBER"
63+
exit 0
64+
fi
65+
echo "::warning::No open PR found. This workflow run might not be attached to an open PR."
66+
exit 0
67+
68+
call-ci-failure-bot:
69+
needs: find-pr
70+
if: ${{ needs.find-pr.outputs.pr_number != '' }}
71+
uses: openwisp/openwisp-utils/.github/workflows/reusable-bot-ci-failure.yml@master
72+
with:
73+
pr_number: ${{ needs.find-pr.outputs.pr_number }}
74+
head_sha: ${{ github.event.workflow_run.head_sha }}
75+
head_repo: ${{ github.event.workflow_run.head_repository.full_name }}
76+
base_repo: ${{ github.repository }}
77+
run_id: ${{ github.event.workflow_run.id }}
78+
pr_author: ${{ needs.find-pr.outputs.pr_author }}
79+
actor: ${{ github.event.workflow_run.actor.login }}
80+
secrets:
81+
GEMINI_API_KEY: ${{ secrets.GEMINI_API_KEY }}
82+
APP_ID: ${{ secrets.OPENWISP_BOT_APP_ID }}
83+
PRIVATE_KEY: ${{ secrets.OPENWISP_BOT_PRIVATE_KEY }}

0 commit comments

Comments
 (0)