<commit_before>//
//  JIRAIssue.swift
//  SwiftyJIRA
//
//  Created by <NAME> on 8/31/14.
//  Copyright (c) 2014 Hathway. All rights reserved.
//

import Foundation
import SwiftyJSON


//protocol IssueType {
//
//    var issueId: String { get set }
//    var issueKey: String { get set }
//    var summary: String { get set }
//    var issueType: JIRAIssueType { get set }
//    var status: String { get set }
//
//}

//public struct JIRAIssue: IssueType {
public class JIRAIssue {

    var issueId: String?
    var issueKey: String?
    var project: JIRAProject?

    var summary: String
    var issueType: JIRAIssueType

    var status: String?

    // TODO: Simplify parent to avoid grand-parent recursion
    var parent: JIRAIssue?

    init(summary: String, issueType: JIRAIssueType) {
        self.summary = summary
        self.issueType = issueType
    }

    init(json: JSON) {
        issueId = json["id"].stringValue
        issueKey = json["key"].stringValue
        issueType = JIRAIssueType(json: json["fields"]["issuetype"])
        summary = json["fields"]["summary"].stringValue
        status = json["fields"]["status"]["name"].stringValue

        if json["fields"]["project"] != JSON.null {
            project = JIRAProject(json: json["fields"]["project"])
        }

        if json["fields"]["parent"] != JSON.null {
            parent = JIRAIssue(json: json["fields"]["parent"])
        }
    }

}


// MARK: Active Record (Query)

public extension JIRAIssue {

    public static func issue(issueKey: String) -> JIRAIssue? {
        let result = JIRASession.sharedSession.get("issue/\(issueKey)", params: JIRAQueryParams())
        if let data = result.data {
            print(data)
            return JIRAIssue(json: JSON(data))
        }
        return nil
    }

    public static func search(query: String) -> [JIRAIssue] {
        let payload: JIRAPayload = [
            "jql": query,
            "startAt": 0,
            "maxResults": 100
        ]
        let result = JIRASession.sharedSession.post("search", params: JIRAQueryParams(),
            payload: payload)
        let json = JSON(result.data ?? [])
        return json["issues"].arrayValue.map { JIRAIssue(json: $0) }
    }

}


// MARK: Active Record (Instance)

public extension JIRAIssue {

    internal var asDictionary: [String: AnyObject?] {
        return [
            "summary": summary,
            "issuetype": issueType.asDictionary as? AnyObject,
            "project": project?.asDictionary as? AnyObject
        ]
    }

    public func save() {
        let payload = [
            "fields": asDictionary
        ]
        if issueKey == nil {
            let result = JIRASession.sharedSession.post("issue", payload: payload as? JIRAPayload)
            print(result)
        } else {
            let result = JIRASession.sharedSession.put("issue/\(issueKey)",
                payload: payload as? JIRAPayload)
            print(result)
        }
    }

}
<commit_msg>Load ticket resolution<commit_after>//
//  JIRAIssue.swift
//  SwiftyJIRA
//
//  Created by <NAME> on 8/31/14.
//  Copyright (c) 2014 Hathway. All rights reserved.
//

import Foundation
import SwiftyJSON


//protocol IssueType {
//
//    var issueId: String { get set }
//    var issueKey: String { get set }
//    var summary: String { get set }
//    var issueType: JIRAIssueType { get set }
//    var status: String { get set }
//
//}

//public struct JIRAIssue: IssueType {
public class JIRAIssue {

    public var issueId: String?
    public var issueKey: String?
    public var project: JIRAProject?

    public var summary: String
    public var issueType: JIRAIssueType

    public var status: String?
    public var resolution: String?

    // TODO: Simplify parent to avoid grand-parent recursion
    public var parent: JIRAIssue?

    public init(summary: String, issueType: JIRAIssueType) {
        self.summary = summary
        self.issueType = issueType
    }

    init(json: JSON) {
        issueId = json["id"].stringValue
        issueKey = json["key"].stringValue
        issueType = JIRAIssueType(json: json["fields"]["issuetype"])
        summary = json["fields"]["summary"].stringValue
        status = json["fields"]["status"]["name"].stringValue
        resolution = json["fields"]["resolution"]["name"].stringValue

        if json["fields"]["project"] != JSON.null {
            project = JIRAProject(json: json["fields"]["project"])
        }

        if json["fields"]["parent"] != JSON.null {
            parent = JIRAIssue(json: json["fields"]["parent"])
        }
    }

}


// MARK: Active Record (Query)

public extension JIRAIssue {

    public static func issue(issueKey: String) -> JIRAIssue? {
        let result = JIRASession.sharedSession.get("issue/\(issueKey)", params: JIRAQueryParams())
        if let data = result.data {
            print(data)
            return JIRAIssue(json: JSON(data))
        }
        return nil
    }

    public static func search(query: String) -> [JIRAIssue] {
        let payload: JIRAPayload = [
            "jql": query,
            "startAt": 0,
            "maxResults": 100
        ]
        let result = JIRASession.sharedSession.post("search", params: JIRAQueryParams(),
            payload: payload)
        let json = JSON(result.data ?? [])
        return json["issues"].arrayValue.map { JIRAIssue(json: $0) }
    }

}


// MARK: Active Record (Instance)

public extension JIRAIssue {

    internal var asDictionary: [String: AnyObject?] {
        return [
            "summary": summary,
            "issuetype": issueType.asDictionary as? AnyObject,
            "project": project?.asDictionary as? AnyObject
        ]
    }

    public func save() {
        let payload = [
            "fields": asDictionary
        ]
        if issueKey == nil {
            let result = JIRASession.sharedSession.post("issue", payload: payload as? JIRAPayload)
            print(result)
        } else {
            let result = JIRASession.sharedSession.put("issue/\(issueKey)",
                payload: payload as? JIRAPayload)
            print(result)
        }
    }

}

{'fraction_non_alphanumeric': 0.12402088772845953, 'fraction_numerical': 0.0045691906005221935, 'mean_word_length': 2.2308908803373746, 'pattern_counts': {'":': 14, '<': 5, '<?xml version=': 0, '>': 9, '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': '478041', 'n_tokens_mistral': 1872, 'n_tokens_neox': 1838, 'n_words': 458}