Search for a repository in our Azure DevOps organization - azure-devops

In our azure devops organization, we have ~100 projects. Each projects holds between 1 and 20 git repositories. This makes it hard to find a specific repository unless you also remember which project it is located in.
The search box at the project list only searches by project names. Is there any way to search and find repository names?
There is a code search, but that returns results from files already in a repository, which gives both redundant and wrong results.

I think the simplest is to call this REST API.
You can call it even from the browser and use the Find feature.
Note that the project element is not required.
To automate, you can use a few lines of Powershell to search in the result JSON document, e.g.
$organization = ...
$PAT = ...a personal access token
$repoNameStart = ...initial name of repo
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes("dummy:${PAT}"))
$result = Invoke-RestMethod -Method Get -Uri "https://dev.azure.com/${organization}/_apis/git/repositories?api-version=4.1" -Headers #{Authorization = "Basic $base64AuthInfo" } -Credential $credential -ContentType "application/json"
$result.value | where {$_.name -like "${repoNameStart}*"}
(Untested code, use it as an example)

Related

Azure cli: clone pipeline

Looking at az pipelines documentation it seems it's not possible to clone a pipeline using cli.
I've looked at getting the yaml (az pipelines show -name=x > x_orig.yaml) and then trying to change json and create pipeline from modified yaml, but that feels like a lot of work that could break after next update.
Is there a way to clone a pipline without going the the Web UI?
Currently, there indeed is not available Azure CLI that can clone or export/import a pipeline to create a new pipeline.
I also searched and tried the Azure DevOps REST API for Pipelines, but did not find the available API.
Ideally, the Azure CLI "az pipelines create" can provide an input parameter that allows users specify an existing pipeline as a starting point for the new pipeline.
If your projects really need this feature, I recommend that you can directly report a feature request on the "Azure/azure-cli" repository to ask adding the parameter like as above mentioned. That will allow you directly interact with the appropriate engineering team, and make it more convenient for the engineering team to collect and categorize your suggestions.
As a workaround, we could clone the build definition via power shell script to call REST API.
Note: We need to change the original build definition name.
REST API
Get build definition:
GET https://dev.azure.com/{organization}/{project}/_apis/build/definitions/{definitionId}?api-version=6.0
Create build definition
POST https://dev.azure.com/{organization}/{project}/_apis/build/definitions?api-version=6.0
Power shell script
$connectionToken="{pat}"
$base64AuthInfo= [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($connectionToken)"))
$BuildDefinitionInfoURL = "https://dev.azure.com/{org name}/{project name}/_apis/build/definitions/386"
$BuildDefinitionInfo = Invoke-RestMethod -Uri $BuildDefinitionInfoURL -Headers #{authorization = "Basic $base64AuthInfo"} -Method Get
Write-Host $BuildDefinitionInfo.name
$BuildDefinitionInfo.name = $BuildDefinitionInfo.name +" clone"
Write-Host $BuildDefinitionInfo.name
$body = $BuildDefinitionInfo | ConvertTo-Json -Depth 99
$createBuildDefinitionURL = "https://dev.azure.com/{org name}/{project name}/_apis/build/definitions?api-version=6.0"
$response = Invoke-RestMethod -Uri $createBuildDefinitionURL -ContentType "application/json" -Body $body -Headers #{Authorization=("Basic {0}" -f $base64AuthInfo)} -Method POST
Write-Host $response.id
Result:

How to retrieve list of projects within an organisation by running a query in Azure DevOps?

I want all the projects within a collection (organization) listed by running a query.
I searched so many resources but I hadn't got any relevant results. anybody
You can get that list if you visit Organization Page. And you cannot get that list using queries, you should REST APIs.
There is an extension in Azure DevOps Marketplace which lists projects and properties like process template etc. you can take a look at it here
You can't do ith with query.
You can do it with Rest API Projects - List, for example in PowerShell:
$pat = "YOUR-PERSONAL-ACCESS-TOKEN"
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user,"$pat")))
$headers = #{Authorization=("Basic {0}" -f $base64AuthInfo)}
$azureDevOpsUrl = "https://dev.azure.com/{your-organization-name}"
$projects = Invoke-RestMethod -Uri "$azureDevOpsUrl/_apis/projects" -Method Get -Headers $headers -ContentType application/json

Connecting Build pipeline in AzureDevOps to newly created repos with certain prefix Automatically

