Programmatically Enable/Disable "Disable Repository" flag at each repository

Rahul Shah 0 Reputation points
2023-06-23T09:02:58.4566667+00:00

Is it possible to programmatically Enable/Disable "Disable Repository" flag at each repository using Azure CLI commands / .Net Core / PowerShell / REST API

Community Center Not monitored
{count} votes

2 answers

Sort by: Most helpful
  1. Tech-Hyd-1989 5,816 Reputation points
    2023-06-23T09:25:46.2866667+00:00

    Hello

    
    Yes, it is possible to programmatically enable or disable the "Disable Repository" flag for each repository in Azure DevOps using various methods such as Azure CLI commands, .NET Core, PowerShell, or REST API.
    
    Azure CLI:
    You can use the Azure CLI commands to enable or disable the "Disable Repository" flag for a repository in Azure DevOps. Here's an example of how you can do it:
    To enable the flag:
    
    bash
    Copy code
    az repos update --id <repositoryId> --disable
    To disable the flag:
    
    bash
    Copy code
    az repos update --id <repositoryId> --enable
    Replace <repositoryId> with the actual ID of the repository you want to enable or disable.
    
    .NET Core:
    Using the Azure DevOps .NET client libraries, you can write code in .NET Core to enable or disable the "Disable Repository" flag. You would need to authenticate and use the Azure DevOps REST API to update the repository settings accordingly. Here's an example of how you can do it:
    csharp
    Copy code
    using Microsoft.VisualStudio.Services.Common;
    using Microsoft.TeamFoundation.SourceControl.WebApi;
    using Microsoft.VisualStudio.Services.WebApi;
    
    ...
    
    string personalAccessToken = "YOUR_PAT";
    string organizationUrl = "https://dev.azure.com/YOUR_ORGANIZATION";
    Guid repositoryId = new Guid("REPOSITORY_ID");
    
    VssConnection connection = new VssConnection(new Uri(organizationUrl), new VssBasicCredential(string.Empty, personalAccessToken));
    GitHttpClient gitClient = connection.GetClient<GitHttpClient>();
    
    var repository = gitClient.GetRepositoryAsync(repositoryId).Result;
    repository.IsDisabled = true; // Set to 'true' to disable or 'false' to enable
    
    repository = gitClient.UpdateRepositoryAsync(repository, repositoryId).Result;
    Make sure to replace YOUR_PAT with your personal access token and YOUR_ORGANIZATION with your organization name.
    
    PowerShell:
    With PowerShell, you can use the Azure DevOps REST API to enable or disable the "Disable Repository" flag. Here's an example:
    powershell
    Copy code
    $personalAccessToken = "YOUR_PAT"
    $organization = "YOUR_ORGANIZATION"
    $repositoryId = "REPOSITORY_ID"
    $disableFlag = $true # Set to $false to enable
    
    $base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$($personalAccessToken)"))
    $headers = @{
        Authorization = "Bearer $personalAccessToken"
        "Content-Type" = "application/json"
    }
    
    $url = "https://dev.azure.com/$organization/_apis/git/repositories/$repositoryId?api-version=6.1-preview.1"
    $body = @{
        isDisabled = $disableFlag
    } | ConvertTo-Json
    
    $response = Invoke-RestMethod -Uri $url -Headers $headers -Method Patch -Body $body
    Replace YOUR_PAT with your personal access token, YOUR_ORGANIZATION with your organization name, and REPOSITORY_ID with the ID of the repository you want to enable or disable.
    
    You can choose the method that suits your requirements and use it to programmatically enable or disable the "Disable Repository" flag for each repository in Azure DevOps.
    
    0 comments No comments

  2. Tech-Hyd-1989 5,816 Reputation points
    2023-06-23T09:28:13.89+00:00

    Hello

    Yes, it is possible to programmatically enable or disable the "Disable Repository" flag for each repository in Azure DevOps using various methods such as Azure CLI commands, .NET Core, PowerShell, or REST API.

    1. Azure CLI: You can use the Azure CLI commands to enable or disable the "Disable Repository" flag for a repository in Azure DevOps. Here's an example of how you can do it:

    To enable the flag:

    az repos update --id <repositoryId> --disable

    To disable the flag:

    az repos update --id <repositoryId> --enable

    Replace <repositoryId> with the actual ID of the repository you want to enable or disable.

    1. .NET Core: Using the Azure DevOps .NET client libraries, you can write code in .NET Core to enable or disable the "Disable Repository" flag. You would need to authenticate and use the Azure DevOps REST API to update the repository settings accordingly. Here's an example of how you can do it:

    using Microsoft.VisualStudio.Services.Common;

    using Microsoft.TeamFoundation.SourceControl.WebApi;

    using Microsoft.VisualStudio.Services.WebApi;

    ...

    string personalAccessToken = "YOUR_PAT";

    string organizationUrl = "https://dev.azure.com/YOUR_ORGANIZATION";

    Guid repositoryId = new Guid("REPOSITORY_ID");

    VssConnection connection = new VssConnection(new Uri(organizationUrl), new VssBasicCredential(string.Empty, personalAccessToken));

    GitHttpClient gitClient = connection.GetClient<GitHttpClient>();

    var repository = gitClient.GetRepositoryAsync(repositoryId).Result;

    repository.IsDisabled = true; // Set to 'true' to disable or 'false' to enable

    repository = gitClient.UpdateRepositoryAsync(repository, repositoryId).Result;

    Make sure to replace YOUR_PAT with your personal access token and YOUR_ORGANIZATION with your organization name.

    1. PowerShell: With PowerShell, you can use the Azure DevOps REST API to enable or disable the "Disable Repository" flag. Here's an example:
    $personalAccessToken = "YOUR_PAT"
    $organization = "YOUR_ORGANIZATION"
    $repositoryId = "REPOSITORY_ID"
    $disableFlag = $true # Set to $false to enable
    
    $base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$($personalAccessToken)"))
    $headers = @{
        Authorization = "Bearer $personalAccessToken"
        "Content-Type" = "application/json"
    }
    
    $url = "https://dev.azure.com/$organization/_apis/git/repositories/$repositoryId?api-version=6.1-preview.1"
    $body = @{
        isDisabled = $disableFlag
    } | ConvertTo-Json
    
    $response = Invoke-RestMethod -Uri $url -Headers $headers -Method Patch -Body $body
    
    
    
    1. Replace YOUR_PAT with your personal access token, YOUR_ORGANIZATION with your organization name, and REPOSITORY_ID with the ID of the repository you want to enable or disable.

    You can choose the method that suits your requirements and use it to programmatically enable or disable the "Disable Repository" flag for each repository in Azure DevOps.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.