/// <summary> /// Updates a CheckRun in the GitHub Api. /// </summary> /// <param name="checkRunId">The id of the CheckRun being updated.</param> /// <param name="owner">The name of the repository owner.</param> /// <param name="repository">The name of the repository.</param> /// <param name="createCheckRun">The CheckRun details.</param> /// <param name="annotations">Array of Annotations for the CheckRun.</param> /// <returns></returns> public async Task UpdateCheckRunAsync(long checkRunId, string owner, string repository, CreateCheckRun createCheckRun, Annotation[] annotations) { try { if (annotations.Length > 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"); } await checkRunsClient.Update(owner, repository, checkRunId, new CheckRunUpdate { Output = new NewCheckRunOutput(createCheckRun.Title, createCheckRun.Summary) { Annotations = annotations .Select(CreateNewCheckRunAnnotation) .ToArray() } }); } catch (Exception ex) { throw new GitHubAppModelException("Error updating CheckRun.", ex); } }
private void EventSourceOnBuildFinished(object sender, BuildFinishedEventArgs e) { var submitSuccess = false; try { var logData = _logDataBuilder.Build(); var hasAnyFailure = logData.Annotations.Any() && logData.Annotations.Any(annotation => annotation.AnnotationLevel == AnnotationLevel.Failure); var stringBuilder = new StringBuilder(); stringBuilder.Append(logData.ErrorCount.ToString()); stringBuilder.Append(" "); stringBuilder.Append(logData.ErrorCount == 1 ? "error" : "errors"); stringBuilder.Append(" - "); stringBuilder.Append(logData.WarningCount.ToString()); stringBuilder.Append(" "); stringBuilder.Append(logData.WarningCount == 1 ? "warning" : "warnings"); var createCheckRun = new CreateCheckRun { Annotations = logData.Annotations, Conclusion = !hasAnyFailure ? CheckConclusion.Success : CheckConclusion.Failure, StartedAt = _startedAt, CompletedAt = DateTimeOffset.Now, Summary = logData.Report, Name = _configuration?.Name ?? "MSBuild Log", Title = stringBuilder.ToString(), }; var contents = createCheckRun.ToJson(); var bytes = Encoding.Unicode.GetBytes(contents); submitSuccess = _submissionService.SubmitAsync(bytes, _parameters).Result; } catch (Exception exception) { _environmentProvider.WriteLine(exception.ToString()); } _environmentProvider.WriteLine($"Submission {(submitSuccess ? "Success" : "Failure")}"); }
public void EquatableTest() { for (int i = 0; i < 10; i++) { var createCheckRun = FakeCheckRun.Generate(); var copy = new CreateCheckRun() { Name = createCheckRun.Name, Title = createCheckRun.Title, StartedAt = createCheckRun.StartedAt, CompletedAt = createCheckRun.CompletedAt, Conclusion = createCheckRun.Conclusion, Summary = createCheckRun.Summary, Annotations = createCheckRun.Annotations?.Select(annotation => annotation).ToArray(), Images = createCheckRun.Images?.Select(annotation => annotation).ToArray(), }; createCheckRun.Equals(copy).Should().BeTrue(); } }
public static string ToJson(this CreateCheckRun createCheckRun) => CreateCheckRunSerializer.Serialize(createCheckRun);
public static string Serialize(CreateCheckRun createCheckRun) => JsonConvert.SerializeObject(createCheckRun, JsonSerializerSettings);
/// <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); } }
/// <inheritdoc /> public async Task <CheckRun> SubmitCheckRunAsync(string owner, string repository, string sha, CreateCheckRun createCheckRun, Annotation[] annotations) { if (string.IsNullOrWhiteSpace(owner)) { throw new ArgumentException("Owner is invalid", nameof(owner)); } if (string.IsNullOrWhiteSpace(repository)) { throw new ArgumentException("Repository is invalid", nameof(repository)); } if (string.IsNullOrWhiteSpace(sha)) { throw new ArgumentException("HeadSha is invalid", nameof(sha)); } if (string.IsNullOrWhiteSpace(createCheckRun.Name)) { throw new ArgumentException("Name is invalid", nameof(createCheckRun.Name)); } if (string.IsNullOrWhiteSpace(createCheckRun.Title)) { throw new ArgumentException("Title is invalid", nameof(createCheckRun.Title)); } var annotationBatches = annotations?.Batch(50).ToArray(); var checkRun = await CreateCheckRunAsync(owner, repository, sha, createCheckRun, annotationBatches?.FirstOrDefault()?.ToArray()) .ConfigureAwait(false); if (annotationBatches != null) { foreach (var annotationBatch in annotationBatches.Skip(1)) { await UpdateCheckRunAsync(checkRun.Id, owner, repository, createCheckRun, annotationBatch.ToArray()).ConfigureAwait(false); } } return(checkRun); }
public void Proces(string inputFile, string outputFile, string cloneRoot, string owner, string repo, string hash, string configurationFile = null) { if (!_fileSystem.File.Exists(inputFile)) { throw new InvalidOperationException($"Input file `{inputFile}` does not exist."); } if (_fileSystem.File.Exists(outputFile)) { throw new InvalidOperationException($"Output file `{outputFile}` already exists."); } CheckRunConfiguration configuration = null; if (configurationFile != null) { if (!_fileSystem.File.Exists(configurationFile)) { throw new InvalidOperationException($"Configuration file `{configurationFile}` does not exist."); } var configurationString = _fileSystem.File.ReadAllText(configurationFile); if (string.IsNullOrWhiteSpace(configurationString)) { throw new InvalidOperationException($"Content of configuration file `{configurationFile}` is null or empty."); } configuration = JsonConvert.DeserializeObject <CheckRunConfiguration>(configurationString, new JsonSerializerSettings { Formatting = Formatting.None, ContractResolver = new CamelCasePropertyNamesContractResolver(), Converters = new List <JsonConverter> { new StringEnumConverter { NamingStrategy = new CamelCaseNamingStrategy() } }, MissingMemberHandling = MissingMemberHandling.Error }); } var dateTimeOffset = DateTimeOffset.Now; var logData = _binaryLogProcessor.ProcessLog(inputFile, cloneRoot, owner, repo, hash, configuration); var hasAnyFailure = logData.Annotations.Any() && logData.Annotations.Any(annotation => annotation.AnnotationLevel == AnnotationLevel.Failure); var stringBuilder = new StringBuilder(); stringBuilder.Append(logData.ErrorCount.ToString()); stringBuilder.Append(" "); stringBuilder.Append(logData.ErrorCount == 1 ? "error": "errors"); stringBuilder.Append(" - "); stringBuilder.Append(logData.WarningCount.ToString()); stringBuilder.Append(" "); stringBuilder.Append(logData.WarningCount == 1 ? "warning" : "warnings"); var createCheckRun = new CreateCheckRun { Annotations = logData.Annotations, Conclusion = !hasAnyFailure ? CheckConclusion.Success : CheckConclusion.Failure, StartedAt = dateTimeOffset, CompletedAt = DateTimeOffset.Now, Summary = logData.Report, Name = configuration?.Name ?? "MSBuild Log", Title = stringBuilder.ToString(), }; var contents = createCheckRun.ToJson(); _fileSystem.File.WriteAllText(outputFile, contents); }