Skip to content

Commit 180c4be

Browse files
committed
feat: add timestamp to CI failure issues and implement automatic closure upon recovery
1 parent 59fcd4d commit 180c4be

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
@@ -24,6 +24,8 @@ jobs:
2424
ACTOR: ${{ github.event.workflow_run.actor.login }}
2525
RUN_URL: ${{ github.event.workflow_run.html_url }}
2626
run: |
27+
reported_at="$(TZ=Asia/Shanghai date '+%Y-%m-%d %H:%M:%S %z')"
28+
issue_title_prefix="[CI failed] $REPOSITORY / $WORKFLOW_NAME at "
2729
body_file="$(mktemp)"
2830
cat > "$body_file" <<EOF
2931
@sunnylqm CI run failed.
@@ -34,10 +36,59 @@ jobs:
3436
Branch: $HEAD_BRANCH
3537
Commit: $HEAD_SHA
3638
Actor: $ACTOR
39+
Reported at: $reported_at
3740
Run: $RUN_URL
3841
EOF
3942
4043
gh issue create \
4144
--repo "$REPOSITORY" \
42-
--title "[CI failed] $REPOSITORY / $WORKFLOW_NAME" \
45+
--title "$issue_title_prefix$reported_at" \
4346
--body-file "$body_file"
47+
48+
close:
49+
if: github.event.workflow_run.conclusion == 'success'
50+
permissions:
51+
issues: write
52+
runs-on: ubuntu-latest
53+
steps:
54+
- name: Close recovered failure issues
55+
env:
56+
GH_TOKEN: ${{ github.token }}
57+
REPOSITORY: ${{ github.repository }}
58+
WORKFLOW_NAME: ${{ github.event.workflow_run.name }}
59+
HEAD_BRANCH: ${{ github.event.workflow_run.head_branch }}
60+
HEAD_SHA: ${{ github.event.workflow_run.head_sha }}
61+
RUN_URL: ${{ github.event.workflow_run.html_url }}
62+
run: |
63+
issue_title_prefix="[CI failed] $REPOSITORY / $WORKFLOW_NAME at "
64+
fixed_at="$(TZ=Asia/Shanghai date '+%Y-%m-%d %H:%M:%S %z')"
65+
issue_numbers="$(
66+
gh issue list \
67+
--repo "$REPOSITORY" \
68+
--state open \
69+
--limit 1000 \
70+
--json number,title |
71+
jq -r --arg prefix "$issue_title_prefix" '.[] | select(.title | startswith($prefix)) | .number'
72+
)"
73+
74+
if [ -z "$issue_numbers" ]; then
75+
exit 0
76+
fi
77+
78+
comment_body="$(cat <<EOF
79+
CI recovered at $fixed_at.
80+
81+
Repository: $REPOSITORY
82+
Workflow: $WORKFLOW_NAME
83+
Branch: $HEAD_BRANCH
84+
Commit: $HEAD_SHA
85+
Run: $RUN_URL
86+
EOF
87+
)"
88+
89+
while IFS= read -r issue_number; do
90+
[ -n "$issue_number" ] || continue
91+
gh issue close "$issue_number" \
92+
--repo "$REPOSITORY" \
93+
--comment "$comment_body"
94+
done <<< "$issue_numbers"

0 commit comments

Comments
 (0)