2013-04-17 10 views
17

Sto cercando di trovare un modo per trovare dettagli su un oggetto di richiesta/risposta di revisione codice in TFS2012.Utilizzando l'API TFS, come posso trovare i commenti che sono stati fatti su una revisione del codice?

posso interrogare per tutte le voci di richiesta/risposta Code Review nel modo seguente:

const string TfsUri = "http://mytfsserver:8080/tfs/Default ProjectCollection"; 

var tfs = new TfsTeamProjectCollection(new Uri(TfsUri)); 
var store = tfs.GetService<WorkItemStore>(); 

var versionStore = tfs.GetService<Microsoft.TeamFoundation.VersionControl.Client.VersionControlServer>(); 

var queryText = "SELECT [System.Id], 
       FROM WorkItems 
       WHERE [System.WorkItemType] = 'Code Review Request' 
       or [System.WorkItemType] = 'Code Review Response'"; 
var query = new Query(store, queryText); 

var result = query.RunQuery().OfType<WorkItem>(); 

Questo mi dà un elenco di WorkItem tipi. Quando eseguo il looping sulla proprietà result.FirstOrDefault().Fields, mi vengono fornite alcune informazioni utili sullo ShelveSet che è correlato alla revisione del codice, il "contesto associato". Utilizzando queste informazioni, posso interrogare per la shelveset:

var versionStore = tfs.GetService<VersionControlServer>(); 
var shelveset = versionStore.QueryShelvesets("someCodeReviewId_xxxx","someUserName"); 

questo mi dà una voce ShelveSet, ma è lì che mi si blocca.

Ho esaminato lo spazio dei nomi Microsoft.TeamFoundation.CodeReview fornito da entrambe le librerie Microsoft.TeamFoundation.CodeReview.Components e Microsoft.TeamFoundation.CodeReview.Controls, ma anche questo non mi aiuta ulteriormente.

La mia domanda è: come posso trovare i commenti effettivi fatti su uno ShelveSet durante una revisione del codice (sia commenti generali che commenti sui file) tramite l'API TFS?

risposta

14

Abbiamo un nuovo requisito per inserire commenti di revisione del codice da TFS ed ecco un breve esempio di ciò che abbiamo implementato. WorkItemId deve essere interrogato tramite un altro metodo. È anche possibile cercarlo in Visual Studio o tramite una query TFS nell'interfaccia utente. Questo è un piccolo sottoinsieme di ciò che è disponibile e ciò che stiamo usando. Ho trovato this link to be helpful after digging through MSDN.

public List<CodeReviewComment> GetCodeReviewComments(int workItemId) 
{ 
     List<CodeReviewComment> comments = new List<CodeReviewComment>(); 

     Uri uri = new Uri(URL_TO_TFS_COLLECTION); 
     TeamFoundationDiscussionService service = new TeamFoundationDiscussionService(); 
     service.Initialize(new Microsoft.TeamFoundation.Client.TfsTeamProjectCollection(uri)); 
     IDiscussionManager discussionManager = service.CreateDiscussionManager(); 

     IAsyncResult result = discussionManager.BeginQueryByCodeReviewRequest(workItemId, QueryStoreOptions.ServerAndLocal, new AsyncCallback(CallCompletedCallback), null); 
     var output = discussionManager.EndQueryByCodeReviewRequest(result); 

     foreach (DiscussionThread thread in output) 
     { 
      if (thread.RootComment != null) 
      { 
       CodeReviewComment comment = new CodeReviewComment(); 
       comment.Author = thread.RootComment.Author.DisplayName; 
       comment.Comment = thread.RootComment.Content; 
       comment.PublishDate = thread.RootComment.PublishedDate.ToShortDateString(); 
       comment.ItemName = thread.ItemPath; 
       comments.Add(comment); 
      } 
     } 

     return comments; 
    } 

    static void CallCompletedCallback(IAsyncResult result) 
    { 
     // Handle error conditions here 
    } 

    public class CodeReviewComment 
    { 
     public string Author { get; set; } 
     public string Comment { get; set; } 
     public string PublishDate { get; set; } 
     public string ItemName { get; set; } 
    } 
Problemi correlati