<commit_before>var request = require('request');


var Redmine = function(config) {
    this.config = config;
    this.statuses = null;
    this.users = {};
    this.projects = {};
    this.mergeRequestField = null;

    this.clearCache = function(config) {
        this.statuses = null;
        this.users = {};
        this.projects = {};
        this.mergeRequestField = null;
    };

    this.getMergeRequestField = function(cb) {
        if(this.mergeRequestField) {
            cb(null, this.mergeRequestField);
        } else {
            //TODO
        }
    };

    this.getUser = function(name, cb) {
        if(name in this.users) {
            cb(null, this.users[name]);
        } else {
            var self = this;

            request.request({
                url: this.config.url + "/users.json",
                method: "GET",
                json: true,
                body: {
                    key: this.config.api_key,
                    name: name
                }
            }, function(err, res, body) {
                if(err) {
                    cb(err);
                } else {
                    //TODO
                }
            });
        }
    };

    this.getStatus = function(name, cb) {
        var key = name.toLowerCase();
        if(this.statuses == null) {
            request.request({
                url: this.config.url + "/issues_statuses.json",
                method: "GET",
                json: true,
                body: {
                    key: this.config.api_key
                }
            }, function(err, res, body) {
                if(err) {
                    cb(err);
                } else {
                    //TODO
                }
            });
        } else {
            if(key in this.statuses) {
                cb(null, this.statuses[key]);
            } else {
                cb("status does not exist");
            }
        }
    };

    this.getRedmineProject = function(name, cb) {
        if(name in this.projects) {
            cb(null, this.projects[name]);
        } else {
            //TODO
        }
    };

    this.updateIssue = function(id, body, impersonate) {
        //TODO
    };
};

<commit_msg>querying users, status, and custom fields works correctly<commit_after>var request = require('request');
var fs = require('fs');
var util = require('util');
var url = require('url');


function Redmine(config) {
    this.config = config;
    this.statuses = null;
    this.users = {};
    this.projects = {};
    this.mergeRequestField = null;
    this.request = null;

    if(config.ssl_ca) {
        this.request = request.defaults({
            agentOptions: {
                ca: fs.readFileSync(this.config.ssl_ca)
            },
            json: true
        });
    } else {
        this.request = request.defaults({
            json: true
        });
    }

    this.clearCache = function(config) {
        this.statuses = null;
        this.users = {};
        this.projects = {};
        this.mergeRequestField = null;
    };

    this.checkResponse = function(err, res, cb) {
        if(err) {
            cb(err);
            return false;
        } else if(res.statusCode != 200) {
            cb("http error code " + res.statusCode.toString());
            return false;
        } else {
            return true;
        }
    };

    this.getMergeRequestField = function(cb) {
        var self = this;

        var ret = function() {
            if(self.mergeRequestField) {
                cb(null, self.mergeRequestField);
            } else {
                cb("merge request field does not exist");
            }
        };

        if(this.mergeRequestField) {
            ret();
        } else {
            var self = this;

            this.request({
                url: url.resolve(this.config.url, "/custom_fields.json"),
                method: "GET",
                body: {
                    key: this.config.api_key
                }
            }, function(err, res, body) {
                if(self.checkResponse(err, res, cb)) {
                    for(var i in body.custom_fields) {
                        var field = body.custom_fields[i];
                        if(field.name.toLowerCase() == "merge request") {
                            self.mergeRequestField = field;
                            break;
                        }
                    }
                    ret();
                }
            });
        }
    };

    this.getUser = function(name, cb) {
        if(name in this.users) {
            cb(null, this.users[name]);
        } else {
            var self = this;

            this.request({
                url: url.resolve(this.config.url, "/users.json"),
                method: "GET",
                body: {
                    key: this.config.api_key,
                    name: name
                }
            }, function(err, res, body) {
                if(self.checkResponse(err, res, cb)) {
                    if(body.total_count == 0) {
                        cb("user does not exist");
                    } else {
                        self.users[name] = body.users[0];
                        cb(null, body.users[0]);
                    }
                }
            });
        }
    };

    this.getStatus = function(name, cb) {
        var key = name.toLowerCase();
        var self = this;

        var ret = function() {
            if(key in self.statuses) {
                cb(null, self.statuses[key]);
            } else {
                console.log(self.statuses);
                cb("status does not exist");
            }
        };

        if(this.statuses == null) {
            this.request({
                uri: url.resolve(this.config.url, "/issue_statuses.json"),
                method: "GET",
                body: {
                    key: this.config.api_key
                }
            }, function(err, res, body) {
                if(self.checkResponse(err, res, cb)) {
                    self.statuses = {};
                    for(var i in body.issue_statuses) {
                        var status = body.issue_statuses[i];
                        status.key = status.name.toLowerCase();
                        self.statuses[status.key] = status;
                    }
                    ret();
                }
            });
        } else {
            ret();
        }
    };

    this.getRedmineProject = function(name, cb) {
        if(name in this.projects) {
            cb(null, this.projects[name]);
        } else {
            //TODO
        }
    };

    this.updateIssue = function(id, body, impersonate) {
        var req = {
            url: url.resolve(this.config.url , "/issue/" + id + ".json"),
            method: "PUT",
            body: _.defaults({}, body, {key: this.config.api_key})
        };

        if(impersonate) {
            req.headers = {
                "X-Redmine-Switch-User": impersonate
            };
        }

        this.request(req, function(err, res, body) {
            if(this.checkResponse(err, res, cb)) {
                
            }
        });
    };
};

module.exports = Redmine;
{'fraction_non_alphanumeric': 0.12240635009051665, 'fraction_numerical': 0.0008355382258738338, 'mean_word_length': 1.1006142146826559, 'pattern_counts': {'":': 1, '<': 3, '<?xml version=': 0, '>': 3, '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': '7405064', 'n_tokens_mistral': 1990, 'n_tokens_neox': 1893, 'n_words': 374}