Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / DevOps / TFS

TFS API–Release Notes From TFS Builds

5.00/5 (2 votes)
26 Dec 2011CPOL3 min read 26.3K  
TFS API-Release notes from TFS Builds

Agile taught us to release early and release often. Agile also taught us to get the end user involved more frequently and directly. Goes without saying that release notes are an important part of any release. But what you don’t want to be doing is manually putting together the release notes with each frequent release you do. TFS lets you manage the application lifecycle with great ease & minimal administrative overhead, if your team is already checking in the code against work items and if you are using the TFS build infrastructure, the good news is that you already have all the raw material for your release notes stored in the TFS repository. In this blog post, I’ll show you how to use the TFS API to generate the release notes from the Team builds.

image

So… How Does It Work?

  • Click connect to get all Build Definitions for a Team Project
  • Filter by excluding Disabled Build Definitions, or select the number of builds to return per build definition, or exclude failed, partially successful, stopped and in progress builds.
  • Click ‘Generate Release Notes’ to generate the release notes, you can also export the results to PDF.

image

Let’s get started, follow the steps below or download a working solution & an executable from here.

1. What You Need…

To be able to extract the work item details from the build, you need to ensure:

  • Developers check in code against work items – Download and Install TFS Power tools and enable check in policy to enforce check ins against work items.
  • Enable the association of work items with change sets – If you are using the default template that ships with TFS 2010, edit the build definition and under the Process tab in the Advanced settings, you will notice the option ‘Associate Change sets and Work Items’, set this to true.

image

2. TFS API – Programmatically get Work Items, Change Sets, etc. from Team Builds

  1. Let’s start by connecting to TFS programmatically using the API:
    C#
       private TfsTeamProjectCollection _tfs;
       private string _selectedTeamProject;
       private IBuildServer _bs;
       private WorkItemStore _wis;
    
       // Connect to TFS and pick team project
       private void ConnectToTfsAndPickAProject()
       {
           TeamProjectPicker tfsPP = new TeamProjectPicker
                                     (TeamProjectPickerMode.SingleProject, false);
           tfsPP.ShowDialog();
           this._tfs = tfsPP.SelectedTeamProjectCollection;
           this._selectedTeamProject = tfsPP.SelectedProjects[0].Name;
       }
  2. Get all Build Definitions for the selected Team Project programmatically:
    C#
    // Get All Build Definitions for the selected Team Project
    private IBuildDefinition[] GetAllBuildDefinitionsFromTheTeamProject()
    {
        _bs = _tfs.GetService<IBuildServer>();
        return _bs.QueryBuildDefinitions(_selectedTeamProject);
    }
    
  3. Get All Builds in the build definition for specific build Definition specifications programmatically. This helps you narrow down the search and get the selected few filtered results, like in the snippet below, we specify the maximum number of builds to return, what build quality builds to return, etc.
    C#
    // Get All Builds for the selected build definition
    private void cmbBuilds_SelectedIndexChanged(object sender, EventArgs e)
    {
        var def = (((ComboBox)sender).SelectedItem) as IBuildDefinition;
    
        var def = _bs.CreateBuildDetailSpec(_selectedTeamProject);
        int noRequeted; // set to 1 in case no value passed
        def.MaxBuildsPerDefinition =
           (int.TryParse(txtNoOfBuilds.Text.ToString(), out noRequeted)) ? noRequeted : 1;
        def.QueryOrder = BuildQueryOrder.FinishTimeDescending;
        def.DefinitionSpec.Name = def.Name;
        def.Status = chkOnlySuccessful.Checked ? BuildStatus.Succeeded : BuildStatus.All;
        var builds = _bs.QueryBuilds(def).Builds;
    
        lstBuilds.Items.Clear();
        foreach (var build in builds)
        {
           lstBuilds.Items.Add(build);
           lstBuilds.DisplayMember = "BuildNumber";
        }
        lstBuilds.SelectionMode = SelectionMode.MultiExtended;
    }
    
  4. Get Associated Work Items for the selected build. You can also get the Associated Change sets, Build errors, Build Warnings, Work flow details, Changeset Ids, Configuration summaries, Opened work item ids, etc., check for more details for InformationNodeConverters. Notice in the snippet below that I am excluding a work item if it has already been added to the release notes as part of the previous selected build.
    C#
       // Data I want to display on the report
       public class ReleaseNote
       {
            public string BuildNumber { get; set; }
            public int WorkItemId { get; set; }
            public string WorkItemTitle { get; set; }
            public string QcRef { get; set; }
       }
    
        // Get the work item details off the selected Builds
        private void btnReleaseNotes_Click(object sender, EventArgs e)
        {
                var workItemIds = new List<int>();
                _wis = _tfs.GetService<WorkItemStore>();
                var workItems = new List<WorkItem>();
                var releaseNotes = new List<ReleaseNote>();
    
                foreach (IBuildDetail build in ((ListBox)lstBuilds).SelectedItems)
                {
                    List<IWorkItemSummary> wis = 
                              InformationNodeConverters.GetAssociatedWorkItems(build);
    
                    foreach (var wi in wis)
                    {
                        var w = _wis.GetWorkItem(wi.WorkItemId);
    
                        if (w.Type != null && w.Type.Name == "Defect")
                        {
                            if (!releaseNotes.Select(r => r.WorkItemId).Contains(w.Id))
                                releaseNotes.Add(new ReleaseNote()
                                {
                                    BuildNumber = build.BuildNumber,
                                    WorkItemId = w.Id,
                                    WorkItemTitle = w.Title,
                                    QcRef = w["QC Reference"].ToString()
                                });
                        }
                    }
                }
                ReleaseNoteBindingSource.DataSource = releaseNotes;
                this.reportViewer1.RefreshReport();
        }

Next… create a Report using the Report Wizard and associate it to the Release Note Binding Source. Download a working solution and executable from here.

3. Review & Next Steps…

You can use the same logic with in a custom activity to churn out the release note details in the format you want in the build output, or use the code in a web application, host it in your network and easily let the team query the release notes as and when they need to, or host this in the cloud so the vendors outside the network can easily access the release notes as well, you could also convert this into a VS extension, right click the build definition and you have the option to generate the release notes. You can also extend the logic further and include more functionality. Happy to hear your feedback! If you have any recommendations or questions, feel free to leave a comment. Thank you for taking the time to read this blog post. If you enjoyed the post, remember to subscribe to http://feeds.feedburner.com/TarunArora.

Check out the other posts on cool tools using TFS API

Image 4

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)