Exemple #1
0
        public async Task Initialize()
        {
            var masterRepository = await gitHubService.GetRepository(RepositoryTarget.Bot);

            if (!Directory.Exists(MasterRepositoryDirectory))
            {
                await gitService.CloneRepository(masterRepository.CloneUrl, MasterRepositoryDirectory);
            }
            else
            {
                await gitService.FetchChanges(MasterRepositoryDirectory);

                await gitService.Checkout(MasterRepositoryDirectory, "master");
            }
        }
        public override Task ExecuteAsync()
        {
            if (Options.AzdoOptions.Path != Options.GitOptions.Path)
            {
                throw new InvalidOperationException(
                          $"The file path for GitHub '{Options.GitOptions.Path}' must be equal to the file path for AzDO '{Options.AzdoOptions.Path}'.");
            }

            _loggerService.WriteHeading("PUBLISHING IMAGE INFO");

            string repoPath = Path.Combine(Path.GetTempPath(), "imagebuilder-repos", Options.GitOptions.Repo);

            if (Directory.Exists(repoPath))
            {
                FileHelper.ForceDeleteDirectory(repoPath);
            }

            try
            {
                _loggerService.WriteSubheading("Cloning GitHub repo");
                using IRepository repo = _gitService.CloneRepository(
                          $"https://github.com/{Options.GitOptions.Owner}/{Options.GitOptions.Repo}",
                          repoPath,
                          new CloneOptions
                {
                    BranchName = Options.GitOptions.Branch
                });

                Uri imageInfoPathIdentifier = GitHelper.GetBlobUrl(Options.GitOptions);

                _loggerService.WriteSubheading("Calculating new image info content");
                string?imageInfoContent = GetUpdatedImageInfo(repoPath);

                if (imageInfoContent is null)
                {
                    _loggerService.WriteMessage($"No changes to the '{imageInfoPathIdentifier}' file were needed.");
                    return(Task.CompletedTask);
                }

                _loggerService.WriteMessage(
                    $"The '{imageInfoPathIdentifier}' file has been updated with the following content:" +
                    Environment.NewLine + imageInfoContent + Environment.NewLine);

                if (!Options.IsDryRun)
                {
                    UpdateGitRepos(imageInfoContent, repoPath, repo);
                }
            }
            finally
            {
                if (Directory.Exists(repoPath))
                {
                    FileHelper.ForceDeleteDirectory(repoPath);
                }
            }

            return(Task.CompletedTask);
        }