Skip to content
This repository has been archived by the owner on Jul 12, 2022. It is now read-only.

Commit

Permalink
Update the analysers (#702)
Browse files Browse the repository at this point in the history
* Update the analysers
Update the analysers
And the new version is a lot stricter on exception types

* This extra code was just playing

* Need the base class JsonException

* Check for null rather than rely on exception
  • Loading branch information
AnthonySteele authored Apr 6, 2019
1 parent 8f33869 commit 4fa9dcd
Show file tree
Hide file tree
Showing 23 changed files with 62 additions and 73 deletions.
7 changes: 3 additions & 4 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@
<RepositoryUrl>https://github.com/NuKeeperDotNet/NuKeeper.git</RepositoryUrl>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.FxCopAnalyzers" Version="2.6.3" PrivateAssets="All" />
<PackageReference Include="Microsoft.CodeQuality.Analyzers" Version="2.6.3" PrivateAssets="All" />
<PackageReference Include="Microsoft.NetCore.Analyzers" Version="2.6.3" PrivateAssets="All" />
<PackageReference Include="Text.Analyzers" Version="2.6.3" PrivateAssets="All" />
<PackageReference Include="Microsoft.CodeAnalysis.FxCopAnalyzers" Version="2.9.1" PrivateAssets="All" />
<PackageReference Include="Microsoft.CodeQuality.Analyzers" Version="2.9.1" PrivateAssets="All" />
<PackageReference Include="Microsoft.NetCore.Analyzers" Version="2.9.1" PrivateAssets="All" />
</ItemGroup>
</Project>
9 changes: 7 additions & 2 deletions NuKeeper.Abstractions/Configuration/FileSettingsReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,16 @@ private FileSettings ReadFile(string fullPath)
_logger.Detailed($"Read settings file at {fullPath}");
return result;
}
catch (Exception ex)
catch (IOException ex)
{
_logger.Error($"Cannot read settings file at {fullPath}", ex);
return FileSettings.Empty();
}
catch (JsonException ex)
{
_logger.Error($"Cannot read json from settings file at {fullPath}", ex);
}

