1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
use std::sync::Arc;

use serde_json::Value;

use crate::types::ResultDynError;
use crate::v3::reference::DeleteReferenceInput;
use crate::v3::reference::GithubReferenceClient;

pub struct GithubBranchClient {
  pub(crate) reference_client: Arc<GithubReferenceClient>,
}

impl GithubBranchClient {
  pub fn new(reference_client: Arc<GithubReferenceClient>) -> ResultDynError<GithubBranchClient> {
    let client = GithubBranchClient { reference_client };

    return Ok(client);
  }
}

#[derive(Debug)]
pub struct DeleteBranchInput<'a> {
  pub repo_path: &'a str,
  pub branch_name: &'a str,
}

impl GithubBranchClient {
  /// This method returns a unit `()` because github returns 204 (no content)
  /// for successful  response.
  /// https://docs.github.com/en/rest/reference/git#delete-a-reference
  pub async fn delete<'a>(&self, input: DeleteBranchInput<'a>) -> ResultDynError<()> {
    let DeleteBranchInput {
      repo_path,
      branch_name,
    } = input;

    log::debug!("Deleting a branch {:?}", input);

    return self
      .reference_client
      .delete(DeleteReferenceInput {
        repo_path,
        reference_path: &format!("heads/{}", branch_name),
      })
      .await;
  }
}