1
votes

I am storing some artifacts from my build pipeline to Artifacts folder using the below statements:

Write-Host "##vso[artifact.upload containerfolder=testresult;artifactname=worksoftresult;]C:\JSONOutput\finalJSON.json"

Now I want to access this particular json file from an Azure DevOps extension that I want to create. Like the following code give me the current logged in user in the extension:

document.getElementById("name").innerText = VSS.getWebContext().user.name;

Is there a way to access the artifacts of the current build from VSS.SDK? If not, how to access the artifacts? I have tried the GetAtrifact rest API, but it tries to download the file:

https://dev.azure.com/XXXX/XXXXX/_apis/build/builds/1166/artifacts?artifactName=testResult&fileName=finalJSON.json&api-version=5.1&%24format=json

I dont want to download the json file but I want the json to show it in the extension. Please help.

2
Maybe you can download the file first, and then read it using code. But it may have some limitations from browsers, check #1, #2.LoLance

2 Answers

1
votes

To answer my question, since I was not able to show artifacts directly in an extension, I have alternatively (for the time being) uploaded it to Task.UploadSummary using the following piece of code:

Write-Host "##vso[task.uploadsummary]$(Build.ArtifactStagingDirectory)\output.md"

This is not totally serving my purpose but I am able to see an Extensions tab in build summary where the file which should have been in artifact is getting rendered: enter image description here

0
votes

I dont want to download the json file but I want the json to show it in the extension.

As I know we don't have such API to access the content of one file within artifact directly.

While the Client Libraries like Web Extensions SDK(VSS.SDk) and .Net Client Library support most Rest API , none of them supports accessing metadata of file directly. For now only the metadata of file in git repo can be accessed via API directly. Let's check the difference:

1.The API to get file in artifact:

Rest API: GET https://dev.azure.com/{organization}/{project}/_apis/build/builds/{buildId}/artifacts?artifactName={artifactName}&fileId={fileId}&fileName={fileName}&api-version=5.1 (The one you used)

C# client: BuildHttpClientBase.GetArtifactContentZipAsync Method (Use BuildHttpClient class)

JS Web(VSS.SDK): getArtifactContentZip()

2.The API to get file metadata in git repo:

Rest API: GET https://dev.azure.com/{organization}/{project}/_apis/git/repositories/{repositoryId}/items?path={path}&api-version=5.1 Supports getting metadata if includeContentMetadata is set to True.

C# client: GitHttpClientBase.GetItemAsync or GitHttpClientBase.GetItemContentAsync (They both supports getting Item Metadata and/or Content for a single item, haven't tested it to check which is better)

JS Web(VSS.SDK): getItem() and getItemContent() . (Get Item Metadata and/or Content for a single item.)

To sum up: What you actually want is to access the metadata so that you can access the content within file directly. But for now the Artifact-related api is not supported to do such job. So I'm afraid the answer is NO, what you want is not supported in current version of API.

As an alternative workaround, I suggest you can submit a feature request here in our User Voice forum to share your feedback. The product team would consider about your idea if it gets enough votes.