<commit_before>package com.assembla.jenkins

import groovyx.net.http.RESTClient
import org.apache.http.conn.HttpHostConnectException
import org.apache.http.client.HttpResponseException
import org.apache.http.HttpRequestInterceptor
import org.apache.http.HttpRequest
import org.apache.http.protocol.HttpContext

import groovyx.net.http.ContentType
import groovyx.net.http.HTTPBuilder
import groovyx.net.http.Method

class Api {
    String assemblaServerUrl = "https://api.assembla.com/"
    String apiKey
    String apiSecret
    String spaceId
    String spaceToolId

    protected RESTClient restClient
    protected HttpRequestInterceptor requestInterceptor

    public String getPinUrl() {
        assemblaServerUrl + "authorization?client_id=$clientId&response_type=pin_code"
    }

    // TODO, when space tool API is ready use it to get gitUrl
    public String getGitUrl() {
        def response = get(path:"v1/spaces/$spaceId/space_tools/${spaceToolId}.json")
        //println(response.data)
        return response.data.url
    }

    public void setAssemblaServerUrl(String assemblaServerUrl) {
        if (!assemblaServerUrl.endsWith("/")) assemblaServerUrl += "/"
        this.assemblaServerUrl = assemblaServerUrl
    }

    List<String> getMergeRequestBranchNames() {
        // API response [[target_symbol:master, status:0, source_symbol_type:1, source_symbol:test, space_tool_id:dicbBK8rqr4ycHeJe7dGu1,
        // commit:null, processed_by_user_id:null, apply_status:null, user_id:bgnP_qA1Gr2QjIaaaHk9wZ, applied_at:null,
        // description:Nice try, target_space_tool_id:dicbBK8rqr4ycHeJe7dGu1, updated_at:2012-08-29T05:21:19-07:00, id:155,
        // created_at:2012-08-29T05:21:19-07:00]]

        def response = get(path: "v1/spaces/$spaceId/space_tools/$spaceToolId/merge_requests.json?status=open")
        def result = []

        if (response.status != 204) {
            println "Found ${response.data.size()} MRs"

            result = response.data.collect {
                println "  ${it.source_symbol}"
                it.source_symbol
            }
        }
        else {
            println "There are no MRs..."
        }

        return result
    }

    protected get(Map map) {
        // force json api
        map.put("contentType", ContentType.JSON)
        map.put("headers", [Accept: 'application/json'])

        // get is destructive to the map, if there's an error we want the values around still
        Map mapCopy = map.clone() as Map
        def response

        assert mapCopy.path != null, "'path' is a required attribute for the GET method"

        try {
            initRestClient()<commit_msg>move query string to separate param (gets encoded otherwise)<commit_after>package com.assembla.jenkins

import groovyx.net.http.RESTClient
import org.apache.http.conn.HttpHostConnectException
import org.apache.http.client.HttpResponseException
import org.apache.http.HttpRequestInterceptor
import org.apache.http.HttpRequest
import org.apache.http.protocol.HttpContext

import groovyx.net.http.ContentType
import groovyx.net.http.HTTPBuilder
import groovyx.net.http.Method

class Api {
    String assemblaServerUrl = "https://api.assembla.com/"
    String apiKey
    String apiSecret
    String spaceId
    String spaceToolId

    protected RESTClient restClient
    protected HttpRequestInterceptor requestInterceptor

    public String getPinUrl() {
        assemblaServerUrl + "authorization?client_id=$clientId&response_type=pin_code"
    }

    // TODO, when space tool API is ready use it to get gitUrl
    public String getGitUrl() {
        def response = get(path:"v1/spaces/$spaceId/space_tools/${spaceToolId}.json")
        //println(response.data)
        return response.data.url
    }

    public void setAssemblaServerUrl(String assemblaServerUrl) {
        if (!assemblaServerUrl.endsWith("/")) assemblaServerUrl += "/"
        this.assemblaServerUrl = assemblaServerUrl
    }

    List<String> getMergeRequestBranchNames() {
        // API response [[target_symbol:master, status:0, source_symbol_type:1, source_symbol:test, space_tool_id:dicbBK8rqr4ycHeJe7dGu1,
        // commit:null, processed_by_user_id:null, apply_status:null, user_id:bgnP_qA1Gr2QjIaaaHk9wZ, applied_at:null,
        // description:Nice try, target_space_tool_id:dicbBK8rqr4ycHeJe7dGu1, updated_at:2012-08-29T05:21:19-07:00, id:155,
        // created_at:2012-08-29T05:21:19-07:00]]

        def response = get(path: "v1/spaces/$spaceId/space_tools/$spaceToolId/merge_requests.json", query: [status: "open"])
        def result = []

        if (response.status != 204) {
            println "Found ${response.data.size()} MRs"

            result = response.data.collect {
                println "  ${it.source_symbol}"
                it.source_symbol
            }
        }
        else {
            println "There are no MRs..."
        }

        return result
    }

    protected get(Map map) {
        // force json api
        map.put("contentType", ContentType.JSON)
        map.put("headers", [Accept: 'application/json'])

        // get is destructive to the map, if there's an error we want the values around still
        Map mapCopy = map.clone() as Map
        def response

        assert mapCopy.path != null, "'path' is a required attribute for the GET method"

        try {
            initRestClient()
{'fraction_non_alphanumeric': 0.09880908075921102, 'fraction_numerical': 0.021213248976553776, 'mean_word_length': 2.94640234948605, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 2, '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': '2551534', 'n_tokens_mistral': 1727, 'n_tokens_neox': 1609, 'n_words': 346}