<commit_before>import {PullRequestTrigger, MergeErrorTrigger} from "../logic/triggers";

var React = require("react");
var Link = require("react-router").Link;
var CommitListView = require("../components/commit-list-view");
var EmailPicker = require("../components/email-picker");
var TriggerMixin = require("../logic/triggers").TriggerMixin([
    MergeErrorTrigger, PullRequestTrigger,
]);

const SQUASH_REWRITING_HISTORY = "Squash rewriting history";
const SQUASH_MERGE = "Squash leaving PR closed not merged";
const MERGE_WITHOUT_SQUASH = "Merge without squashing";

module.exports = React.createClass({displayName: 'MergePage',
    mixins: [TriggerMixin],

    componentDidMount: function() {
        this.setTrigger(
            new PullRequestTrigger(this.props.controller.getPullRequest())
        );
    },

    getInitialState: function() {
        // TODO: Also do this when re-selecting the tab
        return {
            flowStage: "selectMergeKind",
            mergeKind: null,
            commitMessage: null,
            email: null
        };
    },

    promptForCommitMessage: function(event) {
        var pr = this.getTrigger(PullRequestTrigger).pr;

        var commitMessage = pr.getTitle();
        if (pr.getBody()) {
            commitMessage += `\n\n${pr.getBody()}`;
        }

        if (event.target.textContent === SQUASH_MERGE) {
            commitMessage += `\n\nSquash-merged from pull request #${pr.getId()}`;
        }
        else if (event.target.textContent === MERGE_WITHOUT_SQUASH) {
            commitMessage = `Merge pull request #${pr.getId()} from ` +
                `${pr.getSource().getUser().name}/${pr.getSource().getRef()}`;
        }

        this.setState({
            flowStage: "wantCommitMessage",
            mergeKind: event.target.textContent,
            commitMessage: commitMessage
        });
    },

    doMerge: function() {
        this.setState({flowStage: "merging"});
        var pr = this.getTrigger(PullRequestTrigger).pr;
        if (this.state.mergeKind === SQUASH_REWRITING_HISTORY) {
            this.props.controller.squashMergeWithRewrite(
                pr, this.state.commitMessage, this.state.email
            );
        }
        else if (this.state.mergeKind === SQUASH_MERGE) {
            this.props.controller.squashMergeWithoutRewrite(
                pr, this.state.commitMessage, this.state.email
            );
        }
        else if (this.state.mergeKind === MERGE_WITHOUT_SQUASH) {
            this.props.controller.merge(pr, this.state.commitMessage);
        }
    },

    gotMergeError: function() {
        var error = this.getTrigger(MergeErrorTrigger).error;
        return error && Object.keys(error).length !== 0;
    },

    onChangeText: function(event) {
        this.setState({commitMessage: event.target.value});
    },

    close: function() {
        this.props.controller.close(this.getTrigger(PullRequestTrigger).pr);
    },

    open: function() {
        this.props.controller.open(this.getTrigger(PullRequestTrigger).pr);
    },

    onSelectEmail: function(email) {
        this.setState({email: email});
    },

    render: function() {
        var pr = this.getTrigger(PullRequestTrigger).pr;
        if (!pr || Object.keys(pr).length === 0) {
            return <div className="MergeOptionContainer">Loading pull request...</div>;
        }
        if (!this.props.sessionStore.getAccessToken()) {
            return (
                <div className="MergeOptionContainer">
                    You must be <Link to="/">logged in</Link> to merge
                </div>
            );
        }

        if (pr.getState() === "merged") {
            return (
                <div className="MergeOptionContainer">PR has been merged</div>
            );
        }
        var SPACE = " "; // It would be really nice if JSX had a nicer way to force a space.
        if (pr.getState() === "closed") {
            return (
                <div className="MergeOptionContainer">
                    PR has been closed -{SPACE}
                    <span onClick={this.open} className="OpenButton">Re-open</span>
                </div>
            );
        }
        var mergeable = pr.getMergeable();
        if (mergeable === null) {
            return (
                <div className="MergeOptionContainer">
                    Mergeable state unknown; check again soon
                </div>
            );
        }
        if (!mergeable) {
            return (
                <div className="MergeOptionContainer">
                PR is not mergeable;
                try merging {pr.getDest().getRef()} out into {pr.getSource().getRef()}.
                </div>
            );
        }
        if (this.state.flowStage === "selectMergeKind") {
            return (
                <div className="MergeOptionContainer">
                    <span onClick={this.promptForCommitMessage} className="MergeButton_mergable">
                        {SQUASH_REWRITING_HISTORY}<commit_msg>Show close button on un-mergeable PRs<commit_after>import {PullRequestTrigger, MergeErrorTrigger} from "../logic/triggers";

var React = require("react");
var Link = require("react-router").Link;
var CommitListView = require("../components/commit-list-view");
var EmailPicker = require("../components/email-picker");
var TriggerMixin = require("../logic/triggers").TriggerMixin([
    MergeErrorTrigger, PullRequestTrigger,
]);

const SQUASH_REWRITING_HISTORY = "Squash rewriting history";
const SQUASH_MERGE = "Squash leaving PR closed not merged";
const MERGE_WITHOUT_SQUASH = "Merge without squashing";

module.exports = React.createClass({displayName: 'MergePage',
    mixins: [TriggerMixin],

    componentDidMount: function() {
        this.setTrigger(
            new PullRequestTrigger(this.props.controller.getPullRequest())
        );
    },

    getInitialState: function() {
        // TODO: Also do this when re-selecting the tab
        return {
            flowStage: "selectMergeKind",
            mergeKind: null,
            commitMessage: null,
            email: null
        };
    },

    promptForCommitMessage: function(event) {
        var pr = this.getTrigger(PullRequestTrigger).pr;

        var commitMessage = pr.getTitle();
        if (pr.getBody()) {
            commitMessage += `\n\n${pr.getBody()}`;
        }

        if (event.target.textContent === SQUASH_MERGE) {
            commitMessage += `\n\nSquash-merged from pull request #${pr.getId()}`;
        }
        else if (event.target.textContent === MERGE_WITHOUT_SQUASH) {
            commitMessage = `Merge pull request #${pr.getId()} from ` +
                `${pr.getSource().getUser().name}/${pr.getSource().getRef()}`;
        }

        this.setState({
            flowStage: "wantCommitMessage",
            mergeKind: event.target.textContent,
            commitMessage: commitMessage
        });
    },

    doMerge: function() {
        this.setState({flowStage: "merging"});
        var pr = this.getTrigger(PullRequestTrigger).pr;
        if (this.state.mergeKind === SQUASH_REWRITING_HISTORY) {
            this.props.controller.squashMergeWithRewrite(
                pr, this.state.commitMessage, this.state.email
            );
        }
        else if (this.state.mergeKind === SQUASH_MERGE) {
            this.props.controller.squashMergeWithoutRewrite(
                pr, this.state.commitMessage, this.state.email
            );
        }
        else if (this.state.mergeKind === MERGE_WITHOUT_SQUASH) {
            this.props.controller.merge(pr, this.state.commitMessage);
        }
    },

    gotMergeError: function() {
        var error = this.getTrigger(MergeErrorTrigger).error;
        return error && Object.keys(error).length !== 0;
    },

    onChangeText: function(event) {
        this.setState({commitMessage: event.target.value});
    },

    close: function() {
        this.props.controller.close(this.getTrigger(PullRequestTrigger).pr);
    },

    open: function() {
        this.props.controller.open(this.getTrigger(PullRequestTrigger).pr);
    },

    onSelectEmail: function(email) {
        this.setState({email: email});
    },

    render: function() {
        var pr = this.getTrigger(PullRequestTrigger).pr;
        if (!pr || Object.keys(pr).length === 0) {
            return <div className="MergeOptionContainer">Loading pull request...</div>;
        }
        if (!this.props.sessionStore.getAccessToken()) {
            return (
                <div className="MergeOptionContainer">
                    You must be <Link to="/">logged in</Link> to merge
                </div>
            );
        }

        if (pr.getState() === "merged") {
            return (
                <div className="MergeOptionContainer">PR has been merged</div>
            );
        }
        var SPACE = " "; // It would be really nice if JSX had a nicer way to force a space.
        if (pr.getState() === "closed") {
            return (
                <div className="MergeOptionContainer">
                    PR has been closed -{SPACE}
                    <span onClick={this.open} className="OpenButton">Re-open</span>
                </div>
            );
        }
        var mergeable = pr.getMergeable();
        if (mergeable === null) {
            return (
                <div className="MergeOptionContainer">
                    Mergeable state unknown; check again soon
                </div>
            );
        }
        if (!mergeable) {
            return (
                <div className="MergeOptionContainer">
                PR is not mergeable;
                try merging {pr.getDest().getRef()} out into {pr.getSource().getRef()}.
                <br />
                <span onClick={this.close} className="CloseButton">
                    Close
                </span>
                </div>
            );
        }
        if (this.state.flowStage === "selectMergeKind") {
            return (
                <div className="MergeOptionContainer">
                    <span onClick={this.promptForCommitMessage} className="MergeButton_mergable">
                        {SQUASH_REWRITING_HISTORY}
{'fraction_non_alphanumeric': 0.12592083292407424, 'fraction_numerical': 0.00039288871427168253, 'mean_word_length': 1.976322712657118, 'pattern_counts': {'":': 0, '<': 42, '<?xml version=': 0, '>': 42, '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': '2596361', 'n_tokens_mistral': 2848, 'n_tokens_neox': 2732, 'n_words': 550}