Search in sources :

Example 1 with Attachment

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

the class TicketServiceTest method testLifecycle.

@Test
public void testLifecycle() throws Exception {
    // query non-existent ticket
    TicketModel nonExistent = service.getTicket(getRepository(), 0);
    assertNull(nonExistent);
    // create and insert a ticket
    Change c1 = newChange("testCreation() " + Long.toHexString(System.currentTimeMillis()));
    TicketModel ticket = service.createTicket(getRepository(), c1);
    assertTrue(ticket.number > 0);
    // retrieve ticket and compare
    TicketModel constructed = service.getTicket(getRepository(), ticket.number);
    compare(ticket, constructed);
    assertEquals(1, constructed.changes.size());
    // C1: create the ticket
    int changeCount = 0;
    c1 = newChange("testUpdates() " + Long.toHexString(System.currentTimeMillis()));
    ticket = service.createTicket(getRepository(), c1);
    assertTrue(ticket.number > 0);
    changeCount++;
    constructed = service.getTicket(getRepository(), ticket.number);
    compare(ticket, constructed);
    assertEquals(1, constructed.changes.size());
    // C2: set owner
    Change c2 = new Change("C2");
    c2.comment("I'll fix this");
    c2.setField(Field.responsible, c2.author);
    constructed = service.updateTicket(getRepository(), ticket.number, c2);
    assertNotNull(constructed);
    assertEquals(2, constructed.changes.size());
    assertEquals(c2.author, constructed.responsible);
    changeCount++;
    // C3: add a note
    Change c3 = new Change("C3");
    c3.comment("yeah, this is working");
    constructed = service.updateTicket(getRepository(), ticket.number, c3);
    assertNotNull(constructed);
    assertEquals(3, constructed.changes.size());
    changeCount++;
    if (service.supportsAttachments()) {
        // C4: add attachment
        Change c4 = new Change("C4");
        Attachment a = newAttachment();
        c4.addAttachment(a);
        constructed = service.updateTicket(getRepository(), ticket.number, c4);
        assertNotNull(constructed);
        assertTrue(constructed.hasAttachments());
        Attachment a1 = service.getAttachment(getRepository(), ticket.number, a.name);
        assertEquals(a.content.length, a1.content.length);
        assertTrue(Arrays.areEqual(a.content, a1.content));
        changeCount++;
    }
    // C5: close the issue
    Change c5 = new Change("C5");
    c5.comment("closing issue");
    c5.setField(Field.status, Status.Resolved);
    constructed = service.updateTicket(getRepository(), ticket.number, c5);
    assertNotNull(constructed);
    changeCount++;
    assertTrue(constructed.isClosed());
    assertEquals(changeCount, constructed.changes.size());
    List<TicketModel> allTickets = service.getTickets(getRepository());
    List<TicketModel> openTickets = service.getTickets(getRepository(), new TicketFilter() {

        @Override
        public boolean accept(TicketModel ticket) {
            return ticket.isOpen();
        }
    });
    List<TicketModel> closedTickets = service.getTickets(getRepository(), new TicketFilter() {

        @Override
        public boolean accept(TicketModel ticket) {
            return ticket.isClosed();
        }
    });
    assertTrue(allTickets.size() > 0);
    assertEquals(1, openTickets.size());
    assertEquals(1, closedTickets.size());
    // build a new Lucene index
    service.reindex(getRepository());
    List<QueryResult> hits = service.searchFor(getRepository(), "working", 1, 10);
    assertEquals(1, hits.size());
    // reindex a ticket
    ticket = allTickets.get(0);
    Change change = new Change("reindex");
    change.comment("this is a test of reindexing a ticket");
    service.updateTicket(getRepository(), ticket.number, change);
    ticket = service.getTicket(getRepository(), ticket.number);
    hits = service.searchFor(getRepository(), "reindexing", 1, 10);
    assertEquals(1, hits.size());
    service.stop();
    service = getService(false);
    // Lucene field query
    List<QueryResult> results = service.queryFor(Lucene.status.matches(Status.New.name()), 1, 10, Lucene.created.name(), true);
    assertEquals(1, results.size());
    assertTrue(results.get(0).title.startsWith("testCreation"));
    // Lucene field query
    results = service.queryFor(Lucene.status.matches(Status.Resolved.name()), 1, 10, Lucene.created.name(), true);
    assertEquals(1, results.size());
    assertTrue(results.get(0).title.startsWith("testUpdates"));
    // check the ids
    assertEquals("[1, 2]", service.getIds(getRepository()).toString());
    // delete all tickets
    for (TicketModel aTicket : allTickets) {
        assertTrue(service.deleteTicket(getRepository(), aTicket.number, "D"));
    }
}
Also used : QueryResult(com.gitblit.tickets.QueryResult) TicketModel(com.gitblit.models.TicketModel) Attachment(com.gitblit.models.TicketModel.Attachment) Change(com.gitblit.models.TicketModel.Change) TicketFilter(com.gitblit.tickets.ITicketService.TicketFilter) Test(org.junit.Test)

