Repository navigation
135 lines (121 loc) · 5.29 KB
/
Copy pathchatops.yml
File metadata and controls
135 lines (121 loc) · 5.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
name: ChatOps
on:
issue_comment:
types: [created]
permissions:
contents: read
pull-requests: write
issues: write
actions: write
jobs:
dispatch:
name: Parse comment command
if: github.event.issue.pull_request
runs-on: ubuntu-latest
steps:
- name: Verify comment author
id: auth
env:
ASSOC: ${{ github.event.comment.author_association }}
run: |
case "$ASSOC" in
OWNER|MEMBER)
echo "trusted=true" >> "$GITHUB_OUTPUT"
;;
*)
echo "trusted=false" >> "$GITHUB_OUTPUT"
;;
esac
- name: Parse command
id: cmd
env:
BODY: ${{ github.event.comment.body }}
run: |
command=""
if printf '%s' "$BODY" | grep -qE '(^|[[:space:]])/retest([[:space:]]|$)'; then
command="retest"
elif printf '%s' "$BODY" | grep -qE '(^|[[:space:]])/test([[:space:]]|$)'; then
command="test"
fi
echo "command=$command" >> "$GITHUB_OUTPUT"
- name: Ignore unrecognized commands
if: steps.cmd.outputs.command == ''
run: echo "No ChatOps command found; skipping."
- name: Reject untrusted users
if: steps.cmd.outputs.command != '' && steps.auth.outputs.trusted != 'true'
env:
GH_TOKEN: ${{ github.token }}
REPO: ${{ github.repository }}
ISSUE: ${{ github.event.issue.number }}
COMMENT_ID: ${{ github.event.comment.id }}
run: |
gh api -X POST "repos/$REPO/issues/comments/$COMMENT_ID/reactions" \
-f content="-1" >/dev/null || true
gh api -X POST "repos/$REPO/issues/$ISSUE/comments" \
-f body="⛔ Only repository members can run \`${{ steps.cmd.outputs.command }}\`." \
>/dev/null
exit 1
- name: Load pull request metadata
if: steps.cmd.outputs.command != '' && steps.auth.outputs.trusted == 'true'
id: pr
env:
GH_TOKEN: ${{ github.token }}
REPO: ${{ github.repository }}
ISSUE: ${{ github.event.issue.number }}
run: |
json=$(gh api "repos/$REPO/pulls/$ISSUE")
head_sha=$(echo "$json" | jq -r '.head.sha')
head_ref=$(echo "$json" | jq -r '.head.ref')
echo "head_sha=$head_sha" >> "$GITHUB_OUTPUT"
echo "head_ref=$head_ref" >> "$GITHUB_OUTPUT"
- name: React to command
if: steps.cmd.outputs.command != '' && steps.auth.outputs.trusted == 'true'
env:
GH_TOKEN: ${{ github.token }}
REPO: ${{ github.repository }}
COMMENT_ID: ${{ github.event.comment.id }}
run: |
gh api -X POST "repos/$REPO/issues/comments/$COMMENT_ID/reactions" \
-f content=eyes >/dev/null || true
- name: Handle /retest and /test
if: (steps.cmd.outputs.command == 'retest' || steps.cmd.outputs.command == 'test') && steps.auth.outputs.trusted == 'true'
env:
GH_TOKEN: ${{ github.token }}
REPO: ${{ github.repository }}
ISSUE: ${{ github.event.issue.number }}
HEAD_SHA: ${{ steps.pr.outputs.head_sha }}
HEAD_REF: ${{ steps.pr.outputs.head_ref }}
COMMAND: ${{ steps.cmd.outputs.command }}
run: |
set -euo pipefail
run_id=$(gh api "repos/$REPO/actions/workflows/ci_test.yml/runs?head_sha=$HEAD_SHA&event=workflow_dispatch&per_page=5" \
| jq -r '.workflow_runs[0].id // empty')
if [ -n "$run_id" ] && [ "$run_id" != "null" ]; then
gh api -X POST "repos/$REPO/actions/runs/$run_id/rerun" >/dev/null
gh api -X POST "repos/$REPO/issues/$ISSUE/comments" \
-f body="🔁 Re-running CI workflow run [$run_id](https://github.com/$REPO/actions/runs/$run_id) for commit \`${HEAD_SHA:0:7}\` (triggered by \`/$COMMAND\`)." \
>/dev/null
else
gh workflow run ci_test.yml --ref "$HEAD_REF" -R "$REPO" -f infinicore_branch=main
sleep 8
run_id=$(gh api "repos/$REPO/actions/workflows/ci_test.yml/runs?head_sha=$HEAD_SHA&event=workflow_dispatch&per_page=3" \
| jq -r '.workflow_runs[0].id // empty')
if [ -n "$run_id" ] && [ "$run_id" != "null" ]; then
gh api -X POST "repos/$REPO/issues/$ISSUE/comments" \
-f body="✅ Started CI workflow run [$run_id](https://github.com/$REPO/actions/runs/$run_id) for commit \`${HEAD_SHA:0:7}\` on branch \`$HEAD_REF\` (triggered by \`/$COMMAND\`)." \
>/dev/null
else
gh api -X POST "repos/$REPO/issues/$ISSUE/comments" \
-f body="✅ Dispatched CI via \`workflow_dispatch\` for branch \`$HEAD_REF\` (commit \`${HEAD_SHA:0:7}\`, via \`/$COMMAND\`). The workflow run should appear in Checks shortly." \
>/dev/null
fi
fi
- name: React success
if: success() && steps.cmd.outputs.command != '' && steps.auth.outputs.trusted == 'true'
env:
GH_TOKEN: ${{ github.token }}
REPO: ${{ github.repository }}
COMMENT_ID: ${{ github.event.comment.id }}
run: |
gh api -X POST "repos/$REPO/issues/comments/$COMMENT_ID/reactions" \
-f content=rocket >/dev/null || true