예제 #1
0
        public async Task <string> Create()
        {
            // PullRequest
            opts.GitHubPRTitle = TemplateUtil.Apply(opts, opts.GitHubPRTitleTemplate);
            opts.GitHubPRBody  = TemplateUtil.Apply(opts, opts.GitHubPRBodyTemplate);
            var newPr = new NewPullRequest(
                opts.GitHubPRTitle, opts.ManifestDeployBranch, opts.ManifestBaseBranch)
            {
                Body = opts.GitHubPRBody
            };
            var pr = await client.PullRequest.Create(
                opts.ManifestRepoOwner, opts.ManifestRepoName, newPr);

            // Add Reviewers
            var reviewers = new PullRequestReviewRequest(opts.GitHubReviewers.ToList());
            var pr1       = await client.PullRequest.ReviewRequest.Create(
                opts.ManifestRepoOwner, opts.ManifestRepoName, pr.Number, reviewers);

            // Add Assignees
            var assignees = new AssigneesUpdate(opts.GitHubAssignees.ToList());
            var pr2       = await aClient.AddAssignees(
                opts.ManifestRepoOwner, opts.ManifestRepoName, pr.Number, assignees);

            return($"Created deploy request: {pr.HtmlUrl}");
        }
예제 #2
0
        // creates a new pr to the target branch after the old pr is merged
        public async Task CreateNewPR(MergeData data)
        {
            var client = await GetInstallationClientAsync(data.InstallationId);

            // check that the pr went through first
            var mergedPR = await client.PullRequest.Get(data.RepositoryOwner, data.RepositoryName, data.PullRequestNumber);

            if (!mergedPR.Merged)
            {
                log.LogError("The pull request was not merged yet.");
                return;
            }

            var sourceBranch = mergedPR.Base.Ref; // this could? be a commit hash too
            var pr           = new NewPullRequest($"AutoMerge: {sourceBranch} to {data.BranchName} by @{data.MergeIssuer}", sourceBranch, data.BranchName);

            log.LogInformation($"Creating new PR: {sourceBranch} to {data.BranchName}");
            var result = await client.PullRequest.Create(data.RepositoryOwner, data.RepositoryName, pr);

            // assign ownership
            log.LogInformation($"Created PR {result.Number}. Assigning ownership to {data.MergeIssuer}");
            var reviewReq = new PullRequestReviewRequest(new List <string> {
                data.MergeIssuer
            });
            await client.PullRequest.ReviewRequest.Create(data.RepositoryOwner, data.RepositoryName, result.Number, reviewReq);

            // TODO handle invalid BranchName

            log.LogInformation("Leaving a comment on closed PR.");
            // leave a comment on the merged PR
            var comment = $"Ok @{data.MergeIssuer}, I've created Pull Request #{result.Number} for you that merges `{sourceBranch}` into `{data.BranchName}`.";
            await client.Issue.Comment.Create(data.RepositoryOwner, data.RepositoryName, data.PullRequestNumber, comment);
        }
