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

Commit

Permalink
Fix CA1062 in Git and GitHub projects (#976)
Browse files Browse the repository at this point in the history
  • Loading branch information
skolima authored May 17, 2020
1 parent 27d299e commit 7a0b057
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 29 deletions.
30 changes: 3 additions & 27 deletions NuKeeper.Git.Tests/TestDirectoryHelper.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,13 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;

namespace NuKeeper.Git.Tests
{
public static class TestDirectoryHelper
{
private static readonly Dictionary<string, string> toRename = new Dictionary<string, string>
{
{ "dot_git", ".git" },
{ "gitmodules", ".gitmodules" },
};

private static readonly Type[] whitelist = { typeof(IOException), typeof(UnauthorizedAccessException) };

public static string DiscoverPathToGit()
Expand Down Expand Up @@ -53,28 +44,13 @@ public static DirectoryInfo UniqueTemporaryFolder()
return tempDir;
}

public static void CopyFilesRecursively(DirectoryInfo source, DirectoryInfo target)
public static void DeleteDirectory(DirectoryInfo toDelete)
{
// From http://stackoverflow.com/questions/58744/best-way-to-copy-the-entire-contents-of-a-directory-in-c/58779#58779

foreach (DirectoryInfo dir in source.GetDirectories())
if (toDelete == null)
{
CopyFilesRecursively(dir, target.CreateSubdirectory(Rename(dir.Name)));
throw new ArgumentNullException(nameof(toDelete));
}
foreach (FileInfo file in source.GetFiles())
{
file.CopyTo(Path.Combine(target.FullName, Rename(file.Name)));
}
}

private static string Rename(string name)
{
return toRename.ContainsKey(name) ? toRename[name] : name;
}


public static void DeleteDirectory(DirectoryInfo toDelete)
{
// http://stackoverflow.com/questions/329355/cannot-delete-directory-with-directory-deletepath-true/329502#329502
if (!toDelete.Exists)
{
Expand Down
15 changes: 15 additions & 0 deletions NuKeeper.Git/GitCmdDiscoveryDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,22 @@ public GitCmdDiscoveryDriver(string pathToGit, INuKeeperLogger logger)

public async Task<Uri> DiscoverRepo(Uri repositoryUri)
{
if (repositoryUri == null)
{
throw new ArgumentNullException(nameof(repositoryUri));
}

var result = await StartGitProcess("config --get remote.origin.url", true, repositoryUri.LocalPath);
return new Uri(result);
}

public async Task<string> GetCurrentHead(Uri repositoryUri)
{
if (repositoryUri == null)
{
throw new ArgumentNullException(nameof(repositoryUri));
}

var getBranchHead = await StartGitProcess($"symbolic-ref -q --short HEAD", true, repositoryUri.LocalPath);
return string.IsNullOrEmpty(getBranchHead) ?
await StartGitProcess($"rev-parse HEAD", true, repositoryUri.LocalPath) :
Expand All @@ -55,6 +65,11 @@ public async Task<GitRemote> GetRemoteForPlatform(Uri repositoryUri, string plat

public async Task<IEnumerable<GitRemote>> GetRemotes(Uri repositoryUri)
{
if (repositoryUri == null)
{
throw new ArgumentNullException(nameof(repositoryUri));
}

if (!await IsGitRepo(repositoryUri))
{
return Enumerable.Empty<GitRemote>();
Expand Down
5 changes: 5 additions & 0 deletions NuKeeper.Git/LibGit2SharpDiscoveryDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ public async Task<bool> IsGitRepo(Uri repositoryUri)

public async Task<IEnumerable<GitRemote>> GetRemotes(Uri repositoryUri)
{
if (repositoryUri == null)
{
throw new ArgumentNullException(nameof(repositoryUri));
}

if (!await IsGitRepo(repositoryUri))
{
return Enumerable.Empty<GitRemote>();
Expand Down
6 changes: 5 additions & 1 deletion NuKeeper.GitHub/GitHubForkFinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,18 @@ public class GitHubForkFinder : IForkFinder
public GitHubForkFinder(ICollaborationPlatform collaborationPlatform, INuKeeperLogger logger, ForkMode forkMode)
{
_collaborationPlatform = collaborationPlatform;
_logger = logger;
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
_forkMode = forkMode;

_logger.Detailed($"FindPushFork. Fork Mode is {_forkMode}");
}

public async Task<ForkData> FindPushFork(string userName, ForkData fallbackFork)
{
if (fallbackFork == null)
{
throw new ArgumentNullException(nameof(fallbackFork));
}

switch (_forkMode)
{
Expand Down
2 changes: 1 addition & 1 deletion NuKeeper.GitHub/GitHubRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace NuKeeper.GitHub
{
public class GitHubRepository : Repository
internal class GitHubRepository : Repository
{
public GitHubRepository(Octokit.Repository repository)
: base(
Expand Down
6 changes: 6 additions & 0 deletions NuKeeper.GitHub/GitHubRepositoryDiscovery.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
Expand All @@ -22,6 +23,11 @@ public GitHubRepositoryDiscovery(INuKeeperLogger logger, ICollaborationPlatform

public async Task<IEnumerable<RepositorySettings>> GetRepositories(SourceControlServerSettings settings)
{
if (settings == null)
{
throw new ArgumentNullException(nameof(settings));
}

switch (settings.Scope)
{
case ServerScope.Global:
Expand Down
5 changes: 5 additions & 0 deletions NuKeeper.GitHub/GitHubSettingsReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ public async Task<bool> CanRead(Uri repositoryUri)

public void UpdateCollaborationPlatformSettings(CollaborationPlatformSettings settings)
{
if (settings == null)
{
throw new ArgumentNullException(nameof(settings));
}

var envToken = _environmentVariablesProvider.GetEnvironmentVariable("NuKeeper_github_token");
settings.Token = Concat.FirstValue(envToken, settings.Token);
settings.ForkMode ??= ForkMode.PreferFork;
Expand Down
5 changes: 5 additions & 0 deletions NuKeeper.GitHub/GithubUriHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ public static class GithubUriHelpers
{
public static Uri Normalise(Uri value)
{
if (value == null)
{
throw new ArgumentNullException(nameof(value));
}

return Normalise(value.ToString());
}

Expand Down
5 changes: 5 additions & 0 deletions NuKeeper.GitHub/OctokitClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ public OctokitClient(INuKeeperLogger logger)

public void Initialise(AuthSettings settings)
{
if (settings == null)
{
throw new ArgumentNullException(nameof(settings));
}

_apiBase = settings.ApiBase;

_client = new GitHubClient(new ProductHeaderValue("NuKeeper"), _apiBase)
Expand Down

0 comments on commit 7a0b057

Please sign in to comment.