在vsts扩展任务

时间:2017-09-17 19:39:13

标签: json tfs mustache

我创建了一个与REST API对话的任务,用于检索2个选项列表的值。 当仅使用jsonpath时,填充第一个下拉框可以正常工作。 基于第一个选项列表,我想要检索第二个列表的值。

我尝试了一些变化,我正在尝试这样的事情: 我在第一次休息时收到的json类似于:

{
  "id": "45",
  "href": "https://selfservice/api/configurations/45/",
  "name": "Type",
  "description": "",
  "version": "0.0.4",
  "attributes": [
    {
      "value": {
        "sdk-object": {
          "type": "ConfigurationElement",
          "id": "56"
        }
      },
      "type": "ConfigurationElement",
      "name": "win"
    },
    {
      "value": {
        "sdk-object": {
          "type": "ConfigurationElement",
          "id": "57"
        }
      },
      "type": "ConfigurationElement",
      "name": "lin"
    }
  ]
}

我尝试在列表中显示属性名称,并且需要第二个选项列表中属性的id。 我在task.json中创建了以下数据源绑定。当然,目标存在于任务中。 task.json:

{
   "id": "GUID",
   "name": "Spinup",
   "friendlyName": "Create environment",
   "description": "Starts Workflow to create an environment. ___ The following placeholders are created deplending on the template and can be used in the rest of the release:**XLDEnvironment** and **testAgentHostname**",
   "category": "Deploy",
   "author": "***",
   "version": {
      "Major": 0,
      "Minor": 0,
      "Patch": 33
   },
  "minimumAgentVersion": "1.95.3",
  "inputs": [
    {
      "label": "Endpoint",
      "name": "connectedServiceName",
      "required": true,
      "type": "connectedService:server",
      "helpMarkDown": "endpoint to connect to."
    },
    {
      "name": "stage",
      "type": "string",
      "label": "Stage",
      "defaultValue": "$(Release.EnvironmentName)",
      "required": true,
      "helpMarkDown": "Stage of the release, default value is based on the pipeline."
    },
    {
      "name": "releaseName",
      "type": "string",
      "label": "Environment name",
      "defaultValue": "$(Release.ReleaseName)",
      "required": true,
      "helpMarkDown": "Name of the environment that will be created."
    },
    {
      "name": "owner",
      "type": "string",
      "label": "Owner of the environment",
      "defaultValue": "***",
      "required": true,
      "helpMarkDown": "It is common to use the initials of the product owner in this field. There has to be a valid AD user owner of each environment. This can also be set by using a variable through the pipeline."
    },
    {
      "name": "group",
      "type": "string",
      "label": "Group of the owner",
      "defaultValue": "",
      "required": true,
      "helpMarkDown": "group that owns the environment. groups can be requested by the infrastructure department. Example: ****"
    },
    {
      "name": "platform",
      "type": "pickList",
      "label": "Platform (OS)",
      "defaultValue": "",
      "required": true,
      "helpMarkDown": "Choose the type of the target platform."
    },
    {
      "name": "size",
      "type": "pickList",
      "label": "Environment Template",
      "defaultValue": "",
      "required": true,
      "helpMarkDown": "Size of the environment to create."
    }
  ],
  "dataSourceBindings": [
    {
      "dataSourceName": "GetCEPlatformType",
      "endpointId": "$(connectedServiceName)",
      "target": "platform",
      "selector": "jsonpath:$.attributes[*].name",
      "keySelector": "jsonpath:$.attributes[*].value.sdk-object.id"
    },
    {
      "dataSourceName": "GetCEPlatformSize",
      "endpointId": "$(connectedServiceName)",
      "target": "size",
      "parameters": {
        "platformTypeId": "$(platform)"
      }
    }
  ],
   "instanceNameFormat": "Spin up $(size) $(platform) environment",
   "execution": { 
      "PowerShell3": {
         "target": "$(currentDirectory)\\task.ps1",
         "argumentFormat": "",
         "workingDirectory": "$(currentDirectory)"
      }
   }
}

VSS-extension.json:

{
    "manifestVersion": 1,
    "id": "*****",
    "name": "Release Tasks",
    "version": "1.0.1",
    "publisher": "***",
    "targets": [
        {
            "id": "Microsoft.VisualStudio.Services"
        }
    ],    
    "description": "Tools to contact ***. Includes a task to spin up a platform and a task to remove the platform.",
    "categories": [
        "Build and release"
    ],
    "icons": {
        "default": "images/extension-icon.png"        
    },
    "files": [
        {
            "path": "RemoveEnvironment"
        },
        {
            "path": "SpinUpEnvironment"
        }

    ],
    "contributions": [
        {
            "id": "******",
            "type": "ms.vss-distributed-task.task",
            "targets": [
                "ms.vss-distributed-task.tasks"
            ],
            "properties": {
                "name": "RemoveEnvironment"
            }
        },
        {
            "id": "*********",
            "type": "ms.vss-distributed-task.task",
            "targets": [
                "ms.vss-distributed-task.tasks"
            ],
            "properties": {
                "name": "SpinUpEnvironment"
            }
        },
        {
            "description": "Service Endpoint type for all connections",
            "id": "endpoint-type",
            "properties": {
                "authenticationSchemes": [
                    {
                        "inputDescriptors": [
                            {
                                "description": "Username",
                                "id": "username",
                                "inputMode": "textbox",
                                "name": "Username",
                                "validation": {
                                    "dataType": "string",
                                    "isRequired": true
                                }
                            },
                            {
                                "description": "Password",
                                "id": "password",
                                "inputMode": "passwordbox",
                                "isConfidential": true,
                                "name": "Password",
                                "validation": {
                                    "dataType": "string",
                                    "isRequired": false
                                }
                            }
                        ],
                        "type": "ms.vss-endpoint.endpoint-auth-scheme-basic"
                    }
                ],
                "dataSources": [
                {
                    "endpointUrl": "api/configurations/*****/",
                    "name": "GetCEPlatformType",
                    "resultSelector": "jsonpath:$.attributes[*].name"
                },
                {
                    "endpointUrl": "api/configurations/$(platformTypeId)/",
                    "name": "GetCEPlatformSize",
                    "resultSelector": "jsonpath:$.attributes[*].name"
                }
            ],
                "displayName": "*****",
                "helpMarkDown": "Enter the url and credentials to connect to the endpoint.",
                "name": "server",
                "url": {
                    "displayName": "Server URL",
                    "helpText": "Url for the server to connect to."
                }
            },
            "targets": [
                "ms.vss-endpoint.endpoint-types"
            ],
            "type": "ms.vss-endpoint.service-endpoint-type"
        }
    ]
}
可悲的是,我不能让这个工作。数据源存在于端点中,但我不确定如何处理resultSelector。 有没有人知道如何使这个工作?关于这方面的文件并不太好。 我不熟悉胡子,但在胡子测试工具中,这似乎有效。

赞赏的想法!

1 个答案:

答案 0 :(得分:2)

在第一个数据源中尝试使用它:

{
  "dataSourceName": "dsList1",
  "endpointId": "$(connectedServiceName)",
  "target": "list1",
  "selector": "jsonpath:$.attributes[*].name",
  "keySelector": "jsonpath:$.attributes[*].value.sdk-object.id"
}

更新:

你试图使用" platformTypeId"在vss-extension.json文件中,当它在task.json中定义时,这会导致您在注释中提到的错误消息。

顺便说一下,我刚注意到你正在使用" dataSourceBindings",那么你就不能使用" selector"和" KeySelector"解析结果,因为它们用于" sourceDefinitions"。

要使用" dataSourceBindings"实现所需的功能,您可以直接在task.json中定义端点URL,而不是在端点贡献中定义数据源。

因此,您可以先移动vss-extension.json文件中的datasources部分,然后在task.json文件中,更改为以下内容:

"dataSourceBindings": [
    {
      "endpointId": "$(connectedServiceName)",
      "endpointURL": "api/configurations/*****/",
      "target": "platform",
      "resultselector": "jsonpath:$.attributes[*]",
      "resultTemplate": "{ \"Value\" : \"{{{value.id}}}\", \"DisplayValue\" : \"{{{name}}}\" }"
    },
    {
      "endpointId": "$(connectedServiceName)",
      "endpointURL": "api/configurations/$(platformTypeId)/",
      "target": "size"
    }
  ],