Search in sources :

Example 31 with TicketModel

use of com.gitblit.models.TicketModel in project gitblit by gitblit.

the class TicketPage method getTicket.

/**
	 * Returns the ticket (if any) that this commit references.
	 *
	 * @param commit
	 * @return null or a ticket
	 */
protected TicketModel getTicket(RevCommit commit) {
    try {
        Map<String, Ref> refs = getRepository().getRefDatabase().getRefs(Constants.R_TICKETS_PATCHSETS);
        for (Map.Entry<String, Ref> entry : refs.entrySet()) {
            if (entry.getValue().getObjectId().equals(commit.getId())) {
                long id = PatchsetCommand.getTicketNumber(entry.getKey());
                TicketModel ticket = app().tickets().getTicket(getRepositoryModel(), id);
                return ticket;
            }
        }
    } catch (Exception e) {
        logger().error("failed to determine ticket from ref", e);
    }
    return null;
}
Also used : Ref(org.eclipse.jgit.lib.Ref) TicketModel(com.gitblit.models.TicketModel) Map(java.util.Map) RestartResponseException(org.apache.wicket.RestartResponseException) IOException(java.io.IOException)

Example 32 with TicketModel

use of com.gitblit.models.TicketModel in project gitblit by gitblit.

the class TicketPage method createDeletePatchsetLink.

private Link<Void> createDeletePatchsetLink(final RepositoryModel repositoryModel, final Patchset patchset) {
    Link<Void> deleteLink = new Link<Void>("deleteRevision") {

        private static final long serialVersionUID = 1L;

        @Override
        public void onClick() {
            Repository r = app().repositories().getRepository(repositoryModel.name);
            UserModel user = GitBlitWebSession.get().getUser();
            if (r == null) {
                if (app().repositories().isCollectingGarbage(repositoryModel.name)) {
                    error(MessageFormat.format(getString("gb.busyCollectingGarbage"), repositoryModel.name));
                } else {
                    error(MessageFormat.format("Failed to find repository {0}", repositoryModel.name));
                }
                return;
            }
            //Construct the ref name based on the patchset
            String ticketShard = String.format("%02d", ticket.number);
            ticketShard = ticketShard.substring(ticketShard.length() - 2);
            final String refName = String.format("%s%s/%d/%d", Constants.R_TICKETS_PATCHSETS, ticketShard, ticket.number, patchset.number);
            Ref ref = null;
            boolean success = true;
            try {
                ref = r.getRef(refName);
                if (ref != null) {
                    success = JGitUtils.deleteBranchRef(r, ref.getName());
                } else {
                    success = false;
                }
                if (success) {
                    // clear commit cache
                    CommitCache.instance().clear(repositoryModel.name, refName);
                    // optionally update reflog
                    if (RefLogUtils.hasRefLogBranch(r)) {
                        RefLogUtils.deleteRef(user, r, ref);
                    }
                    TicketModel updatedTicket = app().tickets().deletePatchset(ticket, patchset, user.username);
                    if (updatedTicket == null) {
                        success = false;
                    }
                }
            } catch (IOException e) {
                logger().error("failed to determine ticket from ref", e);
                success = false;
            } finally {
                r.close();
            }
            if (success) {
                getSession().info(MessageFormat.format(getString("gb.deletePatchsetSuccess"), patchset.number));
                logger().info(MessageFormat.format("{0} deleted patchset {1} from ticket {2}", user.username, patchset.number, ticket.number));
            } else {
                getSession().error(MessageFormat.format(getString("gb.deletePatchsetFailure"), patchset.number));
            }
            //Force reload of the page to rebuild ticket change cache
            String relativeUrl = urlFor(TicketsPage.class, getPageParameters()).toString();
            String absoluteUrl = RequestUtils.toAbsolutePath(relativeUrl);
            setResponsePage(new RedirectPage(absoluteUrl));
        }
    };
    WicketUtils.setHtmlTooltip(deleteLink, MessageFormat.format(getString("gb.deletePatchset"), patchset.number));
    deleteLink.add(new JavascriptEventConfirmation("onclick", MessageFormat.format(getString("gb.deletePatchset"), patchset.number)));
    return deleteLink;
}
Also used : TicketModel(com.gitblit.models.TicketModel) IOException(java.io.IOException) JavascriptEventConfirmation(com.gitblit.wicket.panels.BasePanel.JavascriptEventConfirmation) UserModel(com.gitblit.models.UserModel) Repository(org.eclipse.jgit.lib.Repository) Ref(org.eclipse.jgit.lib.Ref) RedirectPage(org.apache.wicket.markup.html.pages.RedirectPage) ExternalLink(org.apache.wicket.markup.html.link.ExternalLink) BookmarkablePageLink(org.apache.wicket.markup.html.link.BookmarkablePageLink) SimpleAjaxLink(com.gitblit.wicket.panels.SimpleAjaxLink) Link(org.apache.wicket.markup.html.link.Link) IconAjaxLink(com.gitblit.wicket.panels.IconAjaxLink)

