summaryrefslogtreecommitdiffstats
path: root/src/com/gitblit/utils/TicgitUtils.java
blob: 87a2b3aeaa62d42c825f15919788b75347d05808 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*
 * Copyright 2011 gitblit.com.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.gitblit.utils;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.revwalk.RevCommit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.gitblit.models.PathModel;
import com.gitblit.models.RefModel;
import com.gitblit.models.TicketModel;
import com.gitblit.models.TicketModel.Comment;

/**
 * Utility class for reading Ticgit issues.
 * 
 * @author James Moger
 * 
 */
public class TicgitUtils {

	static final Logger LOGGER = LoggerFactory.getLogger(TicgitUtils.class);

	/**
	 * Returns a RefModel for the Ticgit branch in the repository. If the branch
	 * can not be found, null is returned.
	 * 
	 * @param repository
	 * @return a refmodel for the ticgit branch or null
	 */
	public static RefModel getTicketsBranch(Repository repository) {
		RefModel ticgitBranch = null;
		try {
			// search for ticgit branch in local heads
			for (RefModel ref : JGitUtils.getLocalBranches(repository, false, -1)) {
				if (ref.displayName.endsWith("ticgit")) {
					ticgitBranch = ref;
					break;
				}
			}

			// search for ticgit branch in remote heads
			if (ticgitBranch == null) {
				for (RefModel ref : JGitUtils.getRemoteBranches(repository, false, -1)) {
					if (ref.displayName.endsWith("ticgit")) {
						ticgitBranch = ref;
						break;
					}
				}
			}
		} catch (Throwable t) {
			LOGGER.error("Failed to find ticgit branch!", t);
		}
		return ticgitBranch;
	}

	/**
	 * Returns a list of all tickets in the ticgit branch of the repository.
	 * 
	 * @param repository
	 * @return list of tickets
	 */
	public static List<TicketModel> getTickets(Repository repository) {
		RefModel ticgitBranch = getTicketsBranch(repository);
		if (ticgitBranch == null) {
			return null;
		}
		RevCommit commit = (RevCommit) ticgitBranch.referencedObject;
		List<PathModel> paths = JGitUtils.getFilesInPath(repository, null, commit);
		List<TicketModel> tickets = new ArrayList<TicketModel>();
		for (PathModel ticketFolder : paths) {
			if (ticketFolder.isTree()) {
				try {
					TicketModel t = new TicketModel(ticketFolder.name);
					loadTicketContents(repository, ticgitBranch, t);
					tickets.add(t);
				} catch (Throwable t) {
					LOGGER.error("Failed to get a ticket!", t);
				}
			}
		}
		Collections.sort(tickets);
		Collections.reverse(tickets);
		return tickets;
	}

	/**
	 * Returns a TicketModel for the specified ticgit ticket. Returns null if
	 * the ticket does not exist or some other error occurs.
	 * 
	 * @param repository
	 * @param ticketFolder
	 * @return a ticket
	 */
	public static TicketModel getTicket(Repository repository, String ticketFolder) {
		RefModel ticketsBranch = getTicketsBranch(repository);
		if (ticketsBranch != null) {
			try {
				TicketModel ticket = new TicketModel(ticketFolder);
				loadTicketContents(repository, ticketsBranch, ticket);
				return ticket;
			} catch (Throwable t) {
				LOGGER.error("Failed to get ticket " + ticketFolder, t);
			}
		}
		return null;
	}

	/**
	 * Loads the contents of the ticket.
	 * 
	 * @param repository
	 * @param ticketsBranch
	 * @param ticket
	 */
	private static void loadTicketContents(Repository repository, RefModel ticketsBranch,
			TicketModel ticket) {
		RevCommit commit = (RevCommit) ticketsBranch.referencedObject;
		List<PathModel> ticketFiles = JGitUtils.getFilesInPath(repository, ticket.name, commit);
		for (PathModel file : ticketFiles) {
			String content = JGitUtils.getStringContent(repository, commit.getTree(), file.path)
					.trim();
			if (file.name.equals("TICKET_ID")) {
				ticket.id = content;
			} else if (file.name.equals("TITLE")) {
				ticket.title = content;
			} else {
				String[] chunks = file.name.split("_");
				if (chunks[0].equals("ASSIGNED")) {
					ticket.handler = content;
				} else if (chunks[0].equals("COMMENT")) {
					try {
						Comment c = new Comment(file.name, content);
						ticket.comments.add(c);
					} catch (ParseException e) {
						LOGGER.error("Failed to parse ticket comment", e);
					}
				} else if (chunks[0].equals("TAG")) {
					if (content.startsWith("TAG_")) {
						ticket.tags.add(content.substring(4));
					} else {
						ticket.tags.add(content);
					}
				} else if (chunks[0].equals("STATE")) {
					ticket.state = content;
				}
			}
		}
		Collections.sort(ticket.comments);
	}
}