forked from alexey-ban/ai-dial-chat-themes
-
Notifications
You must be signed in to change notification settings - Fork 2
230 lines (201 loc) · 9.45 KB
/
deploy-review-command.yml
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
name: deploy-review-command
on:
repository_dispatch:
types: [deploy-review-command]
jobs:
debug:
runs-on: ubuntu-latest
steps:
- name: Dump GitHub context
run: echo "$GITHUB_CONTEXT"
env:
GITHUB_CONTEXT: ${{ toJson(github.event) }}
# plan:
# uses: nepalevov/ai-dial-ci/.github/workflows/gh_environment.yml@main
# with:
# operation: plan
# environment_name: ${{ github.event.client_payload.
deploy:
runs-on: ubuntu-latest
steps:
- name: Check repository owner
id: owner
run: |
#!/usr/bin/env bash
set -x # TODO: remove after debug
readonly STATUS_SUCCESS="success"
readonly STATUS_FAILURE="failure"
log_error() {
echo "Error: $1" >&2
}
set_failure_status() {
local message="$1"
log_error "${message}"
echo "status=${FAILURE_MESSAGE}" >> "${GITHUB_OUTPUT}"
exit 1
}
# Validate repository owner
if [[ "${INPUT_DISPATCHED_REPO_OWNER}" != "${GITHUB_REPOSITORY_OWNER}" ]]; then
set_failure_status "The event was not dispatched by a repository within the same owner."
fi
# Validate repository name prefix
if [[ "${INPUT_DISPATCHED_REPO_NAME}" != ai-dial* ]]; then
set_failure_status "The repository name does not start with '${PREFIX}'."
fi
echo "status=${STATUS_SUCCESS}" >> "${GITHUB_OUTPUT}"
echo "Owner checks passed successfully"
env:
INPUT_DISPATCHED_REPO_OWNER: ${{ github.event.client_payload.github.payload.repository.owner.login || github.event.client_payload.github.event.repository.owner.login }}
INPUT_DISPATCHED_REPO_NAME: ${{ github.event.client_payload.github.payload.repository.name || github.event.client_payload.github.event.repository.name }}
- name: Gather PR info
id: pr_info
run: |
#!/usr/bin/env bash
set -x # TODO: remove after debug
make_api_call() {
local url="$1"
curl -s -H "${GITHUB_TOKEN}" "${url}"
}
# Function to output variables to GITHUB_OUTPUT
output_variable() {
local name="$1"
local value="$2"
echo "${name}=${value}" >>"$GITHUB_OUTPUT"
}
# If any required input value is missing, try to resolve via API
if [[ (-z "$INPUT_PR_NUMBER" || -z "$INPUT_HEAD_REPO" || -z "$INPUT_HEAD_REF") && -n "$INPUT_HEAD_SHA" ]]; then
echo "Some INPUT_ values missing but INPUT_HEAD_SHA is set, falling back to API resolution..."
# Get PR details by commit SHA
SEARCH_PR_DATA=$(make_api_call "${GITHUB_API_URL}/search/issues?q=${INPUT_HEAD_SHA}+repo:${INPUT_BASE_REPO}+is:pr")
PR_URL=$(echo "$SEARCH_PR_DATA" | jq -r '.items[0].pull_request.url // empty')
if [[ -n "$PR_URL" && "$PR_URL" != "null" ]]; then
PR_NUMBER=$(echo "$SEARCH_PR_DATA" | jq -r '.items[0].number // empty')
PR_DETAILS=$(make_api_call "$PR_URL")
HEAD_REPO=$(echo "$PR_DETAILS" | jq -r '.head.repo.full_name // empty')
HEAD_REF=$(echo "$PR_DETAILS" | jq -r '.head.ref // empty')
fi
else
# If all inputs are present, just assign them
PR_NUMBER="${INPUT_PR_NUMBER}"
HEAD_REPO="${INPUT_HEAD_REPO}"
HEAD_REF="${INPUT_HEAD_REF}"
fi
# Validate and output
ERROR_MESSAGE=""
required_vars=("PR_NUMBER" "HEAD_REPO" "HEAD_REF")
for var in "${required_vars[@]}"; do
if [[ -z "${!var}" ]]; then
ERROR_MESSAGE+="${var} is missing. "
fi
done
if [[ -n "$ERROR_MESSAGE" ]]; then
echo "$ERROR_MESSAGE"
output_variable "status" "Could not resolve required PR information: $ERROR_MESSAGE"
exit 1
fi
# TODO: remove after debug?
echo "| Parameter | Value |"
echo "|-------------------|-------------|"
echo "| PR_NUMBER | $PR_NUMBER |"
echo "| HEAD_REPO | $HEAD_REPO |"
echo "| HEAD_REF | $HEAD_REF |"
output_variable "pr_number" "$PR_NUMBER"
output_variable "head_repo" "$HEAD_REPO"
output_variable "head_ref" "$HEAD_REF"
# Set trigger URL based on source
if [[ "$INPUT_TRIGGER" == "scd" ]]; then
TRIGGER_URL="${{ github.event.client_payload.github.payload.comment.html_url }}"
else
# Only workflow_run is supported as alternative to slash-command-dispatch for now
TRIGGER_URL="${{ github.event.client_payload.github.event.workflow_run.html_url }}"
fi
output_variable "trigger_url" "$TRIGGER_URL"
echo "PR info gathered successfully"
output_variable "status" "success"
env:
INPUT_PR_NUMBER: ${{ github.event.client_payload.pull_request.number }}
INPUT_HEAD_REPO: ${{ github.event.client_payload.pull_request.head.repo.full_name }}
INPUT_HEAD_REF: ${{ github.event.client_payload.pull_request.head.ref }}
INPUT_HEAD_SHA: ${{ github.event.client_payload.github.event.workflow_run.head_sha }}
INPUT_BASE_REPO: ${{ github.event.client_payload.github.payload.repository.full_name || github.event.client_payload.github.event.repository.full_name }}
INPUT_TRIGGER: ${{ github.event.client_payload.github.action }}
GITHUB_TOKEN: ${{ secrets.ACTIONS_BOT_TOKEN }}
- name: Deploy environment
id: deploy
run: |
# TODO: remove after debug?
echo "Deploying with the following parameters:"
echo "| Parameter | Value |"
echo "|---------------- |---------------------------------------------|"
echo "| GITHUB_PR | pr-${{ steps.pr_info.outputs.pr_number }} |"
echo "| GITHUB_REPO | ${{ steps.pr_info.outputs.head_repo }} |"
echo "| GITHUB_REF | ${{ steps.pr_info.outputs.head_ref }} |"
echo "| GITHUB_TRIGGER | ${{ steps.pr_info.outputs.trigger_url }} |"
echo "status=success" >>$GITHUB_OUTPUT # TODO: remove mock status after debug
echo "status=failure" >>$GITHUB_OUTPUT # TODO: remove mock status after debug
- name: Check overall status
id: status
if: success() || failure()
run: |
#!/usr/bin/env bash
set -x # TODO: remove after debug
# Constants
readonly SUCCESS_FLAG="✅"
readonly FAILURE_FLAG="❌"
readonly WORKFLOW_URL="${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}/actions/runs/${GITHUB_RUN_ID}"
echo "Checking deployment status..."
# Initialize flags and message
OWNER_CHECK_FLAG="$SUCCESS_FLAG"
PR_INFO_CHECK_FLAG="$SUCCESS_FLAG"
DEPLOY_CHECK_FLAG="$SUCCESS_FLAG"
MESSAGE=""
# Check owner status
if [[ "${{ steps.owner.outputs.status }}" != "success" ]]; then
OWNER_CHECK_FLAG="$FAILURE_FLAG"
MESSAGE+="> ${{ steps.owner.outputs.status }}"$'\n'
fi
# Check PR info status
if [[ "${{ steps.pr_info.outputs.status }}" != "success" ]]; then
PR_INFO_CHECK_FLAG="$FAILURE_FLAG"
MESSAGE+="> ${{ steps.pr_info.outputs.status }}"$'\n'
fi
# Check deploy status
if [[ "${{ steps.deploy.outputs.status }}" != "success" ]]; then
DEPLOY_CHECK_FLAG="$FAILURE_FLAG"
MESSAGE+="> ${{ steps.deploy.outputs.status }}"$'\n'
fi
# Append workflow run link if needed
if [[ -z "$MESSAGE" ]]; then
MESSAGE="> OK"
else
MESSAGE+="> Check [GitHub Workflow run](${WORKFLOW_URL}) for details"
fi
# Output markdown table
{
echo "| Check | Status |"
echo "|---------------------|-----------------------|"
echo "| Owner Check | $OWNER_CHECK_FLAG |"
echo "| PR Info Check | $PR_INFO_CHECK_FLAG |"
echo "| Deployment Check | $DEPLOY_CHECK_FLAG |"
} >> "$GITHUB_STEP_SUMMARY"
# Store message for potential comment
echo "message<<EOF" >> "$GITHUB_OUTPUT"
echo "$MESSAGE" >> "$GITHUB_OUTPUT"
echo "EOF" >> "$GITHUB_OUTPUT"
if [[ "$INPUT_TRIGGER" == "scd" ]]; then
TRIGGER_URL="${{ github.event.client_payload.github.payload.comment.html_url }}"
echo "is_comment_trigger=true" >>$GITHUB_OUTPUT
fi
env:
INPUT_TRIGGER: ${{ github.event.client_payload.github.action }}
- name: Return status as append to comment
if: |
(success() || failure()) &&
steps.status.outputs.is_comment_trigger == 'true'
uses: peter-evans/create-or-update-comment@71345be0265236311c031f5c7866368bd1eff043 # v4.0.0
with:
token: ${{ secrets.ACTIONS_BOT_TOKEN }}
repository: ${{ github.event.client_payload.github.payload.repository.full_name || github.event.repository.full_name }}
comment-id: ${{ github.event.client_payload.github.payload.comment.id }}
body: |
${{ steps.status.outputs.message }}