Skip to content

Commit 2900238

Browse files
TECH_DEBT: Resolve short commit hashes via the GitHub commits API
get_deployed_commit.py read the full SHA from payload.commit.sha2 in the JSON served by the GitHub commit page. That path no longer exists — the SHA moved to payload.commitRoute.commit.oid — so the chained .get() calls fell back to the short hash and returned it without raising. The "Attempting to translate..." line was therefore followed by neither a warning nor a translation. FromCommit then reached list-deploy-changes.py as a 7-character hash. It fetches both refs with 'git fetch --depth=1 origin <ref>', which GitHub rejects for anything but a full SHA ("couldn't find remote ref"), and that fetch discards stderr and ignores its exit code, so the deployment summary could silently come out empty. - Query api.github.com/repos/.../commits/<sha> with the 'application/vnd.github.sha' media type, which returns the 40-character hash as plain text, instead of reading the commit page's undocumented JSON payload - Accept the result only when it is exactly 40 characters, warning with the response body otherwise - Warn explicitly when FromCommit is still a short hash, naming the fetch that will fail, so a future breakage is not silent Verified against qa and qa2: 78b98b4 now resolves to 78b98b4. An unknown hash returns HTTP 422, which raises into the existing handler and then trips the short-hash warning.
1 parent 9cc26af commit 2900238

1 file changed

Lines changed: 19 additions & 6 deletions

File tree

Scripts/get_deployed_commit.py

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
import json
2323
import urllib.request
2424

25-
REPO_COMMIT_ROOT = 'https://github.com/lantanagroup/link-cloud/commit/'
25+
REPO_COMMITS_API_ROOT = 'https://api.github.com/repos/lantanagroup/link-cloud/commits/'
2626

2727
def fail(msg: str):
2828
print(f"ERROR: {msg}", file=sys.stderr)
@@ -99,20 +99,33 @@ def main():
9999
if not commit:
100100
fail("Could not find 'Commit' in /api/info response.")
101101

102-
# If we got a short hash, try to match it with the full hash from git log
102+
# If we got a short hash, resolve it to the full hash. list-deploy-changes.py fetches both
103+
# refs with 'git fetch origin <ref>', and GitHub only serves unadvertised objects by full SHA.
104+
# The 'sha' media type returns the 40-character hash as plain text.
103105
if len(commit) < 40: # Full SHA-1 hash is 40 characters
104106
print(f"Attempting to translate short commit hash {commit} to full commit hash")
105107
try:
106108
request = urllib.request.Request(
107-
f"{REPO_COMMIT_ROOT}{commit}",
108-
headers={'Accept': 'application/json'}
109+
f"{REPO_COMMITS_API_ROOT}{commit}",
110+
headers={'Accept': 'application/vnd.github.sha'}
109111
)
110112
with urllib.request.urlopen(request) as response:
111-
full_commit_data = json.loads(response.read().decode("utf-8"))
112-
commit = full_commit_data.get('payload', {}).get('commit', {}).get("sha2", commit)
113+
full_commit = response.read().decode("utf-8").strip()
114+
115+
if len(full_commit) == 40:
116+
commit = full_commit
117+
else:
118+
print(f"Warning: Unexpected response resolving full commit hash: '{full_commit[:100]}'", file=sys.stderr)
113119
except Exception as e:
114120
print(f"Warning: Could not resolve full commit hash: {e}", file=sys.stderr)
115121

122+
# A short hash here breaks the downstream fetch in list-deploy-changes.py, which fails
123+
# silently and yields an empty summary. Say so rather than letting it pass unnoticed.
124+
if len(commit) < 40:
125+
print(f"Warning: '{commit}' is not a full commit hash. "
126+
f"'git fetch origin {commit}' will fail and the deployment summary may be empty.",
127+
file=sys.stderr)
128+
116129
print(f"FromCommit: {commit}")
117130

118131
# 5. Emit Azure DevOps logging command

0 commit comments

Comments
 (0)