-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Ensure we log what the HTTP error message is if we get a failure #78418
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jasonmalinowski
wants to merge
4
commits into
dotnet:main
Choose a base branch
from
jasonmalinowski:show-error-when-checking-uri
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+39
−21
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
f8a3a2d
Ensure we log what the HTTP error message is if we get a failure
jasonmalinowski 43ebe36
Keep validating the rest of a file
jasonmalinowski 0f66c8a
We don't need to call IsNullOrWhiteSpace -- we just did that
jasonmalinowski 2258f7b
Include headers if there is a failure
jasonmalinowski File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ | |
using System.Collections.Generic; | ||
using System.Collections.Immutable; | ||
using System.Diagnostics; | ||
using System.Diagnostics.CodeAnalysis; | ||
using System.Globalization; | ||
using System.IO; | ||
using System.Linq; | ||
|
@@ -19,6 +20,7 @@ | |
using Analyzer.Utilities.PooledObjects; | ||
using Analyzer.Utilities.PooledObjects.Extensions; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
using Microsoft.CodeAnalysis.Diagnostics; | ||
using Microsoft.CodeAnalysis.ReleaseTracking; | ||
using Microsoft.CodeAnalysis.Text; | ||
|
@@ -52,7 +54,7 @@ public static async Task<int> Main(string[] args) | |
validateOnly = false; | ||
} | ||
|
||
var fileNamesWithValidationFailures = new List<string>(); | ||
var fileNamesWithValidationFailures = new HashSet<string>(); | ||
|
||
string analyzerRulesetsDir = args[1]; | ||
string analyzerEditorconfigsDir = args[2]; | ||
|
@@ -211,7 +213,9 @@ public static async Task<int> Main(string[] args) | |
if (fileNamesWithValidationFailures.Count > 0) | ||
{ | ||
await Console.Error.WriteLineAsync("One or more auto-generated documentation files were either edited manually, or not updated. Please revert changes made to the following files (if manually edited) and run `dotnet msbuild /t:pack` at the root of the repo to automatically update them:").ConfigureAwait(false); | ||
fileNamesWithValidationFailures.ForEach(fileName => Console.Error.WriteLine($" {fileName}")); | ||
foreach (var fileName in fileNamesWithValidationFailures) | ||
Console.Error.WriteLine($" {fileName}"); | ||
|
||
return 1; | ||
} | ||
|
||
|
@@ -615,19 +619,17 @@ Rule ID | Missing Help Link | Title | | |
DiagnosticDescriptor descriptor = ruleById.Value; | ||
|
||
var helpLinkUri = descriptor.HelpLinkUri; | ||
if (!string.IsNullOrWhiteSpace(helpLinkUri) && | ||
await checkHelpLinkAsync(helpLinkUri).ConfigureAwait(false)) | ||
{ | ||
// Rule with valid documentation link | ||
if (string.IsNullOrWhiteSpace(helpLinkUri)) | ||
continue; | ||
|
||
string? checkError = await checkHelpLinkAsync(helpLinkUri).ConfigureAwait(false); | ||
|
||
if (checkError is null) | ||
continue; | ||
} | ||
|
||
// The angle brackets around helpLinkUri are added to follow MD034 rule: | ||
// https://github.com/DavidAnson/markdownlint/blob/82cf68023f7dbd2948a65c53fc30482432195de4/doc/Rules.md#md034---bare-url-used | ||
if (!string.IsNullOrWhiteSpace(helpLinkUri)) | ||
{ | ||
helpLinkUri = $"<{helpLinkUri}>"; | ||
} | ||
helpLinkUri = $"<{helpLinkUri}>"; | ||
|
||
var escapedTitle = descriptor.Title.ToString(CultureInfo.InvariantCulture).Replace("<", "\\<"); | ||
var line = $"{ruleId} | {helpLinkUri} | {escapedTitle} |"; | ||
|
@@ -638,11 +640,13 @@ await checkHelpLinkAsync(helpLinkUri).ConfigureAwait(false)) | |
// However, we consider "missing" entries as invalid. This is to force updating the file when new rules are added. | ||
if (!actualContent.Contains(line)) | ||
{ | ||
// The file is missing an entry. | ||
await Console.Error.WriteLineAsync($"Missing entry in {fileWithPath}").ConfigureAwait(false); | ||
await Console.Error.WriteLineAsync(line).ConfigureAwait(false); | ||
// The file is missing an entry. Mark it as invalid and break the loop as there is no need to continue validating. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Stopping further validation seems odd, because it means you might not necessarily know the extent of a potential problem. |
||
await Console.Error.WriteLineAsync(" " + line).ConfigureAwait(false); | ||
await Console.Error.WriteLineAsync("HTTP error while checking the URI: ").ConfigureAwait(false); | ||
await Console.Error.WriteLineAsync(checkError).ConfigureAwait(false); | ||
|
||
fileNamesWithValidationFailures.Add(fileWithPath); | ||
break; | ||
} | ||
} | ||
else | ||
|
@@ -658,27 +662,41 @@ await checkHelpLinkAsync(helpLinkUri).ConfigureAwait(false)) | |
|
||
return; | ||
|
||
async Task<bool> checkHelpLinkAsync(string helpLink) | ||
// Returns null if the URI is valid, or an error otherwise | ||
async Task<string?> checkHelpLinkAsync(string helpLink) | ||
{ | ||
try | ||
{ | ||
if (!Uri.TryCreate(helpLink, UriKind.Absolute, out var uri)) | ||
{ | ||
return false; | ||
return null; | ||
} | ||
|
||
if (validateOffline) | ||
{ | ||
return true; | ||
return null; | ||
} | ||
|
||
var request = new HttpRequestMessage(HttpMethod.Head, uri); | ||
using var response = await httpClient.SendAsync(request).ConfigureAwait(false); | ||
return response?.StatusCode == HttpStatusCode.OK; | ||
|
||
// If it succeeded, we're good | ||
if (response.StatusCode == HttpStatusCode.OK) | ||
return null; | ||
|
||
var errorMessage = new StringBuilder(); | ||
errorMessage.AppendLine("StatusCode: " + response.StatusCode); | ||
errorMessage.AppendLine("Content: " + await response.Content.ReadAsStringAsync().ConfigureAwait(false)); | ||
|
||
foreach (var header in response.Headers) | ||
foreach (var headerValue in header.Value) | ||
errorMessage.AppendLine(header.Key + ": " + headerValue); | ||
|
||
return errorMessage.ToString(); | ||
} | ||
catch (WebException) | ||
jasonmalinowski marked this conversation as resolved.
Show resolved
Hide resolved
|
||
catch (HttpRequestException exception) | ||
{ | ||
return false; | ||
return exception.ToString(); | ||
} | ||
} | ||
} | ||
|
@@ -1180,7 +1198,7 @@ string getRuleActionCore(bool enable, bool enableAsWarning = false) | |
/// <remarks> | ||
/// Don't call this method with auto-generated files that are part of the artifacts because it's expected that they don't initially exist. | ||
/// </remarks> | ||
private static void Validate(string fileWithPath, string fileContents, List<string> fileNamesWithValidationFailures) | ||
private static void Validate(string fileWithPath, string fileContents, HashSet<string> fileNamesWithValidationFailures) | ||
{ | ||
string actual = File.ReadAllText(fileWithPath); | ||
if (actual != fileContents) | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
unless I am misreading this, we will now pass validation if the help link is missing, which isn't what we did before. seems like a missing link should fail validation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, you're right. Will fix.