繁体   English   中英

如何从扩展中获取/识别 Azure DevOps Server 基本 URL?

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

我正在为 Azure DevOps 服务和服务器开发一个扩展,但是一旦我导航到目标资源,我就很难获得 Azure DevOps 服务器版本的基本 URL,例如:拉取请求详细信息。

有什么办法可以得到吗? 例如:

Azure DevOps 服务

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

Azure DevOps 服务器

  • 服务器名称/{?}
  • 服务器名称:8080/{tfs}/{?}

我正在使用此代码:

  1. 使用插件中的document.referrer作为 URL 源。

  2. 找到项目名称并提取项目名称之前的 URL 部分。

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

2021 年 5 月 4 日编辑:由于 document.referrer 对某些浏览器不起作用,我现在使用更多的“DevOps 方式”:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM