public void IsUriAGithubAddress()
 {
     Uri test = new Uri("https://github.com");
     Uri test2 = new Uri("https://api.github.com");
     Assert.True(HostAddress.IsGitHubDotCom(test));
     Assert.True(HostAddress.IsGitHubDotCom(test2));
 }
        bool EnterpriseWorkaround(HostAddress hostAddress, Exception e)
        {
            // Older Enterprise hosts either don't have the API end-point to PUT an authorization, or they
            // return 422 because they haven't white-listed our client ID. In that case, we just ignore
            // the failure, using basic authentication (with username and password) instead of trying
            // to get an authorization token.
            // Since enterprise 2.1 and https://github.com/github/github/pull/36669 the API returns 403
            // instead of 404 to signal that it's not allowed. In the name of backwards compatibility we
            // test for both 404 (NotFoundException) and 403 (ForbiddenException) here.
            var apiException = e as ApiException;

            return(!hostAddress.IsGitHubDotCom() &&
                   (e is NotFoundException ||
                    e is ForbiddenException ||
                    apiException?.StatusCode == (HttpStatusCode)422));
        }
        public RepositoryHost(
            IApiClient apiClient,
            IModelService modelService,
            ILoginCache loginCache,
            ITwoFactorChallengeHandler twoFactorChallengeHandler)
        {
            ApiClient       = apiClient;
            ModelService    = modelService;
            this.loginCache = loginCache;
            this.twoFactorChallengeHandler = twoFactorChallengeHandler;

            Debug.Assert(apiClient.HostAddress != null, "HostAddress of an api client shouldn't be null");
            Address      = apiClient.HostAddress;
            hostAddress  = apiClient.HostAddress;
            isEnterprise = !hostAddress.IsGitHubDotCom();
            Title        = apiClient.HostAddress.Title;
        }
Beispiel #4
0
        async Task <LastCommitAdapter> GetPullRequestLastCommitAdapter(HostAddress address, string owner, string name, int number)
        {
            ICompiledQuery <IEnumerable <LastCommitAdapter> > query;

            if (address.IsGitHubDotCom())
            {
                if (readCommitStatuses == null)
                {
                    readCommitStatuses = new Query()
                                         .Repository(Var(nameof(owner)), Var(nameof(name)))
                                         .PullRequest(Var(nameof(number))).Commits(last: 1).Nodes.Select(
                        commit => new LastCommitAdapter
                    {
                        HeadSha     = commit.Commit.Oid,
                        CheckSuites = commit.Commit.CheckSuites(null, null, null, null, null).AllPages(10)
                                      .Select(suite => new CheckSuiteModel
                        {
                            CheckRuns = suite.CheckRuns(null, null, null, null, null).AllPages(10)
                                        .Select(run => new CheckRunModel
                            {
                                Id          = run.Id.Value,
                                Conclusion  = run.Conclusion.FromGraphQl(),
                                Status      = run.Status.FromGraphQl(),
                                Name        = run.Name,
                                DetailsUrl  = run.Permalink,
                                Summary     = run.Summary,
                                Text        = run.Text,
                                Annotations = run.Annotations(null, null, null, null).AllPages()
                                              .Select(annotation => new CheckRunAnnotationModel
                                {
                                    Title           = annotation.Title,
                                    Message         = annotation.Message,
                                    Path            = annotation.Path,
                                    AnnotationLevel = annotation.AnnotationLevel.Value.FromGraphQl(),
                                    StartLine       = annotation.Location.Start.Line,
                                    EndLine         = annotation.Location.End.Line,
                                }).ToList()
                            }).ToList(),
                            ApplicationName = suite.App != null ? suite.App.Name : "Private App"
                        }).ToList(),
                        Statuses = commit.Commit.Status
                                   .Select(context =>
                                           context.Contexts.Select(statusContext => new StatusModel
                        {
                            State       = statusContext.State.FromGraphQl(),
                            Context     = statusContext.Context,
                            TargetUrl   = statusContext.TargetUrl,
                            Description = statusContext.Description
                        }).ToList()
                                           ).SingleOrDefault()
                    }
                        ).Compile();
                }

                query = readCommitStatuses;
            }
            else
            {
                if (readCommitStatusesEnterprise == null)
                {
                    readCommitStatusesEnterprise = new Query()
                                                   .Repository(Var(nameof(owner)), Var(nameof(name)))
                                                   .PullRequest(Var(nameof(number))).Commits(last: 1).Nodes.Select(
                        commit => new LastCommitAdapter
                    {
                        Statuses = commit.Commit.Status
                                   .Select(context =>
                                           context.Contexts.Select(statusContext => new StatusModel
                        {
                            State       = statusContext.State.FromGraphQl(),
                            Context     = statusContext.Context,
                            TargetUrl   = statusContext.TargetUrl,
                            Description = statusContext.Description,
                        }).ToList()
                                           ).SingleOrDefault()
                    }
                        ).Compile();
                }

                query = readCommitStatusesEnterprise;
            }

            var vars = new Dictionary <string, object>
            {
                { nameof(owner), owner },
                { nameof(name), name },
                { nameof(number), number },
            };

            var connection = await graphqlFactory.CreateConnection(address);

            var result = await connection.Run(query, vars);

            return(result.First());
        }
