diff --git a/src/File/GitHub.cs b/src/File/GitHub.cs index 7746ad9..30333b8 100644 --- a/src/File/GitHub.cs +++ b/src/File/GitHub.cs @@ -33,6 +33,9 @@ public static class GitHub public static bool TryIsInstalled(out string output) => Process.TryExecute("gh", "--version", out output) && output.StartsWith("gh version"); + public static bool TryApi(string endpoint, string jq, out string? output) + => Process.TryExecute("gh", $"api {endpoint} --jq \"{jq}\"", out output); + public static bool TryApi(string endpoint, out JToken? json) { json = null; diff --git a/src/File/Http/GitHubRawHandler.cs b/src/File/Http/GitHubRawHandler.cs index f76c74b..7186003 100644 --- a/src/File/Http/GitHubRawHandler.cs +++ b/src/File/Http/GitHubRawHandler.cs @@ -59,14 +59,10 @@ protected override async Task SendAsync(HttpRequestMessage (originalEtag != newEtag || originalSha == null) && parts.Length > 2 && GitHub.IsInstalled && - GitHub.TryApi($"repos/{parts[0]}/{parts[1]}/commits?per_page=1&path={string.Join('/', parts.Skip(4))}", out var json) && - json is JArray commits && - commits[0] is JObject obj && - obj.Property("sha") is JProperty prop && - prop != null && - prop.Value.Type == JTokenType.String) + GitHub.TryApi($"repos/{parts[0]}/{parts[1]}/commits?per_page=1&path={string.Join('/', parts.Skip(4))}", ".[0]?.sha", out var json) && + json is { Length: > 0 }) { - newSha = prop.Value.ToObject(); + newSha = json; } // Just propagate back what we had initially, as an optimization for HEAD and cases @@ -75,7 +71,13 @@ commits[0] is JObject obj && newSha = originalSha; if (newSha != null) + { + // Make sure the X-Sha matches what we have now. + if (response.Headers.TryGetValues("X-Sha", out _)) + response.Headers.Remove("X-Sha"); + response.Headers.TryAddWithoutValidation("X-Sha", newSha); + } return response; }