private async Task <bool> PushUploadsAsync(
            BuildManifestLocation location,
            string message,
            string remoteCommit,
            IEnumerable <SupplementaryUploadRequest> uploads)
        {
            GitObject[] objects = uploads
                                  .Select(upload => new GitObject
            {
                Path = upload.GetAbsolutePath(location.GitHubBasePath),
                Mode = GitObject.ModeFile,
                Type = GitObject.TypeBlob,
                // Always upload files using LF to avoid bad dev scenarios with Git autocrlf.
                Content = upload.Contents.Replace("\r\n", "\n")
            })
                                  .ToArray();

            GitTree tree = await _github.PostTreeAsync(
                location.GitHubProject,
                remoteCommit,
                objects);

            GitCommit commit = await _github.PostCommitAsync(
                location.GitHubProject,
                message,
                tree.Sha,
                new[] { remoteCommit });

            try
            {
                // Only fast-forward. Don't overwrite other changes: throw exception instead.
                await _github.PatchReferenceAsync(
                    location.GitHubProject,
                    location.GitHubRef,
                    commit.Sha,
                    force : false);
            }
            catch (NotFastForwardUpdateException e)
            {
                // Retry if there has been a commit since this update attempt started.
                Trace.TraceInformation($"Retrying: {e.Message}");
                return(false);
            }

            return(true);
        }
示例#2
0
        public static async Task <GitReference> PushChangesAsync(IGitHubClient client, IGitOptionsHost options, string commitMessage, Func <GitHubBranch, Task <IEnumerable <GitObject> > > getChanges)
        {
            GitOptions    gitOptions = options.GitOptions;
            GitHubProject project    = new GitHubProject(gitOptions.Repo, gitOptions.Owner);
            GitHubBranch  branch     = new GitHubBranch(gitOptions.Branch, project);

            IEnumerable <GitObject> changes = await getChanges(branch);

            if (!changes.Any())
            {
                return(null);
            }

            string       masterRef     = $"heads/{gitOptions.Branch}";
            GitReference currentMaster = await client.GetReferenceAsync(project, masterRef);

            string masterSha = currentMaster.Object.Sha;

            if (!options.IsDryRun)
            {
                GitTree tree = await client.PostTreeAsync(project, masterSha, changes.ToArray());

                GitCommit commit = await client.PostCommitAsync(
                    project, commitMessage, tree.Sha, new[] { masterSha });

                // Only fast-forward. Don't overwrite other changes: throw exception instead.
                return(await client.PatchReferenceAsync(project, masterRef, commit.Sha, force : false));
            }
            else
            {
                Logger.WriteMessage($"The following files would have been updated at {gitOptions.Owner}/{gitOptions.Repo}/{gitOptions.Branch}:");
                Logger.WriteMessage();
                foreach (GitObject gitObject in changes)
                {
                    Logger.WriteMessage($"{gitObject.Path}:");
                    Logger.WriteMessage(gitObject.Content);
                    Logger.WriteMessage();
                }

                return(null);
            }
        }
        private async Task <bool> PushUploadsAsync(
            GitHubProject project,
            string @ref,
            string basePath,
            string message,
            string remoteCommit,
            IEnumerable <SupplementaryUploadRequest> uploads)
        {
            GitObject[] objects = uploads
                                  .Select(upload => new GitObject
            {
                Path    = $"{basePath}/{upload.Path}",
                Mode    = GitObject.ModeFile,
                Type    = GitObject.TypeBlob,
                Content = upload.Contents
            })
                                  .ToArray();

            GitTree tree = await _github.PostTreeAsync(project, remoteCommit, objects);

            GitCommit commit = await _github.PostCommitAsync(
                project,
                message,
                tree.Sha,
                new[] { remoteCommit });

            try
            {
                // Only fast-forward. Don't overwrite other changes: throw exception instead.
                await _github.PatchReferenceAsync(project, @ref, commit.Sha, force : false);
            }
            catch (NotFastForwardUpdateException e)
            {
                // Retry if there has been a commit since this update attempt started.
                Trace.TraceInformation($"Retrying: {e.Message}");
                return(false);
            }

            return(true);
        }