-
Notifications
You must be signed in to change notification settings - Fork 88
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for migrating secret scanning resolution comments #1241
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
|
||
- Add support for migrating secret scanning alert resolution comments |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2507,6 +2507,7 @@ public async Task GetSecretScanningAlertsData() | |
""resolution"": null, | ||
""resolved_by"": null, | ||
""resolved_at"": null, | ||
""resolution_comment"": null, | ||
""push_protection_bypassed"": false, | ||
""push_protection_bypassed_by"": null, | ||
""push_protection_bypassed_at"": null | ||
|
@@ -2540,6 +2541,7 @@ public async Task GetSecretScanningAlertsData() | |
""site_admin"": true | ||
}}, | ||
""resolved_at"": ""2022-04-05T20:57:03"", | ||
""resolution_comment"": null, | ||
""push_protection_bypassed"": false, | ||
""push_protection_bypassed_by"": null, | ||
""push_protection_bypassed_at"": null | ||
|
@@ -2561,6 +2563,7 @@ public async Task GetSecretScanningAlertsData() | |
""resolution"": null, | ||
""resolved_by"": null, | ||
""resolved_at"": null, | ||
""resolution_comment"": null, | ||
""push_protection_bypassed"": false, | ||
""push_protection_bypassed_by"": null, | ||
""push_protection_bypassed_at"": null | ||
|
@@ -2601,6 +2604,7 @@ public async Task GetSecretScanningAlertsData() | |
""site_admin"": true | ||
}}, | ||
""resolved_at"": ""2022-08-15T13:53:42Z"", | ||
""resolution_comment"": null, | ||
""push_protection_bypassed"": false, | ||
""push_protection_bypassed_by"": null, | ||
""push_protection_bypassed_at"": null | ||
|
@@ -2740,6 +2744,7 @@ private void AssertSecretScanningData(GithubSecretScanningAlert actual, JToken e | |
actual.State.Should().Be((string)expectedData["state"]); | ||
actual.SecretType.Should().Be((string)expectedData["secret_type"]); | ||
actual.Resolution.Should().Be((string)expectedData["resolution"]); | ||
actual.ResolutionComment.Should().Be((string)expectedData["resolution_comment"]); | ||
actual.Secret.Should().Be((string)expectedData["secret"]); | ||
} | ||
|
||
|
@@ -2750,16 +2755,18 @@ public async Task UpdateSecretScanningAlert_Calls_The_Right_Endpoint_With_Payloa | |
const int alertNumber = 100; | ||
const string alertState = "resolved"; | ||
const string alertResolution = "wont_fix"; | ||
const string alertResolutionComment = "Risk has been accepted"; | ||
|
||
var url = $"https://api.github.com/repos/{GITHUB_ORG}/{GITHUB_REPO}/secret-scanning/alerts/{alertNumber}"; | ||
var payload = new | ||
{ | ||
state = alertState, | ||
resolution = alertResolution | ||
resolution = alertResolution, | ||
resolution_comment = alertResolutionComment | ||
}; | ||
|
||
// Act | ||
await _githubApi.UpdateSecretScanningAlert(GITHUB_ORG, GITHUB_REPO, alertNumber, alertState, alertResolution); | ||
await _githubApi.UpdateSecretScanningAlert(GITHUB_ORG, GITHUB_REPO, alertNumber, alertState, alertResolution, alertResolutionComment); | ||
|
||
// Assert | ||
_githubClientMock.Verify(m => m.PatchAsync(url, It.Is<object>(x => x.ToJson() == payload.ToJson()), null)); | ||
|
@@ -2782,6 +2789,30 @@ public async Task UpdateSecretScanningAlert_Calls_The_Right_Endpoint_With_Payloa | |
_githubClientMock.Verify(m => m.PatchAsync(url, It.Is<object>(x => x.ToJson() == payload.ToJson()), null)); | ||
} | ||
|
||
|
||
[Fact] | ||
public async Task UpdateSecretScanningAlert_Replaces_Null_Resolution_Comment_With_Empty_String() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Based on my earlier comment we probably won't need this test. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed. As per previous comment, happy to do a separate pull request to align both code scanning and secret scanning, or do the changes in this one. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have opened #1248 and added your suggestions there. Thanks again! |
||
{ | ||
// Arrange | ||
const int alertNumber = 100; | ||
const string alertState = "resolved"; | ||
const string alertResolution = "wont_fix"; | ||
|
||
var url = $"https://api.github.com/repos/{GITHUB_ORG}/{GITHUB_REPO}/secret-scanning/alerts/{alertNumber}"; | ||
var payload = new | ||
{ | ||
state = alertState, | ||
resolution = alertResolution, | ||
resolution_comment = string.Empty | ||
}; | ||
|
||
// Act | ||
await _githubApi.UpdateSecretScanningAlert(GITHUB_ORG, GITHUB_REPO, alertNumber, alertState, alertResolution); | ||
|
||
// Assert | ||
_githubClientMock.Verify(m => m.PatchAsync(url, It.Is<object>(x => x.ToJson() == payload.ToJson()), null)); | ||
} | ||
|
||
[Fact] | ||
public async Task GetDefaultBranch_Returns_Default_Branch_Field() | ||
{ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,6 +41,7 @@ public async Task One_Secret_Updated() | |
SecretType = secretType, | ||
Secret = secret, | ||
Resolution = SecretScanningAlert.ResolutionRevoked, | ||
ResolutionComment = "Revokation explanation comment" | ||
}; | ||
|
||
var sourceLocation = new GithubSecretScanningAlertLocation() | ||
|
@@ -91,7 +92,8 @@ public async Task One_Secret_Updated() | |
TARGET_REPO, | ||
100, | ||
SecretScanningAlert.AlertStateResolved, | ||
SecretScanningAlert.ResolutionRevoked) | ||
SecretScanningAlert.ResolutionRevoked, | ||
"Revokation explanation comment") | ||
); | ||
} | ||
|
||
|
@@ -109,6 +111,7 @@ public async Task No_Matching_Location() | |
SecretType = secretType, | ||
Secret = secret, | ||
Resolution = SecretScanningAlert.ResolutionRevoked, | ||
ResolutionComment = "Revokation explanation comment" | ||
}; | ||
|
||
var sourceLocation = new GithubSecretScanningAlertLocation() | ||
|
@@ -159,6 +162,7 @@ public async Task No_Matching_Location() | |
It.IsAny<string>(), | ||
It.IsAny<int>(), | ||
It.IsAny<string>(), | ||
It.IsAny<string>(), | ||
It.IsAny<string>()), Times.Never); | ||
} | ||
|
||
|
@@ -176,6 +180,7 @@ public async Task No_Matching_Secret() | |
SecretType = secretType, | ||
Secret = secret, | ||
Resolution = SecretScanningAlert.ResolutionRevoked, | ||
ResolutionComment = "Revokation explanation comment" | ||
}; | ||
|
||
var sourceLocation = new GithubSecretScanningAlertLocation() | ||
|
@@ -216,6 +221,7 @@ public async Task No_Matching_Secret() | |
It.IsAny<string>(), | ||
It.IsAny<int>(), | ||
It.IsAny<string>(), | ||
It.IsAny<string>(), | ||
It.IsAny<string>()), Times.Never); | ||
} | ||
|
||
|
@@ -233,6 +239,7 @@ public async Task Dry_Run_Does_Not_Update() | |
SecretType = secretType, | ||
Secret = secret, | ||
Resolution = SecretScanningAlert.ResolutionRevoked, | ||
ResolutionComment = "Revokation explanation comment" | ||
}; | ||
|
||
var sourceLocation = new GithubSecretScanningAlertLocation() | ||
|
@@ -273,6 +280,7 @@ public async Task Dry_Run_Does_Not_Update() | |
It.IsAny<string>(), | ||
It.IsAny<int>(), | ||
It.IsAny<string>(), | ||
It.IsAny<string>(), | ||
It.IsAny<string>()), Times.Never); | ||
} | ||
|
||
|
@@ -292,6 +300,7 @@ public async Task Migrates_Multiple_Alerts() | |
SecretType = secretType, | ||
Secret = secretOne, | ||
Resolution = SecretScanningAlert.ResolutionRevoked, | ||
ResolutionComment = "Revokation explanation comment 1" | ||
}; | ||
|
||
var sourceSecretTwo = new GithubSecretScanningAlert() | ||
|
@@ -301,6 +310,7 @@ public async Task Migrates_Multiple_Alerts() | |
SecretType = secretType, | ||
Secret = secretTwo, | ||
Resolution = SecretScanningAlert.ResolutionRevoked, | ||
ResolutionComment = "Revokation explanation comment 2" | ||
}; | ||
|
||
var sourceSecretThree = new GithubSecretScanningAlert() | ||
|
@@ -310,6 +320,7 @@ public async Task Migrates_Multiple_Alerts() | |
SecretType = secretType, | ||
Secret = secretThree, | ||
Resolution = SecretScanningAlert.ResolutionFalsePositive, | ||
ResolutionComment = "False positive explanation comment" | ||
}; | ||
|
||
var sourceLocation = new GithubSecretScanningAlertLocation() | ||
|
@@ -358,15 +369,86 @@ public async Task Migrates_Multiple_Alerts() | |
TARGET_REPO, | ||
100, | ||
SecretScanningAlert.AlertStateResolved, | ||
SecretScanningAlert.ResolutionRevoked) | ||
SecretScanningAlert.ResolutionRevoked, | ||
"Revokation explanation comment 1") | ||
); | ||
|
||
_mockTargetGithubApi.Verify(m => m.UpdateSecretScanningAlert( | ||
TARGET_ORG, | ||
TARGET_REPO, | ||
300, | ||
SecretScanningAlert.AlertStateResolved, | ||
SecretScanningAlert.ResolutionFalsePositive) | ||
SecretScanningAlert.ResolutionFalsePositive, | ||
"False positive explanation comment") | ||
); | ||
} | ||
|
||
[Fact] | ||
public async Task One_Secret_Updated_With_No_Resolution_Comment() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure if this adds value, seems like a bit of an overkill IMO. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What would be your preferred option:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So I am leaning towards option 1, since the way I think about this is a scenario where no comments are set still means we're setting it but to a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks again for all the comments. I have now removed that test case. |
||
{ | ||
var secretType = "custom"; | ||
var secret = "my-password"; | ||
|
||
// Arrange | ||
var sourceSecret = new GithubSecretScanningAlert() | ||
{ | ||
Number = 1, | ||
State = SecretScanningAlert.AlertStateResolved, | ||
SecretType = secretType, | ||
Secret = secret, | ||
Resolution = SecretScanningAlert.ResolutionRevoked | ||
}; | ||
|
||
var sourceLocation = new GithubSecretScanningAlertLocation() | ||
{ | ||
Path = "my-file.txt", | ||
StartLine = 17, | ||
EndLine = 18, | ||
StartColumn = 22, | ||
EndColumn = 29, | ||
BlobSha = "abc123" | ||
}; | ||
|
||
_mockSourceGithubApi.Setup(x => x.GetSecretScanningAlertsForRepository(SOURCE_ORG, SOURCE_REPO)) | ||
.ReturnsAsync(new[] { sourceSecret }); | ||
_mockSourceGithubApi.Setup(x => x.GetSecretScanningAlertsLocations(SOURCE_ORG, SOURCE_REPO, 1)) | ||
.ReturnsAsync(new[] { sourceLocation }); | ||
|
||
var targetSecret = new GithubSecretScanningAlert() | ||
{ | ||
Number = 100, | ||
State = SecretScanningAlert.AlertStateOpen, | ||
SecretType = secretType, | ||
Secret = secret | ||
}; | ||
|
||
var targetSecretLocation = new GithubSecretScanningAlertLocation() | ||
{ | ||
Path = "my-file.txt", | ||
StartLine = 17, | ||
EndLine = 18, | ||
StartColumn = 22, | ||
EndColumn = 29, | ||
BlobSha = "abc123" | ||
}; | ||
|
||
_mockTargetGithubApi.Setup(x => x.GetSecretScanningAlertsForRepository(TARGET_ORG, TARGET_REPO)) | ||
.ReturnsAsync(new[] { targetSecret }); | ||
|
||
_mockTargetGithubApi.Setup(x => x.GetSecretScanningAlertsLocations(TARGET_ORG, TARGET_REPO, 100)) | ||
.ReturnsAsync(new[] { targetSecretLocation }); | ||
|
||
// Act | ||
await _service.MigrateSecretScanningAlerts(SOURCE_ORG, SOURCE_REPO, TARGET_ORG, TARGET_REPO, false); | ||
|
||
// Assert | ||
_mockTargetGithubApi.Verify(m => m.UpdateSecretScanningAlert( | ||
TARGET_ORG, | ||
TARGET_REPO, | ||
100, | ||
SecretScanningAlert.AlertStateResolved, | ||
SecretScanningAlert.ResolutionRevoked, | ||
null) | ||
); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is it being set to empty string? If nothing is passed it makes sense for the default to be
null
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hello, thanks for the comments.
Added it that way to align with what is done for the dismissedComment for code scanning alerts (cf. https://github.com/github/gh-gei/blob/main/src/Octoshift/Services/GithubApi.cs#L933 )
Your suggestion makes sense though, so would you prefer me to do that in a separate pull request to align both changes, or change both it in this pull request?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👋 ok got it. So I guess as you said the other one needs to be changed too. I think it's better to do that one in a separate PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Understood. I have opened #1248 and added your suggestions there. Thanks again!