Shako
Shako

Reputation: 31

Changing issue status to Done via Jira REST API, Atlassian.Net SDK

I'm working with Jira REST APIs with visual studio (c#) with help of Atlassian.NET SDK and trying to build such model:

Search functionality works good, the problem is with changing its status. Here’s my code:

static async System.Threading.Tasks.Task Main(string[] args)
{

            ServicePointManager.Expect100Continue = true;
            ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072; //TLS 1.2

            try
            {

                // Connection to JIRA using REST client
                var jira = Jira.CreateRestClient("https://XXX.atlassian.net", "user", "token");

                // LINQ syntax for retrieving issues
                var issue = (from i in jira.Issues.Queryable
                             where i.Project == "QA" && i.Summary == "Summary" && i.Status == "To Do"
                             orderby i.Created
                             select i).First();

                await issue.SaveChangesAsync();

                string ticketid = issue.Key.Value;
                string ticketsummary = issue.Summary;
                string ticketkey = issue.JiraIdentifier;

                //Updating found issue
                var closeticket = await jira.Issues.GetIssueAsync(ticketid);
                closeticket.Status = "Done";

                await closeticket.SaveChangesAsync();                

            }
            catch (Exception ex) { /*Result: SUCCESS [no issues found]*/ }
      }

With this code I’m trying to search in project “QA”, where issue summary is “Summary” and status is “To Do”. Then I want to close its Status from “To Do” to “Done”. But I’m getting error on line closeticket.Status = "Done"; with text:

Error CS0200 Property or indexer 'Issue.Status' cannot be assigned to -- it is read only

Please give me some suggestions or I’m about to hang myself…

Thank you in advance.

EDIT: I forgot to mention that I also tried to do the status changing case with help of httpclient. But there's a problem, when I connect to issue transitions, it returns error with statuscode 404.

 var handler = new HttpClientHandler();
            handler.AllowAutoRedirect = true;
            handler.Proxy = new WebProxy("https://XXX.atlassian.net", true, null, CredentialCache.DefaultNetworkCredentials);
            // need to pass a valid username + password to JIRA
            var jiraCredentials = UTF8Encoding.UTF8.GetBytes("user:token");
            var httpClient = new HttpClient(handler);
            httpClient.MaxResponseContentBufferSize = int.MaxValue;
            httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(jiraCredentials));
            httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));


            string transitionURL = "https://XXX.atlassian.net" + "/rest/api/2/issue/" + "QA-10" + "/transitions?expand=transitions.fields";
            string jsonString = "{ \"update\":{},\"transition\": { \"id\": \"" + "31" + "\"}, \"fields\": { } }";
            var sContent = new StringContent(jsonString, Encoding.UTF8, "application/json");
            var httpClientt = new HttpClient();
            HttpResponseMessage transitionResponse = httpClientt.PostAsync(transitionURL, sContent).Result;
            httpClient.Dispose();

Upvotes: 1

Views: 2007

Answers (1)

Shako
Shako

Reputation: 31

After a deep research, I have finally reached the right way which is http request.

If there's anybody suffering with problem, here's a code:

 using (var httpClient = new HttpClient())
            {
                using (var request = new HttpRequestMessage(new HttpMethod("POST"), "https://XXX.atlassian.net/rest/api/2/issue/TST-4/transitions?expand=transitions.fields"))
                {
                    var base64authorization = Convert.ToBase64String(Encoding.ASCII.GetBytes("user:token"));
                    request.Headers.TryAddWithoutValidation("Authorization", $"Basic {base64authorization}");

                    request.Content = new StringContent("{\"transition\":{\"id\":\"31\"}}");
                    request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");

                    var response = await httpClient.SendAsync(request);
                }
            }

Upvotes: 1

Related Questions