Beispiel #5
0
        async Task <LastCommitAdapter> GetPullRequestLastCommitAdapter(HostAddress address, string owner, string name, int number)
        {
            ICompiledQuery <IEnumerable <LastCommitAdapter> > query;

            if (address.IsGitHubDotCom())
            {
                if (readCommitStatuses == null)
                {
                    readCommitStatuses = new Query()
                                         .Repository(Var(nameof(owner)), Var(nameof(name)))
                                         .PullRequest(Var(nameof(number))).Commits(last: 1).Nodes.Select(
                        commit => new LastCommitAdapter
                    {
                        CheckSuites = commit.Commit.CheckSuites(null, null, null, null, null).AllPages(10)
                                      .Select(suite => new CheckSuiteModel
                        {
                            CheckRuns = suite.CheckRuns(null, null, null, null, null).AllPages(10)
                                        .Select(run => new CheckRunModel
                            {
                                Conclusion = run.Conclusion.FromGraphQl(),
                                Status     = run.Status.FromGraphQl(),
                                Name       = run.Name,
                                DetailsUrl = run.Permalink,
                                Summary    = run.Summary,
                            }).ToList()
                        }).ToList(),
                        Statuses = commit.Commit.Status
                                   .Select(context =>
                                           context.Contexts.Select(statusContext => new StatusModel
                        {
                            State       = statusContext.State.FromGraphQl(),
                            Context     = statusContext.Context,
                            TargetUrl   = statusContext.TargetUrl,
                            Description = statusContext.Description,
                        }).ToList()
                                           ).SingleOrDefault()
                    }
                        ).Compile();
                }

                query = readCommitStatuses;
            }
            else
            {
                if (readCommitStatusesEnterprise == null)
                {
                    readCommitStatusesEnterprise = new Query()
                                                   .Repository(Var(nameof(owner)), Var(nameof(name)))
                                                   .PullRequest(Var(nameof(number))).Commits(last: 1).Nodes.Select(
                        commit => new LastCommitAdapter
                    {
                        Statuses = commit.Commit.Status
                                   .Select(context =>
                                           context.Contexts.Select(statusContext => new StatusModel
                        {
                            State       = statusContext.State.FromGraphQl(),
                            Context     = statusContext.Context,
                            TargetUrl   = statusContext.TargetUrl,
                            Description = statusContext.Description,
                        }).ToList()
                                           ).SingleOrDefault()
                    }
                        ).Compile();
                }

                query = readCommitStatusesEnterprise;
            }

            var vars = new Dictionary <string, object>
            {
                { nameof(owner), owner },
                { nameof(name), name },
                { nameof(number), number },
            };

            var connection = await graphqlFactory.CreateConnection(address);

            var result = await connection.Run(query, vars);

            return(result.First());
        }
