在 TFS 的 ISubscriber 插件中读取进程模板版本和 TypeID
Read Process Template Version and TypeID in an ISubscriber plugin for TFS
我正在设置一个 TFS ISubscriber 插件,我希望能够根据安装的流程模板名称 (DONE)、TypeID 和版本来决定是否触发。
读取名称的代码比较简单:
var ics = context.GetService<ICommonStructureService>();
string ProjectName = string.Empty;
string ProjectState = String.Empty;
int templateId = 0;
CommonStructureProjectProperty[] ProjectProperties = null;
ics.GetProjectProperties(context, projectUri.ToString(), out ProjectName, out ProjectState, out ProjectProperties);
// The Projectproperties contains a property called "Process Template", holding the name.
但我找不到读取其他属性的方法...我使用 Reflector 查看 TFS 程序集时截取了这段代码,但它总是 returns 未知:
private ArtifactSpec GetProcessTemplateVersionSpec(string projectUri)
{
var commonService = this.context.GetService<CommonStructureService>();
Guid guid = commonService.GetProject(this.context, projectUri).ToProjectReference().Id;
return new ArtifactSpec(ArtifactKinds.ProcessTemplate, guid.ToByteArray(), 0);
}
public ProcessTemplateVersion GetCurrentProjectProcessVersion(Uri projectUri)
{
return this.GetProjectProcessVersion(projectUri.AbsoluteUri, ProcessTemplateVersionPropertyNames.CurrentVersion);
}
public ProcessTemplateVersion GetCreationProjectProcessVersion(Uri projectUri)
{
return this.GetProjectProcessVersion(projectUri.AbsoluteUri, ProcessTemplateVersionPropertyNames.CreationVersion);
}
private ProcessTemplateVersion GetProjectProcessVersion(string projectUri, string versionPropertyName)
{
ArtifactSpec processTemplateVersionSpec = GetProcessTemplateVersionSpec(projectUri);
ProcessTemplateVersion unknown = ProcessTemplateVersion.Unknown;
using (TeamFoundationDataReader reader = context.GetService<TeamFoundationPropertyService>().GetProperties(context, processTemplateVersionSpec, new string[] { versionPropertyName }))
{
foreach (ArtifactPropertyValue value2 in reader)
{
foreach (PropertyValue value3 in value2.PropertyValues)
{
return TeamFoundationSerializationUtility.Deserialize<ProcessTemplateVersion>(value3.Value as string);
}
return unknown;
}
return unknown;
}
}
更糟糕的是,我还希望能够从客户端对象模型执行此操作,但这似乎更难。
在咨询了 Microsoft 产品团队后,我很遗憾地报告说,此 属性 目前仅受 Visual Studio Online 支持,并且甚至没有为本地实例存储这些值。
Rene van Osnabrugge 采取了一种解决方法来复制这些值 and place them in the "standard" project properties。
将以下内容放入每个流程模板的 Classification.xml 中:
<properties>
<property name="MSPROJ" value="Classification\FieldMapping.xml" isFile="true" />
<property name="Process Template" value="Microsoft Visual Studio Scrum 2.2" />
<property name="Create Version" value="Custom Text is allowed here" />
<property name="Current Version" value="Custom Text is allowed here" />
</properties>
然后使用此代码阅读:
string uri = @"http://myServer:8080/tfs/defaultcollection";
string teamProjectName = "myTeamProject";
// Get the team project
var tpc = new TfsTeamProjectCollection(new Uri(uri));
tpc.Authenticate();
var ics = tpc.GetService<ICommonStructureService>();
var teamProject = ics.GetProjectFromName(teamProjectName);
// Read the properties
string projectName = string.Empty;
string projectState = string.Empty;
int templateId = 0;
ProjectProperty[] projectProperties = null;
ics.GetProjectProperties(teamProject.Uri, out projectName, out projectState, out templateId, out projectProperties);
// Return the properties
string processtemplate = projectProperties.Where(p => (p.Name == "Process Template")).Select(p => p.Value).FirstOrDefault();
string currentVersion = projectProperties.Where(p => (p.Name == "Current version")).Select(p => p.Value).FirstOrDefault();
string createVersion = projectProperties.Where(p => (p.Name == "Create version")).Select(p => p.Value).FirstOrDefault();
//Update the properties
projectProperties.Where(p => (p.Name == "Current version")).First().Value = "MS Scrum 2.2 - Custom 2.3";
ics.UpdateProjectProperties(teamProject.Uri, projectState, projectProperties);
我正在设置一个 TFS ISubscriber 插件,我希望能够根据安装的流程模板名称 (DONE)、TypeID 和版本来决定是否触发。
读取名称的代码比较简单:
var ics = context.GetService<ICommonStructureService>();
string ProjectName = string.Empty;
string ProjectState = String.Empty;
int templateId = 0;
CommonStructureProjectProperty[] ProjectProperties = null;
ics.GetProjectProperties(context, projectUri.ToString(), out ProjectName, out ProjectState, out ProjectProperties);
// The Projectproperties contains a property called "Process Template", holding the name.
但我找不到读取其他属性的方法...我使用 Reflector 查看 TFS 程序集时截取了这段代码,但它总是 returns 未知:
private ArtifactSpec GetProcessTemplateVersionSpec(string projectUri)
{
var commonService = this.context.GetService<CommonStructureService>();
Guid guid = commonService.GetProject(this.context, projectUri).ToProjectReference().Id;
return new ArtifactSpec(ArtifactKinds.ProcessTemplate, guid.ToByteArray(), 0);
}
public ProcessTemplateVersion GetCurrentProjectProcessVersion(Uri projectUri)
{
return this.GetProjectProcessVersion(projectUri.AbsoluteUri, ProcessTemplateVersionPropertyNames.CurrentVersion);
}
public ProcessTemplateVersion GetCreationProjectProcessVersion(Uri projectUri)
{
return this.GetProjectProcessVersion(projectUri.AbsoluteUri, ProcessTemplateVersionPropertyNames.CreationVersion);
}
private ProcessTemplateVersion GetProjectProcessVersion(string projectUri, string versionPropertyName)
{
ArtifactSpec processTemplateVersionSpec = GetProcessTemplateVersionSpec(projectUri);
ProcessTemplateVersion unknown = ProcessTemplateVersion.Unknown;
using (TeamFoundationDataReader reader = context.GetService<TeamFoundationPropertyService>().GetProperties(context, processTemplateVersionSpec, new string[] { versionPropertyName }))
{
foreach (ArtifactPropertyValue value2 in reader)
{
foreach (PropertyValue value3 in value2.PropertyValues)
{
return TeamFoundationSerializationUtility.Deserialize<ProcessTemplateVersion>(value3.Value as string);
}
return unknown;
}
return unknown;
}
}
更糟糕的是,我还希望能够从客户端对象模型执行此操作,但这似乎更难。
在咨询了 Microsoft 产品团队后,我很遗憾地报告说,此 属性 目前仅受 Visual Studio Online 支持,并且甚至没有为本地实例存储这些值。
Rene van Osnabrugge 采取了一种解决方法来复制这些值 and place them in the "standard" project properties。
将以下内容放入每个流程模板的 Classification.xml 中:
<properties>
<property name="MSPROJ" value="Classification\FieldMapping.xml" isFile="true" />
<property name="Process Template" value="Microsoft Visual Studio Scrum 2.2" />
<property name="Create Version" value="Custom Text is allowed here" />
<property name="Current Version" value="Custom Text is allowed here" />
</properties>
然后使用此代码阅读:
string uri = @"http://myServer:8080/tfs/defaultcollection";
string teamProjectName = "myTeamProject";
// Get the team project
var tpc = new TfsTeamProjectCollection(new Uri(uri));
tpc.Authenticate();
var ics = tpc.GetService<ICommonStructureService>();
var teamProject = ics.GetProjectFromName(teamProjectName);
// Read the properties
string projectName = string.Empty;
string projectState = string.Empty;
int templateId = 0;
ProjectProperty[] projectProperties = null;
ics.GetProjectProperties(teamProject.Uri, out projectName, out projectState, out templateId, out projectProperties);
// Return the properties
string processtemplate = projectProperties.Where(p => (p.Name == "Process Template")).Select(p => p.Value).FirstOrDefault();
string currentVersion = projectProperties.Where(p => (p.Name == "Current version")).Select(p => p.Value).FirstOrDefault();
string createVersion = projectProperties.Where(p => (p.Name == "Create version")).Select(p => p.Value).FirstOrDefault();
//Update the properties
projectProperties.Where(p => (p.Name == "Current version")).First().Value = "MS Scrum 2.2 - Custom 2.3";
ics.UpdateProjectProperties(teamProject.Uri, projectState, projectProperties);