Skip to content
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

Rename params to make intent clearer #97

Merged
merged 8 commits into from
Jan 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions SharpBucket/SharpBucket.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
<Compile Include="Authentication\OAuthentication2.cs" />
<Compile Include="Authentication\RequestExecutor.cs" />
<Compile Include="Authentication\Token.cs" />
<Compile Include="Utility\StringExtensions.cs" />
<Compile Include="V2\EndPoints\FilterBuilder.cs" />
<Compile Include="Utility\ObjectToDictionaryHelper.cs" />
<Compile Include="V1\EndPoints\GroupsEndPoint.cs" />
Expand Down
61 changes: 61 additions & 0 deletions SharpBucket/Utility/StringExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using System;
using System.Linq;
using System.Text.RegularExpressions;

namespace SharpBucket.Utility
{
internal static class StringExtensions
{
/// <summary>
/// Converts a repository name into the corresponding slug.
/// If a UUID in B or D format is passed in, it will be returned in B format.
/// </summary>
/// <param name="repoName">The name of a repository.</param>
/// <returns>The slug version of the repository name, or the formatted UUID.</returns>
public static string ToSlug(this string repoName)
{
if (repoName.TryGetGuid(out var guidString))
return guidString;

repoName = repoName.Replace("'", "");
var slugRegex = new Regex(@"[^a-zA-Z\.\-_0-9]+|-{2,}");
return slugRegex.Replace(repoName, "-").ToLowerInvariant();
}

/// <summary>
/// Determines whether the input string is a Guid in B or D format.
/// </summary>
/// <param name="input">The string to test.</param>
/// <param name="guidString">The Guid in B format, or null.</param>
/// <returns>True if the input string is a Guid in B or D format, or false.</returns>
public static bool TryGetGuid(this string input, out string guidString)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we really need to expose that method as a public one ?
It's used only as a private one, and even for unit tests it is highly covered by the tests of the two other methods.
So I would be favourable to define it as private.

It's a minor remarks if the class is globally changed to be internal, so if you prefer to keep like that, just tell me.

{
if (Guid.TryParseExact(input, "B", out _))
{
guidString = input;
return true;
}
else if (Guid.TryParseExact(input, "D", out _))
{
guidString = $"{{{input}}}";
return true;
}
else
{
guidString = null;
return false;
}
}

/// <summary>
/// Returns a formatted Guid if the input string is a Guid in B or D format,
/// or the input string if not.
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public static string GuidOrValue(this string input)
{
return input.TryGetGuid(out var guidString) ? guidString : input;
}
}
}
24 changes: 12 additions & 12 deletions SharpBucket/V2/EndPoints/BranchResource.cs
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
using SharpBucket.V2.Pocos;
using SharpBucket.Utility;
using SharpBucket.V2.Pocos;
using System;
using System.Collections.Generic;

namespace SharpBucket.V2.EndPoints
{
/// <summary>
/// Manage branches for a repository. Use this resource to perform CRUD (create/read/update/delete) operations.
/// More info:
/// https://developer.atlassian.com/bitbucket/api/2/reference/resource/repositories/%7Busername%7D/%7Brepo_slug%7D/refs/branches
/// </summary>
public class BranchResource
{
private readonly string _accountName;
private readonly string _repository;
private readonly string _slug;
private readonly RepositoriesEndPoint _repositoriesEndPoint;

/// <summary>
/// Manage branches for a repository. Use this resource to perform CRUD (create/read/update/delete) operations.
/// More info:
/// https://developer.atlassian.com/bitbucket/api/2/reference/resource/repositories/%7Busername%7D/%7Brepo_slug%7D/refs/branches
/// </summary>
/// <returns></returns>
public BranchResource(string accountName, string repository, RepositoriesEndPoint repositoriesEndPoint)
public BranchResource(string accountName, string repoSlugOrName, RepositoriesEndPoint repositoriesEndPoint)
{
_accountName = accountName;
_repository = repository;
_accountName = accountName.GuidOrValue();
_slug = repoSlugOrName.ToSlug();
_repositoriesEndPoint = repositoriesEndPoint;
}

Expand All @@ -38,7 +38,7 @@ public List<Branch> ListBranches(ListParameters parameters)
{
if (parameters == null)
throw new ArgumentNullException(nameof(parameters));
return _repositoriesEndPoint.ListBranches(_accountName, _repository, parameters);
return _repositoriesEndPoint.ListBranches(_accountName, _slug, parameters);
}
}
}
31 changes: 16 additions & 15 deletions SharpBucket/V2/EndPoints/PullRequestResource.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using SharpBucket.Utility;
using SharpBucket.V2.Pocos;