예제 #3
0
        public void AddReviewerToPullRequest(int pullRequestNumber, string[] userNames)
        {
            _logger.LogDebug("Adding reviewers to pull request {pullRequestNumber}, user names: {userNames}", pullRequestNumber, userNames);
            var pullRequestReviewRequestModel = new PullRequestReviewRequest(userNames);

            CreateGitHubClient().PullRequest.ReviewRequest.Create(_repositoryOwner, _repositoryName, pullRequestNumber, pullRequestReviewRequestModel);
        }
        /// <summary>
        /// Deletes review request for given users on a pull request.
        /// </summary>
        /// <remarks>https://developer.github.com/v3/pulls/review_requests/#delete-a-review-request</remarks>
        /// <param name="owner">The owner of the repository</param>
        /// <param name="name">The name of the repository</param>
        /// <param name="number">The pull request review comment number</param>
        /// <param name="users">List of logins of users that will be not longer requested for review</param>
        public IObservable <Unit> Delete(string owner, string name, int number, PullRequestReviewRequest users)
        {
            Ensure.ArgumentNotNullOrEmptyString(owner, "owner");
            Ensure.ArgumentNotNullOrEmptyString(name, "name");
            Ensure.ArgumentNotNull(users, "users");

            return(_client.Delete(owner, name, number, users).ToObservable());
        }
        public async Task CreatesRequests()
        {
            var number = await CreateTheWorld(_github, _context, createReviewRequests : false);

            var reviewRequestToCreate = PullRequestReviewRequest.ForReviewers(_collaboratorLogins);

            var pr = await _client.Create(_context.RepositoryOwner, _context.RepositoryName, number, reviewRequestToCreate);

            Assert.Equal(_collaboratorLogins.ToList(), pr.RequestedReviewers.Select(rr => rr.Login));
        }
        public async Task CreatesRequestsWithRepositoryId()
        {
            var pullRequestId = await CreateTheWorld(_github, _context, createReviewRequests : false);

            var reviewRequestToCreate = new PullRequestReviewRequest(_collaboratorLogins);

            var pr = await _client.Create(_context.RepositoryId, pullRequestId, reviewRequestToCreate);

            Assert.Equal(_collaboratorLogins.ToList(), pr.RequestedReviewers.Select(rr => rr.Login));
        }
        public async Task DeletesRequestsWithRepositoryId()
        {
            var pullRequestId = await CreateTheWorld(_github, _context);

            var reviewRequestsBeforeDelete = await _client.GetAll(_context.RepositoryId, pullRequestId);

            var reviewRequestToCreate = new PullRequestReviewRequest(_collaboratorLogins);
            await _client.Delete(_context.RepositoryId, pullRequestId, reviewRequestToCreate);

            var reviewRequestsAfterDelete = await _client.GetAll(_context.RepositoryId, pullRequestId);

            Assert.NotEmpty(reviewRequestsBeforeDelete);
            Assert.Empty(reviewRequestsAfterDelete);
        }
        public async Task DeletesRequests()
        {
            var number = await CreateTheWorld(_github, _context);

            var reviewRequestsBeforeDelete = await _client.Get(_context.RepositoryOwner, _context.RepositoryName, number);

            var reviewRequestToCreate = PullRequestReviewRequest.ForReviewers(_collaboratorLogins);
            await _client.Delete(_context.RepositoryOwner, _context.RepositoryName, number, reviewRequestToCreate);

            var reviewRequestsAfterDelete = await _client.Get(_context.RepositoryOwner, _context.RepositoryName, number);

            Assert.NotEmpty(reviewRequestsBeforeDelete.Users);
            Assert.Empty(reviewRequestsAfterDelete.Users);
        }
예제 #9
0
            public void PostsToCorrectUrlWithRepositoryId()
            {
                var gitHubClient = Substitute.For <IGitHubClient>();
                var client       = new ObservablePullRequestReviewRequestsClient(gitHubClient);

                IReadOnlyList <string> fakeReviewers = new List <string> {
                    "zxc", "asd"
                };
                var pullRequestReviewRequest = PullRequestReviewRequest.ForReviewers(fakeReviewers);

                client.Create(42, 13, pullRequestReviewRequest);

                gitHubClient.Received().PullRequest.ReviewRequest.Create(42, 13, pullRequestReviewRequest);
            }
예제 #10
0
            public void PostsToCorrectUrl()
            {
                var gitHubClient = Substitute.For <IGitHubClient>();
                var client       = new ObservablePullRequestReviewRequestsClient(gitHubClient);

                IReadOnlyList <string> fakeReviewers = new List <string> {
                    "zxc", "asd"
                };
                var pullRequestReviewRequest = new PullRequestReviewRequest(fakeReviewers);

                client.Create("fakeOwner", "fakeRepoName", 13, pullRequestReviewRequest);

                gitHubClient.Received().PullRequest.ReviewRequest.Create("fakeOwner", "fakeRepoName", 13, pullRequestReviewRequest);
            }
예제 #11
0
            public async Task PostsToCorrectUrl()
            {
                var gitHubClient = Substitute.For <IGitHubClient>();
                var client       = new ObservablePullRequestReviewRequestsClient(gitHubClient);

                IReadOnlyList <string> fakeReviewers = new List <string> {
                    "zxc", "asd"
                };
                var pullRequestReviewRequest = PullRequestReviewRequest.ForReviewers(fakeReviewers);

                await client.Delete("owner", "name", 13, pullRequestReviewRequest);

                gitHubClient.Received().PullRequest.ReviewRequest.Delete("owner", "name", 13, pullRequestReviewRequest);
            }
예제 #12
0
            public void PostsToCorrectUrlWithRepositoryId()
            {
                var connection = Substitute.For <IApiConnection>();
                var client     = new PullRequestReviewRequestsClient(connection);
                IReadOnlyList <string> fakeReviewers = new List <string> {
                    "zxc", "asd"
                };
                var pullRequestReviewRequest = PullRequestReviewRequest.ForReviewers(fakeReviewers);

                client.Create(42, 13, pullRequestReviewRequest);

                connection.Received().Post <PullRequest>(
                    Arg.Is <Uri>(u => u.ToString() == "repositories/42/pulls/13/requested_reviewers"),
                    pullRequestReviewRequest);
            }
