cribeiro84
cribeiro84

Reputation: 91

How to get/identity the Azure DevOps Server base URL from the extension?

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

Azure DevOps Server

Upvotes: 4

Views: 5748

Answers (2)

Jason
Jason

Reputation: 81

TLDR; Base url includes the /tfs path, i.e. https://myselfhostedserver/tfs

I ran across this looking for the url for calling the api. I had to do this a couple of years ago and it was hard to find it then also. Looking at their docs they specify a url like this:

https://{instance}/{collection}/{project}/_apis/distributedtask/deploymentgroups?api-version=7.0

If I go to the site and want to look at my "Sales" project which is in a collection named "MyCollection", the url looks like this:

https://myselfhostedserver.example.com/tfs/MyCollection/Sales

To call this api then I have to use this url:

https://myselfhostedserver/tfs/MyCollection/Sales/_apis/distributedtask/deploymentgroups?api-version=7.0

Part of my confusion I think is that most apis have a base path for their api, while Azure Devops Server has separate api paths for each level. For instance to list the projects for a collection, and deployment groups for projects, the _apis partial path is in a different place:

https://myselfhostedserver/tfs/MyCollection/_apis/projects?api-version=2.0
https://myselfhostedserver/tfs/MyCollection/Sales/_apis/distributedtask/deploymentgroups?api-version=7.0

Upvotes: 0

Karel Kral
Karel Kral

Reputation: 5486

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}`);

Upvotes: 1

Related Questions