Quantcast
Channel: MSDN Blogs
Viewing all articles
Browse latest Browse all 12366

Using ReleaseManagement REST API's

$
0
0

 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

publicstaticasyncvoidGetReleaseDefinitions()
 { 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/DefaultCollection/{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?
publicstaticasyncvoidUpdateReleaseDefinitionName()
 {try
 {var username = "<username>";var password = "<password>";var definitionUri = "https://{0}.vsrm.visualstudio.com/DefaultCollection/{1}/_apis/release/definitions/{2}/?api-version={3}";var updateDefinitionUri = "https://{0}.vsrm.visualstudio.com/DefaultCollection/{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 onpremisestring 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 StartRelease()
           {try
            {
                var username ="<username>";
                var password ="<password>";

                var releaseUri ="https://{0}.vsrm.visualstudio.com/DefaultCollection/{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.1for TFS onpremise// Specify definitionId, alias, instancereference idand 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 startint releaseId =<releaseId>;int releaseEnvironmentId =<releaseEnvironmentId>;

                string apiVersion ="3.0-preview.2"; // use api-version =2.2-preview.1for TFS onpremise
                var startReleaseUri ="https://{0}.vsrm.visualstudio.com/DefaultCollection/{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?
publicstaticasyncvoidApproveRelease()
        {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 approvalsint approvalid = 31;string apiVersion = "3.0-preview.1"; // use api-version = 2.2-preview.1 for TFS onpremisevar approveReleaseUri = "https://{0}.vsrm.visualstudio.com/DefaultCollection/{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 Approvedvar 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());
            }
        }

Viewing all articles
Browse latest Browse all 12366

Trending Articles