James Moger
2011-09-25 dd9ae71bc1cb13b90dcc8d9689550eb7dfe7d035
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) {
793f76 51         RefModel ticgitBranch = null;
JM 52         try {
53             // search for ticgit branch in local heads
d9f687 54             for (RefModel ref : JGitUtils.getLocalBranches(repository, false, -1)) {
793f76 55                 if (ref.displayName.endsWith("ticgit")) {
JM 56                     ticgitBranch = ref;
57                     break;
58                 }
59             }
60
61             // search for ticgit branch in remote heads
62             if (ticgitBranch == null) {
d9f687 63                 for (RefModel ref : JGitUtils.getRemoteBranches(repository, false, -1)) {
793f76 64                     if (ref.displayName.endsWith("ticgit")) {
JM 65                         ticgitBranch = ref;
66                         break;
67                     }
68                 }
69             }
70         } catch (Throwable t) {
71             LOGGER.error("Failed to find ticgit branch!", t);
72         }
73         return ticgitBranch;
74     }
75
d9f687 76     /**
JM 77      * Returns a list of all tickets in the ticgit branch of the repository.
78      * 
79      * @param repository
80      * @return list of tickets
81      */
82     public static List<TicketModel> getTickets(Repository repository) {
83         RefModel ticgitBranch = getTicketsBranch(repository);
a125cf 84         if (ticgitBranch == null) {
JM 85             return null;
86         }
4ab184 87         RevCommit commit = (RevCommit) ticgitBranch.referencedObject;
d9f687 88         List<PathModel> paths = JGitUtils.getFilesInPath(repository, null, commit);
793f76 89         List<TicketModel> tickets = new ArrayList<TicketModel>();
JM 90         for (PathModel ticketFolder : paths) {
91             if (ticketFolder.isTree()) {
92                 try {
93                     TicketModel t = new TicketModel(ticketFolder.name);
d9f687 94                     loadTicketContents(repository, ticgitBranch, t);
793f76 95                     tickets.add(t);
JM 96                 } catch (Throwable t) {
97                     LOGGER.error("Failed to get a ticket!", t);
98                 }
99             }
100         }
101         Collections.sort(tickets);
102         Collections.reverse(tickets);
103         return tickets;
104     }
105
d9f687 106     /**
JM 107      * Returns a TicketModel for the specified ticgit ticket. Returns null if
108      * the ticket does not exist or some other error occurs.
109      * 
110      * @param repository
111      * @param ticketFolder
112      * @return a ticket
113      */
114     public static TicketModel getTicket(Repository repository, String ticketFolder) {
115         RefModel ticketsBranch = getTicketsBranch(repository);
793f76 116         if (ticketsBranch != null) {
JM 117             try {
118                 TicketModel ticket = new TicketModel(ticketFolder);
d9f687 119                 loadTicketContents(repository, ticketsBranch, ticket);
793f76 120                 return ticket;
JM 121             } catch (Throwable t) {
122                 LOGGER.error("Failed to get ticket " + ticketFolder, t);
123             }
124         }
125         return null;
126     }
127
d9f687 128     /**
JM 129      * Loads the contents of the ticket.
130      * 
131      * @param repository
132      * @param ticketsBranch
133      * @param ticket
134      */
135     private static void loadTicketContents(Repository repository, RefModel ticketsBranch,
136             TicketModel ticket) {
4ab184 137         RevCommit commit = (RevCommit) ticketsBranch.referencedObject;
d9f687 138         List<PathModel> ticketFiles = JGitUtils.getFilesInPath(repository, ticket.name, commit);
793f76 139         for (PathModel file : ticketFiles) {
d9f687 140             String content = JGitUtils.getStringContent(repository, commit.getTree(), file.path)
JM 141                     .trim();
793f76 142             if (file.name.equals("TICKET_ID")) {
JM 143                 ticket.id = content;
144             } else if (file.name.equals("TITLE")) {
145                 ticket.title = content;
146             } else {
147                 String[] chunks = file.name.split("_");
148                 if (chunks[0].equals("ASSIGNED")) {
149                     ticket.handler = content;
150                 } else if (chunks[0].equals("COMMENT")) {
151                     try {
152                         Comment c = new Comment(file.name, content);
153                         ticket.comments.add(c);
154                     } catch (ParseException e) {
a125cf 155                         LOGGER.error("Failed to parse ticket comment", e);
793f76 156                     }
JM 157                 } else if (chunks[0].equals("TAG")) {
158                     if (content.startsWith("TAG_")) {
159                         ticket.tags.add(content.substring(4));
160                     } else {
161                         ticket.tags.add(content);
162                     }
163                 } else if (chunks[0].equals("STATE")) {
164                     ticket.state = content;
165                 }
166             }
167         }
168         Collections.sort(ticket.comments);
169     }
170 }