Skip to content

Commit

Permalink
Update code scanning alert (#673)
Browse files Browse the repository at this point in the history
* Finalizing getting Code Scanning

* Get back mistakelly deleted files

* remove unused import

* lint

* remove legacy test

* remove legacy test

* Update Code Scanning alert (#3)

* Update Code Scanning alert

* Fix 

* Update Code Scanning alert
  • Loading branch information
adrienpessu authored Aug 14, 2024
1 parent ac28279 commit 8527406
Show file tree
Hide file tree
Showing 4 changed files with 150 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/api/code_scannings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
use crate::{models, params, Octocrab, Result};

mod list;
mod update;

/// Handler for GitHub's code scanning API.
///
Expand Down Expand Up @@ -40,4 +41,25 @@ impl<'octo> CodeScanningHandler<'octo> {
pub fn list(&self) -> list::ListCodeScanningsBuilder<'_, '_> {
list::ListCodeScanningsBuilder::new(self)
}

/// Update a code scanning alert
/// ```no_run
/// # use octocrab::params;
///
/// async fn run() -> octocrab::Result<()> {
/// # let octocrab = octocrab::Octocrab::default();
/// use octocrab::models;
///
/// let issue = octocrab.code_scannings("owner", "repo")
/// .update(1234u64)
/// .state(params::AlertState::Dismissed)
/// // Send the request
/// .send()
/// .await?;
/// # Ok(())
/// # }
/// ```
pub fn update(&self, number: u64) -> update::UpdateCodeScanningBuilder<'_, '_> {
update::UpdateCodeScanningBuilder::new(self, number)
}
}
54 changes: 54 additions & 0 deletions src/api/code_scannings/update.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
use super::*;

#[derive(serde::Serialize)]
pub struct UpdateCodeScanningBuilder<'octo, 'a> {
#[serde(skip)]
handler: &'a CodeScanningHandler<'octo>,
#[serde(skip)]
number: u64,
#[serde(skip_serializing_if = "Option::is_none")]
state: Option<params::AlertState>,
#[serde(skip_serializing_if = "Option::is_none")]
dismissed_reason: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
dismissed_comment: Option<String>,
}

impl<'octo, 'a, 'b, 'c> UpdateCodeScanningBuilder<'octo, 'a> {
pub(crate) fn new(handler: &'a CodeScanningHandler<'octo>, number: u64) -> Self {
Self {
handler,
number,
state: None,
dismissed_reason: None,
dismissed_comment: None,
}
}

/// The title of the code scanning.
pub fn state(mut self, state: impl Into<params::AlertState>) -> Self {
self.state = Some(state.into());
self
}

pub fn dismissed_reason(mut self, dismissed_reason: impl Into<String>) -> Self {
self.dismissed_reason = Some(dismissed_reason.into());
self
}

pub fn dismissed_comment(mut self, dismissed_comment: impl Into<String>) -> Self {
self.dismissed_comment = Some(dismissed_comment.into());
self
}

pub async fn send(self) -> Result<models::code_scannings::CodeScanningAlert> {
let route = format!(
"/repos/{owner}/{repo}/code-scanning/alerts/{code_scanning}",
owner = self.handler.owner,
repo = self.handler.repo.as_ref().expect("Repository is required"),
code_scanning = self.number,
);

self.handler.crab.patch(route, Some(&self)).await
}
}
9 changes: 9 additions & 0 deletions src/params.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
//! # Common GitHub Parameter Types
/// The status of a issue or pull request.
#[derive(Debug, Clone, Copy, serde::Serialize)]
#[serde(rename_all = "lowercase")]
#[non_exhaustive]
pub enum AlertState {
Dismissed,
Open,
}

/// The status of a issue or pull request.
#[derive(Debug, Clone, Copy, serde::Serialize)]
#[serde(rename_all = "lowercase")]
Expand Down
65 changes: 65 additions & 0 deletions tests/code_scanning_alert_update_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
use wiremock::{
matchers::{method, path},
Mock, MockServer, ResponseTemplate,
};

use mock_error::setup_error_handler;
use octocrab::models::code_scannings::CodeScanningAlert;
use octocrab::params::AlertState;
use octocrab::Octocrab;

mod mock_error;

async fn setup_issue_check_assignee_api(template: ResponseTemplate) -> MockServer {
let owner: &str = "org";
let repo: &str = "some-repo";
let number: &str = "1";

let mock_server = MockServer::start().await;

Mock::given(method("PATCH"))
.and(path(format!(
"/repos/{owner}/{repo}/code-scanning/alerts/{number}",
owner = owner,
repo = repo
)))
.respond_with(template.clone())
.mount(&mock_server)
.await;

setup_error_handler(
&mock_server,
&format!("GET on /repos/{owner}/{repo}/code-scanning/alerts was not received"),
)
.await;
mock_server
}

fn setup_octocrab(uri: &str) -> Octocrab {
Octocrab::builder().base_uri(uri).unwrap().build().unwrap()
}

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

#[tokio::test]
async fn check_patch_200() {
let s = include_str!("resources/codescanning_alert_single.json");
let alert: CodeScanningAlert = serde_json::from_str(s).unwrap();
let template = ResponseTemplate::new(200).set_body_json(&alert);
let mock_server = setup_issue_check_assignee_api(template).await;
let client = setup_octocrab(&mock_server.uri());

let result = client
.code_scannings(OWNER.to_owned(), REPO.to_owned())
.update(1)
.state(AlertState::Open)
.send()
.await;

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

0 comments on commit 8527406

Please sign in to comment.