Ejemplo n.º 1
0
        /// <summary>
        /// Returns a list of paginated values.
        /// </summary>
        /// <typeparam name="TValue">The type of the value.</typeparam>
        /// <param name="sharpBucketV2"></param>
        /// <param name="overrideUrl">The override URL.</param>
        /// <param name="max">Set to 0 for unlimited size.</param>
        /// <param name="requestParameters"></param>
        /// <returns></returns>
        /// <exception cref="System.Net.WebException">Thrown when the server fails to respond.</exception>
        public static List <TValue> GetPaginatedValues <TValue>(this SharpBucketV2 sharpBucketV2, string overrideUrl, int max = 0, IDictionary <string, object> requestParameters = null)
        {
            var isMaxConstrained = max > 0;

            var pageLen = (isMaxConstrained && max < DEFAULT_PAGE_LEN) ? max : DEFAULT_PAGE_LEN;

            var values = new List <TValue>();

            foreach (var page in IteratePages <TValue>(sharpBucketV2, overrideUrl, pageLen, requestParameters))
            {
                if (page == null)
                {
                    break;
                }

                if (isMaxConstrained &&
                    values.Count + page.Count >= max)
                {
                    values.AddRange(page.GetRange(0, max - values.Count));
                    Debug.Assert(values.Count == max);
                    break;
                }

                values.AddRange(page);
            }

            return(values);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Processes returned values page by page with passed pageDataProcessor action method.
        /// </summary>
        /// <typeparam name="TValue">The type of the value.</typeparam>
        /// <param name="sharpBucketV2"></param>
        /// <param name="overrideUrl">The override URL.</param>
        /// <param name="pageDataProcessor">Action to process page items.</param>
        /// <param name="max">Set to 0 for unlimited size.</param>
        /// <param name="requestParameters"></param>
        /// <exception cref="BitbucketV2Exception">Thrown when the server fails to respond.</exception>
        public static void ProcessPaginatedValues <TValue>(
            this SharpBucketV2 sharpBucketV2,
            string overrideUrl,
            Action <List <TValue> > pageDataProcessor,
            int max = 0,
            IDictionary <string, object> requestParameters = null)
        {
            var isMaxConstrained = max > 0;

            var pageLen = (isMaxConstrained && max < DEFAULT_PAGE_LEN) ? max : DEFAULT_PAGE_LEN;

            int processedCount = 0;

            foreach (var page in IteratePages <TValue>(sharpBucketV2, overrideUrl, pageLen, requestParameters))
            {
                if (page == null)
                {
                    break;
                }

                var pageItems = isMaxConstrained
                    ? page.GetRange(0, max - processedCount)
                    : page;
                pageDataProcessor(pageItems);

                processedCount += pageItems.Count;
                if (isMaxConstrained && processedCount >= max)
                {
                    break;
                }
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Generator that allows lazy access to paginated resources.
        /// </summary>
        private static IEnumerable <List <TValue> > IteratePages <TValue>(SharpBucketV2 sharpBucketV2, string overrideUrl, int pageLen = DEFAULT_PAGE_LEN, IDictionary <string, object> requestParameters = null)
        {
            Debug.Assert(!string.IsNullOrEmpty(overrideUrl));
            Debug.Assert(!overrideUrl.Contains("?"));

            overrideUrl = overrideUrl.Replace(SharpBucketV2.BITBUCKET_URL, "");

            if (requestParameters == null)
            {
                requestParameters = new Dictionary <string, object>();
            }

            requestParameters["pagelen"] = pageLen;

            while (true)
            {
                var response = sharpBucketV2.Get <IteratorBasedPage <TValue> >(overrideUrl, requestParameters);
                if (response == null)
                {
                    break;
                }

                yield return(response.values);

                if (string.IsNullOrWhiteSpace(response.next))
                {
                    break;
                }

                var uri         = new Uri(response.next);
                var parsedQuery = HttpUtility.ParseQueryString(uri.Query);
                requestParameters = parsedQuery.AllKeys.ToDictionary <string, string, object>(key => key, parsedQuery.Get);
            }
        }
Ejemplo n.º 4
0
 public static SharpBucketV2 GetV2ClientAuthenticatedWithOAuth()
 {
     var sharpbucket = new SharpBucketV2();
      // Reads test data information from a file, you should structure it like this:
      // By default it reads from c:\
      // ApiKey:yourApiKey
      // SecretApiKey:yourSecretApiKey
      // AccountName:yourAccountName
      // Repository:testRepository
      var lines = File.ReadAllLines("c:\\TestInformationOauth.txt");
      var consumerKey = lines[0].Split(':')[1];
      var consumerSecretKey = lines[1].Split(':')[1];
      sharpbucket.OAuth2LeggedAuthentication(consumerKey, consumerSecretKey);
      return sharpbucket;
 }
Ejemplo n.º 5
0
 public static SharpBucketV2 GetV2ClientAuthenticatedWithBasicAuthentication()
 {
     var sharpbucket = new SharpBucketV2();
      // Reads test data information from a file, you should structure it like this:
      // By default it reads from c:\
      // Username:yourUsername
      // Password:yourPassword
      // AccountName:yourAccountName
      // Repository:testRepository
      var lines = File.ReadAllLines("c:\\TestInformation.txt");
      var email = lines[0].Split(':')[1];
      var password = lines[1].Split(':')[1];
      sharpbucket.BasicAuthentication(email, password);
      return sharpbucket;
 }
Ejemplo n.º 6
0
 /// <summary>
 /// Returns an enumeration of paginated values. The pages are requested lazily while iterating on the values.
 /// </summary>
 /// <typeparam name="TValue">The type of the value.</typeparam>
 /// <param name="sharpBucketV2"></param>
 /// <param name="overrideUrl">The override URL.</param>
 /// <param name="requestParameters"></param>
 /// <param name="pageLen">The size of a page.</param>
 /// <returns>A lazy enumerable over the values.</returns>
 /// <exception cref="BitbucketV2Exception">Thrown when the server fails to respond.</exception>
 public static async IAsyncEnumerable <TValue> EnumeratePaginatedValuesAsync <TValue>(
     this SharpBucketV2 sharpBucketV2,
     string overrideUrl,
     IDictionary <string, object> requestParameters = null,
     int?pageLen = null,
     [EnumeratorCancellation] CancellationToken token = default)
 {
     await foreach (var page in IteratePagesAsync <TValue>(sharpBucketV2, overrideUrl, pageLen ?? DEFAULT_PAGE_LEN, requestParameters, token))
     {
         if (page == null)
         {
             yield break;
         }
         foreach (var item in page)
         {
             yield return(item);
         }
     }
 }
Ejemplo n.º 7
0
 public void Init()
 {
     sharpBucket = TestHelpers.GetV2ClientAuthenticatedWithBasicAuthentication();
      teamsEndPoint = sharpBucket.TeamsEndPoint(TEAM_NAME);
 }
Ejemplo n.º 8
0
        private static void TestRestRepositoriesEndPoint(SharpBucketV2 sharpBucket)
        {
            var repositoriesEndPoint = sharpBucket.RepositoriesEndPoint();
            var repositories = repositoriesEndPoint.ListRepositories(accountName);
            var publicRepositories = repositoriesEndPoint.ListPublicRepositories();
            var repositoryResource = repositoriesEndPoint.RepositoryResource(accountName, repository);
            //var repoInfo = new RepositoryInfo();
            //var newRepository = repositoriesEndPoint.PutRepository(repo, accountName, repository);
            //var deletedRepository = repositoriesEndPoint.DeleteRepository(newRepository, accountName, repository);
            var watchers = repositoryResource.ListWatchers();
            var forks = repositoryResource.ListForks();
            var branchRestictions = repositoryResource.ListBranchRestrictions();
            var newRestrictions = repositoryResource.PostBranchRestriction(new BranchRestriction());
            int restrictionId = 1;
            var restriction = repositoryResource.GetBranchRestriction(restrictionId);
            var newRestiction = repositoryResource.PutBranchRestriction(new BranchRestriction());
            var deletedRestiction = repositoryResource.DeleteBranchRestriction(restrictionId);
            var diff = repositoryResource.GetDiff(null);
            var patch = repositoryResource.GetPatch(null);
            var commits = repositoryResource.ListCommits();
            string commitId = "sdfsdf";
            var commit = repositoryResource.GetCommit(commitId);
            var commitComments = repositoryResource.ListCommitComments(commitId);
            int commentId = 10;
            var commitComment = repositoryResource.GetCommitComment(commitId, commentId);
            var commitApproval = repositoryResource.ApproveCommit(commitId);
            var deleteApproval = repositoryResource.DeleteCommitApproval(commitId);

            var r = repositoriesEndPoint.RepositoryResource(accountName, repository);
            var dr = r.DeleteRepository();
            var w = r.ListWatchers();
            var f = r.ListForks();
            var br = r.ListBranchRestrictions();
            var gr = r.GetBranchRestriction(restrictionId);
            var nr = r.PostBranchRestriction(new BranchRestriction());
            var dbr = r.DeleteBranchRestriction(restrictionId);
            var diff2 = r.GetDiff(new object());
            var patch2 = r.GetPatch(new object());
            var commits2 = r.ListCommits();
            var commit2 = r.GetCommit(commitId);
            var commitComments2 = r.ListCommitComments(commitId);
            var commitComment2 = r.GetCommitComment(commitId, commentId);
            var commitApproval2 = r.ApproveCommit(commitId);
            var deleteApproval2 = r.DeleteCommitApproval(commitId);

            var pullRequestsResource = r.PullRequestsResource();
            var pullRequests = pullRequestsResource.ListPullRequests();
            var source = new Source{
                branch = new Branch{name = "develop"},
                repository = new Repository{
                    full_name = repository
                }
            };
            var destination = new Source{
                branch = new Branch{name = "master"},
                commit = new Commit{hash = "56c3aca"}
            };
            var newRequest = new PullRequest{
                title = "testing new one",
                description = "My new description",
                source = source,
                destination = destination
            };
            //var newPullRequest = repositoriesEndPoint.PostPullRequest(accountName, repository, newRequest);
            //var updatedPullRequest = repositoriesEndPoint.PutPullRequest(accountName, repository, new PullRequest());
            //var pullRequestId = 1;
            //var pullRequest = repositoriesEndPoint.GetPullRequest(accountName, repository, pullRequestId);
            //pullRequest.title = "HEHEHEH";
            //var updatedPullRequest = repositoriesEndPoint.PutPullRequest(accountName, repository, pullRequest);

            //var commitsInPullRequest = repositoriesEndPoint.ListPullRequestCommits(accountName, repository, pullRequestId);
            //var postPullRequestApproval = repositoriesEndPoint.ApprovePullRequest(accountName, repository, pullRequestId);
            //var deletePullRequest = repositoriesEndPoint.RemovePullRequestApproval(accountName, repository, pullRequestId);
            //var getDiffForPullRequest = repositoriesEndPoint.GetDiffForPullRequest(accountName, repository, pullRequestId);
            //var pullRequestLog = repositoriesEndPoint.GetPullRequestLog(accountName, repository);
            //var pullRequestActivity = repositoriesEndPoint.GetPullRequestActivity(accountName, repository, pullRequestId);
            //var acceptPullRequest = repositoriesEndPoint.AcceptAndMergePullRequest(accountName, repository, pullRequestId);
            //var declinePullRequest = repositoriesEndPoint.DeclinePullRequest(accountName, repository, pullRequestId);
            //var pullRequestComments = repositoriesEndPoint.ListPullRequestComments(accountName, repository, pullRequestId);
            //var commentId = 10;
            //var pullRequestComment = repositoriesEndPoint.GetPullRequestComment(accountName, repository, pullRequestId, commentId);

            //var pullRequestsEP = repositoriesEndPoint.PullReqestsResource(accountName, repository);
            //var PRs = pullRequestsEP.ListPullRequests();
            //var newPR = pullRequestsEP.PostPullRequest(new PullRequest());
            //var updatedPR = pullRequestsEP.PutPullRequest(new PullRequest());
            //var PRId = 10;
            //var PR = pullRequestsEP.GetPullRequest(PRId);
            //var commitsInPR = pullRequestsEP.ListPullRequestCommits(PRId);
            //var postPRApproval = pullRequestsEP.ApprovePullRequest(PRId);
            //var deletePR = pullRequestsEP.RemovePullRequestApproval(PRId);
            //var getDiffForPR = pullRequestsEP.GetDiffForPullRequest(PRId);
            //var PRLog = pullRequestsEP.GetPullRequestLog();
            //var PRActivity = pullRequestsEP.GetPullRequestActivity(PRId);
            //var acceptPR = pullRequestsEP.AcceptAndMergePullRequest(PRId);
            //var declinePR = pullRequestsEP.DeclinePullRequest(PRId);
            //var PRComments = pullRequestsEP.ListPullRequestComments(PRId);
            //var cId = 10;
            //var PRComment = pullRequestsEP.GetPullRequestComment(PRId, cId);

            //var PRId2 = 10;
            //var pullRequestEP = pullRequestsEP.PullRequestEndPoint(PRId);
            //var PR2 = pullRequestEP.GetPullRequest();
            //var commitsInPR2 = pullRequestEP.ListPullRequestCommits();
            //var postPR2Approval = pullRequestEP.ApprovePullRequest();
            //var deletePR2 = pullRequestEP.RemovePullRequestApproval();
            //var getDiffForPR2 = pullRequestEP.GetDiffForPullRequest();
            //var PR2Activity = pullRequestEP.GetPullRequestActivity();
            //var acceptPR2 = pullRequestEP.AcceptAndMergePullRequest();
            //var declinePR2 = pullRequestEP.DeclinePullRequest();
            //var PR2Comments = pullRequestEP.ListPullRequestComments();
            //var cId2 = 10;
            //var PR2Comment = pullRequestEP.GetPullRequestComment(cId2);
        }
Ejemplo n.º 9
0
 private static void TestApiV2()
 {
     var sharpBucket = new SharpBucketV2();
     ReadTestDataOauth();
     sharpBucket.OAuth2LeggedAuthentication(consumerKey, consumerSecretKey);
     //TestUsersEndPoint(sharpBucket);
     //TestTeamsEndPoint(sharpBucket);
     TestRestRepositoriesEndPoint(sharpBucket);
 }
Ejemplo n.º 10
0
 private static void TestUsersEndPoint(SharpBucketV2 sharpBucket)
 {
     var usersEndPoint = sharpBucket.UsersEndPoint(accountName);
     var profile = usersEndPoint.GetProfile();
     var followers = usersEndPoint.ListFollowers();
     var following = usersEndPoint.ListFollowing();
     var repositories = usersEndPoint.ListRepositories();
 }
Ejemplo n.º 11
0
 private static void TestTeamsEndPoint(SharpBucketV2 sharpBucket)
 {
     var TEAM_NAME = "";
     var teamsEndPoint = sharpBucket.TeamsEndPoint(TEAM_NAME);
     var teamProfile = teamsEndPoint.GetProfile();
     var teamMembers = teamsEndPoint.ListMembers();
     var teamFollowers = teamsEndPoint.ListFollowers();
     var teamFollowing = teamsEndPoint.ListFollowing();
 }
Ejemplo n.º 12
0
 public void Init()
 {
     sharpBucket = TestHelpers.GetV2ClientAuthenticatedWithBasicAuthentication();
      var repositoriesEndPoint = sharpBucket.RepositoriesEndPoint();
      repositoryResource = repositoriesEndPoint.RepositoryResource(ACCOUNT_NAME, REPOSITORY_NAME);
 }
 public void Init()
 {
     sharpBucket = TestHelpers.GetV2ClientAuthenticatedWithBasicAuthentication();
      repositoriesEndPoint = sharpBucket.RepositoriesEndPoint();
 }
 /// <summary>
 /// Returns an enumeration of paginated values. The pages are requested lazily while iterating on the values.
 /// </summary>
 /// <typeparam name="TValue">The type of the value.</typeparam>
 /// <param name="sharpBucketV2"></param>
 /// <param name="overrideUrl">The override URL.</param>
 /// <param name="requestParameters"></param>
 /// <param name="pageLen">The size of a page.</param>
 /// <returns>A lazy enumerable over the values.</returns>
 /// <exception cref="BitbucketV2Exception">Thrown when the server fails to respond.</exception>
 public static IEnumerable <TValue> EnumeratePaginatedValues <TValue>(this SharpBucketV2 sharpBucketV2, string overrideUrl, IDictionary <string, object> requestParameters = null, int?pageLen = null)
 {
     return(IteratePages <TValue>(sharpBucketV2, overrideUrl, pageLen ?? DEFAULT_PAGE_LEN, requestParameters)
            .TakeWhile(page => page != null)
            .SelectMany(page => page));
 }
Ejemplo n.º 15
0
 public void Init()
 {
     sharpBucket = TestHelpers.GetV2ClientAuthenticatedWithBasicAuthentication();
      usersEndPoint = sharpBucket.UsersEndPoint(ACCOUNT_NAME);
 }