Search in sources :

Example 1 with SimpleAjaxLink

use of com.gitblit.wicket.panels.SimpleAjaxLink in project gitblit by gitblit.

the class TicketPage method createMergePanel.

/**
 * Adds a merge panel for the patchset to the markup container.  The panel
 * may just a message if the patchset can not be merged.
 *
 * @param c
 * @param user
 * @param repository
 */
protected Component createMergePanel(UserModel user, RepositoryModel repository) {
    Patchset patchset = ticket.getCurrentPatchset();
    if (patchset == null) {
        // no patchset to merge
        return new Label("mergePanel");
    }
    boolean allowMerge;
    if (repository.requireApproval) {
        // repository requires approval
        allowMerge = ticket.isOpen() && ticket.isApproved(patchset);
    } else {
        // vetoes are binding
        allowMerge = ticket.isOpen() && !ticket.isVetoed(patchset);
    }
    MergeStatus mergeStatus = JGitUtils.canMerge(getRepository(), patchset.tip, ticket.mergeTo, repository.mergeType);
    if (allowMerge) {
        if (MergeStatus.MERGEABLE == mergeStatus) {
            // patchset can be cleanly merged to integration branch OR has already been merged
            Fragment mergePanel = new Fragment("mergePanel", "mergeableFragment", this);
            mergePanel.add(new Label("mergeTitle", MessageFormat.format(getString("gb.patchsetMergeable"), ticket.mergeTo)));
            if (user.canPush(repository)) {
                // user can merge locally
                SimpleAjaxLink<String> mergeButton = new SimpleAjaxLink<String>("mergeButton", Model.of(getString("gb.merge"))) {

                    private static final long serialVersionUID = 1L;

                    @Override
                    public void onClick(AjaxRequestTarget target) {
                        // ensure the patchset is still current AND not vetoed
                        Patchset patchset = ticket.getCurrentPatchset();
                        final TicketModel refreshedTicket = app().tickets().getTicket(getRepositoryModel(), ticket.number);
                        if (patchset.equals(refreshedTicket.getCurrentPatchset())) {
                            // patchset is current, check for recent veto
                            if (!refreshedTicket.isVetoed(patchset)) {
                                // patchset is not vetoed
                                // execute the merge using the ticket service
                                app().tickets().exec(new Runnable() {

                                    @Override
                                    public void run() {
                                        PatchsetReceivePack rp = new PatchsetReceivePack(app().gitblit(), getRepository(), getRepositoryModel(), GitBlitWebSession.get().getUser());
                                        MergeStatus result = rp.merge(refreshedTicket);
                                        if (MergeStatus.MERGED == result) {
                                            // notify participants and watchers
                                            rp.sendAll();
                                        } else {
                                            // merge failure
                                            String msg = MessageFormat.format("Failed to merge ticket {0,number,0}: {1}", ticket.number, result.name());
                                            logger().error(msg);
                                            GitBlitWebSession.get().cacheErrorMessage(msg);
                                        }
                                    }
                                });
                            } else {
                                // vetoed patchset
                                String msg = MessageFormat.format("Can not merge ticket {0,number,0}, patchset {1,number,0} has been vetoed!", ticket.number, patchset.number);
                                GitBlitWebSession.get().cacheErrorMessage(msg);
                                logger().error(msg);
                            }
                        } else {
                            // not current patchset
                            String msg = MessageFormat.format("Can not merge ticket {0,number,0}, the patchset has been updated!", ticket.number);
                            GitBlitWebSession.get().cacheErrorMessage(msg);
                            logger().error(msg);
                        }
                        redirectTo(TicketsPage.class, getPageParameters());
                    }
                };
                mergePanel.add(mergeButton);
                Component instructions = getMergeInstructions(user, repository, "mergeMore", "gb.patchsetMergeableMore");
                mergePanel.add(instructions);
            } else {
                mergePanel.add(new Label("mergeButton").setVisible(false));
                mergePanel.add(new Label("mergeMore").setVisible(false));
            }
            return mergePanel;
        } else if (MergeStatus.ALREADY_MERGED == mergeStatus) {
            // patchset already merged
            Fragment mergePanel = new Fragment("mergePanel", "alreadyMergedFragment", this);
            mergePanel.add(new Label("mergeTitle", MessageFormat.format(getString("gb.patchsetAlreadyMerged"), ticket.mergeTo)));
            return mergePanel;
        } else if (MergeStatus.MISSING_INTEGRATION_BRANCH == mergeStatus) {
            // target/integration branch is missing
            Fragment mergePanel = new Fragment("mergePanel", "notMergeableFragment", this);
            mergePanel.add(new Label("mergeTitle", MessageFormat.format(getString("gb.patchsetNotMergeable"), ticket.mergeTo)));
            mergePanel.add(new Label("mergeMore", MessageFormat.format(getString("gb.missingIntegrationBranchMore"), ticket.mergeTo)));
            return mergePanel;
        } else {
            // patchset can not be cleanly merged
            Fragment mergePanel = new Fragment("mergePanel", "notMergeableFragment", this);
            mergePanel.add(new Label("mergeTitle", MessageFormat.format(getString("gb.patchsetNotMergeable"), ticket.mergeTo)));
            if (user.canPush(repository)) {
                // user can merge locally
                Component instructions = getMergeInstructions(user, repository, "mergeMore", "gb.patchsetNotMergeableMore");
                mergePanel.add(instructions);
            } else {
                mergePanel.add(new Label("mergeMore").setVisible(false));
            }
            return mergePanel;
        }
    } else {
        // merge not allowed
        if (MergeStatus.ALREADY_MERGED == mergeStatus) {
            // patchset already merged
            Fragment mergePanel = new Fragment("mergePanel", "alreadyMergedFragment", this);
            mergePanel.add(new Label("mergeTitle", MessageFormat.format(getString("gb.patchsetAlreadyMerged"), ticket.mergeTo)));
            return mergePanel;
        } else if (ticket.isVetoed(patchset)) {
            // patchset has been vetoed
            Fragment mergePanel = new Fragment("mergePanel", "vetoedFragment", this);
            mergePanel.add(new Label("mergeTitle", MessageFormat.format(getString("gb.patchsetNotMergeable"), ticket.mergeTo)));
            return mergePanel;
        } else if (repository.requireApproval) {
            // patchset has been not been approved for merge
            Fragment mergePanel = new Fragment("mergePanel", "notApprovedFragment", this);
            mergePanel.add(new Label("mergeTitle", MessageFormat.format(getString("gb.patchsetNotApproved"), ticket.mergeTo)));
            mergePanel.add(new Label("mergeMore", MessageFormat.format(getString("gb.patchsetNotApprovedMore"), ticket.mergeTo)));
            return mergePanel;
        } else {
            // other case
            return new Label("mergePanel");
        }
    }
}
Also used : PatchsetReceivePack(com.gitblit.git.PatchsetReceivePack) Label(org.apache.wicket.markup.html.basic.Label) TicketLabel(com.gitblit.tickets.TicketLabel) Patchset(com.gitblit.models.TicketModel.Patchset) TicketModel(com.gitblit.models.TicketModel) Fragment(org.apache.wicket.markup.html.panel.Fragment) AjaxRequestTarget(org.apache.wicket.ajax.AjaxRequestTarget) SimpleAjaxLink(com.gitblit.wicket.panels.SimpleAjaxLink) MergeStatus(com.gitblit.utils.JGitUtils.MergeStatus) ShockWaveComponent(com.gitblit.wicket.panels.ShockWaveComponent) Component(org.apache.wicket.Component)

Aggregations

PatchsetReceivePack (com.gitblit.git.PatchsetReceivePack)1 TicketModel (com.gitblit.models.TicketModel)1 Patchset (com.gitblit.models.TicketModel.Patchset)1 TicketLabel (com.gitblit.tickets.TicketLabel)1 MergeStatus (com.gitblit.utils.JGitUtils.MergeStatus)1 ShockWaveComponent (com.gitblit.wicket.panels.ShockWaveComponent)1 SimpleAjaxLink (com.gitblit.wicket.panels.SimpleAjaxLink)1 Component (org.apache.wicket.Component)1 AjaxRequestTarget (org.apache.wicket.ajax.AjaxRequestTarget)1 Label (org.apache.wicket.markup.html.basic.Label)1 Fragment (org.apache.wicket.markup.html.panel.Fragment)1