|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.ComponentModel.Composition; |
| 4 | +using System.Diagnostics; |
| 5 | +using System.Globalization; |
| 6 | +using System.IO; |
| 7 | +using System.Linq; |
| 8 | +using System.Reactive; |
| 9 | +using System.Reactive.Linq; |
| 10 | +using System.Reactive.Threading.Tasks; |
| 11 | +using System.Text; |
| 12 | +using System.Text.RegularExpressions; |
| 13 | +using System.Threading.Tasks; |
| 14 | +using GitHub.Api; |
| 15 | +using GitHub.Extensions; |
| 16 | +using GitHub.Logging; |
| 17 | +using GitHub.Models; |
| 18 | +using GitHub.Primitives; |
| 19 | +using LibGit2Sharp; |
| 20 | +using Octokit.GraphQL; |
| 21 | +using Octokit.GraphQL.Core; |
| 22 | +using Octokit.GraphQL.Model; |
| 23 | +using Rothko; |
| 24 | +using static System.FormattableString; |
| 25 | +using static Octokit.GraphQL.Variable; |
| 26 | + |
| 27 | +namespace GitHub.Services |
| 28 | +{ |
| 29 | + [Export(typeof(IChecksService))] |
| 30 | + [PartCreationPolicy(CreationPolicy.Shared)] |
| 31 | + public class ChecksService : IChecksService |
| 32 | + { |
| 33 | + readonly IGitClient gitClient; |
| 34 | + readonly IGitService gitService; |
| 35 | + readonly IVSGitExt gitExt; |
| 36 | + readonly IGraphQLClientFactory graphqlFactory; |
| 37 | + readonly IOperatingSystem os; |
| 38 | + readonly IUsageTracker usageTracker; |
| 39 | + |
| 40 | + [ImportingConstructor] |
| 41 | + public ChecksService( |
| 42 | + IGitClient gitClient, |
| 43 | + IGitService gitService, |
| 44 | + IVSGitExt gitExt, |
| 45 | + IGraphQLClientFactory graphqlFactory, |
| 46 | + IOperatingSystem os, |
| 47 | + IUsageTracker usageTracker) |
| 48 | + { |
| 49 | + this.gitClient = gitClient; |
| 50 | + this.gitService = gitService; |
| 51 | + this.gitExt = gitExt; |
| 52 | + this.graphqlFactory = graphqlFactory; |
| 53 | + this.os = os; |
| 54 | + this.usageTracker = usageTracker; |
| 55 | + } |
| 56 | + |
| 57 | + static ICompiledQuery<IEnumerable<List<CheckSuiteModel>>> readCheckSuites; |
| 58 | + |
| 59 | + public async Task<List<CheckSuiteModel>> ReadCheckSuites( |
| 60 | + HostAddress address, |
| 61 | + string owner, |
| 62 | + string name, |
| 63 | + int pullRequestNumber) |
| 64 | + { |
| 65 | + if (readCheckSuites == null) |
| 66 | + { |
| 67 | + readCheckSuites = new Query() |
| 68 | + .Repository(Var(nameof(owner)), Var(nameof(name))) |
| 69 | + .PullRequest(Var(nameof(pullRequestNumber))) |
| 70 | + .Commits(last: 1).Nodes.Select( |
| 71 | + commit => commit.Commit.CheckSuites(null,null, null,null, null).AllPages() |
| 72 | + .Select(suite => new CheckSuiteModel |
| 73 | + { |
| 74 | + Conclusion = (CheckSuiteConclusionStateEnum?) suite.Conclusion, |
| 75 | + Status = (CheckSuiteStatusStateEnum) suite.Status, |
| 76 | + CreatedAt = suite.CreatedAt, |
| 77 | + UpdatedAt = suite.UpdatedAt, |
| 78 | + CheckRuns = suite.CheckRuns(null, null, null, null, null).AllPages() |
| 79 | + .Select(run => new CheckRunModel |
| 80 | + { |
| 81 | + Conclusion = (CheckSuiteConclusionStateEnum?) run.Conclusion, |
| 82 | + Status = (CheckSuiteStatusStateEnum) run.Status, |
| 83 | + StartedAt = run.StartedAt, |
| 84 | + CompletedAt = run.CompletedAt, |
| 85 | + Annotations = run.Annotations(null, null, null, null).AllPages() |
| 86 | + .Select(annotation => new CheckRunAnnotationModel |
| 87 | + { |
| 88 | + BlobUrl = annotation.BlobUrl, |
| 89 | + StartLine = annotation.StartLine, |
| 90 | + EndLine = annotation.EndLine, |
| 91 | + Filename = annotation.Filename, |
| 92 | + Message = annotation.Message, |
| 93 | + Title = annotation.Title, |
| 94 | + WarningLevel = (CheckAnnotationLevelEnum?) annotation.WarningLevel, |
| 95 | + RawDetails = annotation.RawDetails |
| 96 | + }).ToList() |
| 97 | + }).ToList() |
| 98 | + }).ToList() |
| 99 | + ).Compile(); |
| 100 | + } |
| 101 | + |
| 102 | + var graphql = await graphqlFactory.CreateConnection(address); |
| 103 | + var vars = new Dictionary<string, object> |
| 104 | + { |
| 105 | + { nameof(owner), owner }, |
| 106 | + { nameof(name), name }, |
| 107 | + { nameof(pullRequestNumber), pullRequestNumber }, |
| 108 | + }; |
| 109 | + |
| 110 | + var result = await graphql.Run(readCheckSuites, vars); |
| 111 | + return result.FirstOrDefault(); |
| 112 | + } |
| 113 | + } |
| 114 | +} |
0 commit comments