예제 #13
0
            public async Task PostsToCorrectUrl()
            {
                var connection = Substitute.For <IApiConnection>();
                var client     = new PullRequestReviewRequestsClient(connection);

                IReadOnlyList <string> fakeReviewers = new List <string> {
                    "zxc", "asd"
                };
                var pullRequestReviewRequest = PullRequestReviewRequest.ForReviewers(fakeReviewers);

                await client.Delete("owner", "name", 13, pullRequestReviewRequest);

                connection.Received().Delete(
                    Arg.Is <Uri>(u => u.ToString() == "repos/owner/name/pulls/13/requested_reviewers"),
                    pullRequestReviewRequest);
            }
예제 #14
0
            public async Task PostsToCorrectUrlWithRepositoryId()
            {
                var connection = Substitute.For <IApiConnection>();
                var client     = new PullRequestReviewRequestsClient(connection);

                IReadOnlyList <string> fakeReviewers = new List <string> {
                    "zxc", "asd"
                };
                var pullRequestReviewRequest = new PullRequestReviewRequest(fakeReviewers);

                await client.Delete(43, 13, pullRequestReviewRequest);

                connection.Received().Delete(
                    Arg.Is <Uri>(u => u.ToString() == "repositories/43/pulls/13/requested_reviewers"),
                    pullRequestReviewRequest,
                    "application/vnd.github.black-cat-preview+json");
            }
예제 #15
0
            public async Task EnsuresNonNullArguments()
            {
                var gitHubClient = Substitute.For <IGitHubClient>();
                var client       = new ObservablePullRequestReviewRequestsClient(gitHubClient);

                IReadOnlyList <string> fakeReviewers = new List <string> {
                    "zxc", "asd"
                };
                var pullRequestReviewRequest = PullRequestReviewRequest.ForReviewers(fakeReviewers);

                Assert.Throws <ArgumentNullException>(() => client.Create(null, "fakeRepoName", 1, pullRequestReviewRequest));
                Assert.Throws <ArgumentNullException>(() => client.Create("fakeOwner", null, 1, pullRequestReviewRequest));
                Assert.Throws <ArgumentNullException>(() => client.Create("fakeOwner", "fakeRepoName", 1, null));
                Assert.Throws <ArgumentNullException>(() => client.Create(42, 1, null));

                Assert.Throws <ArgumentException>(() => client.Create("", "fakeRepoName", 1, pullRequestReviewRequest));
                Assert.Throws <ArgumentException>(() => client.Create("fakeOwner", "", 1, pullRequestReviewRequest));
            }
예제 #16
0
            public void PostsToCorrectUrl()
            {
                var connection = Substitute.For <IApiConnection>();
                var client     = new PullRequestReviewRequestsClient(connection);

                IReadOnlyList <string> fakeReviewers = new List <string> {
                    "zxc", "asd"
                };
                var pullRequestReviewRequest = new PullRequestReviewRequest(fakeReviewers);

                client.Create("fakeOwner", "fakeRepoName", 13, pullRequestReviewRequest);

                connection.Connection.Received().Post <PullRequest>(
                    Arg.Is <Uri>(u => u.ToString() == "repos/fakeOwner/fakeRepoName/pulls/13/requested_reviewers"),
                    pullRequestReviewRequest,
                    "application/vnd.github.black-cat-preview+json",
                    null);
            }
    static async Task <int> CreateTheWorld(IGitHubClient github, RepositoryContext context, bool createReviewRequests = true)
    {
        var master = await github.Git.Reference.Get(context.RepositoryOwner, context.RepositoryName, "heads/master");

        // create new commit for master branch
        var newMasterTree = await CreateTree(github, context, new Dictionary <string, string> {
            { "README.md", "Hello World!" }
        });

        var newMaster = await CreateCommit(github, context, "baseline for pull request", newMasterTree.Sha, master.Object.Sha);

        // update master
        await github.Git.Reference.Update(context.RepositoryOwner, context.RepositoryName, "heads/master", new ReferenceUpdate(newMaster.Sha));

        // create new commit for feature branch
        var featureBranchTree = await CreateTree(github, context, new Dictionary <string, string> {
            { "README.md", "I am overwriting this blob with something new" }
        });

        var featureBranchCommit = await CreateCommit(github, context, "this is the commit to merge into the pull request", featureBranchTree.Sha, newMaster.Sha);

        var featureBranchTree2 = await CreateTree(github, context, new Dictionary <string, string> {
            { "README.md", "I am overwriting this blob with something new a 2nd time" }
        });

        var featureBranchCommit2 = await CreateCommit(github, context, "this is a 2nd commit to merge into the pull request", featureBranchTree2.Sha, featureBranchCommit.Sha);

        // create branch
        await github.Git.Reference.Create(context.RepositoryOwner, context.RepositoryName, new NewReference("refs/heads/my-branch", featureBranchCommit2.Sha));

        // create a pull request
        var pullRequest        = new NewPullRequest("Nice title for the pull request", "my-branch", "master");
        var createdPullRequest = await github.PullRequest.Create(context.RepositoryOwner, context.RepositoryName, pullRequest);

        // Create review requests (optional)
        if (createReviewRequests)
        {
            var reviewRequest = new PullRequestReviewRequest(_collaboratorLogins);
            await github.PullRequest.ReviewRequest.Create(context.RepositoryOwner, context.RepositoryName, createdPullRequest.Number, reviewRequest);
        }

        return(createdPullRequest.Number);
    }
