问题
I need to display the result of a custom build task in summary tab (“ms.vss-build-web.build-results-section”). In order to do this I need to retain some data from build task and use it to call a web service from summary section. Is it possible to store data in a variable using Extension Data Service and use it in summary page? What should be the best approach for this?
Thanks in advance.
回答1:
I have attached my build task data using a Logging command
https://github.com/Microsoft/vsts-tasks/blob/986f8f5112017474962affe58c9ebaf394fb9354/docs/authoring/commands.md
//Build Task
class TestClass {
_name: string;
_age: number;
constructor(name: string, age:number) {
this._name = name;
this._age = age;
}
}
var data = new TestClass(TinTin,100);
//Create a folder
tl.mkdirP("c:/myfolder/");
//Write data to a file
tl.writeFile("c:/myfolder/mydata.txt",JSON.stringify(data));
//Executes command to attach the file to build
console.log("##vso[task.addattachment type=myAttachmentType;name=myAttachmentName;]c:/myfolder/mydata.txt");
Retrieve the attachment from summary page.
https://github.com/Microsoft/vsts-extension-samples/blob/master/build-results-enhancer/src/enhancer/tab.ts
//Summary Page
/// <reference path="../definitions/Q.d.ts" />
/// <reference path="../definitions/vss.d.ts" />
/// <reference path="../definitions/tfs.d.ts" />
/// <reference path="../definitions/jquery.d.ts" />
import VSS_Service = require("VSS/Service");
import Controls = require("VSS/Controls");
import TFS_Build_Contracts = require("TFS/Build/Contracts");
import TFS_Build_Extension_Contracts = require("TFS/Build/ExtensionContracts");
import DT_Client = require("TFS/DistributedTask/TaskRestClient");
export class StatusSection extends Controls.BaseControl {
constructor() {
super();
}
public initialize(): void {
super.initialize();
// Get configuration that's shared between extension and the extension host
var sharedConfig: TFS_Build_Extension_Contracts.IBuildResultsViewExtensionConfig = VSS.getConfiguration();
var vsoContext = VSS.getWebContext();
if(sharedConfig) {
// register your extension with host through callback
sharedConfig.onBuildChanged((build: TFS_Build_Contracts.Build) => {
var taskClient = DT_Client.getClient();
taskClient.getPlanAttachments(vsoContext.project.id, "build", build.orchestrationPlan.planId, "myAttachmentType").then((taskAttachments)=> {
if (taskAttachments.length === 1) {
var recId = taskAttachments[0].recordId;
var timelineId = taskAttachments[0].timelineId;
taskClient.getAttachmentContent(vsoContext.project.id, "build", build.orchestrationPlan.planId,timelineId,recId,"myAttachmentType","myAttachmentName").then((attachementContent)=> {
function arrayBufferToString(buffer){
var arr = new Uint8Array(buffer);
var str = String.fromCharCode.apply(String, arr);
if(/[\u0080-\uffff]/.test(str)){
throw new Error("this string seems to contain (still encoded) multibytes");
}
return str;
}
var summaryPageData = arrayBufferToString(attachementContent);
//Deserialize data
var ob = JSON.parse(summaryPageData);
console.log("Name: " + ob._name);
console.log("Age: " + ob._age);
});
}
});
});
}
}
}
StatusSection.enhance(StatusSection, $(".build-status"), {});
// Notify the parent frame that the host has been loaded
VSS.notifyLoadSucceeded();
回答2:
You can do it but the issue is that the those values are always the values from the latest build, the information in summary page would be incorrect for old builds. So I would recommend to the get the build task result via BuildHttpClient2_2 and then show it in the summary page directly.
来源:https://stackoverflow.com/questions/39031367/vsts-extension-storing-parameters-from-build-task-and-call-a-web-service-from