Example 33 with TicketModel

use of com.gitblit.models.TicketModel in project gitblit by gitblit.

the class TicketPage method review.

protected void review(Score score) {
    UserModel user = GitBlitWebSession.get().getUser();
    Patchset ps = ticket.getCurrentPatchset();
    Change change = new Change(user.username);
    change.review(ps, score, !ticket.isReviewer(user.username));
    if (!ticket.isWatching(user.username)) {
        change.watch(user.username);
    }
    TicketModel updatedTicket = app().tickets().updateTicket(getRepositoryModel(), ticket.number, change);
    app().tickets().createNotifier().sendMailing(updatedTicket);
    redirectTo(TicketsPage.class, getPageParameters());
}
Also used : UserModel(com.gitblit.models.UserModel) Patchset(com.gitblit.models.TicketModel.Patchset) TicketModel(com.gitblit.models.TicketModel) Change(com.gitblit.models.TicketModel.Change)

Example 34 with TicketModel

use of com.gitblit.models.TicketModel in project gitblit by gitblit.

the class CommentPanel method onInitialize.

@Override
protected void onInitialize() {
    super.onInitialize();
    Form<String> form = new Form<String>("editorForm");
    add(form);
    form.add(new AjaxButton("submit", new Model<String>(getString("gb.comment")), form) {

        private static final long serialVersionUID = 1L;

        @Override
        public void onSubmit(AjaxRequestTarget target, Form<?> form) {
            String txt = markdownEditor.getText();
            if (change == null) {
                // new comment
                Change newComment = new Change(user.username);
                newComment.comment(txt);
                if (!ticket.isWatching(user.username)) {
                    newComment.watch(user.username);
                }
                RepositoryModel repository = app().repositories().getRepositoryModel(ticket.repository);
                TicketModel updatedTicket = app().tickets().updateTicket(repository, ticket.number, newComment);
                if (updatedTicket != null) {
                    app().tickets().createNotifier().sendMailing(updatedTicket);
                    redirectTo(pageClass, WicketUtils.newObjectParameter(updatedTicket.repository, "" + ticket.number));
                } else {
                    error("Failed to add comment!");
                }
            } else {
            // TODO update comment
            }
        }

        /**
             * Steal from BasePage to realize redirection.
             * 
             * @see BasePage
             * @author krulls@GitHub; ECG Leipzig GmbH, Germany, 2015
             * 
             * @param pageClass
             * @param parameters
             * @return
             */
        private void redirectTo(Class<? extends BasePage> pageClass, PageParameters parameters) {
            String relativeUrl = urlFor(pageClass, parameters).toString();
            String canonicalUrl = RequestUtils.toAbsolutePath(relativeUrl);
            getRequestCycle().setRequestTarget(new RedirectRequestTarget(canonicalUrl));
        }
    }.setVisible(ticket != null && ticket.number > 0));
    final IModel<String> markdownPreviewModel = Model.of();
    markdownPreview = new Label("markdownPreview", markdownPreviewModel);
    markdownPreview.setEscapeModelStrings(false);
    markdownPreview.setOutputMarkupId(true);
    add(markdownPreview);
    markdownEditor = new MarkdownTextArea("markdownEditor", markdownPreviewModel, markdownPreview);
    markdownEditor.setRepository(repositoryName);
    WicketUtils.setInputPlaceholder(markdownEditor, getString("gb.leaveComment"));
    add(markdownEditor);
}
Also used : Form(org.apache.wicket.markup.html.form.Form) Label(org.apache.wicket.markup.html.basic.Label) TicketModel(com.gitblit.models.TicketModel) Change(com.gitblit.models.TicketModel.Change) RepositoryModel(com.gitblit.models.RepositoryModel) PageParameters(org.apache.wicket.PageParameters) AjaxRequestTarget(org.apache.wicket.ajax.AjaxRequestTarget) AjaxButton(org.apache.wicket.ajax.markup.html.form.AjaxButton) RedirectRequestTarget(org.apache.wicket.request.target.basic.RedirectRequestTarget) BasePage(com.gitblit.wicket.pages.BasePage)