예제 #18
0
            public async Task EnsuresNonNullArguments()
            {
                var connection = Substitute.For <IApiConnection>();
                var client     = new PullRequestReviewRequestsClient(connection);

                IReadOnlyList <string> fakeReviewers = new List <string> {
                    "zxc", "asd"
                };
                var pullRequestReviewRequest = PullRequestReviewRequest.ForReviewers(fakeReviewers);

                await Assert.ThrowsAsync <ArgumentNullException>(() => client.Create(null, "fakeRepoName", 1, pullRequestReviewRequest));

                await Assert.ThrowsAsync <ArgumentNullException>(() => client.Create("fakeOwner", null, 1, pullRequestReviewRequest));

                await Assert.ThrowsAsync <ArgumentNullException>(() => client.Create("fakeOwner", "fakeRepoName", 1, null));

                await Assert.ThrowsAsync <ArgumentNullException>(() => client.Create(1, 1, null));

                await Assert.ThrowsAsync <ArgumentException>(() => client.Create("", "fakeRepoName", 1, pullRequestReviewRequest));

                await Assert.ThrowsAsync <ArgumentException>(() => client.Create("fakeOwner", "", 1, pullRequestReviewRequest));
            }
예제 #19
0
        /// <summary>
        /// Creates review requests on a pull request for specified users.
        /// </summary>
        /// <remarks>https://developer.github.com/v3/pulls/review_requests/#create-a-review-request</remarks>
        /// <param name="owner">The owner of the repository</param>
        /// <param name="name">The name of the repository</param>
        /// <param name="number">The Pull Request number</param>
        /// <param name="users">List of logins of user will be requested for review</param>
        public IObservable <PullRequest> Create(string owner, string name, int number, PullRequestReviewRequest users)
        {
            Ensure.ArgumentNotNullOrEmptyString(owner, nameof(owner));
            Ensure.ArgumentNotNullOrEmptyString(name, nameof(name));
            Ensure.ArgumentNotNull(users, nameof(users));

            return(_client.Create(owner, name, number, users).ToObservable());
        }
예제 #20
0
        /// <summary>
        /// Creates review requests on a pull request for specified users.
        /// </summary>
        /// <remarks>https://developer.github.com/v3/pulls/review_requests/#create-a-review-request</remarks>
        /// <param name="repositoryId">The Id of the repository</param>
        /// <param name="number">The Pull Request number</param>
        /// <param name="users">List of logins of user will be requested for review</param>
        public IObservable <PullRequest> Create(long repositoryId, int number, PullRequestReviewRequest users)
        {
            Ensure.ArgumentNotNull(users, nameof(users));

            return(_client.Create(repositoryId, number, users).ToObservable());
        }
        /// <summary>
        /// Deletes review request for given users on a pull request.
        /// </summary>
        /// <remarks>https://developer.github.com/v3/pulls/review_requests/#delete-a-review-request</remarks>
        /// <param name="repositoryId">The Id of the repository</param>
        /// <param name="number">The pull request review comment number</param>
        /// <param name="users">List of logins of users that will be not longer requested for review</param>
        public IObservable <Unit> Delete(long repositoryId, int number, PullRequestReviewRequest users)
        {
            Ensure.ArgumentNotNull(users, "users");

            return(_client.Delete(repositoryId, number, users).ToObservable());
        }