4
votes

I’m developing an extension for both Azure DevOps Services and Server but I’m struggling to get the base URL for the Azure DevOps Server version once I have some navigations to the target resource, such as: Pull Request details.

Is there any way to get it? For instance:

Azure DevOps Services

  • dev.azure.com/organization
  • organization.visualstudio.com

Azure DevOps Server

  • serverName/{?}
  • serverName:8080/{tfs}/{?}
1
Which tfs version does your extension target? In this document there exists some ways that requires the organization name or ID(devops service) to get the base url. You can check it to get some help.LoLance
@LanceLi-MSFT that page only seems to describe how to get various api urls if you already know the base url. What's requested is how to get the server url (e.g. protocol, port, etc)Anders Forsgren

1 Answers

1
votes

I am using this code:

  1. Use document.referrer from the plugin as a URL source.

  2. Locate the project name and extract the part of the URL before the project name.

    const url = document.referrer;
    // how to detect base Url: get projectName and find 'ProjectName/_'
    // http://tfs2017-test:8080/tfs/Org/MyProject/_apps/hub/...
    const projectService = await SDK.getService<IProjectPageService>(CommonServiceIds.ProjectPageService);
    const project = await projectService.getProject();
    
    if (!project) {
        throw new Error("Cannot get project.")
    }
    
    const findStr = `${project.name}/_`;
    const index = url.indexOf(findStr);
    
    if (index < 0) {
        throw new Error(`URL '${url}' does not contain '${findStr}' substring`);
    }
    
    // extract from url without '_'
    this._baseUrl = url.substring(0, index + findStr.length - 1);
    

Edit 04.05.2021: Because document.referrer is not working good for some browsers, I am using now more "DevOps way":

// https://github.com/microsoft/azure-devops-extension-sdk/issues/28
this._locationService = await SDK.getService<ILocationService>(CommonServiceIds.LocationService);
const hostBaseUrl = await this._locationService.getResourceAreaLocation(
    CoreRestClient.RESOURCE_AREA_ID
);

console.log(`hostBaseUrl: ${hostBaseUrl}`);

const projectService = await SDK.getService<IProjectPageService>(CommonServiceIds.ProjectPageService);
const project = await projectService.getProject();

if (!project) {
    throw new Error("Cannot get project.")
}

this._baseUrl = `${hostBaseUrl}${project.name}/`;
console.log(`baseUrl: ${this._baseUrl}`);