Exemplo n.º 1
0
        /// <summary>
        /// Setup and Get Repositories
        /// </summary>
        public async Task <Dictionary <string, Models.GetRepository> > SetupAndGetRepos()
        {
            var vsts    = new VisualStudioTeamServices(Username, Project.InternalName, PersonalAccessToken);
            var project = await vsts.GetProject();

            if (project == null)
            {
                project = await vsts.CreateProject(Project.InternalName, Project.Title);
            }
            if (project == null)
            {
                return(null);
            }

            var repositoriesNeeded = new Dictionary <string, Models.GetRepository>
            {
                { "Json", null },
                { Project.InternalName, null }
            };

            // Get repositories from VSTS
            var repositories = await vsts.GetProjectRepositories();

            if (repositories != null)
            {
                foreach (var repo in repositories)
                {
                    if (repositoriesNeeded.ContainsKey(repo.Name))
                    {
                        repositoriesNeeded[repo.Name] = repo;
                    }
                }
            }

            var repositoriesCreated = new Dictionary <string, Models.GetRepository>()
            {
            };

            // Double check each repository is setup correctly
            foreach (var repositoryNeeded in repositoriesNeeded)
            {
                var path = Path.Combine(BaseDirectory, repositoryNeeded.Key);
                repositoriesCreated.Add(repositoryNeeded.Key, await EnsureRepoistoryIsInitialised(repositoryNeeded.Key, path, repositoryNeeded.Value, vsts));
            }

            return(repositoriesCreated);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Ensure Repository is Intitialised
        /// </summary>
        public async Task <Models.GetRepository> EnsureRepoistoryIsInitialised(string name, string directory, Models.GetRepository repo, VisualStudioTeamServices vsts)
        {
            if (repo == null)
            {
                repo = await vsts.CreateRepository(name);
            }

            // Create repo folder
            FileHelper.CreateFolder(directory);

            // Check if local repo has been initalised
            if (!Repository.IsValid(directory))
            {
                // if not intialised then also check if directory is empty if not clear it
                if (!FileHelper.IsDirectoryEmpty(directory))
                {
                    Directory.Delete(directory, true);
                    FileHelper.CreateFolder(directory);
                }

                // clone remote repo
                var cloneOptions = new CloneOptions
                {
                    CredentialsProvider =
                        (_url, _user, _cred) => new UsernamePasswordCredentials
                    {
                        Username = "******",
                        Password = PersonalAccessToken
                    }
                };
                Repository.Clone(repo.RemoteUrl, directory, cloneOptions);
            }
            else
            {
                Pull(repo);
            }

            return(repo);
        }