我需要访问以Html / JavaScript编写的Web扩展的全局变量。
扩展清单:
"targets": [
{
"id": "Microsoft.VisualStudio.Services"
}
],
"contributions": [
{
"id": "coverage-web-hub",
"type": "ms.vss-web.hub",
"targets": [
"ms.vss-build-web.build-release-hub-group"
],
"properties": {
"name": "Coverage",
"uri": "index.html"
}
}
],
是否可以通过以下方式访问全局变量:
或其他REST API
通常可以与扩展名共享变量吗?
答案 0 :(得分:0)
1。步骤:将范围添加到扩展清单
"scopes": [
"vso.variablegroups_read",
"vso.variablegroups_manage"
],
2。步骤:获取您的Azure DevOps URL(inside the VSS.ready() function
)
//GET AZURE DEVOPS SERVER URL
var uri = VSS.getWebContext().host.uri
var project = VSS.getWebContext().project.name
var url = uri + project
3。步骤:根据ID验证和读取您的变量组(数字,从1开始)
//AUTHENTIFICATION https://docs.microsoft.com/en-us/azure/devops/extend/develop/auth?view=azure-devops
VSS.require(["VSS/Authentication/Services"],
function (VSS_Auth_Service) {
VSS.getAccessToken().then(function (token) {
//FORMAT THE AUTH HEADER
var authHeader = VSS_Auth_Service.authTokenManager.getAuthorizationHeader(token);
//REQUEST AZURE DEVOPS GLOBAL VARIABLES
var request = new XMLHttpRequest(); //USE 'url' FROM STEP TWO HERE
request.open("GET", url + "/_apis/distributedtask/variablegroups/1?", false); // 1 = ID OF VARIABLE GROUP; false for synchronous request
request.setRequestHeader("Authorization", authHeader)
request.send(null);
var Json = JSON.parse(request.responseText); //GET VARIABLE GROUP AS JSON
var Coverage = Json.variables.Coverage.value //VARIABLE NAMED 'Coverage'
var CoverageHistoryJson = Json.variables.CoverageHistoryJson.value //VARIABLE NAMED 'CoverageHistoryJson'
console.log("RESULT: " + Coverage)
});
});