Share via


How to undelete releases of a release definition?

One of our customer have reported as he has deleted few releases of a given release definition accidentally and wanted to know a way to bulk un-delete those releases. The undelete can be done within 15 days of the soft delete and after that releases are permanently deleted. It can be un-deleted either using Powershell or C# program.

Using powershell:-

===============

 param (
[Parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
[string] $token,
[int32] $definitionId
)

## Construct a basic auth head using PAT
function BasicAuthHeader()
{

  param([string]$authtoken)
  $ba = (":{0}" -f $authtoken)
  $ba = [System.Text.Encoding]::UTF8.GetBytes($ba)
  $ba = [System.Convert]::ToBase64String($ba)
  $h = @{Authorization=("Basic{0}" -f $ba);ContentType="application/json"}
  return $h
}

# Fill in your account name/project name/definition name
$accountName = "anjani1"
$projectName = "TFVCProj"
$definitionNameToRecover = "New Release Definition"
# Find the Id of release definition that got deleted
$deletedReleasesUri = "https://$accountName.vsrm.visualstudio.com/$projectName/_apis/Release/releases?api-version=4.1-preview.6&isDeleted=true&definitionId=$definitionId"
$h = BasicAuthHeader $token
$deletedReleases = Invoke-RestMethod -Uri $deletedReleasesUri -Headers $h -ContentType “application/json" -Method Get
$deletedReleasesIds = $deletedReleases.value.id # | ConvertTo-Json -Depth 100
write-host "Found the total deleted releases : $deletedReleasesIds.count"
For ($i=0; $i -lt $deletedReleasesIds.count; $i++)
{
$deletedReleaseId = $deletedReleasesIds[$i];
Write-Host "Found the deleted id : $deletedReleaseId";
# Recover the deleted release
$undeleteReason = '{ "Comment" : "Deleted by mistake" }'
$undeleteReleaseUri = "https://$accountName.vsrm.visualstudio.com/$projectName/_apis/Release/releases/$deletedReleaseId`?comment=$undeleteReason&api-version=4.1-preview.6"
$undeletedDefinition = Invoke-RestMethod -Uri $undeleteReleaseUri -Headers $h -ContentType “application/json" -Method Put -Body $undeleteReason
write-host "Release id: $deletedReleaseId recovered successfully"
}

 

Using C# You may refer the blog link to know as how to program using ReleaseManagement client

==============

 using System;
using Microsoft.VisualStudio.Services.Client;
using Microsoft.VisualStudio.Services.Common;
using Microsoft.VisualStudio.Services.ReleaseManagement.WebApi.Clients;
using Microsoft.VisualStudio.Services.WebApi;
using Microsoft.VisualStudio.Services.ReleaseManagement.WebApi;
using Microsoft.VisualStudio.Services.ReleaseManagement.WebApi.Contracts;
using System.Diagnostics;

namespace ReleaseHttpClientSample
{
 class Program
 {

  static void Main(string[] args)

    {

      Uri serverUrl = new Uri("https://anjani1.visualstudio.com");
      var project = "TFVCProj";
      VssCredentials credentials = new VssClientCredentials();
      credentials.Storage = new VssClientCredentialStorage();
      VssConnection connection = new VssConnection(serverUrl, credentials);
      ReleaseHttpClient rmClient = connection.GetClient<ReleaseHttpClient>();
      var releases = rmClient.GetReleasesAsync(project, definitionId: 1, isDeleted: true).Result;
      foreach(var release in releases)
          {
             rmClient.UndeleteReleaseAsync(project, release.Id, "Deleted by mistake").SyncResult();
          }
    }
 }
}