public async System.Threading.Tasks.Task <string> Put <T>(string uri, T data) { using (System.Net.Http.HttpClient httpClient = new System.Net.Http.HttpClient()) { System.Net.Http.Headers.MediaTypeHeaderValue mediaType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json"); Newtonsoft.Json.JsonSerializerSettings jsonSerializerSettings = new Newtonsoft.Json.JsonSerializerSettings(); JsonNetFormatter jsonFormatter = new JsonNetFormatter(jsonSerializerSettings); System.Net.Http.ObjectContent content = new System.Net.Http.ObjectContent <T>(data , new System.Net.Http.Formatting.JsonMediaTypeFormatter() ); System.Net.Http.HttpResponseMessage response = await httpClient.PutAsync(uri, content); response.EnsureSuccessStatusCode(); return(response.Content.ReadAsStringAsync().Result); /* * var requestMessage = new System.Net.Http.HttpRequestMessage * (data, mediaType, new System.Net.Http.Formatting.MediaTypeFormatter[] { jsonFormatter }); * * // var result = httpClient.PutAsync("_endpoint", requestMessage.Content).Result; * // return result.Content.ReadAsStringAsync().Result; */ } }
// https://forums.asp.net/t/1773007.aspx?How+to+correctly+use+PostAsync+and+PutAsync+ public string Put <T>(T data) { System.Net.Http.Headers.MediaTypeHeaderValue mediaType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json"); Newtonsoft.Json.JsonSerializerSettings jsonSerializerSettings = new Newtonsoft.Json.JsonSerializerSettings(); // JsonNetFormatter jsonFormatter = new JsonNetFormatter(jsonSerializerSettings); // var requestMessage = new // System.Net.Http.HttpRequestMessage<T>(data, mediaType // , new System.Net.Http.Formatting.MediaTypeFormatter[] { jsonFormatter }); System.Net.Http.ObjectContent content = new System.Net.Http.ObjectContent <T>(data , new System.Net.Http.Formatting.JsonMediaTypeFormatter() ); using (System.Net.Http.HttpClient httpClient = new System.Net.Http.HttpClient()) { string _endpoint = ""; httpClient.PutAsync(_endpoint, content).ContinueWith(httpResponseMessage => { return(httpResponseMessage.Result.Content.ReadAsStringAsync()); // return await Task.Run(() => httpResponseMessage.Result.Content.ReadAsStringAsync()); }); } return(null); }
async System.Threading.Tasks.Task UploadJsonObject2Async <T>(System.Uri uri, T data) { using (System.Net.Http.HttpClient client = new System.Net.Http.HttpClient()) { System.Net.Http.ObjectContent content = new System.Net.Http.ObjectContent <T>(data , new System.Net.Http.Formatting.JsonMediaTypeFormatter() ); System.Net.Http.HttpResponseMessage response = await client.PostAsync(uri, content); response.EnsureSuccessStatusCode(); } }
static void Main(string[] args) { ReadFile r = new ReadFile(); string bug = r.ReadTrxFile(); var data = new Issue(); data.fields.project.key = "EM"; data.fields.summary = "Bug" + " " + DateTime.Now; data.fields.description = bug; data.fields.issuetype.name = "Bug"; // string postUrl = "http://testagent.southeastasia.cloudapp.azure.com:7000/rest/api/latest/issue"; //string postUrl = "http://devopsjira.southeastasia.cloudapp.azure.com/rest/api/latest/issue"; string postUrl = "https://mtdevopscoe.atlassian.net/rest/api/latest/issue"; System.Net.Http.HttpClient client = new System.Net.Http.HttpClient(); client.BaseAddress = new System.Uri(postUrl); // byte[] cred = UTF8Encoding.UTF8.GetBytes("Priyanka:Ult1m@te"); // byte[] cred = UTF8Encoding.UTF8.GetBytes("Abhideep:London@123"); byte[] cred = UTF8Encoding.UTF8.GetBytes("jitendra.kumar:Welcome@123"); client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(cred)); client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json")); System.Net.Http.Formatting.MediaTypeFormatter jsonFormatter = new System.Net.Http.Formatting.JsonMediaTypeFormatter(); //System.Net.Http.HttpContent content = new System.Net.Http.ObjectContent<string>(data, jsonFormatter); System.Net.Http.HttpContent content = new System.Net.Http.ObjectContent <Issue>(data, jsonFormatter); System.Net.Http.HttpResponseMessage response = client.PostAsync("issue", content).Result; if (response.IsSuccessStatusCode) { string result = response.Content.ReadAsStringAsync().Result; // its will be 200 OK (inserted) Console.Write(result); } else { Console.Write(response.StatusCode.ToString()); Console.ReadLine(); } }
//private string issueKey; public void UserStoryCheckCreate(string featureName, string featureFilePath) { List <string> results = new List <string>(); //string issuenum = null; string fileName = featureFilePath; string tktID = null; string tkyKey = null; string JiraUserName = ConfigurationManager.AppSettings["username"].ToString(); string JiraPassword = ConfigurationManager.AppSettings["password"].ToString(); string systemUsr = Environment.UserName; string ProjFolderPath = System.IO.Directory.GetParent(System.IO.Directory.GetParent(Environment.CurrentDirectory).ToString()).ToString(); //Checking whether the user story already exists, if true, do not create a new ticket HttpClient client1 = new HttpClient(); string Apiurl = ("https://spiderlogic.jira.com/rest/api/2/search?jql=project=SFLOW&fields=description&fields=summary"); var credentials = Encoding.ASCII.GetBytes(JiraUserName + ":" + JiraPassword); client1.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(credentials)); Uri uri = new Uri(Apiurl.ToString()); string ApiResponse = client1.GetStringAsync(uri).Result; var root = JsonConvert.DeserializeObject <RootObject>(ApiResponse); var Sumry = root.issues.Count; //Checking if the user story already exists by iterating though the issue list in jira bool FeatureExists = false; var issues = root.issues; string[] textLinesFT = System.IO.File.ReadAllLines(fileName); //Finding the feature name from the feature file which will be the name of the ticket string FeatureKeyword = "Feature"; string FeaureTRName = string.Empty; foreach (string line2 in textLinesFT) { if (line2.Contains(FeatureKeyword)) { results.Add(line2); FeaureTRName = line2; } } //Trim the User Story name to display only the Feature name FeaureTRName = FeaureTRName.Replace("Feature: ", ""); foreach (var item in issues) { var fields = item.fields; var summary = (fields.summary).ToString(); if (summary.Equals(FeaureTRName)) { Console.WriteLine("User story already exists in the project"); FeatureExists = true; tktID = item.id; tkyKey = item.key; } } //Check if the scenario already exists in JIRA within the Feature bool ScenarioExists = false; //If the user story does not exist, create a new user story by using the POST method in JIRA via API if (FeatureExists == false) { string text = System.IO.File.ReadAllText(fileName); string searchKeyword = "Feature"; string[] textLines = System.IO.File.ReadAllLines(fileName); string FTRName = string.Empty; foreach (string line in textLines) { if (line.Contains(searchKeyword)) { results.Add(line); FTRName = line; } } string DescText; int pFrom = text.IndexOf("@") + "@".Length; string reslt = text.Substring(pFrom); DescText = "@" + reslt; string SnrKeyword = "Scenario Outline:"; string ScrName = string.Empty; foreach (string line in textLines) { if (line.Contains(SnrKeyword)) { results.Add(line); ScrName = line; } } //Trim the User Story name to display only the Feature name FTRName = FTRName.Replace("Feature: ", ""); //The fields required to create a new JIRA ticket var data = new Issue(); data.fields.project.key = "SFLOW"; data.fields.summary = FTRName; // data.fields.description = reslt; //data.fields.description = text; data.fields.description = DescText; data.fields.issuetype.name = "User Story"; string postUrl = "https://spiderlogic.jira.com/rest/api/latest/"; System.Net.Http.HttpClient client = new System.Net.Http.HttpClient(); client.BaseAddress = new System.Uri(postUrl); byte[] cred = UTF8Encoding.UTF8.GetBytes(JiraUserName + ":" + JiraPassword); client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(cred)); client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json")); System.Net.Http.Formatting.MediaTypeFormatter jsonFormatter = new System.Net.Http.Formatting.JsonMediaTypeFormatter(); System.Net.Http.HttpContent content = new System.Net.Http.ObjectContent <Issue>(data, jsonFormatter); System.Net.Http.HttpResponseMessage response = client.PostAsync("issue", content).Result; if (response.IsSuccessStatusCode) { string result = response.Content.ReadAsStringAsync().Result; Console.Write(result); } else { Console.Write(response.StatusCode.ToString()); } } else { //This section is to update the jira ticket if a new scenario is added in the feature file if (FeatureExists == true && ScenarioExists == false) { Console.Out.WriteLine("New scenario found and hence updating the ticket with the new scenario"); ////string descriptNew = System.IO.File.ReadAllText(@"C:\Users\subrahp\Documents\Visual Studio 2015\Projects\FlipkartExcelSpecRunCartTest\FlipkartExcelSpecRunCartTest\FlipkartExcelSpecrunCartTest.feature"); //string descriptNew = System.IO.File.ReadAllText(fileName); //string descriptNew2 = descriptNew.Replace(';', ' ').Replace('\r', ' ').Replace('\n', ' ').Replace('\t', ' ').Replace("\n\n", " "); ////Get the id from the existing user story to update the description and post with the updated description //var PostURL2 = "?jql=project=SFLOW&fields=description&fields=summary"; //var POSTRequest = "https://spiderlogic.jira.com/rest/api/2/issue/"; //var request = WebRequest.Create(POSTRequest + tkyKey + PostURL2); //request.Method = "PUT"; //request.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(Encoding.ASCII.GetBytes(JiraUserName + ":" + JiraPassword))); //request.ContentType = "application/json"; //var json = string.Format("{{ \"update\": {{\"description\": [{{\"set\":\"{0}\"}}] }} }};", descriptNew2); //request.GetRequestStream().Write(Encoding.ASCII.GetBytes(json), 0, json.Length); //var response = request.GetResponse(); //var reader = new StreamReader(response.GetResponseStream()); //var output = reader.ReadToEnd(); } } }
public void create(string bugsummary, string errordetails) { string tktID = null; string tkyKey = null; //Checking whether the user story already exists, if true, do not create a new ticket HttpClient client2 = new HttpClient(); string issueurl = ("https://spiderlogic.jira.com/rest/api/2/search?jql=project=SFLOW&fields=issues&fields=summary"); var credentials = Encoding.ASCII.GetBytes("psubrahmanya:Gonikoppal@1234"); client2.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(credentials)); Uri uri = new Uri(issueurl.ToString()); string ApiResponse = client2.GetStringAsync(uri).Result; var root = JsonConvert.DeserializeObject <RootObject>(ApiResponse); var Sumry = root.issues.Count; //Checking if the user story already exists by iterating though the issue list in jira bool bugexists = false; var issues = root.issues; foreach (var issue in issues) { var fields = issue.fields; Console.WriteLine("fields:" + fields); var summary = (fields.summary).ToString(); if (summary.Equals(bugsummary)) { Console.WriteLine("User story already exists in the project"); bugexists = true; tktID = issue.id; tkyKey = issue.key; } } //If the user story does not exist, create a new user story by using the POST method in JIRA via API if (bugexists == false) { //The fields required to create a new JIRA ticket var data = new Issue(); data.fields.project.key = "SFLOW"; data.fields.summary = bugsummary; data.fields.description = errordetails; data.fields.issuetype.name = "Bug"; string postUrl = "https://spiderlogic.jira.com/rest/api/latest/"; System.Net.Http.HttpClient client = new System.Net.Http.HttpClient(); client.BaseAddress = new System.Uri(postUrl); byte[] cred = UTF8Encoding.UTF8.GetBytes("psubrahmanya:Gonikoppal@1234"); client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(cred)); client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json")); System.Net.Http.Formatting.MediaTypeFormatter jsonFormatter = new System.Net.Http.Formatting.JsonMediaTypeFormatter(); System.Net.Http.HttpContent content = new System.Net.Http.ObjectContent <Issue>(data, jsonFormatter); System.Net.Http.HttpResponseMessage response = client.PostAsync("issue", content).Result; if (response.IsSuccessStatusCode) { string result = response.Content.ReadAsStringAsync().Result; Console.Write(result); } else { Console.Write(response.StatusCode.ToString()); Console.ReadLine(); } } }
public void addComment(string bugsummary, string commenttext) { string tktID = null; string tkyKey = null; string state = ""; string issuetype = ""; //Check if the issue exists HttpClient client2 = new HttpClient(); string issueurl = ("https://spiderlogic.jira.com/rest/api/2/search?jql=project=SFLOW&fields=issues&fields=summary&fields=description&fields=status&fields=project&fields=issuetype"); var credentials = Encoding.ASCII.GetBytes("[email protected]:spiderqa"); client2.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(credentials)); Uri uri = new Uri(issueurl.ToString()); string ApiResponse = client2.GetStringAsync(uri).Result; var root = JsonConvert.DeserializeObject <RootObject>(ApiResponse); Console.WriteLine("Inside Add Comment"); //Checking if the issue exists by iterating through the issue list in jira var issues = root.issues; foreach (var issue in issues) { var fields = issue.fields; var summary = (fields.summary).ToString(); state = (fields.status.name).ToString(); issuetype = (fields.issuetype.name).ToString(); if (issuetype == "Bug") { Console.WriteLine("Issue Type" + issuetype); if (summary.Equals(bugsummary)) { Console.WriteLine("Issue exists in the project"); Console.WriteLine("state :" + state); if (state == "Open") { tktID = issue.id; tkyKey = issue.key; var data = new Comment(); data.body = commenttext; string postUrl = "https://spiderlogic.jira.com/rest/api/2/issue/" + tktID + "/comment"; System.Net.Http.HttpClient client = new System.Net.Http.HttpClient(); client.BaseAddress = new System.Uri(postUrl); byte[] cred = UTF8Encoding.UTF8.GetBytes("[email protected]:spiderqa"); client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(cred)); client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json")); System.Net.Http.Formatting.MediaTypeFormatter jsonFormatter = new System.Net.Http.Formatting.JsonMediaTypeFormatter(); System.Net.Http.HttpContent content = new System.Net.Http.ObjectContent <Comment>(data, jsonFormatter); System.Net.Http.HttpResponseMessage response = client.PostAsync("comment", content).Result; if (response.IsSuccessStatusCode) { string result = response.Content.ReadAsStringAsync().Result; Console.Write(result); } else { Console.Write(response.StatusCode.ToString()); Console.ReadLine(); } break; } } } } }
public BugState create(string bugsummary, string errordetails, BugState bg) { string tktID = null; string tkyKey = null; string state = ""; string issuetype = ""; string closedtktID = null; string closedtkyKey = null; bg.bugclosed = false; bg.bugexists = false; bg.bugopen = false; //Checking whether the Bug already exists, if true, do not create a new ticket HttpClient client2 = new HttpClient(); string issueurl = ("https://spiderlogic.jira.com/rest/api/2/search?jql=project=SFLOW&fields=issues&fields=summary&fields=description&fields=status&fields=project&fields=issuetype"); var credentials = Encoding.ASCII.GetBytes("[email protected]:spiderqa"); client2.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(credentials)); Uri uri = new Uri(issueurl.ToString()); string ApiResponse = client2.GetStringAsync(uri).Result; var root = JsonConvert.DeserializeObject <RootObject>(ApiResponse); var Sumry = root.issues.Count; //Checking if the user story already exists by iterating though the issue list in jira var issues = root.issues; Debug.WriteLine("Upali Debug: "); foreach (var issue in issues) { var fields = issue.fields; var summary = (fields.summary).ToString(); state = (fields.status.name).ToString(); issuetype = (fields.issuetype.name).ToString(); if ((issuetype == "Bug") & summary.Equals(bugsummary)) { if (state == "Open") { // the control may not go to if block ever if (bg.bugclosed) { Console.WriteLine("In BugCreate: Bug OPened after closed "); bg.bugexists = true; Console.WriteLine("Ticket Key: " + issue.key); } else { Console.WriteLine("In BugCreate : Bug exists"); bg.bugexists = true; tktID = issue.id; tkyKey = issue.key; Console.WriteLine("Ticket Key: " + issue.key); bg.bugopen = true; } } else if (state == "Closed") { if (bg.bugopen) { Console.WriteLine("In BugCreate: Bug closed and ALSO REOPENED"); bg.bugexists = true; bg.bugclosed = true; bg.bugclosedcount = bg.bugclosedcount + 1; bg.buglist.Add(closedtkyKey); } else { Console.WriteLine("In BugCreate: Bug exists bug closed "); bg.bugexists = false; closedtktID = issue.id; closedtkyKey = issue.key; bg.bugclosed = true; bg.closedtkyKey = issue.key; bg.bugclosedcount = bg.bugclosedcount + 1; bg.buglist.Add(closedtkyKey); } } } } //If the Bug not exist, create a new user story by using the POST method in JIRA via API if (bg.bugexists == false) { //The fields required to create a new JIRA ticket var data = new CreateIssue(); data.fields.project.key = "SFLOW"; data.fields.summary = bugsummary; data.fields.description = errordetails; data.fields.issuetype.name = "Bug"; string postUrl = "https://spiderlogic.jira.com/rest/api/latest/"; System.Net.Http.HttpClient client = new System.Net.Http.HttpClient(); client.BaseAddress = new System.Uri(postUrl); byte[] cred = UTF8Encoding.UTF8.GetBytes("[email protected]:spiderqa"); client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(cred)); client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json")); System.Net.Http.Formatting.MediaTypeFormatter jsonFormatter = new System.Net.Http.Formatting.JsonMediaTypeFormatter(); System.Net.Http.HttpContent content = new System.Net.Http.ObjectContent <CreateIssue>(data, jsonFormatter); System.Net.Http.HttpResponseMessage response = client.PostAsync("issue", content).Result; if (response.IsSuccessStatusCode) { string result = response.Content.ReadAsStringAsync().Result; Console.Write(result); } else { Console.Write(response.StatusCode.ToString()); Console.ReadLine(); } } return(bg); }