<commit_before>﻿using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Linq;
using System.Net;
using Newtonsoft.Json.Linq;
using RestSharp;
using RestSharp.Authenticators;
using VersionOne.JiraConnector.Exceptions;

namespace VersionOne.JiraConnector.Rest
{
    public class JiraRestProxy : IJiraConnector
    {
        private readonly RestClient client;

        private readonly string currentUser;

        public JiraRestProxy(string baseUrl)
            : this(baseUrl, string.Empty, string.Empty)
        {
        }

        public JiraRestProxy(string baseUrl, string username, string password)
        {
            client = new RestClient(baseUrl);

            if (!string.IsNullOrEmpty(username) && !string.IsNullOrEmpty(password))
            {
                client.Authenticator = new HttpBasicAuthenticator(username, password);
            }

            currentUser = username;
        }

        public bool Validate()
        {
            var request = new RestRequest
            {
                Method = Method.GET,
                Resource = "user",
                RequestFormat = DataFormat.Json
            };
            request.AddQueryParameter("username", currentUser);

            var response = client.Execute(request);

            return response.StatusCode.Equals(HttpStatusCode.OK);
        }

        public void Login()
        {
            //throw new NotImplementedException();
        }

        public void Logout()
        {
            //throw new NotImplementedException();
        }

        public Issue[] GetIssuesFromFilter(string issueFilterId)
        {
            var request = new RestRequest
            {
                Method = Method.GET,
                Resource = "search",
                RequestFormat = DataFormat.Json
            };
            request.AddQueryParameter("jql", string.Format("filter={0}", issueFilterId));

            var response = client.Execute(request);

            if (response.StatusCode.Equals(HttpStatusCode.OK))
            {
                dynamic data = JObject.Parse(response.Content);
                return ((JArray)data.issues).Select(CreateIssue).ToArray();
            }
            if (response.StatusCode.Equals(HttpStatusCode.Unauthorized))
                throw new JiraLoginException();
            throw new JiraException(response.StatusDescription, new Exception(response.Content));
        }

