Skip to content

Commit

Permalink
Add delete_ref in RepoHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
tniessen committed Feb 28, 2024
1 parent ff27745 commit 9f3dfa4
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/api/repos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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: &params::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<()> {
Expand Down
56 changes: 56 additions & 0 deletions tests/repo_delete_ref_test.rs
Original file line number Diff line number Diff line change
@@ -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_remove_reaction_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_remove_reaction_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
);
}

0 comments on commit 9f3dfa4

Please # to comment.