Can ExtensionDataService be used from a PowerShell-based VSTS build task?

483 views Asked by At

I have created a PowerShell-based build task for Visual Studio Team Services (formerly Visual Studio Online). I have implemented the majority of the functionality I need, but for the last bit of functionality I need to be able to persist a small amount of data between builds.

The ExtensionDataService seems like exactly what I want (in particular, the setValue and getValue methods), but the documentation and examples I have found are for node.js-based build tasks:

    VSS.getService(VSS.ServiceIds.ExtensionData).then(function(dataService) {
    // Set a user-scoped preference
    dataService.setValue("pref1", 12345, {scopeType: "User"}).then(function(value) {
        console.log("User preference value is " + value);
    });

The previous page also has a partial example of calling the REST API, but I have gotten 404 errors when trying to use it to either save or retrieve values:

GET _apis/ExtensionManagement/InstalledExtensions/{publisherName}/{extensionName}/Data/Scopes/User/Me/Collections/%24settings/Documents
{
    "id": "myKey",
    "__etag": -1,
    "value": "myValue"
}

Can PowerShell be used to access the ExtensionDataService, either by using a library or by calling the REST API directly?

1

There are 1 answers

11
starian chen-MSFT On BEST ANSWER

You can call REST API through PowerShell.

Set value (Put request):

 https://[vsts name].extmgmt.visualstudio.com/_apis/ExtensionManagement/InstalledExtensions/{publisherName}/{extension id}/Data/Scopes/User/Me/Collections/%24settings/Documents?api-version=3.1-preview.1

Body (Content-Type:application/json)

{
  "id": "myKey",
  "__etag": -1,
  "value": "myValue"
}

Get value (Get request):

https://[vsts name].extmgmt.visualstudio.com/_apis/ExtensionManagement/InstalledExtensions/{publisherName}/{extension id}/Data/Scopes/User/Me/Collections/%24settings/Documents/mykey?api-version=3.1-preview.1

The publisher name and extension id could be get in package json file (e.g. vss-extension.json)

Regarding call REST API through PowerShell, you can refer to this article: Calling VSTS APIs with PowerShell

Simple sample to call REST API:

Param(
   [string]$vstsAccount = "<VSTS-ACCOUNT-NAME>",
   [string]$projectName = "<PROJECT-NAME>",
   [string]$buildNumber = "<BUILD-NUMBER>",
   [string]$keepForever = "true",
   [string]$user = "",
   [string]$token = "<PERSONAL-ACCESS-TOKEN>"
)

# Base64-encodes the Personal Access Token (PAT) appropriately
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user,$token)))

$uri = "https://$($vstsAccount).visualstudio.com/DefaultCollection/$($projectName)/_apis/build/builds?api-version=2.0&buildNumber=$($buildNumber)"

$result = Invoke-RestMethod -Uri $uri -Method Get -ContentType "application/json" -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}

PowerShell script to get the base URL:

Function GetURL{
param([string]$url)
$regex=New-Object System.Text.RegularExpressions.Regex("https:\/\/(.*).visualstudio.com")
$match=$regex.Match($url)
 if($match.Success)
    {
        $vstsAccount=$match.Groups[1]
        $resultURL="https://$vstsAccount.extmgmt.visualstudio.com"
    }
}
GetURL "https://codetiger.visualstudio.com/"