Example 2 with Attachment

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

the class TicketIndexer method ticketToDoc.

/**
 * Creates a Lucene document from a ticket.
 *
 * @param ticket
 * @return a Lucene document
 */
private Document ticketToDoc(TicketModel ticket) {
    Document doc = new Document();
    // repository and document ids for Lucene querying
    toDocField(doc, Lucene.rid, StringUtils.getSHA1(ticket.repository));
    toDocField(doc, Lucene.did, StringUtils.getSHA1(ticket.repository + ticket.number));
    toDocField(doc, Lucene.project, ticket.project);
    toDocField(doc, Lucene.repository, ticket.repository);
    toDocField(doc, Lucene.number, ticket.number);
    toDocField(doc, Lucene.title, ticket.title);
    toDocField(doc, Lucene.body, ticket.body);
    toDocField(doc, Lucene.created, ticket.created);
    toDocField(doc, Lucene.createdby, ticket.createdBy);
    toDocField(doc, Lucene.updated, ticket.updated);
    toDocField(doc, Lucene.updatedby, ticket.updatedBy);
    toDocField(doc, Lucene.responsible, ticket.responsible);
    toDocField(doc, Lucene.milestone, ticket.milestone);
    toDocField(doc, Lucene.topic, ticket.topic);
    toDocField(doc, Lucene.status, ticket.status.name());
    toDocField(doc, Lucene.comments, ticket.getComments().size());
    toDocField(doc, Lucene.type, ticket.type == null ? null : ticket.type.name());
    toDocField(doc, Lucene.mergesha, ticket.mergeSha);
    toDocField(doc, Lucene.mergeto, ticket.mergeTo);
    toDocField(doc, Lucene.labels, StringUtils.flattenStrings(ticket.getLabels(), ";").toLowerCase());
    toDocField(doc, Lucene.participants, StringUtils.flattenStrings(ticket.getParticipants(), ";").toLowerCase());
    toDocField(doc, Lucene.watchedby, StringUtils.flattenStrings(ticket.getWatchers(), ";").toLowerCase());
    toDocField(doc, Lucene.mentions, StringUtils.flattenStrings(ticket.getMentions(), ";").toLowerCase());
    toDocField(doc, Lucene.votes, ticket.getVoters().size());
    toDocField(doc, Lucene.priority, ticket.priority.getValue());
    toDocField(doc, Lucene.severity, ticket.severity.getValue());
    List<String> attachments = new ArrayList<String>();
    for (Attachment attachment : ticket.getAttachments()) {
        attachments.add(attachment.name.toLowerCase());
    }
    toDocField(doc, Lucene.attachments, StringUtils.flattenStrings(attachments, ";"));
    List<Patchset> patches = ticket.getPatchsets();
    if (!patches.isEmpty()) {
        toDocField(doc, Lucene.patchsets, patches.size());
        Patchset patchset = patches.get(patches.size() - 1);
        String flat = patchset.number + ":" + patchset.rev + ":" + patchset.tip + ":" + patchset.base + ":" + patchset.commits;
        doc.add(new org.apache.lucene.document.Field(Lucene.patchset.name(), flat, TextField.TYPE_STORED));
    }
    doc.add(new TextField(Lucene.content.name(), ticket.toIndexableString(), Store.NO));
    return doc;
}
Also used : ArrayList(java.util.ArrayList) TextField(org.apache.lucene.document.TextField) Attachment(com.gitblit.models.TicketModel.Attachment) Patchset(com.gitblit.models.TicketModel.Patchset) Document(org.apache.lucene.document.Document)