        public Issue UpdateIssue(string issueKey, string fieldName, string fieldValue)
        {
            dynamic editMetadata = GetEditMetadata(issueKey);
            dynamic fieldMeta = editMetadata.fields[fieldName];

            if (fieldMeta == null)
                throw new JiraException("Field metadata is missing", null);

            var type = fieldMeta.schema["type"];
            if (type == null)
                throw new JiraException("Field metadata is missing a type", null);

            var request = new RestRequest
            {
                Method = Method.PUT,
                Resource = "issue/{issueIdOrKey}",
                RequestFormat = DataFormat.Json,
            };
            request.AddUrlSegment("issueIdOrKey", issueKey);

            dynamic body;
            if (type.ToString().Equals("array"))
            {
                var custom = fieldMeta.schema["custom"];

                if (custom != null && (custom.ToString().Equals("com.atlassian.jira.plugin.system.customfieldtypes:multiselect")))
                {
                    dynamic operation = new ExpandoObject();
                    ((IDictionary<string, object>)operation).Add(fieldName, new List<dynamic>
                    {
                        new
                        {
                            set = new List<object> { new { value = fieldValue} }
                        }
                    });
                    body = new { update = operation };
                }
				else if (custom != null && (custom.ToString().Equals("com.atlassian.jira.plugin.system.customfieldtypes:select")))
                {
                    dynamic field = new ExpandoObject();
                    ((IDictionary<string, object>)field).Add(fieldName, new { value = fieldValue });
                    body = new { fields = field };
                }
                else
                {
                    dynamic operation = new ExpandoObject();
                    ((IDictionary<string, object>)operation).Add(fieldName, new List<dynamic>
                    {
                        new
                        {
                            set = new List<string> { fieldValue }
                        }
                    });
                    body = new { update = operation };
                }
            }
            else
            {
                dynamic field = new ExpandoObject();
                ((IDictionary<string, object>)field).Add(fieldName, fieldValue);
                body = new { fields = field };
            }
            request.AddBody(body);

            var response = client.Execute(request);
<commit_msg>S-59465 updated single select custom field<commit_after>﻿using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Linq;
using System.Net;
using Newtonsoft.Json.Linq;
using RestSharp;
using RestSharp.Authenticators;
using VersionOne.JiraConnector.Exceptions;

namespace VersionOne.JiraConnector.Rest
{
    public class JiraRestProxy : IJiraConnector
    {
        private readonly RestClient client;

        private readonly string currentUser;

        public JiraRestProxy(string baseUrl)
            : this(baseUrl, string.Empty, string.Empty)
        {
        }

        public JiraRestProxy(string baseUrl, string username, string password)
        {
            client = new RestClient(baseUrl);

            if (!string.IsNullOrEmpty(username) && !string.IsNullOrEmpty(password))
            {
                client.Authenticator = new HttpBasicAuthenticator(username, password);
            }

            currentUser = username;
        }

        public bool Validate()
        {
            var request = new RestRequest
            {
                Method = Method.GET,
                Resource = "user",
                RequestFormat = DataFormat.Json
            };
            request.AddQueryParameter("username", currentUser);

            var response = client.Execute(request);

            return response.StatusCode.Equals(HttpStatusCode.OK);
        }

        public void Login()
        {
            //throw new NotImplementedException();
        }

        public void Logout()
        {
            //throw new NotImplementedException();
        }

        public Issue[] GetIssuesFromFilter(string issueFilterId)
        {
            var request = new RestRequest
            {
                Method = Method.GET,
                Resource = "search",
                RequestFormat = DataFormat.Json
            };
            request.AddQueryParameter("jql", string.Format("filter={0}", issueFilterId));

            var response = client.Execute(request);

            if (response.StatusCode.Equals(HttpStatusCode.OK))
            {
                dynamic data = JObject.Parse(response.Content);
                return ((JArray)data.issues).Select(CreateIssue).ToArray();
            }
            if (response.StatusCode.Equals(HttpStatusCode.Unauthorized))
                throw new JiraLoginException();
            throw new JiraException(response.StatusDescription, new Exception(response.Content));
        }

        public Issue UpdateIssue(string issueKey, string fieldName, string fieldValue)
        {
            dynamic editMetadata = GetEditMetadata(issueKey);
            dynamic fieldMeta = editMetadata.fields[fieldName];

            if (fieldMeta == null)
                throw new JiraException("Field metadata is missing", null);

            var type = fieldMeta.schema["type"];
            if (type == null)
                throw new JiraException("Field metadata is missing a type", null);

            var request = new RestRequest
            {
                Method = Method.PUT,
                Resource = "issue/{issueIdOrKey}",
                RequestFormat = DataFormat.Json,
            };
            request.AddUrlSegment("issueIdOrKey", issueKey);

            dynamic body;
            if (type.ToString().Equals("array"))
            {
                var custom = fieldMeta.schema["custom"];

                if (custom != null && (custom.ToString().Equals("com.atlassian.jira.plugin.system.customfieldtypes:multiselect")))
                {
                    dynamic operation = new ExpandoObject();
                    ((IDictionary<string, object>)operation).Add(fieldName, new List<dynamic>
                    {
                        new
                        {
                            set = new List<object> { new { value = fieldValue} }
                        }
                    });
                    body = new { update = operation };
                }
		else if (custom != null && (custom.ToString().Equals("com.atlassian.jira.plugin.system.customfieldtypes:select")))
                {
                    dynamic operation = new ExpandoObject();
                    ((IDictionary<string, object>)operation).Add(fieldName, new List<dynamic>
                    {
                        new
                        {
                            set = new List<object> { new { value = fieldValue} }
                        }
                    });
                    body = new { update = operation };
                }
                else
                {
                    dynamic operation = new ExpandoObject();
                    ((IDictionary<string, object>)operation).Add(fieldName, new List<dynamic>
                    {
                        new
                        {
                            set = new List<string> { fieldValue }
                        }
                    });
                    body = new { update = operation };
                }
            }
            else
            {
                dynamic field = new ExpandoObject();
                ((IDictionary<string, object>)field).Add(fieldName, fieldValue);
                body = new { fields = field };
            }
            request.AddBody(body);

            var response = client.Execute(request);

{'fraction_non_alphanumeric': 0.09103977000479156, 'fraction_numerical': 0.0006708193579300432, 'mean_word_length': 1.4572639510242524, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '125414', 'n_tokens_mistral': 2447, 'n_tokens_neox': 2320, 'n_words': 529}