Skip to content

Commit e7c2f6a

Browse files
authored
[ci] Enabled CI failure bot
1 parent 39b85dc commit e7c2f6a

1 file changed

Lines changed: 87 additions & 0 deletions

File tree

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
name: CI Failure Bot
2+
3+
on:
4+
workflow_run:
5+
workflows: ["Netjsonconfig CI Build"]
6+
types:
7+
- completed
8+
9+
permissions:
10+
pull-requests: read
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' && github.event.workflow_run.event == 'pull_request' }}
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 // empty' 2>/dev/null || echo "")
38+
if [ -z "$pr_author" ] || [ "$pr_author" = "null" ]; 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+
permissions:
72+
pull-requests: write
73+
actions: write
74+
contents: read
75+
uses: openwisp/openwisp-utils/.github/workflows/reusable-bot-ci-failure.yml@master
76+
with:
77+
pr_number: ${{ needs.find-pr.outputs.pr_number }}
78+
head_sha: ${{ github.event.workflow_run.head_sha }}
79+
head_repo: ${{ github.event.workflow_run.head_repository.full_name }}
80+
base_repo: ${{ github.repository }}
81+
run_id: ${{ github.event.workflow_run.id }}
82+
pr_author: ${{ needs.find-pr.outputs.pr_author }}
83+
actor: ${{ github.event.workflow_run.actor.login }}
84+
secrets:
85+
GEMINI_API_KEY: ${{ secrets.GEMINI_API_KEY }}
86+
APP_ID: ${{ secrets.OPENWISP_BOT_APP_ID }}
87+
PRIVATE_KEY: ${{ secrets.OPENWISP_BOT_PRIVATE_KEY }}

0 commit comments

Comments
 (0)