按Jira中的问题ID搜索

时间:2015-05-05 04:09:52

标签: c# api jira jira-rest-api

以下是我在项目中搜索问题的代码:

JiraManager.cs

namespace JiraExample
{
    public class JiraManager
    {
        private const string m_BaseUrl = "https://jira.ops.expertcity.com/rest/api/latest/";
        private string m_Username;
        private string m_Password;

        public JiraManager(string username, string password)
        {
            m_Username = username;
            m_Password = password;
        }

        /// <summary>
        /// Runs a query towards the JIRA REST api
        /// </summary>
        /// <param name="resource">The kind of resource to ask for</param>
        /// <param name="argument">Any argument that needs to be passed, such as a project key</param>
        /// <param name="data">More advanced data sent in POST requests</param>
        /// <param name="method">Either GET or POST</param>
        /// <returns></returns>
        protected string RunQuery(
            JiraResource resource, 
            string argument = null, 
            string data = null,
            string method = "GET")
        {
            string url = string.Format("{0}{1}/", m_BaseUrl, resource.ToString());

            if (argument != null)
            {
                url = string.Format("{0}{1}/", url, argument);
            }

            HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
            request.ContentType = "application/json";
            request.Method = method;

            if (data != null)
            {
                using (StreamWriter writer = new StreamWriter(request.GetRequestStream()))
                {
                    writer.Write(data);
                }
            }

            string base64Credentials = GetEncodedCredentials();
            request.Headers.Add("Authorization", "Basic " + base64Credentials);

            HttpWebResponse response = request.GetResponse() as HttpWebResponse;

            string result = string.Empty;
            using (StreamReader reader = new StreamReader(response.GetResponseStream()))
            {
                result = reader.ReadToEnd();
            }

            return result;
        }

        protected string RunQueryTEST(JiraResource resource,
           string argument = null,
           string data = null,
           string method = "GET")
        {
            string result="";
            string url = m_BaseUrl+"SFAND-1069"; 

            if (argument != null)
            {
                url = string.Format("{0}{1}/", url, argument);
            }
            try
            {
                HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
                request.ContentType = "application/json";
                request.Method = method;

                if (data != null)
                {
                    using (StreamWriter writer = new StreamWriter(request.GetRequestStream()))
                    {
                        writer.Write(data);
                    }
                }

                string base64Credentials = GetEncodedCredentials();
                request.Headers.Add("Authorization", "Basic " + base64Credentials);

                HttpWebResponse response = request.GetResponse() as HttpWebResponse;

                result = string.Empty;
                using (StreamReader reader = new StreamReader(response.GetResponseStream()))
                {
                    result = reader.ReadToEnd();
                }
            }
            catch(Exception e){}

            return result;
        }

        public string getmyissue()
        {
            string issuedescription = RunQueryTEST(JiraResource.issue);
            return issuedescription;
        }

        public List<ProjectDescription> GetProjects()
        {
            List<ProjectDescription> projects = new List<ProjectDescription>();
            string projectsString = RunQuery(JiraResource.project);

            return JsonConvert.DeserializeObject<List<ProjectDescription>>(projectsString);
        }

        public List<Issue> GetIssues(
            string jql, 
            List<string> fields = null, 
            int startAt = 0, 
            int maxResult = 200)
        {
            fields = fields ?? new List<string>{"summary", "status", "assignee"};

            SearchRequest request = new SearchRequest();
            request.Fields = fields;
            request.JQL = jql;
            request.MaxResults = maxResult;
            request.StartAt = startAt;

            string data = JsonConvert.SerializeObject(request);
            string result = RunQuery(JiraResource.search, data: data, method: "POST");

            SearchResponse response = JsonConvert.DeserializeObject<SearchResponse>(result);

            return response.IssueDescriptions;
        }

        private string GetEncodedCredentials()
        {
            string mergedCredentials = string.Format("{0}:{1}", m_Username, m_Password);

            byte[] byteCredentials = UTF8Encoding.UTF8.GetBytes(mergedCredentials);
            return Convert.ToBase64String(byteCredentials);
        }
    }
}

Program.cs的

namespace JiraExample
{
class Program
{
    public static string ReadPassword()
    {
        string password = "";
        ConsoleKeyInfo info = Console.ReadKey(true);
        while (info.Key != ConsoleKey.Enter)
        {
            if (info.Key != ConsoleKey.Backspace)
            {
                Console.Write("*");
                password += info.KeyChar;
            }
            else if (info.Key == ConsoleKey.Backspace)
            {
                if (!string.IsNullOrEmpty(password))
                {
                    // remove one character from the list of password characters
                    password = password.Substring(0, password.Length - 1);
                    // get the location of the cursor
                    int pos = Console.CursorLeft;
                    // move the cursor to the left by one character
                    Console.SetCursorPosition(pos - 1, Console.CursorTop);
                    // replace it with space
                    Console.Write(" ");
                    // move the cursor to the left by one character again
                    Console.SetCursorPosition(pos - 1, Console.CursorTop);
                }
            }
            info = Console.ReadKey(true);
        }
        // add a new line because user pressed enter at the end of their password
        Console.WriteLine();
        return password;
    }

    static void Main(string[] args)
    {
        Console.WriteLine("Hello and welcome to a Jira Example application!");

        #region Create manager
        Console.Write("Username: ");
        string username = Console.ReadLine();

        Console.Write("Password: ");
        string password = ReadPassword();

        JiraManager manager = new JiraManager(username, password);
        #endregion

        Console.Clear();

        //Console.WriteLine(manager.getmyissue());

        List<ProjectDescription> projects = manager.GetProjects();
        Console.WriteLine("Select a project: ");
        for (int i = 0; i < projects.Count; i++)
        {
            Console.WriteLine("{0}: {1}", i, projects[i].Name);
        }

        Console.Write("Project to open: ");
        string projectStringIndex = Console.ReadLine();
        int projectIndex = 0;
        if (!int.TryParse(projectStringIndex, out projectIndex))
        {
            Console.WriteLine("You failed to select a project...");
            Environment.Exit(0);
        }

        ProjectDescription selectedProject = projects[projectIndex];
        string projectKey = selectedProject.Key;

        string jql = "project = " + projectKey;
        List<Issue> issueDescriptions = manager.GetIssues(jql);
        foreach (Issue description in issueDescriptions)
        {
            Console.WriteLine("{0}: {1}", description.Key, description.Fields.Summary);
        }

        Console.Read();
    }
}
}

目前代码的作用是首先询问用户项目编号,然后显示项目中的所有问题。

我想要的是,如果我给出一个问题编号,它应该检查问题是否存在(它可能存在于任何项目中),如果它存在,它应该返回问题的所有细节,如状态,所有者,提交日期等。

为此,我需要在哪里对代码进行更改?

我对此有点新,因此是一个复杂的问题。任何帮助都将受到高度赞赏。

0 个答案:

没有答案