Skip to content

Commit 496abc7

Browse files
authored
fix: improve org name extraction from url
Hi I’ve updated the `getOrgNameFromUrl` logic to handle both cloud and on-prem Azure DevOps URLs: 1. **Cloud URL** (`dev.azure.com`) ```ts const devMatch = url.match(/https?:\/\/dev\.azure\.com\/([^/]+)/); if (devMatch) return devMatch[1]; ``` 2. **On-prem/custom server** (only if URL contains "azure") ```ts const fallbackMatch = url.match(/https?:\/\/[^/]+\/([^/]+)/); return fallbackMatch ? fallbackMatch[1] : 'unknown-organization'; ``` 3. **Anything else** returns `"unknown-organization"` immediately. **Behavior examples:** | Input URL | Output | | --------------------------------------------------------- | ------------------------ | | `undefined` | `unknown-organization` | | `https://dev.azure.com/acme-inc` | `acme-inc` | | `https://dev.azure.com/acme-inc/project` | `acme-inc` | | `https://azure.abnoosgasht.org/DefaultCollection` | `DefaultCollection` | | `https://example.com/foo` | `unknown-organization` | With this change, setting ``` AZURE_DEVOPS_ORG_URL=https://azure.abnoosgasht.org/DefaultCollection ``` now correctly resolves the organization ID to `DefaultCollection`. Let me know if you spot any edge cases!
1 parent 48ecfac commit 496abc7

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

src/features/users/get-me/feature.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,11 @@ function extractOrgFromUrl(url: string): { organization: string } {
8383
match = url.match(/https?:\/\/([^.]+)\.visualstudio\.com/);
8484
}
8585

86+
// Fallback: capture the first path segment for any URL
87+
if (!match) {
88+
match = url.match(/https?:\/\/[^/]+\/([^/]+)/);
89+
}
90+
8691
const organization = match ? match[1] : '';
8792

8893
if (!organization) {

src/utils/environment.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,16 @@ dotenv.config();
1111
*/
1212
export function getOrgNameFromUrl(url?: string): string {
1313
if (!url) return 'unknown-organization';
14-
const match = url.match(/https?:\/\/dev\.azure\.com\/([^/]+)/);
15-
return match ? match[1] : 'unknown-organization';
14+
const devMatch = url.match(/https?:\/\/dev\.azure\.com\/([^/]+)/);
15+
if (devMatch) {
16+
return devMatch[1];
17+
}
18+
// Fallback only for Azure DevOps Server URLs
19+
if (url.includes('azure')) {
20+
const fallbackMatch = url.match(/https?:\/\/[^/]+\/([^/]+)/);
21+
return fallbackMatch ? fallbackMatch[1] : 'unknown-organization';
22+
}
23+
return 'unknown-organization';
1624
}
1725

1826
/**

0 commit comments

Comments
 (0)