/// <summary>
        /// Builds the list of repositories for the selected team or user account.
        ///
        /// May be called recursively if multiple pages of results exist
        /// </summary>
        /// <param name="url">URL of next page of results</param>
        /// <returns></returns>
        private async Task GetRepositoriesForAccount(string url = null)
        {
            var baseUrl = string.Format(BitbucketAccountReposUrl, (TeamAccountBackupEnabled) ? _config["teamName"] : _config["userName"]);

            var apiUrl = (string.IsNullOrWhiteSpace(url)) ? baseUrl : url;

            var response = await Client.GetAsync(apiUrl);

            var jsonString = await response.Content.ReadAsStringAsync();

            dynamic json = JObject.Parse(jsonString);

            foreach (var repo in json.values)
            {
                _repositories.Add(BitBucketRepository.Create(repo));
            }

            int numPages    = json.size / json.pagelen;
            int currentPage = int.Parse((string)json.page);
            int nextPage    = (currentPage < numPages) ? currentPage + 1 : 0;

            if (nextPage != 0 && nextPage <= numPages)
            {
                await GetRepositoriesForAccount(string.Join("?", baseUrl, "page=" + nextPage));
            }
        }
        private void FetchUpdatesForRepo(BitBucketRepository repo, string repoDestination)
        {
            Logger.Current.Log(_logLabel, string.Format("Updating {0} repo from remotes...", repo.Name));

            var logMessage = string.Empty;

            using (var existingRepo = new Repository(repoDestination))
            {
                var options = new FetchOptions();
                options.CredentialsProvider = (url, usernameFromUrl, types) =>
                                              new UsernamePasswordCredentials()
                {
                    Username = _config["user"],
                    Password = _config["password"]
                };

                foreach (Remote remote in existingRepo.Network.Remotes)
                {
                    IEnumerable <string> refSpecs = remote.FetchRefSpecs.Select(x => x.Specification);
                    Commands.Fetch(existingRepo, remote.Name, refSpecs, options, logMessage);
                }
            }

            if (!string.IsNullOrWhiteSpace(logMessage))
            {
                Logger.Current.Log(_logLabel, logMessage);
            }

            Logger.Current.Log(_logLabel, string.Format("Fetch complete for {0} repo.", repo.Name));
        }
Example #3
0
        public static BitBucketRepository Create(JToken token)
        {
            var result = new BitBucketRepository();

            dynamic jsonValues = token;

            result.Name = jsonValues.slug;
            result.SourceControlProvider = jsonValues.scm;
            result.HasWiki = jsonValues.has_wiki;

            var cloneUrls = jsonValues.links.clone;

            foreach (var child in cloneUrls.Children())
            {
                if (child.name == "https")
                {
                    result.CloneUrl = child.href;
                }

                if (child.name == "ssh")
                {
                    result.SshCloneUrl = child.href;
                }
            }

            return(result);
        }
        private void CloneBareRepo(BitBucketRepository repo, string repoDestination)
        {
            // Already exists or not empty
            if (Directory.Exists(repoDestination) && Directory.EnumerateFileSystemEntries(repoDestination).Any())
            {
                Logger.Current.Log(_logLabel, string.Format("Unable to clone {0} repo to an existing or non-empty directory.  Aborting operation.", repo.Name));
                return;
            }

            Logger.Current.Log(_logLabel, string.Format("Creating bare clone of repo {0} at {1}", repo.Name, repoDestination));

            var cloneOpts = new CloneOptions
            {
                CredentialsProvider =
                    (url, user, cred) => new UsernamePasswordCredentials
                {
                    Username = _config["user"],
                    Password = _config["password"]
                },
                IsBare = true
            };

            Repository.Clone(repo.CloneUrl, repoDestination, cloneOpts);
        }