Skip to content

Commit

Permalink
Secret scanning alert locations API (#735)
Browse files Browse the repository at this point in the history
* Initial struct and method for secret alerts locations

* Test for secret scanning alert locations

* Test fix
  • Loading branch information
vlad1slav authored Nov 22, 2024
1 parent 9c39eaa commit 592f165
Show file tree
Hide file tree
Showing 5 changed files with 248 additions and 0 deletions.
9 changes: 9 additions & 0 deletions examples/get_secret_scanning_alerts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,13 @@ async fn main() {
.await
.unwrap();
println!("{:?}", updated_alert);

// Get alert locations
let alert_location = octocrab
.repos("owner", "repo")
.secrets_scanning()
.get_alert_locations(5)
.await
.unwrap();
println!("{:?}", alert_location);
}
28 changes: 28 additions & 0 deletions src/api/repos/secret_scanning_alerts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,4 +204,32 @@ impl<'octo> RepoSecretScanningAlertsHandler<'octo> {
);
self.handler.crab.patch(route, alert_update).await
}

// Get a Secret Scanning alert locations.
/// You must authenticate using an access token with the `repo` or `security_events ` scope to use this endpoint.
/// ```no_run
/// # async fn run() -> octocrab::Result<()> {
/// # let octocrab = octocrab::Octocrab::default();
/// use octocrab::models::repos::secret_scanning_alert::SecretsScanningAlertLocation;
///
/// let result = octocrab.repos("owner", "repo")
/// .secrets_scanning()
/// .get_alert_locations(
/// 5
/// )
/// .await?;
/// # Ok(())
/// # }
pub async fn get_alert_locations(
&self,
alert_number: u32,
) -> crate::Result<
crate::Page<crate::models::repos::secret_scanning_alert::SecretsScanningAlertLocation>,
> {
let route = format!(
"/{}/secret-scanning/alerts/{}/locations",
self.handler.repo, alert_number
);
self.handler.crab.get(route, None::<&()>).await
}
}
62 changes: 62 additions & 0 deletions src/models/repos/secret_scanning_alert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,65 @@ pub struct UpdateSecretScanningAlert<'a> {
#[serde(skip_serializing_if = "Option::is_none")]
pub resolution_comment: Option<&'a str>,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(tag = "type", content = "details")]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum SecretsScanningAlertLocation {
Commit {
path: String,
start_line: u32,
end_line: u32,
start_column: u32,
end_column: u32,
blob_sha: String,
blob_url: String,
commit_sha: String,
commit_url: String,
},
WikiCommit {
path: String,
start_line: u32,
end_line: u32,
start_column: u32,
end_column: u32,
blob_sha: String,
page_url: String,
commit_sha: String,
commit_url: String,
},
IssueTitle {
issue_title_url: String,
},
IssueBody {
issue_body_url: String,
},
IssueComment {
issue_comment_url: String,
},
DiscussionTitle {
discussion_title_url: String,
},
DiscussionBody {
discussion_body_url: String,
},
DiscussionComment {
discussion_comment_url: String,
},
PullRequestTitle {
pull_request_title_url: String,
},
PullRequestBody {
pull_request_body_url: String,
},
PullRequestComment {
pull_request_comment_url: String,
},
PullRequestReview {
pull_request_review_url: String,
},
PullRequestReviewComment {
pull_request_review_comment_url: String,
},
}
96 changes: 96 additions & 0 deletions tests/resources/check_secrets_alerts_locations.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
[
{
"type": "commit",
"details": {
"path": "/example/secrets.txt",
"start_line": 1,
"end_line": 1,
"start_column": 1,
"end_column": 64,
"blob_sha": "af5626b4a114abcb82d63db7c8082c3c4756e51b",
"blob_url": "https://api.github.com/repos/octocat/hello-world/git/blobs/af5626b4a114abcb82d63db7c8082c3c4756e51b",
"commit_sha": "f14d7debf9775f957cf4f1e8176da0786431f72b",
"commit_url": "https://api.github.com/repos/octocat/hello-world/git/commits/f14d7debf9775f957cf4f1e8176da0786431f72b"
}
},
{
"type": "wiki_commit",
"details": {
"path": "/example/Home.md",
"start_line": 1,
"end_line": 1,
"start_column": 1,
"end_column": 64,
"blob_sha": "af5626b4a114abcb82d63db7c8082c3c4756e51b",
"page_url": "https://github.com/octocat/Hello-World/wiki/Home/302c0b7e200761c9dd9b57e57db540ee0b4293a5",
"commit_sha": "302c0b7e200761c9dd9b57e57db540ee0b4293a5",
"commit_url": "https://github.com/octocat/Hello-World/wiki/_compare/302c0b7e200761c9dd9b57e57db540ee0b4293a5"
}
},
{
"type": "issue_title",
"details": {
"issue_title_url": "https://api.github.com/repos/octocat/Hello-World/issues/1347"
}
},
{
"type": "issue_body",
"details": {
"issue_body_url": "https://api.github.com/repos/octocat/Hello-World/issues/1347"
}
},
{
"type": "issue_comment",
"details": {
"issue_comment_url": "https://api.github.com/repos/octocat/Hello-World/issues/comments/1081119451"
}
},
{
"type": "discussion_title",
"details": {
"discussion_title_url": "https://github.com/community/community/discussions/39082"
}
},
{
"type": "discussion_body",
"details": {
"discussion_body_url": "https://github.com/community/community/discussions/39082#discussion-4566270"
}
},
{
"type": "discussion_comment",
"details": {
"discussion_comment_url": "https://github.com/community/community/discussions/39082#discussioncomment-4158232"
}
},
{
"type": "pull_request_title",
"details": {
"pull_request_title_url": "https://api.github.com/repos/octocat/Hello-World/pull/2846"
}
},
{
"type": "pull_request_body",
"details": {
"pull_request_body_url": "https://api.github.com/repos/octocat/Hello-World/pulls/2846"
}
},
{
"type": "pull_request_comment",
"details": {
"pull_request_comment_url": "https://api.github.com/repos/octocat/Hello-World/issues/comments/1825855898"
}
},
{
"type": "pull_request_review",
"details": {
"pull_request_review_url": "https://api.github.com/repos/octocat/Hello-World/pulls/2846/reviews/80"
}
},
{
"type": "pull_request_review_comment",
"details": {
"pull_request_review_comment_url": "https://api.github.com/repos/octocat/Hello-World/pulls/comments/12"
}
}
]
53 changes: 53 additions & 0 deletions tests/secrets_alerts_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ use wiremock::{

use mock_error::setup_error_handler;
use octocrab::models::repos::secret_scanning_alert::SecretScanningAlert;
use octocrab::models::repos::secret_scanning_alert::SecretsScanningAlertLocation;
use octocrab::Octocrab;

mod mock_error;

const OWNER: &str = "org";
const REPO: &str = "some-repo";
const ALERT_NUMBER: u32 = 5;

async fn setup_secrets_api(template: ResponseTemplate) -> MockServer {
let mock_server = MockServer::start().await;
Expand All @@ -37,6 +39,31 @@ async fn setup_secrets_api(template: ResponseTemplate) -> MockServer {
mock_server
}

async fn setup_secrets_locations_api(template: ResponseTemplate) -> MockServer {
let mock_server = MockServer::start().await;

Mock::given(method("GET"))
.and(path(format!(
"/repos/{owner}/{repo}/secret-scanning/alerts/5/locations",
owner = OWNER,
repo = REPO
)))
.respond_with(template.clone())
.mount(&mock_server)
.await;
setup_error_handler(
&mock_server,
&format!(
"GET on /repos/{owner}/{repo}/secret-scanning/alerts/{ALERT_NUMBER}/locations was not received",
owner = OWNER,
repo = REPO
),
)
.await;

mock_server
}

fn setup_octocrab(uri: &str) -> Octocrab {
Octocrab::builder().base_uri(uri).unwrap().build().unwrap()
}
Expand Down Expand Up @@ -76,3 +103,29 @@ async fn check_secrets_alert_list_200() {
);
}
}

#[tokio::test]
async fn check_secrets_alert_locations_list_200() {
let s: &str = include_str!("resources/check_secrets_alerts_locations.json");
let alert: Vec<SecretsScanningAlertLocation> = serde_json::from_str(s).unwrap();
let template = ResponseTemplate::new(200).set_body_json(&alert);
let mock_server = setup_secrets_locations_api(template).await;
let client = setup_octocrab(&mock_server.uri());

let result = client
.repos(OWNER.to_owned(), REPO.to_owned())
.secrets_scanning()
.get_alert_locations(ALERT_NUMBER)
.await;

assert!(
result.is_ok(),
"expected successful result, got error: {:?}",
result
);

let response = result.unwrap();
let items = response.items;

assert_eq!(items.len(), 13);
}

0 comments on commit 592f165

Please sign in to comment.