Beispiel #6
0
        public async Task <Page <PullRequestListItemModel> > ReadPullRequests(
            HostAddress address,
            string owner,
            string name,
            string after,
            PullRequestStateEnum[] states)
        {
            ICompiledQuery <Page <PullRequestListItemModel> > query;

            if (address.IsGitHubDotCom())
            {
                if (readPullRequests == null)
                {
                    readPullRequests = new Query()
                                       .Repository(Var(nameof(owner)), Var(nameof(name)))
                                       .PullRequests(
                        first: 100,
                        after: Var(nameof(after)),
                        orderBy: new IssueOrder {
                        Direction = OrderDirection.Desc, Field = IssueOrderField.CreatedAt
                    },
                        states: Var(nameof(states)))
                                       .Select(page => new Page <PullRequestListItemModel>
                    {
                        EndCursor   = page.PageInfo.EndCursor,
                        HasNextPage = page.PageInfo.HasNextPage,
                        TotalCount  = page.TotalCount,
                        Items       = page.Nodes.Select(pr => new ListItemAdapter
                        {
                            Id         = pr.Id.Value,
                            LastCommit = pr.Commits(null, null, 1, null).Nodes.Select(commit =>
                                                                                      new LastCommitSummaryAdapter
                            {
                                CheckSuites = commit.Commit.CheckSuites(null, null, null, null, null).AllPages(10)
                                              .Select(suite => new CheckSuiteSummaryModel
                                {
                                    CheckRuns = suite.CheckRuns(null, null, null, null, null).AllPages(10)
                                                .Select(run => new CheckRunSummaryModel
                                    {
                                        Conclusion = run.Conclusion.FromGraphQl(),
                                        Status     = run.Status.FromGraphQl()
                                    }).ToList()
                                }).ToList(),
                                Statuses = commit.Commit.Status
                                           .Select(context =>
                                                   context.Contexts.Select(statusContext => new StatusSummaryModel
                                {
                                    State = statusContext.State.FromGraphQl(),
                                }).ToList()
                                                   ).SingleOrDefault()
                            }).ToList().FirstOrDefault(),
                            Author = new ActorModel
                            {
                                Login     = pr.Author.Login,
                                AvatarUrl = pr.Author.AvatarUrl(null),
                            },
                            CommentCount = pr.Comments(0, null, null, null).TotalCount,
                            Number       = pr.Number,
                            Reviews      = pr.Reviews(null, null, null, null, null, null).AllPages().Select(review => new ReviewAdapter
                            {
                                Body         = review.Body,
                                CommentCount = review.Comments(null, null, null, null).TotalCount,
                            }).ToList(),
                            State     = pr.State.FromGraphQl(),
                            Title     = pr.Title,
                            UpdatedAt = pr.UpdatedAt,
                        }).ToList(),
                    }).Compile();
                }

                query = readPullRequests;
            }
            else
            {
                if (readPullRequestsEnterprise == null)
                {
                    readPullRequestsEnterprise = new Query()
                                                 .Repository(Var(nameof(owner)), Var(nameof(name)))
                                                 .PullRequests(
                        first: 100,
                        after: Var(nameof(after)),
                        orderBy: new IssueOrder {
                        Direction = OrderDirection.Desc, Field = IssueOrderField.CreatedAt
                    },
                        states: Var(nameof(states)))
                                                 .Select(page => new Page <PullRequestListItemModel>
                    {
                        EndCursor   = page.PageInfo.EndCursor,
                        HasNextPage = page.PageInfo.HasNextPage,
                        TotalCount  = page.TotalCount,
                        Items       = page.Nodes.Select(pr => new ListItemAdapter
                        {
                            Id         = pr.Id.Value,
                            LastCommit = pr.Commits(null, null, 1, null).Nodes.Select(commit =>
                                                                                      new LastCommitSummaryAdapter
                            {
                                Statuses = commit.Commit.Status
                                           .Select(context =>
                                                   context.Contexts.Select(statusContext => new StatusSummaryModel
                                {
                                    State = statusContext.State.FromGraphQl(),
                                }).ToList()
                                                   ).SingleOrDefault()
                            }).ToList().FirstOrDefault(),
                            Author = new ActorModel
                            {
                                Login     = pr.Author.Login,
                                AvatarUrl = pr.Author.AvatarUrl(null),
                            },
                            CommentCount = pr.Comments(0, null, null, null).TotalCount,
                            Number       = pr.Number,
                            Reviews      = pr.Reviews(null, null, null, null, null, null).AllPages().Select(review => new ReviewAdapter
                            {
                                Body         = review.Body,
                                CommentCount = review.Comments(null, null, null, null).TotalCount,
                            }).ToList(),
                            State     = pr.State.FromGraphQl(),
                            Title     = pr.Title,
                            UpdatedAt = pr.UpdatedAt,
                        }).ToList(),
                    }).Compile();
                }

                query = readPullRequestsEnterprise;
            }

            var graphql = await graphqlFactory.CreateConnection(address);

            var vars = new Dictionary <string, object>
            {
                { nameof(owner), owner },
                { nameof(name), name },
                { nameof(after), after },
                { nameof(states), states.Select(x => (PullRequestState)x).ToList() },
            };

            var result = await graphql.Run(query, vars);

            foreach (var item in result.Items.Cast <ListItemAdapter>())
            {
                item.CommentCount += item.Reviews.Sum(x => x.Count);
                item.Reviews       = null;

                var checkRuns = item.LastCommit?.CheckSuites?.SelectMany(model => model.CheckRuns).ToArray();

                var hasCheckRuns = checkRuns?.Any() ?? false;
                var hasStatuses  = item.LastCommit?.Statuses?.Any() ?? false;

                if (!hasCheckRuns && !hasStatuses)
                {
                    item.Checks = PullRequestChecksState.None;
                }
                else
                {
                    var checksHasFailure         = false;
                    var checksHasCompleteSuccess = true;

                    if (hasCheckRuns)
                    {
                        checksHasFailure = checkRuns
                                           .Any(model => model.Conclusion.HasValue &&
                                                (model.Conclusion.Value == CheckConclusionState.Failure ||
                                                 model.Conclusion.Value == CheckConclusionState.ActionRequired));

                        if (!checksHasFailure)
                        {
                            checksHasCompleteSuccess = checkRuns
                                                       .All(model => model.Conclusion.HasValue &&
                                                            (model.Conclusion.Value == CheckConclusionState.Success ||
                                                             model.Conclusion.Value == CheckConclusionState.Neutral));
                        }
                    }

                    var statusHasFailure         = false;
                    var statusHasCompleteSuccess = true;

                    if (!checksHasFailure && hasStatuses)
                    {
                        statusHasFailure = item.LastCommit
                                           .Statuses
                                           .Any(status => status.State == StatusState.Failure ||
                                                status.State == StatusState.Error);

                        if (!statusHasFailure)
                        {
                            statusHasCompleteSuccess =
                                item.LastCommit.Statuses.All(status => status.State == StatusState.Success);
                        }
                    }

                    if (checksHasFailure || statusHasFailure)
                    {
                        item.Checks = PullRequestChecksState.Failure;
                    }
                    else if (statusHasCompleteSuccess && checksHasCompleteSuccess)
                    {
                        item.Checks = PullRequestChecksState.Success;
                    }
                    else
                    {
                        item.Checks = PullRequestChecksState.Pending;
                    }
                }

                item.LastCommit = null;
            }

            return(result);
        }
 public void UriIsNotAGitHubAddress()
 {
     Uri test = new Uri("https://gorphub.com");
     Assert.False(HostAddress.IsGitHubDotCom(test));
 }