| 1 | #!/usr/bin/env bash |
| 2 | # Resolve a CID to its current defectInstanceId via /reports/defects.json. |
| 3 | # |
| 4 | # Why: many Coverity endpoints (defectdetails.json, the source-browser deep |
| 5 | # links) take a defectInstanceId, NOT a CID. The CID is stable across runs; |
| 6 | # the defectInstanceId is per-scan. When you only have a CID (e.g. you're |
| 7 | # acting on a stale list, or processing per-cid output from a diff tool), |
| 8 | # you need to look up the current defectInstanceId. |
| 9 | # |
| 10 | # Usage: |
| 11 | # resolve-cid-to-diid.sh <cid> |
| 12 | # |
| 13 | # Prints the defectInstanceId on stdout if resolved, or "GONE" if Coverity has |
| 14 | # no current defect instance for this CID (the underlying code was removed or |
| 15 | # the scan no longer reports it). |
| 16 | # |
| 17 | # This call is INDEPENDENT of view state -- it queries the defect by CID |
| 18 | # directly, so it does not interact with the server-side view pagination. |
| 19 | |
| 20 | set -euo pipefail |
| 21 | |
| 22 | # shellcheck source=./_lib.sh |
| 23 | # shellcheck disable=SC1091 |
| 24 | source "$(dirname "$0")/_lib.sh" |
| 25 | cov_load_env |
| 26 | |
| 27 | CID="${1:?usage: $0 <cid>}" |
| 28 | cov_require_numeric_cid "${CID}" |
| 29 | |
| 30 | url="$(curl -sS --max-time 30 \ |
| 31 | -H "accept: application/json, text/plain, */*" \ |
| 32 | -H "referer: ${COVERITY_HOST}/" \ |
| 33 | -H "user-agent: ${COVERITY_USER_AGENT}" \ |
| 34 | -b "${COVERITY_COOKIE}" \ |
| 35 | "${COVERITY_HOST}/reports/defects.json?projectId=${COVERITY_PROJECT_ID}&cid=${CID}" \ |
| 36 | | jq -r '.url // ""')" |
| 37 | |
| 38 | if [[ -z "${url}" ]]; then |
| 39 | echo "GONE" |
| 40 | exit 0 |
| 41 | fi |
| 42 | |
| 43 | # The .url field looks like: |
| 44 | # /reports.htm#v70389/p15826/defectInstanceId=14451237&fileInstanceId=...&mergedDefectId=... |
| 45 | diid="$(printf '%s' "${url}" | sed -n 's/.*defectInstanceId=\([0-9]*\).*/\1/p')" |
| 46 | if [[ -z "${diid}" ]]; then |
| 47 | echo "GONE" |
| 48 | exit 0 |
| 49 | fi |
| 50 | |
| 51 | echo "${diid}" |