Exemplo n.º 1
0
 public AzureDevOpsPush(AzureDevOpsRefUpdate refUpdate, AzureDevOpsCommit vstsCommit)
 {
     RefUpdates = new List <AzureDevOpsRefUpdate> {
         refUpdate
     };
     Commits = new List <AzureDevOpsCommit> {
         vstsCommit
     };
 }
        public async Task PushFilesAsync(
            List <GitFile> filesToCommit,
            string repoUri,
            string branch,
            string commitMessage)
        {
            _logger.LogInformation($"Pushing files to '{branch}'...");

            var    changes  = new List <AzureDevOpsChange>();
            string repoName = SetApiUriAndGetRepoName(repoUri);

            foreach (GitFile gitfile in filesToCommit)
            {
                string blobSha = await CheckIfFileExistsAsync(repoUri, gitfile.FilePath, branch);

                var change = new AzureDevOpsChange(gitfile.FilePath, gitfile.Content);

                if (!string.IsNullOrEmpty(blobSha))
                {
                    change.ChangeType = AzureDevOpsChangeType.Edit;
                }

                changes.Add(change);
            }

            var commit = new AzureDevOpsCommit(changes, "Dependency files update");

            string latestSha = await GetLastCommitShaAsync(repoName, branch);

            var refUpdate = new AzureDevOpsRefUpdate($"refs/heads/{branch}", latestSha);

            var azureDevOpsPush = new AzureDevOpsPush(refUpdate, commit);

            string body = JsonConvert.SerializeObject(azureDevOpsPush, _serializerSettings);

            // Azure DevOps' contents API is only supported in version 5.0-preview.2
            await this.ExecuteGitCommand(
                HttpMethod.Post,
                $"repositories/{repoName}/pushes",
                _logger,
                body,
                "5.0-preview.2");

            _logger.LogInformation($"Pushing files to '{branch}' succeeded!");
        }