|
1 | 1 | using System.Collections.ObjectModel;
|
2 | 2 | using System.Linq;
|
| 3 | +using System.Net; |
| 4 | +using System.Net.Http; |
3 | 5 | using System.Threading.Tasks;
|
4 | 6 | using FluentAssertions;
|
5 | 7 | using Moq;
|
@@ -872,6 +874,74 @@ public async Task MigrateAlerts_Dry_Run_Will_Not_Adjust_Any_Alerts_On_Target()
|
872 | 874 | ), Times.Never);
|
873 | 875 | }
|
874 | 876 |
|
| 877 | + [Fact] |
| 878 | + public async Task MigrateAlerts_Skips_An_Analysis_When_SARIF_Report_Not_Found() |
| 879 | + { |
| 880 | + // Arrange |
| 881 | + var Ref = "refs/heads/main"; |
| 882 | + var analysis1 = new CodeScanningAnalysis |
| 883 | + { |
| 884 | + Id = 1, |
| 885 | + CreatedAt = "2022-03-30T00:00:00Z", |
| 886 | + CommitSha = "SHA_1", |
| 887 | + Ref = Ref |
| 888 | + }; |
| 889 | + var analysis2 = new CodeScanningAnalysis |
| 890 | + { |
| 891 | + Id = 2, |
| 892 | + CreatedAt = "2022-03-31T00:00:00Z", |
| 893 | + CommitSha = "SHA_2", |
| 894 | + Ref = Ref |
| 895 | + }; |
| 896 | + |
| 897 | + const string sarifResponse2 = "SARIF_RESPONSE_2"; |
| 898 | + var processingStatus = new SarifProcessingStatus |
| 899 | + { |
| 900 | + Status = SarifProcessingStatus.Complete, |
| 901 | + Errors = new Collection<string>() |
| 902 | + }; |
| 903 | + |
| 904 | + _mockSourceGithubApi.Setup(x => x.GetCodeScanningAnalysisForRepository(SOURCE_ORG, SOURCE_REPO, "main")).ReturnsAsync(new[] { analysis1, analysis2 }); |
| 905 | + _mockSourceGithubApi.Setup(x => x.GetSarifReport(SOURCE_ORG, SOURCE_REPO, analysis1.Id)) |
| 906 | + .ThrowsAsync(new HttpRequestException("No analysis found for analysis ID 1", null, HttpStatusCode.NotFound)); |
| 907 | + _mockSourceGithubApi.Setup(x => x.GetSarifReport(SOURCE_ORG, SOURCE_REPO, analysis2.Id)).ReturnsAsync(sarifResponse2); |
| 908 | + _mockTargetGithubApi.Setup(x => x.UploadSarifReport(TARGET_ORG, TARGET_REPO, It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>())).ReturnsAsync("sarif-id"); |
| 909 | + _mockTargetGithubApi.Setup(x => x.GetSarifProcessingStatus(TARGET_ORG, TARGET_REPO, It.IsAny<string>())) |
| 910 | + .ReturnsAsync(processingStatus); |
| 911 | + |
| 912 | + // Act |
| 913 | + await _alertService.MigrateAnalyses(SOURCE_ORG, SOURCE_REPO, TARGET_ORG, TARGET_REPO, "main", false); |
| 914 | + |
| 915 | + // Assert |
| 916 | + _mockTargetGithubApi.Verify( |
| 917 | + x => x.UploadSarifReport( |
| 918 | + TARGET_ORG, |
| 919 | + TARGET_REPO, |
| 920 | + It.IsAny<string>(), |
| 921 | + It.IsAny<string>(), |
| 922 | + It.IsAny<string>() |
| 923 | + ), |
| 924 | + Times.Once); |
| 925 | + _mockTargetGithubApi.Verify( |
| 926 | + x => x.UploadSarifReport( |
| 927 | + TARGET_ORG, |
| 928 | + TARGET_REPO, |
| 929 | + sarifResponse2, |
| 930 | + analysis2.CommitSha, |
| 931 | + Ref |
| 932 | + ), |
| 933 | + Times.Once); |
| 934 | + |
| 935 | + _mockTargetGithubApi.Verify( |
| 936 | + x => x.GetSarifProcessingStatus( |
| 937 | + TARGET_ORG, |
| 938 | + TARGET_REPO, |
| 939 | + "sarif-id"), |
| 940 | + Times.Once); |
| 941 | + |
| 942 | + _mockOctoLogger.Verify(log => log.LogWarning($"Skipping analysis {analysis1.Id} because no analysis was found for it (1 / 2)...")); |
| 943 | + } |
| 944 | + |
875 | 945 | // Avoid having referential equal instances to have real use case tests
|
876 | 946 | private CodeScanningAlertInstance CopyInstance(CodeScanningAlertInstance codeScanningAlertInstance)
|
877 | 947 | {
|
|
0 commit comments