Search in sources :

Example 21 with TicketModel

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

the class BranchTicketService method getAttachment.

/**
	 * Retrieves the specified attachment from a ticket.
	 *
	 * @param repository
	 * @param ticketId
	 * @param filename
	 * @return an attachment, if found, null otherwise
	 */
@Override
public Attachment getAttachment(RepositoryModel repository, long ticketId, String filename) {
    if (ticketId <= 0L) {
        return null;
    }
    // deserialize the ticket model so that we have the attachment metadata
    TicketModel ticket = getTicket(repository, ticketId);
    Attachment attachment = ticket.getAttachment(filename);
    // attachment not found
    if (attachment == null) {
        return null;
    }
    // retrieve the attachment content
    Repository db = repositoryManager.getRepository(repository.name);
    try {
        String attachmentPath = toAttachmentPath(ticketId, attachment.name);
        RevTree tree = JGitUtils.getCommit(db, BRANCH).getTree();
        byte[] content = JGitUtils.getByteContent(db, tree, attachmentPath, false);
        attachment.content = content;
        attachment.size = content.length;
        return attachment;
    } finally {
        db.close();
    }
}
Also used : Repository(org.eclipse.jgit.lib.Repository) TicketModel(com.gitblit.models.TicketModel) Attachment(com.gitblit.models.TicketModel.Attachment) RevTree(org.eclipse.jgit.revwalk.RevTree)

Example 22 with TicketModel

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

the class FileTicketService method getAttachment.

/**
	 * Retrieves the specified attachment from a ticket.
	 *
	 * @param repository
	 * @param ticketId
	 * @param filename
	 * @return an attachment, if found, null otherwise
	 */
@Override
public Attachment getAttachment(RepositoryModel repository, long ticketId, String filename) {
    if (ticketId <= 0L) {
        return null;
    }
    // deserialize the ticket model so that we have the attachment metadata
    TicketModel ticket = getTicket(repository, ticketId);
    Attachment attachment = ticket.getAttachment(filename);
    // attachment not found
    if (attachment == null) {
        return null;
    }
    // retrieve the attachment content
    Repository db = repositoryManager.getRepository(repository.name);
    try {
        String attachmentPath = toAttachmentPath(ticketId, attachment.name);
        File file = new File(db.getDirectory(), attachmentPath);
        if (file.exists()) {
            attachment.content = FileUtils.readContent(file);
            attachment.size = attachment.content.length;
        }
        return attachment;
    } finally {
        db.close();
    }
}
Also used : Repository(org.eclipse.jgit.lib.Repository) TicketModel(com.gitblit.models.TicketModel) Attachment(com.gitblit.models.TicketModel.Attachment) File(java.io.File)

Example 23 with TicketModel

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

the class ITicketService method renameMilestone.

/**
	 * Renames a milestone.
	 *
	 * @param repository
	 * @param oldName
	 * @param newName
	 * @param createdBy
	 * @param notifyOpenTickets
	 * @return true if successful
	 * @since 1.6.0
	 */
public synchronized boolean renameMilestone(RepositoryModel repository, String oldName, String newName, String createdBy, boolean notifyOpenTickets) {
    if (StringUtils.isEmpty(newName)) {
        throw new IllegalArgumentException("new milestone can not be empty!");
    }
    Repository db = null;
    try {
        db = repositoryManager.getRepository(repository.name);
        TicketMilestone tm = getMilestone(repository, oldName);
        if (tm == null) {
            return false;
        }
        StoredConfig config = db.getConfig();
        config.unsetSection(MILESTONE, oldName);
        config.setString(MILESTONE, newName, STATUS, tm.status.name());
        config.setString(MILESTONE, newName, COLOR, tm.color);
        if (tm.due != null) {
            config.setString(MILESTONE, newName, DUE, new SimpleDateFormat(DUE_DATE_PATTERN).format(tm.due));
        }
        config.save();
        milestonesCache.remove(repository.name);
        TicketNotifier notifier = createNotifier();
        for (QueryResult qr : tm.tickets) {
            Change change = new Change(createdBy);
            change.setField(Field.milestone, newName);
            TicketModel ticket = updateTicket(repository, qr.number, change);
            if (notifyOpenTickets && ticket.isOpen()) {
                notifier.queueMailing(ticket);
            }
        }
        if (notifyOpenTickets) {
            notifier.sendAll();
        }
        return true;
    } catch (IOException e) {
        log.error("failed to rename milestone " + oldName + " in " + repository, e);
    } finally {
        if (db != null) {
            db.close();
        }
    }
    return false;
}
Also used : StoredConfig(org.eclipse.jgit.lib.StoredConfig) Repository(org.eclipse.jgit.lib.Repository) TicketModel(com.gitblit.models.TicketModel) Change(com.gitblit.models.TicketModel.Change) IOException(java.io.IOException) SimpleDateFormat(java.text.SimpleDateFormat)

