James Moger
2012-09-10 fabe060d3a435f116128851f828e35c2af5fde67
commit | author | age
793f76 1 /*
JM 2  * Copyright 2011 gitblit.com.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.gitblit.utils;
17
18 import java.text.ParseException;
19 import java.util.ArrayList;
20 import java.util.Collections;
21 import java.util.List;
22
23 import org.eclipse.jgit.lib.Repository;
4ab184 24 import org.eclipse.jgit.revwalk.RevCommit;
793f76 25 import org.slf4j.Logger;
JM 26 import org.slf4j.LoggerFactory;
27
28 import com.gitblit.models.PathModel;
29 import com.gitblit.models.RefModel;
30 import com.gitblit.models.TicketModel;
31 import com.gitblit.models.TicketModel.Comment;
32
d9f687 33 /**
JM 34  * Utility class for reading Ticgit issues.
35  * 
36  * @author James Moger
37  * 
38  */
793f76 39 public class TicgitUtils {
JM 40
41     static final Logger LOGGER = LoggerFactory.getLogger(TicgitUtils.class);
42
d9f687 43     /**
JM 44      * Returns a RefModel for the Ticgit branch in the repository. If the branch
45      * can not be found, null is returned.
46      * 
47      * @param repository
48      * @return a refmodel for the ticgit branch or null
49      */
50     public static RefModel getTicketsBranch(Repository repository) {
cc1cd7 51         return JGitUtils.getBranch(repository, "ticgit");
793f76 52     }
JM 53
d9f687 54     /**
JM 55      * Returns a list of all tickets in the ticgit branch of the repository.
56      * 
57      * @param repository
58      * @return list of tickets
59      */
60     public static List<TicketModel> getTickets(Repository repository) {
61         RefModel ticgitBranch = getTicketsBranch(repository);
a125cf 62         if (ticgitBranch == null) {
JM 63             return null;
64         }
4ab184 65         RevCommit commit = (RevCommit) ticgitBranch.referencedObject;
d9f687 66         List<PathModel> paths = JGitUtils.getFilesInPath(repository, null, commit);
793f76 67         List<TicketModel> tickets = new ArrayList<TicketModel>();
JM 68         for (PathModel ticketFolder : paths) {
69             if (ticketFolder.isTree()) {
70                 try {
71                     TicketModel t = new TicketModel(ticketFolder.name);
d9f687 72                     loadTicketContents(repository, ticgitBranch, t);
793f76 73                     tickets.add(t);
JM 74                 } catch (Throwable t) {
75                     LOGGER.error("Failed to get a ticket!", t);
76                 }
77             }
78         }
79         Collections.sort(tickets);
80         Collections.reverse(tickets);
81         return tickets;
82     }
83
d9f687 84     /**
JM 85      * Returns a TicketModel for the specified ticgit ticket. Returns null if
86      * the ticket does not exist or some other error occurs.
87      * 
88      * @param repository
89      * @param ticketFolder
90      * @return a ticket
91      */
92     public static TicketModel getTicket(Repository repository, String ticketFolder) {
93         RefModel ticketsBranch = getTicketsBranch(repository);
793f76 94         if (ticketsBranch != null) {
JM 95             try {
96                 TicketModel ticket = new TicketModel(ticketFolder);
d9f687 97                 loadTicketContents(repository, ticketsBranch, ticket);
793f76 98                 return ticket;
JM 99             } catch (Throwable t) {
100                 LOGGER.error("Failed to get ticket " + ticketFolder, t);
101             }
102         }
103         return null;
104     }
105
d9f687 106     /**
JM 107      * Loads the contents of the ticket.
108      * 
109      * @param repository
110      * @param ticketsBranch
111      * @param ticket
112      */
113     private static void loadTicketContents(Repository repository, RefModel ticketsBranch,
114             TicketModel ticket) {
4ab184 115         RevCommit commit = (RevCommit) ticketsBranch.referencedObject;
d9f687 116         List<PathModel> ticketFiles = JGitUtils.getFilesInPath(repository, ticket.name, commit);
793f76 117         for (PathModel file : ticketFiles) {
d9f687 118             String content = JGitUtils.getStringContent(repository, commit.getTree(), file.path)
JM 119                     .trim();
793f76 120             if (file.name.equals("TICKET_ID")) {
JM 121                 ticket.id = content;
122             } else if (file.name.equals("TITLE")) {
123                 ticket.title = content;
124             } else {
125                 String[] chunks = file.name.split("_");
126                 if (chunks[0].equals("ASSIGNED")) {
127                     ticket.handler = content;
128                 } else if (chunks[0].equals("COMMENT")) {
129                     try {
130                         Comment c = new Comment(file.name, content);
131                         ticket.comments.add(c);
132                     } catch (ParseException e) {
a125cf 133                         LOGGER.error("Failed to parse ticket comment", e);
793f76 134                     }
JM 135                 } else if (chunks[0].equals("TAG")) {
136                     if (content.startsWith("TAG_")) {
137                         ticket.tags.add(content.substring(4));
138                     } else {
139                         ticket.tags.add(content);
140                     }
141                 } else if (chunks[0].equals("STATE")) {
142                     ticket.state = content;
143                 }
144             }
145         }
146         Collections.sort(ticket.comments);
147     }
148 }