【问题标题】:VSTS Extension - Storing parameters from build task and call a web service from summary tabVSTS 扩展 - 从构建任务存储参数并从摘要选项卡调用 Web 服务
【发布时间】:2016-08-19 04:54:48
【问题描述】:

我需要在摘要选项卡(“ms.vss-build-web.build-results-section”)中显示自定义构建任务的结果。为了做到这一点,我需要从构建任务中保留一些数据,并使用它从摘要部分调用 Web 服务。是否可以使用扩展数据服务将数据存储在变量中并在摘要页面中使用?最好的方法应该是什么?

提前致谢。

【问题讨论】:

  • 构建完成后能否调用 Web 服务?基于此示例github.com/Microsoft/vsts-extension-samples/tree/master/…,它会在构建完成时更新图像。
  • 我需要将一些数据传递到摘要页面,我打算为此使用 VSS 扩展数据服务。 1.) 将构建任务的结果保存为扩展数据服务中的设置。 2.) 在摘要页面加载时从扩展数据服务中读取设置并将这些值绑定到 html。我认为这是正确的方法。谢谢-

标签: tfs azure-devops tfsbuild


【解决方案1】:

我已使用 Logging 命令附加了我的构建任务数据

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");

从摘要页面检索附件。

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();

【讨论】:

  • 老兄!我不敢相信 MS 没有在他们的教程中写“getAttachmentContent”,我已经工作了 2 天来解决如何获取这些数据。谢谢!!!
【解决方案2】:

您可以这样做,但问题是这些值始终是最新版本的值,摘要页面中的信息对于旧版本是不正确的。所以我建议通过BuildHttpClient2_2获取构建任务结果,然后直接显示在摘要页面中。

【讨论】:

  • 嗨 Eddie,我的扩展程序检索构建工件并将其上传到 Web 服务。 Web 服务接受这些工件并返回一些我需要在摘要部分显示的信息(xml 格式的摘要)。(我假设需要存储 xml 数据并加载到摘要 html 页面中。)但我不是确定如何做到这一点。正如您所解释的,用户可以查看以前的构建,他应该看到相应的结果摘要。我分析了来自 Microsoft 的样本,但他们没有针对我的问题场景的类似样本。您能否指出任何可以帮助我解决此问题的示例或文档?
  • @Edddie, Yes Eddie 我考虑过这个选项,问题是如果我使用扩展数据服务,我必须手动管理在数据存储中创建的那些文档。我正在寻找最简单的方法来避免使用 ext 数据服务 :)。我注意到 TFS_Build_Contracts.Build 对象包含构建相关信息。有没有办法将我的任务输出(我提到的 xml)作为构建合同中某处的对象保存,以便我可以从摘要页面中检索它?还是其他更好的选择?
  • @BandR 不,没有任何方法可以更改更新 TFS_Build_Contracts.Build 对象。我不确定 Web 服务返回什么信息。但如果可能的话,我建议您在构建过程中生成这些信息并将它们存储在构建日志中。然后你很容易在扩展中得到它们。
  • 感谢 Eddie,看来我可以检索带有 url 的 BuildLogReference 对象来记录资源。您能否指出任何开发人员示例代码如何做到这一点?
猜你喜欢
  • 2023-03-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-03
  • 1970-01-01
  • 2014-09-05
相关资源
最近更新 更多