From 5a9b0ca47c56d2c232d2bf04c2a0fc586252cfc3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tobias=20Nie=C3=9Fen?= Date: Tue, 5 Mar 2024 09:54:48 +0100 Subject: [PATCH] Add delete_ref in RepoHandler (#573) Fixes: https://github.com/XAMPPRocky/octocrab/issues/572 --- src/api/repos.rs | 26 ++++++++++++++++ tests/repo_delete_ref_test.rs | 56 +++++++++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+) create mode 100644 tests/repo_delete_ref_test.rs diff --git a/src/api/repos.rs b/src/api/repos.rs index 065ed0ec..6e169f03 100644 --- a/src/api/repos.rs +++ b/src/api/repos.rs @@ -210,6 +210,32 @@ impl<'octo> RepoHandler<'octo> { .await } + /// Deletes an existing reference from the repository. + /// ```no_run + /// # async fn run() -> octocrab::Result<()> { + /// # let master_sha = ""; + /// use octocrab::params::repos::Reference; + /// + /// // Deletes the "heads/temporary-branch" reference. + /// octocrab::instance() + /// .repos("owner", "repo") + /// .delete_ref(&Reference::Branch("temporary-branch".to_string())) + /// .await?; + /// # Ok(()) + /// # } + /// ``` + pub async fn delete_ref(&self, reference: ¶ms::repos::Reference) -> Result<()> { + let route = format!( + "/repos/{owner}/{repo}/git/refs/{ref}", + owner = self.owner, + repo = self.repo, + ref = reference.ref_url() + ); + crate::map_github_error(self.crab._delete(route, None::<&()>).await?) + .await + .map(drop) + } + /// Get repository content. /// ```no_run /// # async fn run() -> octocrab::Result<()> { diff --git a/tests/repo_delete_ref_test.rs b/tests/repo_delete_ref_test.rs new file mode 100644 index 00000000..870bd8b1 --- /dev/null +++ b/tests/repo_delete_ref_test.rs @@ -0,0 +1,56 @@ +mod mock_error; + +use mock_error::setup_error_handler; +use octocrab::{params::repos::Reference, Octocrab}; +use wiremock::{ + matchers::{method, path}, + Mock, MockServer, ResponseTemplate, +}; + +async fn setup_delete_ref_api(template: ResponseTemplate) -> MockServer { + let owner: &str = "org"; + let repo: &str = "some-repo"; + let reference: &str = "heads/foo"; + + let mock_server = MockServer::start().await; + + Mock::given(method("DELETE")) + .and(path(format!("/repos/{owner}/{repo}/git/refs/{reference}"))) + .respond_with(template.clone()) + .mount(&mock_server) + .await; + + setup_error_handler( + &mock_server, + &format!("DELETE on /repos/{owner}/{repo}/git/refs/{reference} 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"; +const BRANCH: &str = "foo"; + +#[tokio::test] +async fn should_delete_reference() { + let template = ResponseTemplate::new(204); + let mock_server = setup_delete_ref_api(template).await; + let client = setup_octocrab(&mock_server.uri()); + + let repos = client.repos(OWNER.to_owned(), REPO.to_owned()); + + let result = repos + .delete_ref(&Reference::Branch(BRANCH.to_owned())) + .await; + + assert!( + result.is_ok(), + "expected successful result, got error: {:#?}", + result + ); +}