Example 35 with TicketModel

use of com.gitblit.models.TicketModel in project gitblit by gitblit.

the class GitblitReceivePack method executeCommands.

/** Execute commands to update references. */
@Override
protected void executeCommands() {
    List<ReceiveCommand> toApply = filterCommands(Result.NOT_ATTEMPTED);
    if (toApply.isEmpty()) {
        return;
    }
    ProgressMonitor updating = NullProgressMonitor.INSTANCE;
    boolean sideBand = isCapabilityEnabled(CAPABILITY_SIDE_BAND_64K);
    if (sideBand) {
        SideBandProgressMonitor pm = new SideBandProgressMonitor(msgOut);
        pm.setDelayStart(250, TimeUnit.MILLISECONDS);
        updating = pm;
    }
    BatchRefUpdate batch = getRepository().getRefDatabase().newBatchUpdate();
    batch.setAllowNonFastForwards(isAllowNonFastForwards());
    batch.setRefLogIdent(getRefLogIdent());
    batch.setRefLogMessage("push", true);
    for (ReceiveCommand cmd : toApply) {
        if (Result.NOT_ATTEMPTED != cmd.getResult()) {
            // Already rejected by the core receive process.
            continue;
        }
        batch.addCommand(cmd);
    }
    if (!batch.getCommands().isEmpty()) {
        try {
            batch.execute(getRevWalk(), updating);
        } catch (IOException err) {
            for (ReceiveCommand cmd : toApply) {
                if (cmd.getResult() == Result.NOT_ATTEMPTED) {
                    sendRejection(cmd, "lock error: {0}", err.getMessage());
                }
            }
        }
    }
    //
    if (ticketService != null) {
        List<ReceiveCommand> allUpdates = ReceiveCommand.filter(batch.getCommands(), Result.OK);
        if (!allUpdates.isEmpty()) {
            int ticketsProcessed = 0;
            for (ReceiveCommand cmd : allUpdates) {
                switch(cmd.getType()) {
                    case CREATE:
                    case UPDATE:
                        if (cmd.getRefName().startsWith(Constants.R_HEADS)) {
                            Collection<TicketModel> tickets = processReferencedTickets(cmd);
                            ticketsProcessed += tickets.size();
                            for (TicketModel ticket : tickets) {
                                ticketNotifier.queueMailing(ticket);
                            }
                        }
                        break;
                    case UPDATE_NONFASTFORWARD:
                        if (cmd.getRefName().startsWith(Constants.R_HEADS)) {
                            String base = JGitUtils.getMergeBase(getRepository(), cmd.getOldId(), cmd.getNewId());
                            List<TicketLink> deletedRefs = JGitUtils.identifyTicketsBetweenCommits(getRepository(), settings, base, cmd.getOldId().name());
                            for (TicketLink link : deletedRefs) {
                                link.isDelete = true;
                            }
                            Change deletion = new Change(user.username);
                            deletion.pendingLinks = deletedRefs;
                            ticketService.updateTicket(repository, 0, deletion);
                            Collection<TicketModel> tickets = processReferencedTickets(cmd);
                            ticketsProcessed += tickets.size();
                            for (TicketModel ticket : tickets) {
                                ticketNotifier.queueMailing(ticket);
                            }
                        }
                        break;
                    case DELETE:
                        //Identify if the branch has been merged 
                        SortedMap<Integer, String> bases = new TreeMap<Integer, String>();
                        try {
                            ObjectId dObj = cmd.getOldId();
                            Collection<Ref> tips = getRepository().getRefDatabase().getRefs(Constants.R_HEADS).values();
                            for (Ref ref : tips) {
                                ObjectId iObj = ref.getObjectId();
                                String mergeBase = JGitUtils.getMergeBase(getRepository(), dObj, iObj);
                                if (mergeBase != null) {
                                    int d = JGitUtils.countCommits(getRepository(), getRevWalk(), mergeBase, dObj.name());
                                    bases.put(d, mergeBase);
                                    //All commits have been merged into some other branch
                                    if (d == 0) {
                                        break;
                                    }
                                }
                            }
                            if (bases.isEmpty()) {
                            //TODO: Handle orphan branch case
                            } else {
                                if (bases.firstKey() > 0) {
                                    //Delete references from the remaining commits that haven't been merged
                                    String mergeBase = bases.get(bases.firstKey());
                                    List<TicketLink> deletedRefs = JGitUtils.identifyTicketsBetweenCommits(getRepository(), settings, mergeBase, dObj.name());
                                    for (TicketLink link : deletedRefs) {
                                        link.isDelete = true;
                                    }
                                    Change deletion = new Change(user.username);
                                    deletion.pendingLinks = deletedRefs;
                                    ticketService.updateTicket(repository, 0, deletion);
                                }
                            }
                        } catch (IOException e) {
                            LOGGER.error(null, e);
                        }
                        break;
                    default:
                        break;
                }
            }
            if (ticketsProcessed == 1) {
                sendInfo("1 ticket updated");
            } else if (ticketsProcessed > 1) {
                sendInfo("{0} tickets updated", ticketsProcessed);
            }
        }
        // reset the ticket caches for the repository
        ticketService.resetCaches(repository);
    }
}
Also used : ReceiveCommand(org.eclipse.jgit.transport.ReceiveCommand) AnyObjectId(org.eclipse.jgit.lib.AnyObjectId) ObjectId(org.eclipse.jgit.lib.ObjectId) TicketModel(com.gitblit.models.TicketModel) IOException(java.io.IOException) Change(com.gitblit.models.TicketModel.Change) TreeMap(java.util.TreeMap) NullProgressMonitor(org.eclipse.jgit.lib.NullProgressMonitor) ProgressMonitor(org.eclipse.jgit.lib.ProgressMonitor) Ref(org.eclipse.jgit.lib.Ref) TicketLink(com.gitblit.models.TicketModel.TicketLink) BatchRefUpdate(org.eclipse.jgit.lib.BatchRefUpdate)

Aggregations

TicketModel (com.gitblit.models.TicketModel)62 Change (com.gitblit.models.TicketModel.Change)32 Test (org.junit.Test)28 RevCommit (org.eclipse.jgit.revwalk.RevCommit)21 IOException (java.io.IOException)18 Reference (com.gitblit.models.TicketModel.Reference)16 Repository (org.eclipse.jgit.lib.Repository)12 Patchset (com.gitblit.models.TicketModel.Patchset)8 RepositoryModel (com.gitblit.models.RepositoryModel)7 TicketLink (com.gitblit.models.TicketModel.TicketLink)7 Ref (org.eclipse.jgit.lib.Ref)5 UserModel (com.gitblit.models.UserModel)4 ArrayList (java.util.ArrayList)4 ReceiveCommand (org.eclipse.jgit.transport.ReceiveCommand)4 Attachment (com.gitblit.models.TicketModel.Attachment)3 ParseException (java.text.ParseException)3 RevWalk (org.eclipse.jgit.revwalk.RevWalk)3 TicketHook (com.gitblit.extensions.TicketHook)2 PathChangeModel (com.gitblit.models.PathModel.PathChangeModel)2 BranchTicketService (com.gitblit.tickets.BranchTicketService)2