1use crate::{error::GiteeError, GiteeClient};
2use reqwest::Method;
3
4mod models;
5pub use models::*;
6
7impl GiteeClient {
8 pub async fn list_labels(&self, owner: &str, repo: &str) -> Result<Vec<Label>, GiteeError> {
10 let url = format!("{}/repos/{}/{}/labels", self.base_url(), owner, repo);
11 let response = self
12 .client()
13 .request(Method::GET, &url)
14 .header("Authorization", self.auth_header())
15 .send()
16 .await?;
17
18 if !response.status().is_success() {
19 return Err(GiteeError::ApiError(format!(
20 "Failed to list labels: {}",
21 response.status()
22 )));
23 }
24
25 let labels: Vec<Label> = response.json().await?;
26 Ok(labels)
27 }
28
29 pub async fn create_label(
31 &self,
32 owner: &str,
33 repo: &str,
34 name: &str,
35 color: &str,
36 description: Option<&str>,
37 ) -> Result<Label, GiteeError> {
38 let url = format!("{}/repos/{}/{}/labels", self.base_url(), owner, repo);
39
40 let mut payload = std::collections::HashMap::new();
41 payload.insert("name", name.to_string());
42 payload.insert("color", color.to_string());
43 if let Some(desc) = description {
44 payload.insert("description", desc.to_string());
45 }
46
47 let response = self
48 .client()
49 .request(Method::POST, &url)
50 .header("Authorization", self.auth_header())
51 .json(&payload)
52 .send()
53 .await?;
54
55 if !response.status().is_success() {
56 return Err(GiteeError::ApiError(format!(
57 "Failed to create label: {}",
58 response.status()
59 )));
60 }
61
62 let label: Label = response.json().await?;
63 Ok(label)
64 }
65
66 pub async fn update_label(
68 &self,
69 owner: &str,
70 repo: &str,
71 name: &str,
72 new_name: Option<&str>,
73 color: Option<&str>,
74 description: Option<&str>,
75 ) -> Result<Label, GiteeError> {
76 let url = format!("{}/repos/{}/{}/labels/{}", self.base_url(), owner, repo, name);
77
78 let mut payload = std::collections::HashMap::new();
79 if let Some(new_n) = new_name {
80 payload.insert("name", new_n.to_string());
81 }
82 if let Some(c) = color {
83 payload.insert("color", c.to_string());
84 }
85 if let Some(desc) = description {
86 payload.insert("description", desc.to_string());
87 }
88
89 let response = self
90 .client()
91 .request(Method::PATCH, &url)
92 .header("Authorization", self.auth_header())
93 .json(&payload)
94 .send()
95 .await?;
96
97 if !response.status().is_success() {
98 return Err(GiteeError::ApiError(format!(
99 "Failed to update label: {}",
100 response.status()
101 )));
102 }
103
104 let label: Label = response.json().await?;
105 Ok(label)
106 }
107
108 pub async fn delete_label(
110 &self,
111 owner: &str,
112 repo: &str,
113 name: &str,
114 ) -> Result<(), GiteeError> {
115 let url = format!("{}/repos/{}/{}/labels/{}", self.base_url(), owner, repo, name);
116 let response = self
117 .client()
118 .request(Method::DELETE, &url)
119 .header("Authorization", self.auth_header())
120 .send()
121 .await?;
122
123 if !response.status().is_success() {
124 return Err(GiteeError::ApiError(format!(
125 "Failed to delete label: {}",
126 response.status()
127 )));
128 }
129
130 Ok(())
131 }
132}