Geeks With Blogs

News




View Tarun Arora's profile on LinkedIn

profile for Tarun Arora at Stack Overflow, Q&A for professional and enthusiast programmers

Tarun Arora - Visual Studio ALM MVP ALM, Agile, Automation, Performance Testing, Software QA, Cloud, ...

 

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, good news is 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 no.
  •        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
   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
    // 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.
     // 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.
   // 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 in to 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 out and reading 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. In love

 

Share this post :
Posted on Saturday, December 24, 2011 8:25 PM TFS2010 , TFS API , TFS Utilties | Back to top


Comments on this post: TFS API–Release Notes From TFS Builds

# Mr
Requesting Gravatar...
Thanks for this posting. This is a great proof of concept for getting release notes automatically. I believe it is an important ingredient in achieving streamlined software deliveries.
Left by David Allen on Dec 27, 2011 9:11 PM

comments powered by Disqus

Copyright © Tarun Arora [Microsoft MVP] | Powered by: GeeksWithBlogs.net