22
33from azure .devops .connection import Connection
44from azure .devops .credentials import BasicAuthentication
5- from azure .devops .v7_1 .git import GitClient
65from azure .devops .v7_1 .git .models import (
6+ Comment ,
77 GitPullRequest ,
88 GitPullRequestCommentThread ,
99 GitPullRequestCompletionOptions ,
10- GitRef ,
1110 GitRefUpdate ,
12- GitRefUpdateResult ,
13- GitRepository ,
14- Comment ,
1511)
1612from msrest .exceptions import ClientException
1713
2218
2319class AzureDevOpsGitRepoApiAdapter (GitRepoApi ):
2420 """Azure DevOps SDK adapter for GitOps CLI operations."""
25-
21+
2622 def __init__ (
2723 self ,
2824 git_provider_url : str ,
@@ -40,10 +36,10 @@ def __init__(
4036 self .__password = password
4137 self .__project_name = organisation # In Azure DevOps, "organisation" param is actually the project
4238 self .__repository_name = repository_name
43-
39+
4440 if not password :
4541 raise GitOpsException ("Password (Personal Access Token) is required for Azure DevOps" )
46-
42+
4743 # Create connection using Basic Authentication with PAT
4844 credentials = BasicAuthentication (self .__username , password )
4945 self .__connection = Connection (base_url = self .__base_url , creds = credentials )
@@ -79,36 +75,32 @@ def create_pull_request(
7975 # Ensure branch names have proper refs/ prefix
8076 source_ref = from_branch if from_branch .startswith ("refs/" ) else f"refs/heads/{ from_branch } "
8177 target_ref = to_branch if to_branch .startswith ("refs/" ) else f"refs/heads/{ to_branch } "
82-
78+
8379 pull_request = GitPullRequest (
8480 source_ref_name = source_ref ,
8581 target_ref_name = target_ref ,
8682 title = title ,
8783 description = description ,
8884 )
89-
85+
9086 created_pr = self .__git_client .create_pull_request (
9187 git_pull_request_to_create = pull_request ,
9288 repository_id = self .__repository_name ,
9389 project = self .__project_name ,
9490 )
95-
96- return GitRepoApi .PullRequestIdAndUrl (
97- pr_id = created_pr .pull_request_id ,
98- url = created_pr .url
99- )
100-
91+
92+ return GitRepoApi .PullRequestIdAndUrl (pr_id = created_pr .pull_request_id , url = created_pr .url )
93+
10194 except ClientException as ex :
10295 error_msg = str (ex )
10396 if "401" in error_msg :
10497 raise GitOpsException ("Bad credentials" ) from ex
105- elif "404" in error_msg :
98+ if "404" in error_msg :
10699 raise GitOpsException (
107100 f"Repository '{ self .__project_name } /{ self .__repository_name } ' does not exist"
108101 ) from ex
109- else :
110- raise GitOpsException (f"Error creating pull request: { error_msg } " ) from ex
111- except Exception as ex :
102+ raise GitOpsException (f"Error creating pull request: { error_msg } " ) from ex
103+ except Exception as ex : # noqa: BLE001
112104 raise GitOpsException (f"Error connecting to '{ self .__base_url } '" ) from ex
113105
114106 def merge_pull_request (
@@ -124,7 +116,7 @@ def merge_pull_request(
124116 pull_request_id = pr_id ,
125117 project = self .__project_name ,
126118 )
127-
119+
128120 # Map merge methods to Azure DevOps completion options
129121 completion_options = GitPullRequestCompletionOptions ()
130122 if merge_method == "squash" :
@@ -133,132 +125,134 @@ def merge_pull_request(
133125 completion_options .merge_strategy = "rebase"
134126 else : # merge
135127 completion_options .merge_strategy = "noFastForward"
136-
128+
137129 # Apply any additional merge parameters
138130 if merge_parameters :
139131 for key , value in merge_parameters .items ():
140132 setattr (completion_options , key , value )
141-
133+
142134 # Update the pull request to complete it
143135 pr_update = GitPullRequest (
144136 status = "completed" ,
145137 last_merge_source_commit = pr .last_merge_source_commit ,
146138 completion_options = completion_options ,
147139 )
148-
140+
149141 self .__git_client .update_pull_request (
150142 git_pull_request_to_update = pr_update ,
151143 repository_id = self .__repository_name ,
152144 pull_request_id = pr_id ,
153145 project = self .__project_name ,
154146 )
155-
147+
156148 except ClientException as ex :
157149 error_msg = str (ex )
158150 if "401" in error_msg :
159151 raise GitOpsException ("Bad credentials" ) from ex
160- elif "404" in error_msg :
152+ if "404" in error_msg :
161153 raise GitOpsException (f"Pull request with ID '{ pr_id } ' does not exist" ) from ex
162- else :
163- raise GitOpsException (f"Error merging pull request: { error_msg } " ) from ex
164- except Exception as ex :
154+ raise GitOpsException (f"Error merging pull request: { error_msg } " ) from ex
155+ except Exception as ex : # noqa: BLE001
165156 raise GitOpsException (f"Error connecting to '{ self .__base_url } '" ) from ex
166157
167- def add_pull_request_comment (self , pr_id : int , text : str , parent_id : int | None = None ) -> None :
158+ def add_pull_request_comment (self , pr_id : int , text : str , parent_id : int | None = None ) -> None : # noqa: ARG002
168159 try :
169160 comment = Comment (content = text , comment_type = "text" )
170161 thread = GitPullRequestCommentThread (
171162 comments = [comment ],
172163 status = "active" ,
173164 )
174-
165+
175166 # Azure DevOps doesn't support direct reply to comments in the same way as other platforms
176167 # parent_id is ignored for now
177-
168+
178169 self .__git_client .create_thread (
179170 comment_thread = thread ,
180171 repository_id = self .__repository_name ,
181172 pull_request_id = pr_id ,
182173 project = self .__project_name ,
183174 )
184-
175+
185176 except ClientException as ex :
186177 error_msg = str (ex )
187178 if "401" in error_msg :
188179 raise GitOpsException ("Bad credentials" ) from ex
189- elif "404" in error_msg :
180+ if "404" in error_msg :
190181 raise GitOpsException (f"Pull request with ID '{ pr_id } ' does not exist" ) from ex
191- else :
192- raise GitOpsException (f"Error adding comment: { error_msg } " ) from ex
193- except Exception as ex :
182+ raise GitOpsException (f"Error adding comment: { error_msg } " ) from ex
183+ except Exception as ex : # noqa: BLE001
194184 raise GitOpsException (f"Error connecting to '{ self .__base_url } '" ) from ex
195185
196186 def delete_branch (self , branch : str ) -> None :
187+ def _raise_branch_not_found () -> None :
188+ raise GitOpsException (f"Branch '{ branch } ' does not exist" )
189+
197190 try :
198191 # Get the branch reference first
199192 refs = self .__git_client .get_refs (
200193 repository_id = self .__repository_name ,
201194 project = self .__project_name ,
202195 filter = f"heads/{ branch } " ,
203196 )
204-
197+
205198 if not refs :
206- raise GitOpsException ( f"Branch ' { branch } ' does not exist" )
207-
199+ _raise_branch_not_found ( )
200+
208201 branch_ref = refs [0 ]
209-
202+
210203 # Create ref update to delete the branch
211204 ref_update = GitRefUpdate (
212205 name = f"refs/heads/{ branch } " ,
213206 old_object_id = branch_ref .object_id ,
214207 new_object_id = "0000000000000000000000000000000000000000" ,
215208 )
216-
209+
217210 self .__git_client .update_refs (
218211 ref_updates = [ref_update ],
219212 repository_id = self .__repository_name ,
220213 project = self .__project_name ,
221214 )
222-
215+
223216 except GitOpsException :
224217 # Re-raise GitOpsException without modification
225218 raise
226219 except ClientException as ex :
227220 error_msg = str (ex )
228221 if "401" in error_msg :
229222 raise GitOpsException ("Bad credentials" ) from ex
230- elif "404" in error_msg :
223+ if "404" in error_msg :
231224 raise GitOpsException (f"Branch '{ branch } ' does not exist" ) from ex
232- else :
233- raise GitOpsException (f"Error deleting branch: { error_msg } " ) from ex
234- except Exception as ex :
225+ raise GitOpsException (f"Error deleting branch: { error_msg } " ) from ex
226+ except Exception as ex : # noqa: BLE001
235227 raise GitOpsException (f"Error connecting to '{ self .__base_url } '" ) from ex
236228
237229 def get_branch_head_hash (self , branch : str ) -> str :
230+ def _raise_branch_not_found () -> None :
231+ raise GitOpsException (f"Branch '{ branch } ' does not exist" )
232+
238233 try :
239234 refs = self .__git_client .get_refs (
240235 repository_id = self .__repository_name ,
241236 project = self .__project_name ,
242237 filter = f"heads/{ branch } " ,
243238 )
244-
239+
245240 if not refs :
246- raise GitOpsException ( f"Branch ' { branch } ' does not exist" )
247-
248- return refs [0 ].object_id
249-
241+ _raise_branch_not_found ( )
242+
243+ return str ( refs [0 ].object_id )
244+
250245 except GitOpsException :
251246 # Re-raise GitOpsException without modification
252247 raise
253248 except ClientException as ex :
254249 error_msg = str (ex )
255250 if "401" in error_msg :
256251 raise GitOpsException ("Bad credentials" ) from ex
257- elif "404" in error_msg :
252+ if "404" in error_msg :
258253 raise GitOpsException (f"Branch '{ branch } ' does not exist" ) from ex
259- else :
260- raise GitOpsException (f"Error getting branch hash: { error_msg } " ) from ex
261- except Exception as ex :
254+ raise GitOpsException (f"Error getting branch hash: { error_msg } " ) from ex
255+ except Exception as ex : # noqa: BLE001
262256 raise GitOpsException (f"Error connecting to '{ self .__base_url } '" ) from ex
263257
264258 def get_pull_request_branch (self , pr_id : int ) -> str :
@@ -268,22 +262,21 @@ def get_pull_request_branch(self, pr_id: int) -> str:
268262 pull_request_id = pr_id ,
269263 project = self .__project_name ,
270264 )
271-
265+
272266 # Extract branch name from sourceRefName (remove refs/heads/ prefix)
273- source_ref = pr .source_ref_name
267+ source_ref = str ( pr .source_ref_name )
274268 if source_ref .startswith ("refs/heads/" ):
275269 return source_ref [11 :] # Remove "refs/heads/" prefix
276270 return source_ref
277-
271+
278272 except ClientException as ex :
279273 error_msg = str (ex )
280274 if "401" in error_msg :
281275 raise GitOpsException ("Bad credentials" ) from ex
282- elif "404" in error_msg :
276+ if "404" in error_msg :
283277 raise GitOpsException (f"Pull request with ID '{ pr_id } ' does not exist" ) from ex
284- else :
285- raise GitOpsException (f"Error getting pull request: { error_msg } " ) from ex
286- except Exception as ex :
278+ raise GitOpsException (f"Error getting pull request: { error_msg } " ) from ex
279+ except Exception as ex : # noqa: BLE001
287280 raise GitOpsException (f"Error connecting to '{ self .__base_url } '" ) from ex
288281
289282 def add_pull_request_label (self , pr_id : int , pr_labels : list [str ]) -> None :
@@ -298,22 +291,21 @@ def __get_default_branch(self) -> str:
298291 repository_id = self .__repository_name ,
299292 project = self .__project_name ,
300293 )
301-
294+
302295 default_branch = repo .default_branch or "refs/heads/main"
303296 # Remove refs/heads/ prefix if present
304297 if default_branch .startswith ("refs/heads/" ):
305298 return default_branch [11 :]
306299 return default_branch
307-
300+
308301 except ClientException as ex :
309302 error_msg = str (ex )
310303 if "401" in error_msg :
311304 raise GitOpsException ("Bad credentials" ) from ex
312- elif "404" in error_msg :
305+ if "404" in error_msg :
313306 raise GitOpsException (
314307 f"Repository '{ self .__project_name } /{ self .__repository_name } ' does not exist"
315308 ) from ex
316- else :
317- raise GitOpsException (f"Error getting repository info: { error_msg } " ) from ex
318- except Exception as ex :
319- raise GitOpsException (f"Error connecting to '{ self .__base_url } '" ) from ex
309+ raise GitOpsException (f"Error getting repository info: { error_msg } " ) from ex
310+ except Exception as ex : # noqa: BLE001
311+ raise GitOpsException (f"Error connecting to '{ self .__base_url } '" ) from ex
0 commit comments