Reputation: 1
I have a list of git repositories in TFS 2015 that I have accessed with the TFS Rest API v. 2.0 in Powershell, and I would like only to return the most recent release branch for each, however the response still returns the 'defaultBranch' branch property (an earlier release).
I have tried setting the value of the 'versionDescriptor.version' uri parameter to a regular expression that matches this branch. Our release branches take the generic pattern, "releases/x.x.xx," so my regex would need to find the highest digit for each 'x.'
$tfsUrl = 'http://tfs.infosys.com:8080/tfs/'
$collectionName = 'BCSA'
$collectionUrl = "$($tfsUrl)$($collectionName)"
$getProjectsUrl = "$($collectionUrl)/BHBs/_apis/git/repositories/?api-version=2.0"
$getItemsURL = "$($collectionUrl)/BHBs/_apis/git/repositories/"
[datetime]$dateTime = Get-Date -Format u
$refDate = $dateTime.AddYears(-2)
[regex]$branchName = "^releases/d[3].\d[0-9].\d[0-9]$"
$response = Invoke-RestMethod -UseDefaultCredentials -uri $getProjectsUrl
foreach ($repository in $response.value)
{
$itemsURI = "$($getItemsURL)$($repository.id)" + "/items?
recursionLevel=full&versionDescriptor.versionType=branch&versionDescriptor.version=$branchName&latestProcessedChange=true&includeContent=true&api-version=2.0"
$repoResponse = Invoke-RestMethod -UseDefaultCredentials -uri $itemsURI
$repoResponse = $repoResponse.value | Where-Object {$_.gitObjectType -eq
"blob" -and [datetime]$_.latestProcessedChange.author.date -gt $refDate} |
Select-Object -Property Path, latestProcessedChange
}
Upvotes: 0
Views: 362
Reputation: 3
[regex]$branchName = "^releases/d[3].\d[0-9].\d[0-9]$"
Seems your code is just pulling branch based on a specific name version name releases/x.x.xx
.
In my case I tried to handle this:
VssBasicCredential credintials = new VssBasicCredential(String.Empty, "YOUR SECRET CODE HERE");
VssConnection connection = new VssConnection(new Uri("https://yourserverurl.visualstudio.com/"), credintials);
GitHttpClient client = connection.GetClient<GitHttpClient>();
List<GitRepository> repositories = await client.GetRepositoriesAsync(true); // or use GetRepositoryAsync()
var repo = repositories.FirstOrDefault(r => r.Name == "Some.Repo.Name");
GitVersionDescriptor descriptor = new GitVersionDescriptor()
{
VersionType = GitVersionType.Branch,
Version = "develop",
VersionOptions = GitVersionOptions.None
};
List<GitItem> items = await client.GetItemsAsync(repo.Id, scopePath: "/", recursionLevel: VersionControlRecursionType.Full, versionDescriptor: descriptor);
Then if you will try directly to get contents of an item client.GetItemTextAsync(repo.Id, item.Path)
you will receive an error in case when the branch isn't default. Just provide GitVersionDescriptior again.
GitVersionDescriptor commitDescriptior = new GitVersionDescriptor()
{
VersionType = GitVersionType.Commit,
Version = item.CommitId,
VersionOptions = GitVersionOptions.None
};
Stream stream = await client.GetItemTextAsync(repo.Id, item.Path, download: true, versionDescriptor: commitDescriptior)
Upvotes: 0