Skip to main content

grp_core/common/orgs/
delete.rs

1use crate::platform::Platform;
2use crate::error::structs::Error;
3use crate::config::Config;
4use crate::common::structs::{Context, RequestType};
5use crate::animation::Animation;
6use crate::specific::gitlab;
7
8
9impl Platform {
10    /// Deletes the org that its given in the selected platform.
11    /// 
12    /// - `name`: the name or path of the **org** of the repo to be deleted.
13    /// - `config`: a `grp_core::Config`.
14    /// - `permanent`: only valid for "Gitlab", it will mark for deletion and delete permanently that org. 
15    /// - `animation`: a struct wich implements the trait `grp_core::animation::Animation`
16    /// 
17    /// # Return
18    /// `()` if the delete succed
19    /// 
20    /// # Error
21    /// a `grp_core::Error` containing the detail of the error. 
22    pub async fn delete_org<T: Into<String>, A: Animation + ?Sized>(&self,
23        name: T,
24        config: &Config,
25        permanent: bool,
26        animation: &Box<A>
27    ) -> Result<(), Error> {
28        let mut name = name.into();
29        let name_copy = name.clone();
30        
31        if matches!(self, Platform::Gitlab) {
32            animation.change_message("getting group id");
33            let user = gitlab::groups::get::get_group_with_path(self, &name, config).await?;
34            name = user.id;
35        }
36        
37        animation.change_message("generating url ...");
38        let url = self.url_delete_org(&name, &config.endpoint);
39        
40        animation.change_message("Deleting organization ...");
41        let result = self.delete(&url, config).await?;
42        
43        match (self, result.status().as_u16()) {
44            (Platform::Gitlab, 202 | 400) if permanent => {
45                animation.change_message("Permamently deleting gitlab group ...");
46                let user = gitlab::groups::get::get_group_by_id(self, &name, config).await?;
47                let _ = gitlab::groups::delete::premanently_remove(&self, &user, config).await?;
48                Ok(())
49            },
50            (
51                Platform::Codeberg |
52                Platform::Forgejo
53                , 204
54            ) | (_, 202) => Ok(()),
55            (_, _) => {
56                let context = Context {
57                    request_type: RequestType::DeleteOrg,
58                    owner: Some(name_copy),
59                    repo: None,
60                    additional: None
61                };
62                
63                let base_message = "Error deleting organization";
64                Err(self.unwrap(result, base_message, &config, context).await.unwrap_err())
65            }
66        }
67    }
68}