return FileSettings.Empty();
}
}
}
2 changes: 1 addition & 1 deletion NuKeeper.AzureDevOps/AzureDevOpsRestClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ private async Task<T> HandleResponse<T>(HttpResponseMessage response, [CallerMem
{
return JsonConvert.DeserializeObject<T>(responseBody);
}
catch (Exception ex)
catch (JsonException ex)
{
msg = $"{caller} failed to parse json to {typeof(T)}: {ex.Message}";
_logger.Error(msg);
Expand Down
2 changes: 1 addition & 1 deletion NuKeeper.AzureDevOps/AzureDevOpsSettingsReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ private RepositorySettings CreateSettingsFromLocal(Uri repositoryUri, string tar
return CreateRepositorySettings(org, repositoryUri, project, repoName, remoteInfo);
}

private RepositorySettings CreateRepositorySettings(string org, Uri repositoryUri, string project, string repoName, RemoteInfo remoteInfo = null) => new RepositorySettings
private static RepositorySettings CreateRepositorySettings(string org, Uri repositoryUri, string project, string repoName, RemoteInfo remoteInfo = null) => new RepositorySettings
{
ApiUri = new Uri($"https://dev.azure.com/{org}/"),
RepositoryUri = repositoryUri,
Expand Down
2 changes: 1 addition & 1 deletion NuKeeper.AzureDevOps/VSTSSettingsReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ private RepositorySettings CreateSettingsFromLocal(Uri repositoryUri, string tar
return RepositorySettings(org, project, repoName, remoteInfo);
}

private RepositorySettings RepositorySettings(string org, string project, string repoName, RemoteInfo remoteInfo = null) => new RepositorySettings
private static RepositorySettings RepositorySettings(string org, string project, string repoName, RemoteInfo remoteInfo = null) => new RepositorySettings
{
ApiUri = new Uri($"https://{org}.visualstudio.com/"),
RepositoryUri = new Uri($"https://{org}.visualstudio.com/{project}/_git/{repoName}/"),
Expand Down
4 changes: 2 additions & 2 deletions NuKeeper.GitHub/OctokitClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public async Task<Repository> MakeUserFork(string owner, string repositoryName)
_logger.Normal($"User fork created at {result.GitUrl} for {result.Owner.Login}");
return new GitHubRepository(result);
}
catch (Exception ex)
catch (ApiException ex)
{
_logger.Error("User fork not created", ex);
return null;
Expand Down Expand Up @@ -184,7 +184,7 @@ await _client.Issue.Labels.AddToIssue(target.Owner, target.Name, issueNumber,
labelsToApply);

}
catch (Exception ex)
catch (ApiException ex)
{
_logger.Error("Failed to add labels. Continuing", ex);
}
Expand Down
3 changes: 2 additions & 1 deletion NuKeeper.Gitlab/GitlabRestClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ private async Task<T> HandleResponse<T>(HttpResponseMessage response,
if (customErrorHandling != null)
{
var result = customErrorHandling(response.StatusCode);

if (result.IsSuccessful)
return result.Value;
}
Expand All @@ -125,7 +126,7 @@ private async Task<T> HandleResponse<T>(HttpResponseMessage response,
{
return JsonConvert.DeserializeObject<T>(responseBody);
}
catch (Exception ex)
catch (JsonException ex)
{
msg = $"{caller} failed to parse json to {typeof(T)}: {ex.Message}";
_logger.Error(msg);
Expand Down
2 changes: 1 addition & 1 deletion NuKeeper.Gitlab/Result.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ public static Result<T> Success(T value)

public static Result<T> Failure()
{
return new Result<T>(default(T), false);
return new Result<T>(default, false);
}
}
4 changes: 2 additions & 2 deletions NuKeeper.Inspection/Files/Folder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public IReadOnlyCollection<FileInfo> Find(string pattern)
.ToList();

}
catch (Exception ex)
catch (IOException ex)
{
_logger.Minimal(ex.Message);
return new List<FileInfo>();
Expand All @@ -45,7 +45,7 @@ public void TryDelete()
DeleteDirectoryInternal(_root.FullName);
_logger.Detailed($"Deleted folder {_root.FullName}");
}
catch (Exception ex)
catch (IOException ex)
{
_logger.Detailed($"Folder delete failed: {ex.GetType().Name} {ex.Message}");
}
Expand Down
2 changes: 2 additions & 0 deletions NuKeeper.Inspection/NuGetApi/PackageVersionsLookup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ private async Task<IEnumerable<PackageSearchMedatadata>> RunFinderForSource(
var metadatas = await FindPackage(metadataResource, packageName, includePrerelease);
return metadatas.Select(m => BuildPackageData(source, m));
}
#pragma warning disable CA1031
catch (Exception ex)
#pragma warning restore CA1031
{
_nuKeeperLogger.Normal($"Getting {packageName} from {source} returned exception: {ex.Message}");
return Enumerable.Empty<PackageSearchMedatadata>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public IReadOnlyCollection<PackageInProject> ReadFile(string baseDirectory, stri
return Read(fileContents, packagePath);
}
}
catch (Exception ex)
catch (IOException ex)
{
throw new NuKeeperException($"Unable to parse file {packagePath.FullName}", ex);
}
Expand Down Expand Up @@ -61,22 +61,14 @@ public IReadOnlyCollection<PackageInProject> Read(Stream fileContents, PackagePa

private PackageInProject XmlToPackage(XElement el, PackagePath path)
{
try
var id = el.Attribute("Include")?.Value;
if (id == null)
{
var id = el.Attribute("Include")?.Value;
if (id == null)
{
id = el.Attribute("Update")?.Value;
}
var version = el.Attribute("Version")?.Value;

return _packageInProjectReader.Read(id, version, path, null);
}
catch (Exception ex)
{
_logger.Error($"Could not read package from {el} in file {path.FullName}", ex);
return null;
id = el.Attribute("Update")?.Value;
}
var version = el.Attribute("Version")?.Value;

return _packageInProjectReader.Read(id, version, path, null);
}
}
}
17 changes: 4 additions & 13 deletions NuKeeper.Inspection/RepositoryInspection/NuspecFileReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public IReadOnlyCollection<PackageInProject> ReadFile(string baseDirectory, stri
return Read(fileContents, packagePath);
}
}
catch (Exception ex)
catch (IOException ex)
{
throw new NuKeeperException($"Unable to parse file {packagePath.FullName}", ex);
}
Expand Down Expand Up @@ -61,19 +61,10 @@ public IReadOnlyCollection<PackageInProject> Read(Stream fileContents, PackagePa

private PackageInProject XmlToPackage(XElement el, PackagePath path)
{
try
{
var id = el.Attribute("id")?.Value;
var version = el.Attribute("version")?.Value;

return _packageInProjectReader.Read(id, version, path, null);
var id = el.Attribute("id")?.Value;
var version = el.Attribute("version")?.Value;

}
catch (Exception ex)
{
_logger.Error($"Could not read package from {el} in file {path.FullName}", ex);
return null;
}
return _packageInProjectReader.Read(id, version, path, null);
}
}
}
16 changes: 4 additions & 12 deletions NuKeeper.Inspection/RepositoryInspection/PackagesFileReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public IReadOnlyCollection<PackageInProject> ReadFile(string baseDirectory, stri
return Read(fileContents, packagePath);
}
}
catch (Exception ex)
catch (IOException ex)
{
throw new ApplicationException($"Unable to parse file {packagePath.FullName}", ex);
}
Expand Down Expand Up @@ -61,18 +61,10 @@ public IReadOnlyCollection<PackageInProject> Read(Stream fileContents, PackagePa

private PackageInProject XmlToPackage(XElement el, PackagePath path)
{
try
{
var id = el.Attribute("id")?.Value;
var version = el.Attribute("version")?.Value;
var id = el.Attribute("id")?.Value;
var version = el.Attribute("version")?.Value;

return _packageInProjectReader.Read(id, version, path, null);
}
catch (Exception ex)
{
_logger.Error($"Could not read package from {el} in file {path.FullName}", ex);
return null;
}
return _packageInProjectReader.Read(id, version, path, null);
}
}
}
16 changes: 4 additions & 12 deletions NuKeeper.Inspection/RepositoryInspection/ProjectFileReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public IReadOnlyCollection<PackageInProject> ReadFile(string baseDirectory, stri
return Read(fileContents, baseDirectory, relativePath);
}
}
catch (Exception ex)
catch (IOException ex)
{
throw new ApplicationException($"Unable to parse file {filePath}", ex);
}
Expand Down Expand Up @@ -92,18 +92,10 @@ private static PackagePath CreatePackagePath(XNamespace xmlNamespace, string bas
private PackageInProject XmlToPackage(XNamespace ns, XElement el,
PackagePath path, IEnumerable<string> projectReferences)
{
try
{
var id = el.Attribute("Include")?.Value;
var version = el.Attribute("Version")?.Value ?? el.Element(ns + "Version")?.Value;
var id = el.Attribute("Include")?.Value;
var version = el.Attribute("Version")?.Value ?? el.Element(ns + "Version")?.Value;

return _packageInProjectReader.Read(id, version, path, projectReferences);
}
catch (Exception ex)
{
_logger.Error($"Could not read package from {el} in file {path.FullName}", ex);
return null;
}
return _packageInProjectReader.Read(id, version, path, projectReferences);
}
}
}
2 changes: 2 additions & 0 deletions NuKeeper/Commands/CollaborationPlatformCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,9 @@ protected override ValidationResult PopulateSettings(SettingsContainer settings)
return collaborationResult;
}
}
#pragma warning disable CA1031
catch (Exception ex)
#pragma warning restore CA1031
{
return ValidationResult.Failure(ex.Message);
}
Expand Down
4 changes: 2 additions & 2 deletions NuKeeper/Commands/CommandBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ private ValidationResult PopulatePackageIncludes(
{
settings.PackageFilters.Includes = new Regex(value);
}
catch (Exception ex)
catch (ArgumentException ex)
{
{
return ValidationResult.Failure(
Expand All @@ -230,7 +230,7 @@ private ValidationResult PopulatePackageExcludes(
{
settings.PackageFilters.Excludes = new Regex(value);
}
catch (Exception ex)
catch (ArgumentException ex)
{
{
return ValidationResult.Failure(
Expand Down
4 changes: 2 additions & 2 deletions NuKeeper/Commands/MultipleRepositoryCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ private ValidationResult PopulateIncludeRepos(SettingsContainer settings)
{
settings.SourceControlServerSettings.IncludeRepos = new Regex(value);
}
catch (Exception ex)
catch (ArgumentException ex)
{
return ValidationResult.Failure($"Unable to parse regex '{value}' for IncludeRepos: {ex.Message}");
}
Expand All @@ -93,7 +93,7 @@ private ValidationResult PopulateExcludeRepos(SettingsContainer settings)
{
settings.SourceControlServerSettings.ExcludeRepos = new Regex(value);
}
catch (Exception ex)
catch (ArgumentException ex)
{
return ValidationResult.Failure($"Unable to parse regex '{value}' for ExcludeRepos: {ex.Message}");
}
Expand Down
7 changes: 6 additions & 1 deletion NuKeeper/Commands/RepositoryCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,18 @@ public RepositoryCommand(ICollaborationEngine engine, IConfigureLogger logger, I

protected override ValidationResult PopulateSettings(SettingsContainer settings)
{
if (string.IsNullOrWhiteSpace(RepositoryUri))
{
return ValidationResult.Failure($"Missing repository URI");
}

Uri repoUri;

try
{
repoUri = RepositoryUri.ToUri();
}
catch
catch (UriFormatException)
{
return ValidationResult.Failure($"Bad repository URI: '{RepositoryUri}'");
}
Expand Down
2 changes: 2 additions & 0 deletions NuKeeper/Engine/GitRepositoryEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,9 @@ public async Task<int> Run(RepositorySettings repository,

return updatesDone;
}
#pragma warning disable CA1031
catch (Exception ex)
#pragma warning restore CA1031
{
_logger.Error($"Failed on repo {repository.RepositoryName}", ex);
return 1;
Expand Down
2 changes: 2 additions & 0 deletions NuKeeper/Engine/Packages/ExistingBranchFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ public async Task<bool> CanMakeBranchFor(PackageUpdateSet packageUpdateSet,
var branchExists = await _collaborationFactory.CollaborationPlatform.RepositoryBranchExists(pushFork.Owner, pushFork.Name, branchName);
return !branchExists;
}
#pragma warning disable CA1031
catch (Exception ex)
#pragma warning restore CA1031
{
_logger.Error($"Failed on existing branch check at {pushFork.Owner}/{pushFork.Name}", ex);
return false;
Expand Down
2 changes: 2 additions & 0 deletions NuKeeper/Engine/Packages/PackageUpdater.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ public async Task<int> MakeUpdatePullRequests(
totalCount += updatesMade;
}
}
#pragma warning disable CA1031
catch (Exception ex)
#pragma warning restore CA1031
{
_logger.Error("Updates failed", ex);
}
Expand Down
2 changes: 2 additions & 0 deletions NuKeeper/Engine/RepositoryFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ public async Task<bool> ContainsDotNetProjects(RepositorySettings repository)

return true;
}
#pragma warning disable CA1031
catch (Exception ex)
#pragma warning restore CA1031
{
_logger.Error("Repository search failed.", ex);
}
Expand Down
2 changes: 1 addition & 1 deletion Nukeeper.BitBucketLocal/BitbucketLocalRestClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ private async Task<T> HandleResponse<T>(HttpResponseMessage response, [CallerMem
{
return JsonConvert.DeserializeObject<T>(responseBody);
}
catch (Exception)
catch (JsonException)
{
msg = $"{caller}: Json exception";
_logger.Error(msg);
Expand Down

0 comments on commit 4fa9dcd

Please sign in to comment.