Share via


Using ReleaseManagement REST API's

 Refer the documentation https://www.visualstudio.com/integrate/api/rm/overview (below samples uses alternate credential for authentication [https://www.visualstudio.com/en-us/integrate/get-started/auth/overview])

1. How to get ReleaseDefinition using ReleaseManagement REST API's

 public static async void GetReleaseDefinitions()
 { 
 try 
 { 
 var username = "<alternate credential username>"; 
 var password = "<password>"; 
 
 using (HttpClient client = new HttpClient()) 
 { 
 client.DefaultRequestHeaders.Accept.Add( 
 new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json")); 
 
 client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", 
 Convert.ToBase64String( 
 System.Text.ASCIIEncoding.ASCII.GetBytes( 
 string.Format("{0}:{1}", username, password)))); 
 
 using (HttpResponseMessage response = client.GetAsync( "https://{account}.vsrm.visualstudio.com/{projectIdorName}/_apis/release/definitions").Result) 
 { 
 response.EnsureSuccessStatusCode(); 
 string responseBody = await response.Content.ReadAsStringAsync(); 
 Console.WriteLine(responseBody); 
 } 
 } 
 } 
 catch (Exception ex) 
 { 
 Console.WriteLine(ex.ToString()); 
 } 
 } 
 2. How to update name of an existing ReleaseDefinition? 
 public static async void UpdateReleaseDefinitionName()
 {
 try
 {
 
 var username = "<username>";
 var password = "<password>";
 
 var definitionUri = "https://{0}.vsrm.visualstudio.com/{1}/_apis/release/definitions/{2}/?api-version={3}";
 var updateDefinitionUri = "https://{0}.vsrm.visualstudio.com/{1}/_apis/release/definitions/?api-version={2}";
 
 using (HttpClient client = new HttpClient())
 {
 //Using basic auth for authorization here. https://www.visualstudio.com/en-us/integrate/get-started/auth/overview
 client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic",
 Convert.ToBase64String(
 System.Text.ASCIIEncoding.ASCII.GetBytes(
 string.Format("{0}:{1}", username, password))));
 
 string accountName = "<accountName>";
 string projectName = "<projectName>";
 int definitionId = 1;
 string apiVersion = "3.0-preview.1"; // use api-version = 2.2-preview.1 for TFS onpremise
 string responseBody = null; 
 using (HttpResponseMessage response = client.GetAsync(string.Format(definitionUri, accountName, projectName, definitionId, apiVersion)).Result)
 {
 response.EnsureSuccessStatusCode();
 responseBody = await response.Content.ReadAsStringAsync();
 
 }
 
 dynamic definitionJsonObject = JObject.Parse(responseBody);
 // updating name of the release definition object
 definitionJsonObject.name = "Fabirkam-New";
 var updatedDefinitionObject = new StringContent(definitionJsonObject.ToString(), Encoding.UTF8, "application/json");
 using (HttpResponseMessage response = client.PutAsync(string.Format(updateDefinitionUri, accountName, projectName, apiVersion), updatedDefinitionObject).Result)
 {
 response.EnsureSuccessStatusCode();
 }
 }
 }
 catch (Exception ex)
 {
 Console.WriteLine(ex.ToString());
 }
 }
 3. How to create a release for a given ReleaseDefinition? 
 public static async void CreateRelease()
           {
            try
            {
                
                var username = "<username>";
                var password = "<password>";

                var releaseUri = "https://{0}.vsrm.visualstudio.com/{1}/_apis/release/releases/?api-version={2}";

                using (HttpClient client = new HttpClient())
                {
                    //Using basic auth for authorization here. https://www.visualstudio.com/en-us/integrate/get-started/auth/overview
                    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic",
                        Convert.ToBase64String(
                            System.Text.ASCIIEncoding.ASCII.GetBytes(
                                string.Format("{0}:{1}", username, password))));

                    string accountName = "<accountname>";
                    string projectName = "<projectname>";
                    string apiVersion = "3.0-preview.1"; // use api-version = 2.2-preview.1 for TFS onpremise

                    // Specify definitionId, alias, instancereference id and name correctly. InstanceReference name is optional for VSTS but madatory for TFS onpremise.
                     string startReleaseMetaData =
                        @"{ "
                        + "\"definitionId\": 6, "
                        + "\"artifacts\": "
                        + "[ { \"alias\": \"FabrikamBD\", \"instanceReference\": { \"id\": \"3\", \"name\": \"20160415.2\" }}]}";

                    var releaseContent = new StringContent(startReleaseMetaData, Encoding.UTF8, "application/json");
                    using (HttpResponseMessage response = client.PostAsync(string.Format(releaseUri, accountName, projectName, apiVersion), releaseContent).Result)
                    {
                        response.EnsureSuccessStatusCode();
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }
 4. How to start a release? 
 public static async void StartRelease()
        {
            try
            {

                var username = "<username>";
                var password = "<password>";

                string accountName = "<accountName>";
                string projectName = "<projectName>";

                //One can get the releaseEnvironmentId after doing a GET on the particular release to start
                int releaseId = <releaseId>;
                int releaseEnvironmentId = <releaseEnvironmentId>;

                string apiVersion = "3.0-preview.2"; // use api-version = 2.2-preview.1 for TFS onpremise
                var startReleaseUri = "https://{0}.vsrm.visualstudio.com/{1}/_apis/release/releases/{2}/environments/{3}?api-version={4}";

                using (HttpClient client = new HttpClient())
                {
                    //Using basic auth for authorization here. https://www.visualstudio.com/en-us/integrate/get-started/auth/overview
                    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic",
                        Convert.ToBase64String(
                            System.Text.ASCIIEncoding.ASCII.GetBytes(
                                string.Format("{0}:{1}", username, password))));

                    var method = new HttpMethod("PATCH");
                    string startReleaseMetaData = "{\"status\":2}"; // status = 2 mean InProgress
                    var request = new HttpRequestMessage(method, string.Format(startReleaseUri, accountName, projectName, releaseId, releaseEnvironmentId, apiVersion))
                    {
                      Content = new StringContent(startReleaseMetaData, Encoding.UTF8, "application/json")   
                   };

                    using (HttpResponseMessage response = client.SendAsync(request).Result)
                    {
                        response.EnsureSuccessStatusCode();
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }
 4. How to approve a release? 
 public static async void ApproveRelease()
        {
            try
            {
                var username = "<username>";
                var password = "<password>";

                string accountName = "<accountName>";
                string projectName = "<projectName";

                //One can get the approvalid after doing a GET Approval or GET on the release having approvals
                int approvalid = 31;

                string apiVersion = "3.0-preview.1"; // use api-version = 2.2-preview.1 for TFS onpremise
                var approveReleaseUri = "https://{0}.vsrm.visualstudio.com/{1}/_apis/release/approvals/{2}?api-version={3}";

                using (HttpClient client = new HttpClient())
                {
                    //Using basic auth for authorization here. https://www.visualstudio.com/en-us/integrate/get-started/auth/overview
                    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic",
                        Convert.ToBase64String(
                            System.Text.ASCIIEncoding.ASCII.GetBytes(
                                string.Format("{0}:{1}", username, password))));

                    var method = new HttpMethod("PATCH");
                    string approvveReleaseMetaData = "{\"status\":2, \"comments\":\"Good to go\"}"; // status = 2 mean Approved
                    var request = new HttpRequestMessage(method, string.Format(approveReleaseUri, accountName, projectName, approvalid, apiVersion))
                    {
                        Content = new StringContent(approvveReleaseMetaData, Encoding.UTF8, "application/json")
                    };

                    using (HttpResponseMessage response = client.SendAsync(request).Result)
                    {
                        response.EnsureSuccessStatusCode();
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }