<commit_before>package com.github.jk1.ytplugin.rest

import com.github.jk1.ytplugin.issues.model.Issue
import com.github.jk1.ytplugin.issues.model.IssueWorkItem
import com.github.jk1.ytplugin.tasks.YouTrackServer
import com.google.gson.JsonArray
import com.google.gson.JsonParser
import org.apache.commons.httpclient.NameValuePair
import org.apache.commons.httpclient.methods.GetMethod
import org.apache.commons.httpclient.methods.PostMethod
import org.apache.commons.httpclient.methods.StringRequestEntity
import java.io.InputStreamReader
import java.nio.charset.StandardCharsets


/**
 * Fetches YouTrack issues with issue description formatted from wiki into html on server side.
 */
class IssuesRestClient(override val repository: YouTrackServer) : IssuesRestClientBase, RestClientTrait {

    override fun createDraft(summary: String): String {
        val method = PostMethod("${repository.url}/api/admin/users/me/drafts")
        method.requestEntity = StringRequestEntity(summary, "application/json", StandardCharsets.UTF_8.name())

        return method.connect {
            val status = httpClient.executeMethod(method)
            if (status == 200) {
                val stream = InputStreamReader(method.responseBodyAsLoggedStream(), "UTF-8")
                JsonParser.parseReader(stream).asJsonObject.get("id").asString
            } else {
                throw RuntimeException(method.responseBodyAsLoggedString())
            }
        }
    }

    override fun getIssue(id: String): Issue? {
        val method = GetMethod("${repository.url}/api/issues/$id")
        val fields = NameValuePair("fields", "id,idReadable,updated,created," +
                "tags(color(foreground,background),name),project,links(value,direction,issues(idReadable)," +
                "linkType(name,sourceToTarget,targetToSource),id),comments(id,text,created,updated," +
                "author(name,%20authorFullName,login)),summary,wikifiedDescription,customFields(name,value(name)," +
                "id,projectCustomField),resolved,attachments(name,url),description,reporter(login)")
        method.setQueryString(arrayOf(fields))
        return method.execute { IssueJsonParser.parseIssue(it, repository.url) }
    }

    private fun parseIssues(method: GetMethod): MutableList<Issue> {
        return method.connect {
            val list: MutableList<Issue> = mutableListOf()
            val status = httpClient.executeMethod(method)
            val json: JsonArray = JsonParser.parseString(method.responseBodyAsString) as JsonArray

            json.map { list.add(IssueParser().parseIssue(it.asJsonObject, repository.url)) }

            if (status == 200) {
                list
            } else {
                throw RuntimeException(method.responseBodyAsLoggedString())
            }
        }
    }

    private fun parseWorkItems(method: GetMethod, issues: MutableList<Issue>): MutableList<Issue> {<commit_msg>added support for the specific issue work items<commit_after>package com.github.jk1.ytplugin.rest

import com.github.jk1.ytplugin.issues.model.Issue
import com.github.jk1.ytplugin.issues.model.IssueWorkItem
import com.github.jk1.ytplugin.tasks.YouTrackServer
import com.google.gson.JsonArray
import com.google.gson.JsonParser
import org.apache.commons.httpclient.NameValuePair
import org.apache.commons.httpclient.methods.GetMethod
import org.apache.commons.httpclient.methods.PostMethod
import org.apache.commons.httpclient.methods.StringRequestEntity
import java.io.InputStreamReader
import java.nio.charset.StandardCharsets


/**
 * Fetches YouTrack issues with issue description formatted from wiki into html on server side.
 */
class IssuesRestClient(override val repository: YouTrackServer) : IssuesRestClientBase, RestClientTrait {

    override fun createDraft(summary: String): String {
        val method = PostMethod("${repository.url}/api/admin/users/me/drafts")
        method.requestEntity = StringRequestEntity(summary, "application/json", StandardCharsets.UTF_8.name())

        return method.connect {
            val status = httpClient.executeMethod(method)
            if (status == 200) {
                val stream = InputStreamReader(method.responseBodyAsLoggedStream(), "UTF-8")
                JsonParser.parseReader(stream).asJsonObject.get("id").asString
            } else {
                throw RuntimeException(method.responseBodyAsLoggedString())
            }
        }
    }

    override fun getIssue(id: String): Issue? {
        val method = GetMethod("${repository.url}/api/issues/$id")
        val fields = NameValuePair("fields", "id,idReadable,updated,created," +
                "tags(color(foreground,background),name),project,links(value,direction,issues(idReadable)," +
                "linkType(name,sourceToTarget,targetToSource),id),comments(id,text,created,updated," +
                "author(name,%20authorFullName,login)),summary,wikifiedDescription,customFields(name,value(name)," +
                "id,projectCustomField),resolved,attachments(name,url),description,reporter(login)")
        method.setQueryString(arrayOf(fields))
        val issue = method.execute { IssueJsonParser.parseIssue(it, repository.url) }

        return if (issue != null)
            getWorkItemsForIssues(issue.issueId, mutableListOf(issue))[0]
        else
            null
    }

    private fun parseIssues(method: GetMethod): MutableList<Issue> {
        return method.connect {
            val list: MutableList<Issue> = mutableListOf()
            val status = httpClient.executeMethod(method)
            val json: JsonArray = JsonParser.parseString(method.responseBodyAsString) as JsonArray

            json.map { list.add(IssueParser().parseIssue(it.asJsonObject, repository.url)) }

            if (status == 200) {
                list
            } else {
                throw RuntimeException(method.responseBodyAsLoggedString())
            }
        }
    }

    private fun parseWorkItems(method: GetMethod, issues: MutableList<Issue>): MutableList<Issue> {
{'fraction_non_alphanumeric': 0.1058297449486585, 'fraction_numerical': 0.004802914872474329, 'mean_word_length': 3.3729181752353368, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, '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': '257406', 'n_tokens_mistral': 1688, 'n_tokens_neox': 1606, 'n_words': 278}