Example 3 with Attachment

use of com.gitblit.models.TicketModel.Attachment 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 4 with Attachment

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

the class BranchTicketService method createIndex.

/**
 * Creates an in-memory index of the ticket change.
 *
 * @param changeId
 * @param change
 * @return an in-memory index
 * @throws IOException
 */
private DirCache createIndex(Repository db, long ticketId, Change change) throws IOException, ClassNotFoundException, NoSuchFieldException {
    String ticketPath = toTicketPath(ticketId);
    DirCache newIndex = DirCache.newInCore();
    DirCacheBuilder builder = newIndex.builder();
    ObjectInserter inserter = db.newObjectInserter();
    Set<String> ignorePaths = new TreeSet<String>();
    try {
        // create/update the journal
        // exclude the attachment content
        List<Change> changes = getJournal(db, ticketId);
        changes.add(change);
        String journal = TicketSerializer.serializeJournal(changes).trim();
        byte[] journalBytes = journal.getBytes(Constants.ENCODING);
        String journalPath = ticketPath + "/" + JOURNAL;
        final DirCacheEntry journalEntry = new DirCacheEntry(journalPath);
        journalEntry.setLength(journalBytes.length);
        journalEntry.setLastModified(change.date.getTime());
        journalEntry.setFileMode(FileMode.REGULAR_FILE);
        journalEntry.setObjectId(inserter.insert(org.eclipse.jgit.lib.Constants.OBJ_BLOB, journalBytes));
        // add journal to index
        builder.add(journalEntry);
        ignorePaths.add(journalEntry.getPathString());
        // Add any attachments to the index
        if (change.hasAttachments()) {
            for (Attachment attachment : change.attachments) {
                // build a path name for the attachment and mark as ignored
                String path = toAttachmentPath(ticketId, attachment.name);
                ignorePaths.add(path);
                // create an index entry for this attachment
                final DirCacheEntry entry = new DirCacheEntry(path);
                entry.setLength(attachment.content.length);
                entry.setLastModified(change.date.getTime());
                entry.setFileMode(FileMode.REGULAR_FILE);
                // insert object
                entry.setObjectId(inserter.insert(org.eclipse.jgit.lib.Constants.OBJ_BLOB, attachment.content));
                // add to temporary in-core index
                builder.add(entry);
            }
        }
        for (DirCacheEntry entry : JGitUtils.getTreeEntries(db, BRANCH, ignorePaths)) {
            builder.add(entry);
        }
        // finish the index
        builder.finish();
    } finally {
        inserter.close();
    }
    return newIndex;
}
Also used : DirCache(org.eclipse.jgit.dircache.DirCache) DirCacheBuilder(org.eclipse.jgit.dircache.DirCacheBuilder) DirCacheEntry(org.eclipse.jgit.dircache.DirCacheEntry) ObjectInserter(org.eclipse.jgit.lib.ObjectInserter) TreeSet(java.util.TreeSet) Attachment(com.gitblit.models.TicketModel.Attachment) Change(com.gitblit.models.TicketModel.Change)

Example 5 with Attachment

use of com.gitblit.models.TicketModel.Attachment 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)

Aggregations

Attachment (com.gitblit.models.TicketModel.Attachment)6 TicketModel (com.gitblit.models.TicketModel)3 Change (com.gitblit.models.TicketModel.Change)2 Repository (org.eclipse.jgit.lib.Repository)2 Patchset (com.gitblit.models.TicketModel.Patchset)1 TicketFilter (com.gitblit.tickets.ITicketService.TicketFilter)1 QueryResult (com.gitblit.tickets.QueryResult)1 File (java.io.File)1 ArrayList (java.util.ArrayList)1 TreeSet (java.util.TreeSet)1 Document (org.apache.lucene.document.Document)1 TextField (org.apache.lucene.document.TextField)1 DirCache (org.eclipse.jgit.dircache.DirCache)1 DirCacheBuilder (org.eclipse.jgit.dircache.DirCacheBuilder)1 DirCacheEntry (org.eclipse.jgit.dircache.DirCacheEntry)1 ObjectInserter (org.eclipse.jgit.lib.ObjectInserter)1 RevTree (org.eclipse.jgit.revwalk.RevTree)1 Test (org.junit.Test)1