Skip to main content

grp_core/common/repos/
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 repo that its given in the selected platform.
11    /// 
12    /// - `owner`: the name or path of the **user** or **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 repo. 
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_repo<T: Into<String>, A: Animation + ?Sized>(&self,
23        owner: T, repo: T,
24        config: &Config,
25        permanent: bool,
26        animation: &Box<A>
27    ) -> Result<(), Error> {
28        let mut owner = owner.into(); let repo = repo.into();
29        let owner_copy = owner.clone();
30        
31        if matches!(self, Platform::Gitlab) {
32            animation.change_message("getting project id");
33            let project = gitlab::projects::get::get_project_with_path(&self, &owner, &repo, config).await?;
34            owner = project.id.to_string();
35        }
36        
37        animation.change_message("generating url ...");
38        let url = self.url_delete_repo(&owner, &repo, &config.endpoint).await;
39        
40        animation.change_message("Deleting repository ...");
41        let result = self.delete(&url, config).await?;
42        
43        match (self, result.status().as_u16()) {
44            (
45                Platform::Gitea |
46                Platform::Codeberg |
47                Platform::Forgejo | 
48                Platform::Github, 204
49            ) => Ok(()),
50            (Platform::Gitlab, 202 | 400) if permanent => {
51                animation.change_message("Permamently deleting gitlab project ...");
52                let project = gitlab::projects::get::get_project_with_id(&self, &owner, config).await?;
53                let _ = gitlab::projects::delete::premanently_remove(&self, &project, config).await?;
54                Ok(())
55            },
56            (Platform::Gitlab, 202) => Ok(()),
57            (_, _) => {
58                let context = Context {
59                    request_type: RequestType::Delete,
60                    owner: Some(owner_copy),
61                    repo: Some(repo),
62                    additional: None
63                };
64                
65                let base_message = "Error deleting repository";
66                Err(self.unwrap(result, base_message, &config, context).await.unwrap_err())
67            }
68        }
69    }
70}