Extract shared Azure DevOps authentication logic to reduce duplication#56
Extract shared Azure DevOps authentication logic to reduce duplication#56
Conversation
Co-authored-by: norschel <12895005+norschel@users.noreply.github.com>
There was a problem hiding this comment.
Pull Request Overview
This PR centralizes Azure DevOps authentication by extracting shared PAT-based connection logic into a new utility, removing duplicate implementations in work item and pull request modules, and adding a basic test script.
- Added
getAzureDevOpsConnectioninazDevOpsUtils.tsfor consistent PAT-based WebApi connections. - Removed duplicate connection logic from
azDevOpsWorkItemFunctions.tsandazDevOpsPullrequestFunctions.tsand updated both to use the shared utility. - Introduced a simple manual test in
src-tests/azDevOpsConnectionTest.ts.
Reviewed Changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| src/azd/workitems/azDevOpsWorkItemFunctions.ts | Removed duplicate auth helper and imported shared getAzureDevOpsConnection. |
| src/azd/pullrequests/azDevOpsPullrequestFunctions.ts | Removed duplicate auth helper, added orgUrl computation, and imported shared util. |
| src/azd/azDevOpsUtils.ts | Added new getAzureDevOpsConnection utility with PAT retrieval and error handling. |
| src-tests/azDevOpsConnectionTest.ts | Added a manual test script logging calls to the shared auth utility. |
Comments suppressed due to low confidence (3)
src-tests/azDevOpsConnectionTest.ts:7
- This file uses console.log for manual verification but isn't integrated into any test framework or assertions. Consider converting it into proper unit tests (e.g., using Mocha or Jest) with real assertions to automate validation.
console.log("=== Azure DevOps Connection Test ===\n");
src-tests/azDevOpsConnectionTest.ts:20
- The error handling checks are described but not actually executed or asserted. You should mock the VS Code configuration to trigger the error path and assert that the error is thrown and the warning is shown.
console.log("✅ Function should throw error when PAT token not configured");
src/azd/azDevOpsUtils.ts:72
- [nitpick] The log message refers to 'Microsoft authentication', but this helper specifically handles the PAT-based path. Consider clarifying the message to reference Azure DevOps PAT explicitly for better diagnostics.
logInfo("Microsoft authentication not available, falling back to PAT token configuration required");
| */ | ||
| export async function getAzureDevOpsConnection(orgUrl: string): Promise<azdev.WebApi> { |
There was a problem hiding this comment.
[nitpick] Each call to this function retrieves the PAT and creates a new WebApi instance. If this is called multiple times, consider caching the created WebApi connection (or the token) to avoid repeated configuration lookups and object instantiation.
| */ | |
| export async function getAzureDevOpsConnection(orgUrl: string): Promise<azdev.WebApi> { | |
| */ | |
| // Cache for storing WebApi instances keyed by orgUrl | |
| const webApiCache: Map<string, azdev.WebApi> = new Map(); | |
| export async function getAzureDevOpsConnection(orgUrl: string): Promise<azdev.WebApi> { | |
| // Check if a cached connection exists for the orgUrl | |
| if (webApiCache.has(orgUrl)) { | |
| return webApiCache.get(orgUrl)!; | |
| } |
| let sharedRepoId: string; | ||
| try { | ||
| const connection = await getAzureDevOpsApi(requestHandlerContext, org); | ||
| const orgUrl = `https://dev.azure.com/${org}`; |
There was a problem hiding this comment.
[nitpick] The orgUrl assembly is duplicated across modules. Consider moving URL construction (e.g., https://dev.azure.com/${org}) into the shared utility or a separate helper to keep URL formatting consistent and avoid duplication.
| const orgUrl = `https://dev.azure.com/${org}`; | |
| const orgUrl = constructAzDevOpsUrl(org); |
The authentication logic was duplicated between work item and pull request functions, creating maintenance overhead and inconsistency. This PR extracts the shared authentication logic into a common utility function.
Changes Made
🔧 Extracted Shared Function
getAzureDevOpsConnection(orgUrl: string)toazDevOpsUtils.ts🧹 Eliminated Duplication
getAzureDevOpsConnection()fromazDevOpsWorkItemFunctions.ts(22 lines)getAzureDevOpsApi()fromazDevOpsPullrequestFunctions.ts(19 lines)✅ Benefits
Example Usage
The changes are minimal and surgical, maintaining all existing functionality while improving code maintainability.
Fixes #55.
💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.