I have a created a build pipeline in Azure DevOps and it is connected to one repo that I have in my Github. However, I want to connect/clone this build pipeline into any newly created repo in my github with a certain prefix in its name, like the word 'Build'.
You can navigate to your Build Pipeline, select the Option menu from the right hand side on the pipeline details page, and choose the Clone item.
You can then point the cloned build pipeline to your new Git repository and change the pipeline's name to have the prefix you wish.
Connecting Build pipeline in AzureDevOps to newly created repos with certain prefix Automatically
To automate the process, you need use the definition REST AP get the json body:
https://learn.microsoft.com/en-us/rest/api/azure/devops/build/definitions/get?view=azure-devops-rest-5.0
Then we could change the json file whatever we feel necessary, like Repository URL, change it to the new path for your newly created repo in my github.
At last, we could use create definition REST API with above Json file to create new pipeline:
https://learn.microsoft.com/en-us/rest/api/azure/devops/build/definitions/create?view=azure-devops-rest-5.0
$thisBuildDef.Name = $Clone_Name
$thisBuildDef.path = $BuildDefURL # Relative to the Project name; like "Release/2019"
$thisBuildDef.buildNumberFormat = $BuildNumberFormat
# Update source control path to new branch
$defAsJson = $thisBuildDef | ConvertTo-Json -Depth 100
$defAsJson = $defAsJson.Replace($sourceControlMainline, $sourceControlBranch)
$Uri = "https://dev.azure.com/{organization}/{project}/_apis/build/definitions?api-version=2.0"
$newBuildDef = Invoke-RestMethod -Uri $Uri -Headers #{Authorization=("Basic {0}" -f $base64AuthInfo)} -Method Post -Body $defAsJson -ContentType "application/json" -ErrorAction Stop
Check the document Using JSON via REST to create build definitions in VSO and the vsts-clone-build-with-powershell.ps1 sample.
Hope this helps.

export complete TFS work item incl. history with PowerShell

I am working for a customer who is running a Dynamics AX project and is using TFS. I do have access to TFS and am able to check all the different work items - however, what I am missing is an overview to build metrics as the only way to get data into a table (Excel) does not allow me to get the history of a work item.
I am hence wondering how I could do this using PowerShell. I am completely new to this, accordingly step-by-step guidance would be highly appreciated.
You can use TFS Rest API.
For example:
$serverUrl = "http://tfsServer:8080/tfs/Collection"
$workItemId = "1"
#Get the Work Item
$workItem = Invoke-RestMethod -Uri "$($serverUrl)/_apis/wit/workitems/$($workItemId)?api-version=3.0" -UseDefaultCredentials -Method Get
#Print the revisions number
Write-Host $workItem.rev
#Get the specific revision details
$revision = Invoke-RestMethod -Uri "$($serverUrl)/_apis/wit/workitems/$($workItemId)/revisions/2?api-version=3.0" -UseDefaultCredentials -Method Get
#Print the Work Item details in the specific revision
Write-Host $revision.fields

VSTS Personal Access Token (PAT) vs OAUTH: different results of query

I am using a PowerShell task to among others query a TFVC repository for changes after a certain point of time. To develop locally I created a PAT related to my user. In the release definition I enabled Allow scripts to access OAUTH token. If I now execute the release definition with the PAT respectively the OAUTH I get different results for existing changes (e.g. count is 1 for PAT and 0 for OAUTH). The queries are exactly the same:
https://xxx.visualstudio.com/xxx/_apis/tfvc/changesets?searchCriteria.itemPath=$projectPath&searchCriteria.fromDate=$cloudVersionTimestampUTC&api-version=4.1
Here the code to execute the query:
Write-Host "Get Request with the URI '$uri'"
if ($localDevelopment) {
$GetResponse = Invoke-RestMethod `
-Uri $uri `
-Headers #{Authorization = 'Basic ' + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$($env:SYSTEM_ACCESSTOKEN)")) }
Write-Host "Requested last changes for local development: $($GetResponse | ConvertTo-Json -Depth 100)"
}
else {
$GetResponse = Invoke-RestMethod `
-Uri $uri `
-Headers #{Authorization = "Bearer $env:SYSTEM_ACCESSTOKEN" }
Write-Host "Requested last changes: $($GetResponse | ConvertTo-Json -Depth 100)"
}
Does someone know why this is the case?
Thanks
Edits:
here the permissions of the Project Collection Build Service (xxx) account:
It is also important to mention that we don't use hosted agents but custom ones.
The problem is the different handling of timestamps when comparing PAT and OAUTH. As you can see I use the variable $cloudVersionTimestampUTC for fromDate. When I set the timestamp I did not add Z (like MM-dd-yyyy HH:mm:ss instead of MM-dd-yyyy HH:mm:ssZ). PAT uses the given timestamp as UTC, OAUTH interprets it as local time (but I am not sure of what - e.g. the default profile) when Z is not provided. This of course led to a different number of changesets (if the missing changeset was in the difference between UTC and local time).
In the end I had to be explicit about the timestamps. So what I did was to get the timestamp as string that was stored as tag in the Azure Resource and do the following:
$cloudVersionTimestampUTC = [DateTime]($cloudJobDefinition.Tags.'version-timestamp')
$cloudVersionTimestampUTC = (Get-Date -Date $cloudVersionTimestampUTC).ToString("MM-dd-yyyy HH:mm:ssZ")
Then I was able to make the REST Call and retrieve the same number of changesets.