/// <summary> /// Triggers GitHub to rerequest an existing check suite, without pushing new code to a repository /// </summary> /// <remarks> /// See the <a href="https://developer.github.com/v3/checks/suites/#request-check-suites">Check Suites API documentation</a> for more information. /// </remarks> /// <param name="owner">The owner of the repository</param> /// <param name="name">The name of the repository</param> /// <param name="checkSuiteId">The Id of the check suite</param> public IObservable <bool> Rerequest(string owner, string name, long checkSuiteId) { Ensure.ArgumentNotNullOrEmptyString(owner, nameof(owner)); Ensure.ArgumentNotNullOrEmptyString(name, nameof(name)); return(_client.Rerequest(owner, name, checkSuiteId).ToObservable()); }
private async Task StartExecution() { IReadOnlyList <Installation> installations = await this.gitHubAppClient.GitHubApps.GetAllInstallationsForCurrent().ConfigureAwait(false); try { if (!this.IsGitHubInstallationClientValid()) { throw new InvalidOperationException("Error: gitHubInstallationClient is invalid."); } if (IsPullRequest) { ICheckSuitesClient checkSuiteClient = gitHubInstallationClient.Check.Suite; CheckSuitesResponse x = await checkSuiteClient.GetAllForReference(CurrentRepository.Id, CommitSha).ConfigureAwait(false); if (x.TotalCount > 0) { long checkSuiteId = x.CheckSuites.FirstOrDefault().Id; bool res = await checkSuiteClient.Rerequest(CurrentRepository.Id, checkSuiteId); } else { var newCheckSuite = new NewCheckSuite(CommitSha); try { CheckSuite suite = await checkSuiteClient.Create( CurrentRepository.Owner.Login, CurrentRepository.Name, newCheckSuite) .ConfigureAwait(false); } catch (Exception ex) { } } return; } ICheckRunsClient checkRunClient = gitHubInstallationClient.Check.Run; // Create a new heckRun in GitHub var newCheckRun = new NewCheckRun("ScanX", CommitSha) { Status = CheckStatus.Queued, }; CheckRun checkRun = await checkRunClient.Create( CurrentRepository.Owner.Login, CurrentRepository.Name, newCheckRun) .ConfigureAwait(false); // --- Downoad a ZIP --- byte[] buffer = await ScanHelper.DownloadRepoZip(gitHubInstallationClient, CurrentRepository.Id, CommitSha).ConfigureAwait(false); int size = buffer.Length; // Upload ZIP to a storage blob string blobName = $"{RequestId.ToString()}"; string blobUri = await ScanHelper.UploadBufferToStorage(buffer, blobName); // Update check's status to "in progress" CheckRunUpdate checkRunUpdate = new CheckRunUpdate { Status = CheckStatus.InProgress, Name = checkRun.Name }; checkRun = await checkRunClient.Update(CurrentRepository.Id, checkRun.Id, checkRunUpdate).ConfigureAwait(false); // --- Start a scan --- // Simulate sending of a message to a SB queue // Create worker notification message MalwareDeterminationRequest scanRequest = new MalwareDeterminationRequest(); scanRequest.ClientId = "GitHubScanX"; scanRequest.FileName = $"{RequestId.ToString()}.zip"; scanRequest.FileSizeInBytes = 1000; //dummy scanRequest.RequestId = RequestId; scanRequest.Uri = new Uri(blobUri); // Notify worker (aka put the notification message to a queue) ScanXMock mock = new ScanXMock(); await mock.SendScanRequest(scanRequest).ConfigureAwait(false); // --- Poll for a scan completion --- MalwareDeterminationResult scanResult; do { await Task.Delay(500).ConfigureAwait(false); if (await mock.TryGetResult(RequestId)) { scanResult = await mock.GetResult(RequestId).ConfigureAwait(false); break; } }while (true); //!!!! for POC only checkRunUpdate.Status = CheckStatus.Completed; checkRunUpdate.CompletedAt = DateTime.UtcNow; checkRunUpdate.Conclusion = scanResult.WorkStatus == WorkStatus.Clean ? CheckConclusion.Success : CheckConclusion.Failure; if (checkRunUpdate.Conclusion == CheckConclusion.Failure) { checkRunUpdate.Output = new NewCheckRunOutput( "Scan Report", $"GitScan detected {scanResult.ConfirmedMalwares.Count()} infected files. See details below."); checkRunUpdate.Output.Text = "| File Path| Malware Type| AV Engines|\n"; checkRunUpdate.Output.Text += "|:---|:---|:---|\n"; foreach (var entry in scanResult.ConfirmedMalwares) { checkRunUpdate.Output.Text += $"|{entry.FileName}|{entry.MalwareInfo}|{string.Join(",", entry.AvEngines.ToArray())}"; } } checkRun = await checkRunClient.Update(CurrentRepository.Id, checkRun.Id, checkRunUpdate).ConfigureAwait(false); } catch (Exception ex) { Console.WriteLine($"Exception: {ex.Message}"); } }