-
Notifications
You must be signed in to change notification settings - Fork 6
303 lines (284 loc) · 11.4 KB
/
request_api.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
# This workflow will install Python dependencies, run tests and lint with a single version of Python
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-python-with-github-actions
name: Python Request API
on:
push:
branches: [ master ]
pull_request:
branches: [ master ]
repository_dispatch:
types: [ deploy_success ]
workflow_dispatch:
inputs:
environment:
description: 'Environment to run tests against'
required: true
default: 'staging'
type: choice
options:
- staging
- production
- india
schedule:
- cron: '30 6 * * 1-5'
jobs:
set_matrix:
runs-on: ubuntu-latest
if: ${{ !(github.event_name == 'repository_dispatch' && github.event.client_payload.environment == 'staging') }}
outputs:
matrix: ${{ steps.set-matrix-schedule.outputs.matrix || steps.set-matrix-deploy.outputs.matrix || steps.set-matrix-manual.outputs.matrix || steps.set-matrix-default.outputs.matrix }}
steps:
- id: set-matrix-schedule
if: ${{ github.event_name == 'schedule' }}
run: |
echo "::set-output name=matrix::{\"environment\": [\"staging\"]}"
- id: set-matrix-deploy
if: ${{ github.event_name == 'repository_dispatch' && github.event.client_payload.environment != 'staging' }}
run: |
echo "::set-output name=matrix::{\"environment\": [\"${{ github.event.client_payload.environment }}\"]}"
- id: set-matrix-manual
if: ${{ github.event_name == 'workflow_dispatch' }}
run: |
echo "::set-output name=matrix::{\"environment\": [\"${{ inputs.environment }}\"]}"
- id: set-matrix-default
if: ${{ github.event_name != 'repository_dispatch' }}
run: |
echo "::set-output name=matrix::{\"environment\": [\"production\", \"staging\",\"india\"]}"
build:
needs: set_matrix
strategy:
fail-fast: false
matrix: ${{ fromJSON(needs.set_matrix.outputs.matrix) }}
concurrency:
group: ${{ github.workflow }}-${{ matrix.environment }}
cancel-in-progress: true
name: Request API on '${{ matrix.environment }}'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Set up Python 3.9
uses: actions/setup-python@v2
with:
python-version: 3.9
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r RequestAPI/requirements.txt
- name: Requests API tests with pytest
env:
DIMAGIQA_ENV: ${{ matrix.environment }} #${{ secrets.DIMAGIQA_URL }}
DIMAGIQA_PASSWORD: ${{ secrets.DIMAGIQA_API_PASSWORD }}
DIMAGIQA_LOGIN_USER: ${{ secrets.DIMAGIQA_APILOGIN_USERNAME }}
DIMAGIQA_LOGIN_PASS: ${{ secrets.DIMAGIQA_APILOGIN_PASSWORD }}
DIMAGIQA_PROD_API_KEY: ${{secrets.DIMAGIQA_APIPROD_API_KEY}}
DIMAGIQA_STAGING_API_KEY: ${{secrets.DIMAGIQA_APISTAGING_API_KEY}}
DIMAGIQA_INDIA_API_KEY: ${{secrets.DIMAGIQA_INDIA_API_KEY}}
run: |
echo "client_payload: ${{ toJson(github.event.client_payload) }}"
echo "matrix environment: ${{ matrix.environment }}"
echo "NOW=$(date +'%m-%d %H:%M')" >> $GITHUB_ENV
echo ${{env.NOW}}
pytest -v --tb=short -n auto --dist=loadfile --reruns 1 --html=report_api_${{ matrix.environment }}.html --self-contained-html --rootdir= RequestAPI/testCases
- name: Parse test counts
id: parse_counts
if: always()
run: |
# Extract variables from the api_test_counts.txt file
while IFS= read -r line; do
echo "::set-output name=${line%=*}::${line#*=}"
done < test_counts_${{ matrix.environment }}.txt
- name: Archive test results
id: artifact-upload-step
if: always()
uses: actions/upload-artifact@v4
with:
name: test-result-reports-${{ matrix.environment }}-${{ github.run_id }}
path: /home/runner/work/dimagi-qa/dimagi-qa/report_api_${{ matrix.environment }}.html
retention-days: 2
- name: Fetch artifact ID
run: echo 'Artifact ID is ${{ steps.artifact-upload-step.outputs.artifact-id }}'
- name: Send Failure Email
uses: dawidd6/action-send-mail@v3
if: failure()
with:
server_address: smtp.gmail.com
server_port: 465
username: ${{secrets.DIMAGIQA_MAIL_USERNAME}}
password: ${{secrets.DIMAGIQA_MAIL_PASSWORD}}
subject: ${{ github.event.client_payload.environment }} FAIL - Request API Tests[#${{github.run_number}}] on branch "${{ github.ref_name }}", ${{env.NOW}}
from: <${{secrets.DIMAGIQA_MAIL_USERNAME}}>
html_body: file:////home/runner/work/dimagi-qa/dimagi-qa/RequestAPI/email_fail.html
attachments: /home/runner/work/dimagi-qa/dimagi-qa/report_api_${{ matrix.environment }}.html
priority: normal
- name: Set slack vars
if: ${{ always() }}
id: configure_slack
uses: actions/github-script@v6
env:
JOB_STATUS: ${{ job.status }}
CC_ENV: ${{ matrix.environment }}
CC_EVENT: ${{ github.event_name }}
with:
result-encoding: string
script: |
const {CC_ENV, JOB_STATUS, CC_EVENT} = process.env
let SLACK_WEBHOOK_URL = '${{ secrets.SLACK_WEBHOOK_URL_SMOKE }}'
if (CC_EVENT == 'repository_dispatch' && CC_ENV == 'production') {
SLACK_WEBHOOK_URL = '${{ secrets.SLACK_WEBHOOK_URL_PROD_RESULTS }}'
console.log( "Prod deploy")
} else if (CC_EVENT != 'repository_dispatch' && CC_ENV == 'production' && JOB_STATUS == 'failure') {
SLACK_WEBHOOK_URL = '${{ secrets.SLACK_WEBHOOK_URL_SMOKE }}'
console.log( "Not Prod deploy but Prof failure")
} else if (CC_ENV == 'staging' && JOB_STATUS == 'failure') {
SLACK_WEBHOOK_URL = '${{ secrets.SLACK_WEBHOOK_URL_SMOKE }}'
console.log("Staging Failure")
} else if (CC_EVENT == 'repository_dispatch' && CC_ENV == 'india') {
SLACK_WEBHOOK_URL = '${{ secrets.SLACK_WEBHOOK_URL_INDIA }}'
console.log("India Failure")
}else if (CC_EVENT != 'repository_dispatch' && CC_ENV == 'india' && JOB_STATUS == 'failure') {
SLACK_WEBHOOK_URL = '${{ secrets.SLACK_WEBHOOK_URL_SMOKE }}'
console.log("India Success")
}else {
SLACK_WEBHOOK_URL = ' '
console.log("No notification sent")
}
return SLACK_WEBHOOK_URL
- name: Post to Slack channel on Failure
id: slack-api-staging-fail
uses: slackapi/[email protected]
if: ${{ steps.configure_slack.outputs.result != ' ' && failure() }}
with:
payload: |
{
"attachments": [
{
"color": "#ff0000",
"blocks": [
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": " Hola 👋 \n*${{ github.workflow }}* were just triggered!\n"
}
},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "*Passed:* ${{ steps.parse_counts.outputs.PASSED }} *Failed:* ${{ steps.parse_counts.outputs.FAILED }} *Error:* ${{ steps.parse_counts.outputs.ERROR }} *Skipped:* ${{ steps.parse_counts.outputs.SKIPPED }} *XFail:* ${{ steps.parse_counts.outputs.XFAIL }}\n"
}
},
{
"type": "context",
"elements": [
{
"type": "mrkdwn",
"text": "*Environment: *\n ${{ matrix.environment }} \n"
},
{
"type": "mrkdwn",
"text": " "
},
{
"type": "mrkdwn",
"text": "*Status: *\n ${{ job.status }} :x:"
}
]
},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "Here's the corresponding report :arrow_right::arrow_right:"
},
"accessory": {
"type": "button",
"text": {
"type": "plain_text",
"text": "Click to Downlaod",
"emoji": true
},
"value": "click_me_123",
"url": "${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}/artifacts/${{ steps.artifact-upload-step.outputs.artifact-id }}",
"action_id": "button-action",
"style": "danger"
}
}
]
}
]
}
parse-json-secrets: true
env:
SLACK_WEBHOOK_URL: ${{ steps.configure_slack.outputs.result }}
SLACK_WEBHOOK_TYPE: INCOMING_WEBHOOK
- name: Post to Slack channel on Success
id: slack-api-staging-pass
uses: slackapi/[email protected]
if: ${{ steps.configure_slack.outputs.result != ' ' && success() }}
with:
payload: |
{
"attachments": [
{
"color": "#36a64f",
"blocks": [
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": " Hola 👋 \n*${{ github.workflow }}* were just triggered! \n"
}
},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "*Passed:* ${{ steps.parse_counts.outputs.PASSED }} *Failed:* ${{ steps.parse_counts.outputs.FAILED }} *Error:* ${{ steps.parse_counts.outputs.ERROR }} *Skipped:* ${{ steps.parse_counts.outputs.SKIPPED }} *XFail:* ${{ steps.parse_counts.outputs.XFAIL }}\n"
}
},
{
"type": "context",
"elements": [
{
"type": "mrkdwn",
"text": "*Environment: *\n ${{ matrix.environment }} \n"
},
{
"type": "mrkdwn",
"text": " "
},
{
"type": "mrkdwn",
"text": "*Status: *\n ${{ job.status }} :white_check_mark:"
}
]
},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "Here's the corresponding report :arrow_right::arrow_right:"
},
"accessory": {
"type": "button",
"text": {
"type": "plain_text",
"text": "Click to Downlaod",
"emoji": true
},
"value": "click_me_123",
"url": "${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}/artifacts/${{ steps.artifact-upload-step.outputs.artifact-id }}",
"action_id": "button-action",
"style": "primary"
}
}
]
}
]
}
parse-json-secrets: true
env:
SLACK_WEBHOOK_URL: ${{ steps.configure_slack.outputs.result }}
SLACK_WEBHOOK_TYPE: INCOMING_WEBHOOK