Fix the exit code of a blue-green deploy script
Your task
An assistant's blue-green deploy script puts a new build live only after a health check. It ran that check as curl ... | grep -c inside an if with no else. When the service went down, curl printed nothing and the script still exited 0. Now a function decides the exit code from what each step produced.
An assistant wrote deploy_exit_code(artifact_url, downloaded_bytes, health_body). It is in the editor. It has a bug. Find it and fix it.
Input and output:
artifact_url: what the artifact lookup printed.jq -rprintsnullwhen the field is missing.downloaded_bytes: the size of the downloaded file in bytes, an integer. It can be 0 or negative.health_body: the body the health endpoint sent back. It is empty when the request failed.- Return the exit code, an integer: 0, 2, 3 or 4.
Rules the tests check:
- Run the checks in this order and return at the first one that fails.
- Trim whitespace at both ends of
artifact_url. If the result is empty, is exactlynull, or does not start withhttps://(case sensitive), return 2. - If
downloaded_bytesis 0 or less, return 3. - If
health_bodydoes not contain the exact text"status":"ok", return 4. The text can sit anywhere in the body. The match is case sensitive and has no spaces, so"status": "ok"with a space does not count. - If every check passes, return 0.
Examples
deploy_exit_code(artifact_url: str, downloaded_bytes: int, health_body: str) -> intExample 1
Input
deploy_exit_code("https://artifacts.acme.io/app.tar.gz", 10485760, "{\"status\":\"ok\",\"version\":\"42\"}")Output
0Example 2
Input
deploy_exit_code("null", 0, "")Output
2