private static GitHubAppModelService CreateTarget(
            IGitHubAppClientFactory gitHubAppClientFactory = null,
            IGitHubClient gitHubClient = null,
            IChecksClient checkClient  = null,
            IRepositoryContentsClient repositoryContentsClient = null,
            IRepositoriesClient repositoriesClient             = null,
            ICheckRunsClient checkRunsClient = null,
            ITokenGenerator tokenGenerator   = null)
        {
            if (checkRunsClient == null)
            {
                checkRunsClient = Substitute.For <ICheckRunsClient>();
            }

            if (checkClient == null)
            {
                checkClient = Substitute.For <IChecksClient>();
                checkClient.Run.Returns(checkRunsClient);
            }

            if (repositoryContentsClient == null)
            {
                repositoryContentsClient = Substitute.For <IRepositoryContentsClient>();
            }

            if (repositoriesClient == null)
            {
                repositoriesClient = Substitute.For <IRepositoriesClient>();
                repositoriesClient.Content.Returns(repositoryContentsClient);
            }

            if (gitHubClient == null)
            {
                gitHubClient = Substitute.For <IGitHubClient>();
                gitHubClient.Check.Returns(checkClient);
                gitHubClient.Repository.Returns(repositoriesClient);
            }

            if (gitHubAppClientFactory == null)
            {
                gitHubAppClientFactory = Substitute.For <IGitHubAppClientFactory>();
                gitHubAppClientFactory.CreateAppClient(Arg.Any <ITokenGenerator>()).Returns(gitHubClient);
                gitHubAppClientFactory.CreateAppClientForLoginAsync(Arg.Any <ITokenGenerator>(), Arg.Any <string>())
                .Returns(gitHubClient);
            }


            tokenGenerator = tokenGenerator ?? Substitute.For <ITokenGenerator>();

            return(new GitHubAppModelService(gitHubAppClientFactory, tokenGenerator));
        }
        /// <inheritdoc />
        public async Task <CheckRun> CreateCheckRunAsync(string owner, string repository, string sha,
                                                         string checkRunName,
                                                         string checkRunTitle, string checkRunSummary, bool checkRunIsSuccess, Annotation[] annotations,
                                                         DateTimeOffset?startedAt,
                                                         DateTimeOffset?completedAt)
        {
            if (owner == null)
            {
                throw new ArgumentNullException(nameof(owner));
            }
            if (repository == null)
            {
                throw new ArgumentNullException(nameof(repository));
            }
            if (sha == null)
            {
                throw new ArgumentNullException(nameof(sha));
            }
            if (checkRunTitle == null)
            {
                throw new ArgumentNullException(nameof(checkRunTitle));
            }
            if (checkRunSummary == null)
            {
                throw new ArgumentNullException(nameof(checkRunSummary));
            }

            if ((annotations?.Length ?? 0) > 50)
            {
                throw new ArgumentException("Cannot create more than 50 annotations at a time");
            }

            var gitHubClient = await _gitHubAppClientFactory.CreateAppClientForLoginAsync(_tokenGenerator, owner);

            var checkRunsClient = gitHubClient?.Check?.Run;

            if (checkRunsClient == null)
            {
                throw new InvalidOperationException("ICheckRunsClient is null");
            }

            var newCheckRun = new NewCheckRun(checkRunName, sha)
            {
                Output = new NewCheckRunOutput(checkRunTitle, checkRunSummary)
                {
                    Annotations = annotations?
                                  .Select(annotation => new NewCheckRunAnnotation(annotation.Filename, annotation.BlobHref,
                                                                                  annotation.LineNumber, annotation.EndLine, GetCheckWarningLevel(annotation),
                                                                                  annotation.Message))
                                  .ToArray()
                },
                Status      = CheckStatus.Completed,
                StartedAt   = startedAt,
                CompletedAt = completedAt,
                Conclusion  = checkRunIsSuccess ? CheckConclusion.Success : CheckConclusion.Failure
            };

            var checkRun = await checkRunsClient.Create(owner, repository, newCheckRun);

            return(new CheckRun
            {
                Id = checkRun.Id,
                Url = checkRun.HtmlUrl,
            });
        }
Beispiel #3
0
        /// <summary>
        /// Creates a CheckRun in the GitHub Api.
        /// </summary>
        /// <param name="owner">The name of the repository owner.</param>
        /// <param name="repository">The name of the repository.</param>
        /// <param name="sha">The sha we are creating this CheckRun for.</param>
        /// <param name="createCheckRun"></param>
        /// <param name="annotations">Array of Annotations for the CheckRun.</param>
        /// <param name="name">The name of the CheckRun.</param>
        /// <param name="title">The title of the CheckRun.</param>
        /// <param name="summary">The summary of the CheckRun.</param>
        /// <param name="success">If the CheckRun is a success.</param>
        /// <param name="startedAt">The time when processing started</param>
        /// <param name="completedAt">The time when processing finished</param>
        /// <returns></returns>
        public async Task <CheckRun> CreateCheckRunAsync(string owner, string repository, string sha,
                                                         CreateCheckRun createCheckRun, Annotation[] annotations)
        {
            try
            {
                if (owner == null)
                {
                    throw new ArgumentNullException(nameof(owner));
                }
                if (repository == null)
                {
                    throw new ArgumentNullException(nameof(repository));
                }
                if (sha == null)
                {
                    throw new ArgumentNullException(nameof(sha));
                }

                if ((annotations?.Length ?? 0) > 50)
                {
                    throw new ArgumentException("Cannot create more than 50 annotations at a time");
                }

                var gitHubClient = await _gitHubAppClientFactory.CreateAppClientForLoginAsync(_tokenGenerator, owner);

                var checkRunsClient = gitHubClient?.Check?.Run;

                if (checkRunsClient == null)
                {
                    throw new InvalidOperationException("ICheckRunsClient is null");
                }

                var newCheckRun = new NewCheckRun(createCheckRun.Name, sha)
                {
                    Output = new NewCheckRunOutput(createCheckRun.Title, createCheckRun.Summary)
                    {
                        Text   = createCheckRun.Text,
                        Images = createCheckRun.Images?.Select(image => new NewCheckRunImage(image.Alt, image.ImageUrl)
                        {
                            Caption = image.Caption
                        }).ToArray(),
                        Annotations = annotations?
                                      .Select(CreateNewCheckRunAnnotation)
                                      .ToArray()
                    },
                    Status      = CheckStatus.Completed,
                    StartedAt   = createCheckRun.StartedAt,
                    CompletedAt = createCheckRun.CompletedAt,
                    Conclusion  = createCheckRun.Conclusion.ToOctokit()
                };

                var checkRun = await checkRunsClient.Create(owner, repository, newCheckRun);

                return(new CheckRun
                {
                    Id = checkRun.Id,
                    Url = checkRun.HtmlUrl
                });
            }
            catch (Exception ex)
            {
                throw new GitHubAppModelException("Error creating CheckRun.", ex);
            }
        }