Beispiel #1
0
        public async Task CreateOrUpdateAsync(
            string commitMessage,
            string title,
            string description,
            GitHubBranch baseBranch,
            GitHubProject origin,
            PullRequestOptions options,
            IGitHubClient client)
        {
            options = options ?? new PullRequestOptions();
            client.AdjustOptionsToCapability(options);

            var upstream = baseBranch.Project;

            GitHubBranch      originBranch        = null;
            GitHubPullRequest pullRequestToUpdate = null;

            IUpdateBranchNamingStrategy namingStrategy = options.BranchNamingStrategy
                                                         ?? new SingleBranchNamingStrategy("UpdateDependencies");

            string upgradeBranchPrefix = namingStrategy.Prefix(baseBranch.Name);

            if (!options.ForceCreate)
            {
                string myAuthorId = await client.GetMyAuthorIdAsync();

                pullRequestToUpdate = await client.SearchPullRequestsAsync(
                    upstream,
                    upgradeBranchPrefix,
                    myAuthorId);

                if (pullRequestToUpdate == null)
                {
                    Trace.TraceInformation($"No existing pull request found.");
                }
                else
                {
                    Trace.TraceInformation(
                        $"Pull request already exists for {upgradeBranchPrefix} in {upstream.Segments}. " +
                        $"#{pullRequestToUpdate.Number}, '{pullRequestToUpdate.Title}'");

                    GitCommit headCommit = await client.GetCommitAsync(
                        origin,
                        pullRequestToUpdate.Head.Sha);

                    string blockedReason = GetUpdateBlockedReason(
                        pullRequestToUpdate,
                        headCommit,
                        upgradeBranchPrefix,
                        origin,
                        options);

                    if (blockedReason == null)
                    {
                        if (options.TrackDiscardedCommits)
                        {
                            await PostDiscardedCommitCommentAsync(
                                baseBranch.Project,
                                pullRequestToUpdate,
                                headCommit,
                                client);
                        }

                        originBranch = new GitHubBranch(
                            pullRequestToUpdate.Head.Ref,
                            origin);
                    }
                    else
                    {
                        string comment =
                            $"Couldn't update this pull request: {blockedReason}\n" +
                            $"Would have applied '{commitMessage}'";

                        Trace.TraceInformation($"Sending comment to PR: {comment}");

                        await client.PostCommentAsync(upstream, pullRequestToUpdate.Number, comment);

                        return;
                    }
                }

                // No existing branch to update: push to a new one.
                if (originBranch == null)
                {
                    string newBranchName =
                        namingStrategy.Prefix(baseBranch.Name) +
                        namingStrategy.CreateFreshBranchNameSuffix(baseBranch.Name);

                    originBranch = new GitHubBranch(newBranchName, origin);
                }

                PushNewCommit(originBranch, commitMessage, client);

                if (pullRequestToUpdate != null)
                {
                    await client.UpdateGitHubPullRequestAsync(
                        upstream,
                        pullRequestToUpdate.Number,
                        title,
                        description,
                        maintainersCanModify : options.MaintainersCanModify);
                }
                else
                {
                    await client.PostGitHubPullRequestAsync(
                        title,
                        description,
                        originBranch,
                        baseBranch,
                        options.MaintainersCanModify);
                }
            }
        }