<commit_before>const sha = require("crypto").createHash.bind(null, "sha1");
const create = Object.create;
const define = Object.defineProperties;
const freeze = Object.freeze;
const JitRub$internal = new WeakMap();

/**
 * Create Jira-GitHub sync.
 * @param {Jira} jira
 * @param {GitHub} github
 * @returns {JitRub}
 */
function jitrub(jira, github) {
    jitrub = create(JitRub);

    JitRub$internal.set(jitrub, {
        jira: jira,
        github: github,
        project: "",
        base: "master",
        head: "jitrub",
        statuses: new Set()
    });

    return jitrub;
}

const JitRub = {
    /**
     * Sync GitHub build branch with Jira issue feature branches.
     * @returns {Promise}
     */
    sync: function() {
        var project = this.project,
            statuses = this.statuses;

        return this.jira.issues(project, statuses).then(issues => {
            var branches;

            issues = issues.map(issue => issue.key);
            issues.sort();
            branches = issues.map(issue => this.github.branch(issue));

            return this.github.branch(this.base).then(base => {
                if (!base) throw new Error(`no '${this.base}' base branch`);
                return Promise.all(branches).then(branches => {
                    return [base].concat(branches.filter(b => b));
                });
            });
        }).then(features => {
            var tag, hash = sha();
            features.forEach(branch => hash.update(branch.commit.sha));
            tag = "jitrub-" + hash.digest("hex");
            return tag;
        });
    },

    /**
     * Select Jira project from which tickets will be synced.
     * @param {string} projectCode
     */
    selectProject: function(projectCode) {
        projectCode = projectCode ? String(projectCode) : "";
        JitRub$internal.get(this).project = projectCode;
    },

    /**
     * Select base branch to start with when merging features into the head
     * branch.
     * @param {string} baseBranch<commit_msg>implement basic merge strategy in JitRub<commit_after>const sha = require("crypto").createHash.bind(null, "sha1");
const create = Object.create;
const define = Object.defineProperties;
const freeze = Object.freeze;
const JitRub$internal = new WeakMap();

/**
 * Create Jira-GitHub sync.
 * @param {Jira} jira
 * @param {GitHub} github
 * @returns {JitRub}
 */
function jitrub(jira, github) {
    jitrub = create(JitRub);

    JitRub$internal.set(jitrub, {
        jira: jira,
        github: github,
        project: "",
        base: "master",
        head: "jitrub",
        statuses: new Set()
    });

    return jitrub;
}

const JitRub = {
    /**
     * Sync GitHub build branch with Jira issue feature branches.
     * @returns {Promise}
     */
    sync: function() {
        var project = this.project,
            statuses = this.statuses;

        return this.jira.issues(project, statuses).then(issues => {
            var branches;

            issues = issues.map(issue => issue.key);
            issues.sort();
            branches = issues.map(issue => this.github.branch(issue));

            return this.github.branch(this.base).then(base => {
                if (!base) throw new Error(`no '${this.base}' base branch`);
                return Promise.all(branches).then(branches => {
                    return [base].concat(branches.filter(b => b));
                });
            });
        }).then(features => {
            var tag, hash = sha();

            features.forEach(branch => hash.update(branch.commit.sha));
            tag = "jitrub-" + hash.digest("hex");

            return this.github.tag(tag).then(tag => {
                if (tag) return tag.name;

                return this.github.lock(this.head)
                    .then(() => this.github.reset(this.head, this.base))
                    .then(() => this.github.merge(this.head, features))
                    .then(() => this.github.createTag(tag, this.head))
                    .catch(err => err)
                    .then(result => this.github.unlock(this.head).then(() => {
                        if (result instanceof Error) throw result;
                        return result;
                    }));
            });
        });
    },

    /**
     * Select Jira project from which tickets will be synced.
     * @param {string} projectCode
     */
    selectProject: function(projectCode) {
        projectCode = projectCode ? String(projectCode) : "";
        JitRub$internal.get(this).project = projectCode;
    },

    /**
     * Select base branch to start with when merging features into the head
     * branch.
     * @param {string} baseBranch
{'fraction_non_alphanumeric': 0.13642186165670367, 'fraction_numerical': 0.0004269854824935952, 'mean_word_length': 1.9595704358812382, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 26, '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': '4281871', 'n_tokens_mistral': 1374, 'n_tokens_neox': 1311, 'n_words': 315}