Example 24 with TicketModel

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

the class ITicketService method updateTicket.

/**
	 * Updates a ticket and promotes pending links into references.
	 *
	 * @param repository
	 * @param ticketId, or 0 to action pending links in general
	 * @param change
	 * @return the ticket model if successful, null if failure or using 0 ticketId
	 * @since 1.4.0
	 */
public final TicketModel updateTicket(RepositoryModel repository, long ticketId, Change change) {
    if (change == null) {
        throw new RuntimeException("change can not be null!");
    }
    if (StringUtils.isEmpty(change.author)) {
        throw new RuntimeException("must specify a change author!");
    }
    boolean success = true;
    TicketModel ticket = null;
    if (ticketId > 0) {
        TicketKey key = new TicketKey(repository, ticketId);
        ticketsCache.invalidate(key);
        success = commitChangeImpl(repository, ticketId, change);
        if (success) {
            ticket = getTicket(repository, ticketId);
            ticketsCache.put(key, ticket);
            indexer.index(ticket);
            // call the ticket hooks
            if (pluginManager != null) {
                for (TicketHook hook : pluginManager.getExtensions(TicketHook.class)) {
                    try {
                        hook.onUpdateTicket(ticket, change);
                    } catch (Exception e) {
                        log.error("Failed to execute extension", e);
                    }
                }
            }
        }
    }
    if (success) {
        //Now that the ticket has been successfully persisted add references to this ticket from linked tickets
        if (change.hasPendingLinks()) {
            for (TicketLink link : change.pendingLinks) {
                TicketModel linkedTicket = getTicket(repository, link.targetTicketId);
                Change dstChange = null;
                //Ignore if not available or self reference 
                if (linkedTicket != null && link.targetTicketId != ticketId) {
                    dstChange = new Change(change.author, change.date);
                    switch(link.action) {
                        case Comment:
                            {
                                if (ticketId == 0) {
                                    throw new RuntimeException("must specify a ticket when linking a comment!");
                                }
                                dstChange.referenceTicket(ticketId, change.comment.id);
                            }
                            break;
                        case Commit:
                            {
                                dstChange.referenceCommit(link.hash);
                            }
                            break;
                        default:
                            {
                                throw new RuntimeException(String.format("must add persist logic for link of type %s", link.action));
                            }
                    }
                }
                if (dstChange != null) {
                    //If not deleted then remain null in journal
                    if (link.isDelete) {
                        dstChange.reference.deleted = true;
                    }
                    if (updateTicket(repository, link.targetTicketId, dstChange) != null) {
                        link.success = true;
                    }
                }
            }
        }
    }
    return ticket;
}
Also used : TicketHook(com.gitblit.extensions.TicketHook) TicketModel(com.gitblit.models.TicketModel) Change(com.gitblit.models.TicketModel.Change) TicketLink(com.gitblit.models.TicketModel.TicketLink) ParseException(java.text.ParseException) IOException(java.io.IOException)

Example 25 with TicketModel

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

the class ITicketService method deleteComment.

/**
	 * Deletes a comment from a ticket.
	 *
	 * @param ticket
	 * @param commentId
	 *            the id of the comment to delete
	 * @param deletedBy
	 * 			the user deleting the comment
	 * @return the revised ticket if the deletion was successful
	 * @since 1.4.0
	 */
public final TicketModel deleteComment(TicketModel ticket, String commentId, String deletedBy) {
    Change deletion = new Change(deletedBy);
    deletion.comment("");
    deletion.comment.id = commentId;
    deletion.comment.deleted = true;
    RepositoryModel repository = repositoryManager.getRepositoryModel(ticket.repository);
    TicketModel revisedTicket = updateTicket(repository, ticket.number, deletion);
    return revisedTicket;
}
Also used : TicketModel(com.gitblit.models.TicketModel) Change(com.gitblit.models.TicketModel.Change) RepositoryModel(com.gitblit.models.RepositoryModel)

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