简体   繁体   中英

Get name of "Team project" from changeset object?

I've written a service that grabs all changesets in all collections on my TFS.

I can dynamically get the collection name of any changeset from the changeset object but I'm having trouble trying to find the team project name that the changeset exists in.

Can I somehow find the team project name a changeset belongs to?

    foreach (Changeset cs in allChangeSets)
    {
           if (cs.Comment != null && ChangeSetFinderMVC.Utils.TFSUtil.Contains(cs.Comment, id))
           {
                  var cso = new ChangeSetObj();
                  cso.ChangesetId = cs.ChangesetId;
                  cso.CheckinNote = cs.CheckinNote;
                  cso.Comment = cs.Comment;
                  cso.Committer = cs.Committer;
                  cso.CommitterDisplayName = cs.CommitterDisplayName;
                  cso.Collection = cs.VersionControlServer.TeamProjectCollection.Name;
                  cso.TeamProject = "????";

                  changeSetList.Add(cso);

           }
    }

As @Toomaja said, we can have many TeamProjects related to a single changeset. His solution sugested that you always know what TeamProjects you are looking for. Even you know the TeamProjects beforehand, using a regex to do something that is already done by available methods would low your code lifespan and hold your solution to specific TeamProjects. I would sugest you to get the unique TeamProjects using the server path of the changes using the following:

private IEnumerable<string> _GetUniqueTeamProjects(Changeset changeset)
    {
        //We can have many changes in files in differentes TeamProjects within the same changeset
        HashSet<string> teamProjects = new HashSet<string>();

        foreach (var ch in changeset.Changes)
        {
            //Get TeamProject using available repository access and change paths on server side.
            var tp = ch.Item.VersionControlServer.GetTeamProjectForServerPath(ch.Item.ServerItem);
            //Let the HashTable handle the unique value
            teamProjects.Add(tp.Name);
        }

        return teamProjects;
    }

Since a changeset may apply to multiple projects at once, you will need to look at each item that changed in the changeset and see what project it was associated with.

Inside the Changeset , you can access the TFS path of each changed item and see its server path.

foreach (Changeset changeSet in changeSets)
{
    foreach (Change change in changeSet.Changes)
    {
        string tfsDir = change.Item.ServerItem;
        // Example: "$/ProjectName/SomeFolders/SomeFile.cs"
        // More logic to handle this string goes here.
    }
}

If you need some regex to find the project name, try this: ^\\$\\/(?<Project>.+?)\\/

Make sure you access the 'Project' group in the regex match.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM