1
- use crate :: handlers_prelude:: eyre_to_axum_err ;
1
+ use crate :: handlers_prelude:: ApiError ;
2
2
use crate :: AppState ;
3
3
use axum:: routing:: { get, post, put} ;
4
4
use axum:: {
@@ -11,20 +11,6 @@ use serde::{Deserialize, Serialize};
11
11
use serde_json:: Value ;
12
12
use tracing:: { error, info} ;
13
13
14
- /// General API response structure
15
- #[ derive( Serialize , Debug ) ]
16
- pub struct ApiResponse < T > {
17
- pub status : String ,
18
- pub message : String ,
19
- pub data : Option < T > ,
20
- }
21
-
22
- /// Error response structure
23
- #[ derive( Serialize , Debug ) ]
24
- pub struct ApiErrorResponse {
25
- pub error : String ,
26
- }
27
-
28
14
/// Represents the structure for a pull request creation response
29
15
#[ derive( Serialize , Debug ) ]
30
16
pub struct CreatePRData {
@@ -72,13 +58,9 @@ pub struct UpdatePRRequest {
72
58
/// Fetches the list of branches from a GitHub repository.
73
59
pub async fn list_branches_handler (
74
60
State ( state) : State < AppState > ,
75
- ) -> Result < ( StatusCode , Json < ApiResponse < BranchesData > > ) , ( StatusCode , String ) > {
61
+ ) -> Result < Json < BranchesData > , ApiError > {
76
62
// Fetch the branch details from GitHub using the GitHubClient instance
77
- let branch_details = state
78
- . gh_client
79
- . list_branches ( )
80
- . await
81
- . map_err ( eyre_to_axum_err) ?;
63
+ let branch_details = state. gh_client . list_branches ( ) . await ?;
82
64
83
65
// Extract branch names and handle protection status if needed
84
66
let branches: Vec < String > = branch_details
@@ -93,23 +75,16 @@ pub async fn list_branches_handler(
93
75
} )
94
76
. collect ( ) ;
95
77
96
- // Return success response
78
+ // Wrap the branches data in the BranchesData struct and return as JSON
97
79
info ! ( "Branches fetched successfully." ) ;
98
- Ok ( (
99
- StatusCode :: OK ,
100
- Json ( ApiResponse {
101
- status : "success" . to_string ( ) ,
102
- message : "Branches fetched successfully" . to_string ( ) ,
103
- data : Some ( BranchesData { branches } ) ,
104
- } ) ,
105
- ) )
80
+ Ok ( Json ( BranchesData { branches } ) )
106
81
}
107
82
108
83
/// Handler to create a pull request from a specified head branch to a base branch.
109
84
pub async fn create_pull_request_handler (
110
85
State ( state) : State < AppState > ,
111
86
Json ( payload) : Json < CreatePRRequest > ,
112
- ) -> Result < ( StatusCode , Json < ApiResponse < CreatePRData > > ) , ( StatusCode , String ) > {
87
+ ) -> Result < ( StatusCode , Json < CreatePRData > ) , ( StatusCode , String ) > {
113
88
// Create the pull request using the new method from GitHubClient
114
89
match state
115
90
. gh_client
@@ -128,14 +103,7 @@ pub async fn create_pull_request_handler(
128
103
"Pull request created successfully from {} to {}" ,
129
104
payload. head_branch, payload. base_branch
130
105
) ;
131
- Ok ( (
132
- StatusCode :: CREATED ,
133
- Json ( ApiResponse {
134
- status : "success" . to_string ( ) ,
135
- message : "Pull request created successfully" . to_string ( ) ,
136
- data : Some ( CreatePRData { pull_request_url } ) ,
137
- } ) ,
138
- ) )
106
+ Ok ( ( StatusCode :: CREATED , Json ( CreatePRData { pull_request_url } ) ) )
139
107
}
140
108
Err ( err) => {
141
109
// Handle error case in creating the pull request
@@ -149,7 +117,7 @@ pub async fn create_pull_request_handler(
149
117
pub async fn update_pull_request_handler (
150
118
State ( state) : State < AppState > ,
151
119
Json ( payload) : Json < UpdatePRRequest > ,
152
- ) -> Result < ( StatusCode , Json < ApiResponse < String > > ) , ( StatusCode , String ) > {
120
+ ) -> Result < ( StatusCode , Json < String > ) , ( StatusCode , String ) > {
153
121
// Update the pull request
154
122
match state
155
123
. gh_client
@@ -164,14 +132,7 @@ pub async fn update_pull_request_handler(
164
132
{
165
133
Ok ( updated_pr_url) => {
166
134
info ! ( "Pull request #{} updated successfully" , payload. pr_number) ;
167
- Ok ( (
168
- StatusCode :: OK ,
169
- Json ( ApiResponse {
170
- status : "success" . to_string ( ) ,
171
- message : "Pull request updated successfully." . to_string ( ) ,
172
- data : Some ( updated_pr_url) ,
173
- } ) ,
174
- ) )
135
+ Ok ( ( StatusCode :: OK , Json ( updated_pr_url) ) )
175
136
}
176
137
Err ( err) => {
177
138
let error_message = format ! ( "Failed to update pull request: {:?}" , err) ;
@@ -184,18 +145,14 @@ pub async fn update_pull_request_handler(
184
145
pub async fn close_pull_request_handler (
185
146
State ( state) : State < AppState > ,
186
147
Path ( pr_number) : Path < u64 > ,
187
- ) -> Result < ( StatusCode , Json < ApiResponse < String > > ) , ( StatusCode , String ) > {
148
+ ) -> Result < ( StatusCode , Json < String > ) , ( StatusCode , String ) > {
188
149
// Attempt to close the pull request
189
150
match state. gh_client . close_pull_request ( pr_number) . await {
190
151
Ok ( _) => {
191
152
info ! ( "Pull request #{} closed successfully" , pr_number) ;
192
153
Ok ( (
193
154
StatusCode :: OK ,
194
- Json ( ApiResponse {
195
- status : "success" . to_string ( ) ,
196
- message : "Pull request closed successfully." . to_string ( ) ,
197
- data : Some ( format ! ( "Pull request #{} closed." , pr_number) ) ,
198
- } ) ,
155
+ Json ( format ! ( "Pull request #{} closed." , pr_number) ) ,
199
156
) )
200
157
}
201
158
Err ( err) => {
@@ -234,18 +191,17 @@ pub async fn checkout_or_create_branch_handler(
234
191
pub async fn pull_handler (
235
192
State ( state) : State < AppState > ,
236
193
Path ( branch) : Path < String > ,
237
- ) -> Result < ( StatusCode , Json < ApiResponse < String > > ) , ( StatusCode , String ) > {
194
+ ) -> Result < ( StatusCode , Json < String > ) , ( StatusCode , String ) > {
238
195
// Attempt to pull the latest changes for the specified branch
239
196
match state. git . git_pull_branch ( & branch) {
240
197
Ok ( _) => {
241
198
info ! ( "Repository pulled successfully for branch '{}'." , branch) ;
242
199
Ok ( (
243
200
StatusCode :: OK ,
244
- Json ( ApiResponse {
245
- status : "success" . to_string ( ) ,
246
- message : format ! ( "Repository pulled successfully for branch '{}'." , branch) ,
247
- data : Some ( "Pull operation completed." . to_string ( ) ) ,
248
- } ) ,
201
+ Json ( format ! (
202
+ "Repository pulled successfully for branch '{}'." ,
203
+ branch
204
+ ) ) ,
249
205
) )
250
206
}
251
207
Err ( err) => {
@@ -264,21 +220,12 @@ pub async fn pull_handler(
264
220
/// Handler for fetching the current branch of the repository.
265
221
pub async fn get_current_branch_handler (
266
222
State ( state) : State < AppState > ,
267
- ) -> Result < ( StatusCode , Json < ApiResponse < String > > ) , ( StatusCode , String ) > {
223
+ ) -> Result < ( StatusCode , Json < String > ) , ( StatusCode , String ) > {
268
224
// Use the git::Interface from AppState to get the current branch
269
225
match state. git . get_current_branch ( ) . await {
270
226
Ok ( branch_name) => {
271
227
info ! ( "Current branch is: {}" , branch_name) ;
272
-
273
- // Return the branch name in the response
274
- Ok ( (
275
- StatusCode :: OK ,
276
- Json ( ApiResponse {
277
- status : "success" . to_string ( ) ,
278
- message : "Current branch fetched successfully." . to_string ( ) ,
279
- data : Some ( branch_name) ,
280
- } ) ,
281
- ) )
228
+ Ok ( ( StatusCode :: OK , Json ( branch_name) ) )
282
229
}
283
230
Err ( err) => {
284
231
error ! ( "Failed to get current branch: {}" , err) ;
@@ -293,21 +240,12 @@ pub async fn get_current_branch_handler(
293
240
/// Handler for fetching the default branch of the repository.
294
241
pub async fn get_default_branch_handler (
295
242
State ( state) : State < AppState > ,
296
- ) -> Result < ( StatusCode , Json < ApiResponse < String > > ) , ( StatusCode , String ) > {
243
+ ) -> Result < ( StatusCode , Json < String > ) , ( StatusCode , String ) > {
297
244
// Use the `get_default_branch` method from the `Gh` struct in AppState
298
245
match state. gh_client . get_default_branch ( ) . await {
299
246
Ok ( default_branch) => {
300
247
info ! ( "Default branch is: {}" , default_branch) ;
301
-
302
- // Return the default branch name in the response
303
- Ok ( (
304
- StatusCode :: OK ,
305
- Json ( ApiResponse {
306
- status : "success" . to_string ( ) ,
307
- message : "Default branch fetched successfully." . to_string ( ) ,
308
- data : Some ( default_branch) ,
309
- } ) ,
310
- ) )
248
+ Ok ( ( StatusCode :: OK , Json ( default_branch) ) )
311
249
}
312
250
Err ( err) => {
313
251
error ! ( "Failed to get default branch: {}" , err) ;
@@ -323,18 +261,14 @@ pub async fn get_default_branch_handler(
323
261
pub async fn get_issues_handler (
324
262
State ( state) : State < AppState > ,
325
263
Path ( state_param) : Path < String > ,
326
- ) -> Result < ( StatusCode , Json < ApiResponse < IssuesData > > ) , ( StatusCode , String ) > {
264
+ ) -> Result < ( StatusCode , Json < IssuesData > ) , ( StatusCode , String ) > {
327
265
let state_param = state_param. as_str ( ) ;
328
266
329
267
// Fetch issues using the GitHub client
330
268
match state. gh_client . get_issues ( Some ( state_param) , None ) . await {
331
269
Ok ( issues) => {
332
270
info ! ( "Issues fetched successfully." ) ;
333
- let response = ApiResponse {
334
- status : "success" . to_string ( ) ,
335
- message : "Issues fetched successfully." . to_string ( ) ,
336
- data : Some ( IssuesData { issues } ) ,
337
- } ;
271
+ let response = IssuesData { issues } ;
338
272
Ok ( ( StatusCode :: OK , Json ( response) ) )
339
273
}
340
274
Err ( err) => {
0 commit comments