-
Notifications
You must be signed in to change notification settings - Fork 59
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
Add basic pagination support for fetching values #10
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 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
This file contains 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
This file contains 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
This file contains 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 |
---|---|---|
@@ -0,0 +1,62 @@ | ||
using SharpBucket.V2.Pocos; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Diagnostics; | ||
|
||
namespace SharpBucket.V2.EndPoints { | ||
public class EndPoint { | ||
protected readonly SharpBucketV2 _sharpBucketV2; | ||
protected readonly string _baseUrl; | ||
|
||
public EndPoint (SharpBucketV2 sharpBucketV2, string resourcePath) { | ||
_sharpBucketV2 = sharpBucketV2; | ||
_baseUrl = resourcePath; | ||
} | ||
|
||
/// <summary> | ||
/// Iterates through a series of paginated values. | ||
/// </summary> | ||
/// <typeparam name="TValue"></typeparam> | ||
/// <param name="overrideUrl"></param> | ||
/// <param name="max">Set to 0 for unlimited size.</param> | ||
/// <returns></returns> | ||
protected List<TValue> GetPaginatedValues<TValue> (string overrideUrl, int max = 0) { | ||
// default page length in many cases is 10, requiring lots of requests for larger collections | ||
const int DEFAULT_PAGE_LEN = 100; | ||
int pageLen = (max > 0 && max < DEFAULT_PAGE_LEN) ? max : DEFAULT_PAGE_LEN; | ||
|
||
Debug.Assert(!overrideUrl.Contains("?")); | ||
overrideUrl += "?pagelen=" + pageLen; | ||
|
||
IteratorBasedPage<TValue> response = _sharpBucketV2.Get(new IteratorBasedPage<TValue>(), overrideUrl); | ||
List<TValue> values = null; | ||
|
||
if (response != null && response.values != null) { | ||
|
||
values = response.values; | ||
|
||
while (!String.IsNullOrEmpty(response.next) && (max <= 0 || values.Count < max)) { | ||
overrideUrl = response.next.Replace(SharpBucketV2.BITBUCKET_URL, ""); | ||
Debug.Assert(overrideUrl.Length < response.next.Length); | ||
|
||
response = _sharpBucketV2.Get(new IteratorBasedPage<TValue>(), overrideUrl); | ||
|
||
// should this case throw? | ||
if (response == null) { break; } | ||
|
||
// if BitBucket is providing what should be a valid "next" URL, | ||
// why is this ever null? | ||
if (response.values != null) { | ||
values.AddRange(response.values); | ||
} | ||
} | ||
} | ||
|
||
if (values != null && max > 0 && values.Count > max) { | ||
values.RemoveRange(max - 1, values.Count - max); | ||
} | ||
|
||
return values; | ||
} | ||
} | ||
} |
This file contains 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
This file contains 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 |
---|---|---|
|
@@ -11,15 +11,12 @@ namespace SharpBucket.V2.EndPoints{ | |
/// More info: | ||
/// https://confluence.atlassian.com/display/BITBUCKET/repositories+Endpoint | ||
/// </summary> | ||
public class RepositoriesEndPoint{ | ||
private readonly SharpBucketV2 _sharpBucketV2; | ||
private readonly string _baseUrl; | ||
public class RepositoriesEndPoint : EndPoint { | ||
|
||
#region Repositories End Point | ||
|
||
public RepositoriesEndPoint(SharpBucketV2 sharpBucketV2){ | ||
_sharpBucketV2 = sharpBucketV2; | ||
_baseUrl = "repositories/"; | ||
public RepositoriesEndPoint (SharpBucketV2 sharpBucketV2) | ||
: base(sharpBucketV2, "repositories/") { | ||
} | ||
|
||
/// <summary> | ||
|
@@ -28,20 +25,22 @@ public RepositoriesEndPoint(SharpBucketV2 sharpBucketV2){ | |
/// Otherwise, this method returns a collection of the public repositories. | ||
/// </summary> | ||
/// <param name="accountName">The account whose repositories you wish to get.</param> | ||
/// <param name="max">The maximum number of items to return. 0 returns all items.</param> | ||
/// <returns></returns> | ||
public List<Repository> ListRepositories(string accountName){ | ||
public List<Repository> ListRepositories(string accountName, int max = 0){ | ||
var overrideUrl = _baseUrl + accountName + "/"; | ||
return _sharpBucketV2.Get(new RepositoryInfo(), overrideUrl).values; | ||
return GetPaginatedValues<Repository>(overrideUrl, max); | ||
} | ||
|
||
/// <summary> | ||
/// List of all the public repositories on Bitbucket. This produces a paginated response. | ||
/// Pagination only goes forward (it's not possible to navigate to previous pages) and navigation is done by following the URL for the next page. | ||
/// The returned repositories are ordered by creation date, oldest repositories first. Only public repositories are returned. | ||
/// </summary> | ||
/// <param name="max">The maximum number of items to return. 0 returns all items.</param> | ||
/// <returns></returns> | ||
public List<Repository> ListPublicRepositories(){ | ||
return _sharpBucketV2.Get(new RepositoryInfo(), _baseUrl).values; | ||
public List<Repository> ListPublicRepositories(int max = 0){ | ||
return GetPaginatedValues<Repository>(_baseUrl, max); | ||
} | ||
|
||
#endregion | ||
|
@@ -81,14 +80,14 @@ private string GetRepositoryUrl(string accountName, string repository, string ap | |
return string.Format(format, accountName, repository, append); | ||
} | ||
|
||
internal List<Watcher> ListWatchers(string accountName, string repository){ | ||
internal List<Watcher> ListWatchers(string accountName, string repository, int max = 0){ | ||
var overrideUrl = GetRepositoryUrl(accountName, repository, "watchers"); | ||
return _sharpBucketV2.Get(new WatcherInfo(), overrideUrl).values; | ||
return GetPaginatedValues<Watcher>(overrideUrl, max); | ||
} | ||
|
||
internal List<Fork> ListForks(string accountName, string repository){ | ||
internal List<Fork> ListForks(string accountName, string repository, int max = 0){ | ||
var overrideUrl = GetRepositoryUrl(accountName, repository, "forks"); | ||
return _sharpBucketV2.Get(new ForkInfo(), overrideUrl).values; | ||
return GetPaginatedValues<Fork>(overrideUrl, max); | ||
} | ||
|
||
#endregion | ||
|
@@ -99,9 +98,9 @@ public PullRequestsResource PullReqestsResource(string accountName, string repos | |
return new PullRequestsResource(accountName, repository, this); | ||
} | ||
|
||
internal PullRequestsInfo ListPullRequests(string accountName, string repository){ | ||
internal List<PullRequest> ListPullRequests(string accountName, string repository, int max = 0) { | ||
var overrideUrl = GetRepositoryUrl(accountName, repository, "pullrequests/"); | ||
return _sharpBucketV2.Get(new PullRequestsInfo(), overrideUrl); | ||
return GetPaginatedValues<PullRequest>(overrideUrl, max); | ||
} | ||
|
||
internal PullRequest PostPullRequest(string accountName, string repository, PullRequest pullRequest){ | ||
|
@@ -114,9 +113,9 @@ internal PullRequest PutPullRequest(string accountName, string repository, PullR | |
return _sharpBucketV2.Put(pullRequest, overrideUrl); | ||
} | ||
|
||
internal ActivityInfo GetPullRequestLog(string accountName, string repository){ | ||
internal List<Activity> GetPullRequestLog(string accountName, string repository, int max = 0){ | ||
var overrideUrl = GetRepositoryUrl(accountName, repository, "pullrequests/activity/"); | ||
return _sharpBucketV2.Get(new ActivityInfo(), overrideUrl); | ||
return GetPaginatedValues<Activity>(overrideUrl, max); | ||
} | ||
|
||
#endregion | ||
|
@@ -128,12 +127,12 @@ internal PullRequest GetPullRequest(string accountName, string repository, int p | |
return _sharpBucketV2.Get(new PullRequest(), overrideUrl); | ||
} | ||
|
||
internal CommitInfo ListPullRequestCommits(string accountName, string repository, int pullRequestId){ | ||
internal List<Commit> ListPullRequestCommits(string accountName, string repository, int pullRequestId, int max = 0){ | ||
var overrideUrl = GetRepositoryUrl(accountName, repository, "pullrequests/" + pullRequestId + "/commits/"); | ||
return _sharpBucketV2.Get(new CommitInfo(), overrideUrl); | ||
return GetPaginatedValues<Commit>(overrideUrl, max); | ||
} | ||
|
||
internal PullRequestInfo ApprovePullRequest(string accountName, string repository, int pullRequestId){ | ||
internal PullRequestInfo ApprovePullRequest(string accountName, string repository, int pullRequestId) { | ||
var overrideUrl = GetRepositoryUrl(accountName, repository, "pullrequests/" + pullRequestId + "/approve/"); | ||
return _sharpBucketV2.Post(new PullRequestInfo(), overrideUrl); | ||
} | ||
|
@@ -148,9 +147,9 @@ internal object GetDiffForPullRequest(string accountName, string repository, int | |
return _sharpBucketV2.Get(new Object(), overrideUrl); | ||
} | ||
|
||
internal ActivityInfo GetPullRequestActivity(string accountName, string repository, int pullRequestId){ | ||
internal List<Activity> GetPullRequestActivity(string accountName, string repository, int pullRequestId, int max = 0) { | ||
var overrideUrl = GetRepositoryUrl(accountName, repository, "pullrequests/" + pullRequestId + "/activity/"); | ||
return _sharpBucketV2.Get(new ActivityInfo(), overrideUrl); | ||
return GetPaginatedValues<Activity>(overrideUrl, max); | ||
} | ||
|
||
internal Merge AcceptAndMergePullRequest(string accountName, string repository, int pullRequestId){ | ||
|
@@ -163,9 +162,9 @@ internal Merge DeclinePullRequest(string accountName, string repository, int pul | |
return _sharpBucketV2.Get(new Merge(), overrideUrl); | ||
} | ||
|
||
internal object ListPullRequestComments(string accountName, string repository, int pullRequestId){ | ||
internal List<Comment> ListPullRequestComments(string accountName, string repository, int pullRequestId, int max = 0){ | ||
var overrideUrl = GetRepositoryUrl(accountName, repository, "pullrequests/" + pullRequestId + "/comments/"); | ||
return _sharpBucketV2.Get(new ActivityInfo(), overrideUrl); | ||
return GetPaginatedValues<Comment>(overrideUrl, max); | ||
} | ||
|
||
internal Comment GetPullRequestComment(string accountName, string repository, int pullRequestId, int commentId){ | ||
|
@@ -177,10 +176,9 @@ internal Comment GetPullRequestComment(string accountName, string repository, in | |
|
||
#region Branch Restrictions resource | ||
|
||
internal List<BranchRestriction> ListBranchRestrictions(string accountName, string repository){ | ||
internal List<BranchRestriction> ListBranchRestrictions(string accountName, string repository, int max = 0){ | ||
var overrideUrl = GetRepositoryUrl(accountName, repository, "branch-restrictions/"); | ||
var branchRestrictions = _sharpBucketV2.Get(new BranchRestrictionInfo(), overrideUrl); | ||
return branchRestrictions == null ? null : branchRestrictions.values; | ||
return GetPaginatedValues<BranchRestriction>(overrideUrl, max); | ||
} | ||
|
||
internal BranchRestriction PostBranchRestriction(string accountName, string repository, BranchRestriction restriction){ | ||
|
@@ -221,22 +219,22 @@ internal object GetPatch(string accountName, string repository, object options){ | |
|
||
#region Commits Resource | ||
|
||
internal CommitInfo ListCommits(string accountName, string repository, string branchortag = null ){ | ||
internal List<Commit> ListCommits(string accountName, string repository, string branchortag = null, int max = 0){ | ||
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. Maybe we should allow to set the max in all the paginated calls? |
||
var overrideUrl = GetRepositoryUrl(accountName, repository, "commits/"); | ||
if ( !string.IsNullOrEmpty( branchortag )){ | ||
overrideUrl += branchortag; | ||
} | ||
return _sharpBucketV2.Get(new CommitInfo(), overrideUrl); | ||
return GetPaginatedValues<Commit>(overrideUrl, max); | ||
} | ||
|
||
internal Commit GetCommit(string accountName, string repository, string revision){ | ||
var overrideUrl = GetRepositoryUrl(accountName, repository, "commit/" + revision); | ||
return _sharpBucketV2.Get(new Commit(), overrideUrl); | ||
} | ||
|
||
internal List<object> ListCommitComments(string accountName, string repository, string revision){ | ||
internal List<Comment> ListCommitComments(string accountName, string repository, string revision, int max = 0){ | ||
var overrideUrl = GetRepositoryUrl(accountName, repository, "commits/" + revision + "/comments/"); | ||
return _sharpBucketV2.Get(new List<object>(), overrideUrl); | ||
return GetPaginatedValues<Comment>(overrideUrl, max); | ||
} | ||
|
||
internal object GetCommitComment(string accountName, string repository, string revision, int commentId){ | ||
|
@@ -256,4 +254,4 @@ internal object DeleteCommitApproval(string accountName, string repository, stri | |
|
||
#endregion | ||
} | ||
} | ||
} |
This file contains 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
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.
We should change this in all files or on none. Otherwise it will just get quite inconsistent. Lets not change it for now and maybe do a PR just for that.
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.
Cool, will do