namespace SharpBucket.V2.EndPoints
Expand All @@ -10,13 +11,13 @@ public class PullRequestResource
{
private readonly int _pullRequestId;
private readonly RepositoriesEndPoint _repositoriesEndPoint;
private readonly string _repository;
private readonly string _slug;
private readonly string _accountName;

public PullRequestResource(string accountName, string repository, int pullRequestId, RepositoriesEndPoint repositoriesEndPoint)
public PullRequestResource(string accountName, string repoSlugOrName, int pullRequestId, RepositoriesEndPoint repositoriesEndPoint)
{
_accountName = accountName;
_repository = repository;
_accountName = accountName.GuidOrValue();
_slug = repoSlugOrName.ToSlug();
_repositoriesEndPoint = repositoriesEndPoint;
_pullRequestId = pullRequestId;
}
Expand All @@ -26,7 +27,7 @@ public PullRequestResource(string accountName, string repository, int pullReques
/// </summary>
public PullRequest GetPullRequest()
{
return _repositoriesEndPoint.GetPullRequest(_accountName, _repository, _pullRequestId);
return _repositoriesEndPoint.GetPullRequest(_accountName, _slug, _pullRequestId);
}

/// <summary>
Expand All @@ -35,7 +36,7 @@ public PullRequest GetPullRequest()
/// <returns></returns>
public List<Commit> ListPullRequestCommits()
{
return _repositoriesEndPoint.ListPullRequestCommits(_accountName, _repository, _pullRequestId);
return _repositoriesEndPoint.ListPullRequestCommits(_accountName, _slug, _pullRequestId);
}

/// <summary>
Expand All @@ -45,7 +46,7 @@ public List<Commit> ListPullRequestCommits()
/// <returns></returns>
public PullRequestInfo ApprovePullRequest()
{
return _repositoriesEndPoint.ApprovePullRequest(_accountName, _repository, _pullRequestId);
return _repositoriesEndPoint.ApprovePullRequest(_accountName, _slug, _pullRequestId);
}

/// <summary>
Expand All @@ -54,7 +55,7 @@ public PullRequestInfo ApprovePullRequest()
/// <returns></returns>
public PullRequestInfo RemovePullRequestApproval()
{
return _repositoriesEndPoint.RemovePullRequestApproval(_accountName, _repository, _pullRequestId);
return _repositoriesEndPoint.RemovePullRequestApproval(_accountName, _slug, _pullRequestId);
}

/// <summary>
Expand All @@ -64,7 +65,7 @@ public PullRequestInfo RemovePullRequestApproval()
/// <returns></returns>
public object GetDiffForPullRequest()
{
return _repositoriesEndPoint.GetDiffForPullRequest(_accountName, _repository, _pullRequestId);
return _repositoriesEndPoint.GetDiffForPullRequest(_accountName, _slug, _pullRequestId);
}

/// <summary>
Expand All @@ -73,7 +74,7 @@ public object GetDiffForPullRequest()
/// <returns></returns>
public List<Activity> GetPullRequestActivity()
{
return _repositoriesEndPoint.GetPullRequestActivity(_accountName, _repository, _pullRequestId);
return _repositoriesEndPoint.GetPullRequestActivity(_accountName, _slug, _pullRequestId);
}

/// <summary>
Expand All @@ -82,7 +83,7 @@ public List<Activity> GetPullRequestActivity()
/// <returns></returns>
public Merge AcceptAndMergePullRequest()
{
return _repositoriesEndPoint.AcceptAndMergePullRequest(_accountName, _repository, _pullRequestId);
return _repositoriesEndPoint.AcceptAndMergePullRequest(_accountName, _slug, _pullRequestId);
}

/// <summary>
Expand All @@ -91,7 +92,7 @@ public Merge AcceptAndMergePullRequest()
/// <returns></returns>
public PullRequest DeclinePullRequest()
{
return _repositoriesEndPoint.DeclinePullRequest(_accountName, _repository, _pullRequestId);
return _repositoriesEndPoint.DeclinePullRequest(_accountName, _slug, _pullRequestId);
}

/// <summary>
Expand All @@ -100,7 +101,7 @@ public PullRequest DeclinePullRequest()
/// <returns></returns>
public List<Comment> ListPullRequestComments()
{
return _repositoriesEndPoint.ListPullRequestComments(_accountName, _repository, _pullRequestId);
return _repositoriesEndPoint.ListPullRequestComments(_accountName, _slug, _pullRequestId);
}

/// <summary>
Expand All @@ -109,12 +110,12 @@ public List<Comment> ListPullRequestComments()
/// <param name="commentId">The comment identifier.</param> /// <returns></returns>
public Comment GetPullRequestComment(int commentId)
{
return _repositoriesEndPoint.GetPullRequestComment(_accountName, _repository, _pullRequestId, commentId);
return _repositoriesEndPoint.GetPullRequestComment(_accountName, _slug, _pullRequestId, commentId);
}

public Comment PostPullRequestComment(Comment comment)
{
return _repositoriesEndPoint.PostPullRequestComment(_accountName, _repository, _pullRequestId, comment);
return _repositoriesEndPoint.PostPullRequestComment(_accountName, _slug, _pullRequestId, comment);
}
}
}
41 changes: 21 additions & 20 deletions SharpBucket/V2/EndPoints/PullRequestsResource.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using SharpBucket.Utility;
using SharpBucket.V2.Pocos;

