title: Dynamic Workspace.Merge TFS changesets
body: <p>I'm writing a TFS plugin to automate merging of changesets related to a work item whenever said work item is changed from state "Resolved" to state "Closed". The following code is what I have so far:</p>
  
  <p><strong>C#</strong></p>
  
  <pre><code>private void Action_ResolvedToClosed()
  {
      //Linq query for getting changesets associated with the current work item
      var changeSets = WorkItem.Links
          .OfType&lt;ExternalLink&gt;()
          .Select(link =&gt;
              VersionControlServer.ArtifactProvider.GetChangeset(new Uri(link.LinkedArtifactUri))).ToList();
  
      if (!changeSets.Any())
      {
          LOG_NOCHANGESETS(WorkItem.Id);
          return;
      }
  
      Workspace workspace = VersionControlServer.GetWorkspace(&lt;My Workspace&gt;);
  
      var source = URI_LOCAL; // $/&lt;Project Name&gt;/&lt;Working Branch&gt;
      var destination = URI_DEV; // $/&lt;Project Name&gt;/&lt;Development Branch&gt;
  
      // Merge applicable changesets
      foreach (var versionSpec in changeSets.Select(changeset =&gt; new ChangesetVersionSpec(changeset.ChangesetId)))
      {
          workspace.Merge(source, destination, versionSpec, versionSpec);
          workspace.CheckIn(workspace.GetPendingChanges(), "**Automated Merge**");
          LOG_SUCCESS(versionSpec.ChangesetId, WorkItem.Id);
      }
  }
  </code></pre>
  
  <p>Is there a way to dynamically generate the <code>workspace</code> variable? Odds are I won't be the one actually making changes - the goal is to automate this process for our devs.</p>
  
  <p><strong>UPDATE:</strong> I'm pretty sure what I'm looking for in this second part is <a href="http://msdn.microsoft.com/en-us/library/microsoft.teamfoundation.versioncontrol.client.getstatus%28v=vs.110%29.aspx" rel="nofollow noreferrer">GetStatus</a>, so it can be ignored. The paragraph above is my real question.</p>
  
  <p><em>Secondary:</em> I feel like automating merges can't be <a href="https://stackoverflow.com/questions/12092854/how-to-merge-tfs-change-sets-programmatically/12110848#12110848">this simple</a>. What happens if merge conflicts arise? Does <code>Workspace.Merge</code> fail gracefully? Are there any other glaring issues that someone with a bit more experience with the TFS API can point out?</p>