Fix the exit code of a blue-green deploy script

Review the AI's codeEasy10 minAmazon style

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 -r prints null when 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 exactly null, or does not start with https:// (case sensitive), return 2.
  • If downloaded_bytes is 0 or less, return 3.
  • If health_body does 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) -> int
Example 1
Inputdeploy_exit_code("https://artifacts.acme.io/app.tar.gz", 10485760, "{\"status\":\"ok\",\"version\":\"42\"}")
Output0
Example 2
Inputdeploy_exit_code("null", 0, "")
Output2