namespace SharpBucket.V2.EndPoints
Expand All @@ -12,15 +13,15 @@ namespace SharpBucket.V2.EndPoints
public class PullRequestsResource
{
private readonly RepositoriesEndPoint _repositoriesEndPoint;
private readonly string _repository;
private readonly string _slug;
private readonly string _accountName;

#region Pull Requests Resource

public PullRequestsResource(string accountName, string repository, RepositoriesEndPoint repositoriesEndPoint)
public PullRequestsResource(string accountName, string repoSlugOrName, RepositoriesEndPoint repositoriesEndPoint)
{
_accountName = accountName;
_repository = repository;
_accountName = accountName.GuidOrValue();
_slug = repoSlugOrName.ToSlug();
_repositoriesEndPoint = repositoriesEndPoint;
}

Expand All @@ -39,7 +40,7 @@ public List<PullRequest> ListPullRequests(ListParameters parameters)
{
if (parameters == null)
throw new ArgumentNullException(nameof(parameters));
return _repositoriesEndPoint.ListPullRequests(_accountName, _repository, parameters);
return _repositoriesEndPoint.ListPullRequests(_accountName, _slug, parameters);
}

/// <summary>
Expand All @@ -50,7 +51,7 @@ public List<PullRequest> ListPullRequests(ListParameters parameters)
/// <returns></returns>
public PullRequest PostPullRequest(PullRequest pullRequest)
{
return _repositoriesEndPoint.PostPullRequest(_accountName, _repository, pullRequest);
return _repositoriesEndPoint.PostPullRequest(_accountName, _slug, pullRequest);
}

/// <summary>
Expand All @@ -63,7 +64,7 @@ public PullRequest PostPullRequest(PullRequest pullRequest)
/// <returns></returns>
public PullRequest PutPullRequest(PullRequest pullRequest)
{
return _repositoriesEndPoint.PutPullRequest(_accountName, _repository, pullRequest);
return _repositoriesEndPoint.PutPullRequest(_accountName, _slug, pullRequest);
}

/// <summary>
Expand All @@ -72,7 +73,7 @@ public PullRequest PutPullRequest(PullRequest pullRequest)
/// <returns></returns>
public List<Activity> GetPullRequestLog()
{
return _repositoriesEndPoint.GetPullRequestLog(_accountName, _repository);
return _repositoriesEndPoint.GetPullRequestLog(_accountName, _slug);
}

#endregion
Expand All @@ -88,62 +89,62 @@ public List<Activity> GetPullRequestLog()
/// <returns></returns>
public PullRequestResource PullRequestResource(int pullRequestId)
{
return new PullRequestResource(_accountName, _repository, pullRequestId, _repositoriesEndPoint);
return new PullRequestResource(_accountName, _slug, pullRequestId, _repositoriesEndPoint);
}

internal PullRequest GetPullRequest(int pullRequestId)
{
return _repositoriesEndPoint.GetPullRequest(_accountName, _repository, pullRequestId);
return _repositoriesEndPoint.GetPullRequest(_accountName, _slug, pullRequestId);
}

internal List<Commit> ListPullRequestCommits(int pullRequestId)
{
return _repositoriesEndPoint.ListPullRequestCommits(_accountName, _repository, pullRequestId);
return _repositoriesEndPoint.ListPullRequestCommits(_accountName, _slug, pullRequestId);
}

internal PullRequestInfo ApprovePullRequest(int pullRequestId)
{
return _repositoriesEndPoint.ApprovePullRequest(_accountName, _repository, pullRequestId);
return _repositoriesEndPoint.ApprovePullRequest(_accountName, _slug, pullRequestId);
}

internal PullRequestInfo RemovePullRequestApproval(int pullRequestId)
{
return _repositoriesEndPoint.RemovePullRequestApproval(_accountName, _repository, pullRequestId);
return _repositoriesEndPoint.RemovePullRequestApproval(_accountName, _slug, pullRequestId);
}

internal object GetDiffForPullRequest(int pullRequestId)
{
return _repositoriesEndPoint.GetDiffForPullRequest(_accountName, _repository, pullRequestId);
return _repositoriesEndPoint.GetDiffForPullRequest(_accountName, _slug, pullRequestId);
}

internal List<Activity> GetPullRequestActivity(int pullRequestId)
{
return _repositoriesEndPoint.GetPullRequestActivity(_accountName, _repository, pullRequestId);
return _repositoriesEndPoint.GetPullRequestActivity(_accountName, _slug, pullRequestId);
}

internal Merge AcceptAndMergePullRequest(int pullRequestId)
{
return _repositoriesEndPoint.AcceptAndMergePullRequest(_accountName, _repository, pullRequestId);
return _repositoriesEndPoint.AcceptAndMergePullRequest(_accountName, _slug, pullRequestId);
}

internal PullRequest DeclinePullRequest(int pullRequestId)
{
return _repositoriesEndPoint.DeclinePullRequest(_accountName, _repository, pullRequestId);
return _repositoriesEndPoint.DeclinePullRequest(_accountName, _slug, pullRequestId);
}

internal List<Comment> ListPullRequestComments(int pullRequestId)
{
return _repositoriesEndPoint.ListPullRequestComments(_accountName, _repository, pullRequestId);
return _repositoriesEndPoint.ListPullRequestComments(_accountName, _slug, pullRequestId);
}

internal Comment GetPullRequestComment(int pullRequestId, int commentId)
{
return _repositoriesEndPoint.GetPullRequestComment(_accountName, _repository, pullRequestId, commentId);
return _repositoriesEndPoint.GetPullRequestComment(_accountName, _slug, pullRequestId, commentId);
}

internal Comment PostPullRequestComment(int pullRequestId, Comment comment)
{
return _repositoriesEndPoint.PostPullRequestComment(_accountName, _repository, pullRequestId, comment);
return _repositoriesEndPoint.PostPullRequestComment(_accountName, _slug, pullRequestId, comment);
}

#endregion
Expand Down
Loading