Skip to content

Commit c4ad352

Browse files
committed
feat: add timestamp to CI failure issues and auto-close them upon workflow success
1 parent 5d730b6 commit c4ad352

1 file changed

Lines changed: 52 additions & 1 deletion

File tree

.github/workflows/ci-failure-email.yml

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ jobs:
2828
ACTOR: ${{ github.event.workflow_run.actor.login }}
2929
RUN_URL: ${{ github.event.workflow_run.html_url }}
3030
run: |
31+
reported_at="$(TZ=Asia/Shanghai date '+%Y-%m-%d %H:%M:%S %z')"
32+
issue_title_prefix="[CI failed] $REPOSITORY / $WORKFLOW_NAME at "
3133
body_file="$(mktemp)"
3234
cat > "$body_file" <<EOF
3335
@sunnylqm CI run failed.
@@ -38,10 +40,59 @@ jobs:
3840
Branch: $HEAD_BRANCH
3941
Commit: $HEAD_SHA
4042
Actor: $ACTOR
43+
Reported at: $reported_at
4144
Run: $RUN_URL
4245
EOF
4346
4447
gh issue create \
4548
--repo "$REPOSITORY" \
46-
--title "[CI failed] $REPOSITORY / $WORKFLOW_NAME" \
49+
--title "$issue_title_prefix$reported_at" \
4750
--body-file "$body_file"
51+
52+
close:
53+
if: github.event.workflow_run.conclusion == 'success'
54+
permissions:
55+
issues: write
56+
runs-on: ubuntu-latest
57+
steps:
58+
- name: Close recovered failure issues
59+
env:
60+
GH_TOKEN: ${{ github.token }}
61+
REPOSITORY: ${{ github.repository }}
62+
WORKFLOW_NAME: ${{ github.event.workflow_run.name }}
63+
HEAD_BRANCH: ${{ github.event.workflow_run.head_branch }}
64+
HEAD_SHA: ${{ github.event.workflow_run.head_sha }}
65+
RUN_URL: ${{ github.event.workflow_run.html_url }}
66+
run: |
67+
issue_title_prefix="[CI failed] $REPOSITORY / $WORKFLOW_NAME at "
68+
fixed_at="$(TZ=Asia/Shanghai date '+%Y-%m-%d %H:%M:%S %z')"
69+
issue_numbers="$(
70+
gh issue list \
71+
--repo "$REPOSITORY" \
72+
--state open \
73+
--limit 1000 \
74+
--json number,title |
75+
jq -r --arg prefix "$issue_title_prefix" '.[] | select(.title | startswith($prefix)) | .number'
76+
)"
77+
78+
if [ -z "$issue_numbers" ]; then
79+
exit 0
80+
fi
81+
82+
comment_body="$(cat <<EOF
83+
CI recovered at $fixed_at.
84+
85+
Repository: $REPOSITORY
86+
Workflow: $WORKFLOW_NAME
87+
Branch: $HEAD_BRANCH
88+
Commit: $HEAD_SHA
89+
Run: $RUN_URL
90+
EOF
91+
)"
92+
93+
while IFS= read -r issue_number; do
94+
[ -n "$issue_number" ] || continue
95+
gh issue close "$issue_number" \
96+
--repo "$REPOSITORY" \
97+
--comment "$comment_body"
98+
done <<< "$issue_numbers"

0 commit comments

Comments
 (0)