Skip to content

Commit

Permalink
Merged #22 "Tie mirroring, pushing, and the BranchTicketService toget…
Browse files Browse the repository at this point in the history
…her"
  • Loading branch information
gitblit committed Mar 6, 2014
2 parents e6ea453 + 148b408 commit aae5843
Show file tree
Hide file tree
Showing 14 changed files with 387 additions and 15 deletions.
4 changes: 3 additions & 1 deletion build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -570,7 +570,8 @@
<page name="overview" src="tickets_overview.mkd" />
<page name="using" src="tickets_using.mkd" />
<page name="barnum" src="tickets_barnum.mkd" />
<page name="setup" src="tickets_setup.mkd" />
<page name="setup" src="tickets_setup.mkd" />
<page name="replication &amp; advanced administration" src="tickets_replication.mkd" />
</menu>
<divider />
<page name="federation" src="federation.mkd" />
Expand Down Expand Up @@ -909,6 +910,7 @@
<page name="using" src="tickets_using.mkd" />
<page name="barnum" src="tickets_barnum.mkd" />
<page name="setup" src="tickets_setup.mkd" />
<page name="replication &amp; advanced administration" src="tickets_replication.mkd" />
</menu>
<divider />
<page name="federation" src="federation.mkd" />
Expand Down
9 changes: 9 additions & 0 deletions src/main/distrib/linux/reindex-tickets.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,14 @@
#
# --------------------------------------------------------------------------

if [ -z $1 ]; then
echo "Please specify your baseFolder!";
echo "";
echo "usage:";
echo " reindex-tickets <baseFolder>";
echo "";
exit 1;
fi

java -cp gitblit.jar:./ext/* com.gitblit.ReindexTickets --baseFolder $1

17 changes: 13 additions & 4 deletions src/main/distrib/win/reindex-tickets.cmd
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,19 @@
@REM Since the Tickets feature is undergoing massive churn it may be necessary
@REM to reindex tickets due to model or index changes.
@REM
@REM Always use forward-slashes for the path separator in your parameters!!
@REM usage:
@REM reindex-tickets <baseFolder>
@REM
@REM Set FOLDER to the baseFolder.
@REM --------------------------------------------------------------------------
@SET FOLDER=data
@if [%1]==[] goto nobasefolder

@java -cp gitblit.jar;"%CD%\ext\*" com.gitblit.ReindexTickets --baseFolder %FOLDER%
@java -cp gitblit.jar;"%CD%\ext\*" com.gitblit.ReindexTickets --baseFolder %1
@goto end

:nobasefolder
@echo "Please specify your baseFolder!"
@echo
@echo " reindex-tickets c:/gitblit-data"
@echo

:end
2 changes: 1 addition & 1 deletion src/main/java/com/gitblit/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ public String toString() {
public static enum RpcRequest {
// Order is important here. anything above LIST_SETTINGS requires
// administrator privileges and web.allowRpcManagement.
CLEAR_REPOSITORY_CACHE, GET_PROTOCOL, LIST_REPOSITORIES, LIST_BRANCHES, GET_USER, LIST_SETTINGS,
CLEAR_REPOSITORY_CACHE, REINDEX_TICKETS, GET_PROTOCOL, LIST_REPOSITORIES, LIST_BRANCHES, GET_USER, LIST_SETTINGS,
CREATE_REPOSITORY, EDIT_REPOSITORY, DELETE_REPOSITORY,
LIST_USERS, CREATE_USER, EDIT_USER, DELETE_USER,
LIST_TEAMS, CREATE_TEAM, EDIT_TEAM, DELETE_TEAM,
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/com/gitblit/git/GitblitReceivePack.java
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,15 @@ public void onPostReceive(ReceivePack rp, Collection<ReceiveCommand> commands) {
LOGGER.error(MessageFormat.format("Failed to update {0} pushlog", repository.name), e);
}

// check for updates pushed to the BranchTicketService branch
// if the BranchTicketService is active it will reindex, as appropriate
for (ReceiveCommand cmd : commands) {
if (Result.OK.equals(cmd.getResult())
&& BranchTicketService.BRANCH.equals(cmd.getRefName())) {
rp.getRepository().fireEvent(new ReceiveCommandEvent(repository, cmd));
}
}

// run Groovy hook scripts
Set<String> scripts = new LinkedHashSet<String>();
scripts.addAll(gitblit.getPostReceiveScriptsInherited(repository));
Expand Down
33 changes: 26 additions & 7 deletions src/main/java/com/gitblit/git/PatchsetReceivePack.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
import com.gitblit.models.TicketModel.PatchsetType;
import com.gitblit.models.TicketModel.Status;
import com.gitblit.models.UserModel;
import com.gitblit.tickets.BranchTicketService;
import com.gitblit.tickets.ITicketService;
import com.gitblit.tickets.TicketMilestone;
import com.gitblit.tickets.TicketNotifier;
Expand Down Expand Up @@ -105,7 +106,7 @@ public class PatchsetReceivePack extends GitblitReceivePack {

protected final TicketNotifier ticketNotifier;

private boolean requireCleanMerge;
private boolean requireMergeablePatchset;

public PatchsetReceivePack(IGitblit gitblit, Repository db, RepositoryModel repository, UserModel user) {
super(gitblit, db, repository, user);
Expand Down Expand Up @@ -257,12 +258,26 @@ protected void validateCommands() {
/** Execute commands to update references. */
@Override
protected void executeCommands() {
// we process patchsets unless the user is pushing something special
boolean processPatchsets = true;
for (ReceiveCommand cmd : filterCommands(Result.NOT_ATTEMPTED)) {
if (ticketService instanceof BranchTicketService
&& BranchTicketService.BRANCH.equals(cmd.getRefName())) {
// the user is pushing an update to the BranchTicketService data
processPatchsets = false;
}
}

// workaround for JGit's awful scoping choices
//
// reset the patchset refs to NOT_ATTEMPTED (see validateCommands)
for (ReceiveCommand cmd : filterCommands(Result.OK)) {
if (isPatchsetRef(cmd.getRefName())) {
cmd.setResult(Result.NOT_ATTEMPTED);
} else if (ticketService instanceof BranchTicketService
&& BranchTicketService.BRANCH.equals(cmd.getRefName())) {
// the user is pushing an update to the BranchTicketService data
processPatchsets = false;
}
}

Expand Down Expand Up @@ -292,7 +307,7 @@ protected void executeCommands() {
continue;
}

if (isPatchsetRef(cmd.getRefName())) {
if (isPatchsetRef(cmd.getRefName()) && processPatchsets) {
if (ticketService == null) {
sendRejection(cmd, "Sorry, the ticket service is unavailable and can not accept patchsets at this time.");
continue;
Expand Down Expand Up @@ -393,6 +408,8 @@ protected void executeCommands() {
for (ReceiveCommand cmd : toApply) {
if (cmd.getResult() == Result.NOT_ATTEMPTED) {
sendRejection(cmd, "lock error: {0}", err.getMessage());
LOGGER.error(MessageFormat.format("failed to lock {0}:{1}",
repository.name, cmd.getRefName()), err);
}
}
}
Expand Down Expand Up @@ -436,10 +453,12 @@ protected void executeCommands() {
case CREATE:
case UPDATE:
case UPDATE_NONFASTFORWARD:
Collection<TicketModel> tickets = processMergedTickets(cmd);
ticketsProcessed += tickets.size();
for (TicketModel ticket : tickets) {
ticketNotifier.queueMailing(ticket);
if (cmd.getRefName().startsWith(Constants.R_HEADS)) {
Collection<TicketModel> tickets = processMergedTickets(cmd);
ticketsProcessed += tickets.size();
for (TicketModel ticket : tickets) {
ticketNotifier.queueMailing(ticket);
}
}
break;
default:
Expand Down Expand Up @@ -537,7 +556,7 @@ private PatchsetCommand preparePatchset(ReceiveCommand cmd) {
case MERGEABLE:
break;
default:
if (ticket == null || requireCleanMerge) {
if (ticket == null || requireMergeablePatchset) {
sendError("");
sendError("Your patchset can not be cleanly merged into {0}.", forBranch);
sendError("Please rebase your patchset and push again.");
Expand Down
38 changes: 38 additions & 0 deletions src/main/java/com/gitblit/git/ReceiveCommandEvent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright 2014 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.git;

import org.eclipse.jgit.events.RefsChangedEvent;
import org.eclipse.jgit.transport.ReceiveCommand;

import com.gitblit.models.RepositoryModel;

/**
* The event fired by other classes to allow this service to index tickets.
*
* @author James Moger
*/
public class ReceiveCommandEvent extends RefsChangedEvent {

public final RepositoryModel model;

public final ReceiveCommand cmd;

public ReceiveCommandEvent(RepositoryModel model, ReceiveCommand cmd) {
this.model = model;
this.cmd = cmd;
}
}
32 changes: 32 additions & 0 deletions src/main/java/com/gitblit/service/MirrorService.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,20 @@
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.lib.StoredConfig;
import org.eclipse.jgit.transport.FetchResult;
import org.eclipse.jgit.transport.ReceiveCommand;
import org.eclipse.jgit.transport.ReceiveCommand.Type;
import org.eclipse.jgit.transport.RemoteConfig;
import org.eclipse.jgit.transport.TrackingRefUpdate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.gitblit.IStoredSettings;
import com.gitblit.Keys;
import com.gitblit.git.ReceiveCommandEvent;
import com.gitblit.manager.IRepositoryManager;
import com.gitblit.models.RepositoryModel;
import com.gitblit.models.UserModel;
import com.gitblit.tickets.BranchTicketService;
import com.gitblit.utils.JGitUtils;

/**
Expand Down Expand Up @@ -145,6 +149,7 @@ public void run() {
FetchResult result = git.fetch().setRemote(mirror.getName()).setDryRun(testing).call();
Collection<TrackingRefUpdate> refUpdates = result.getTrackingRefUpdates();
if (refUpdates.size() > 0) {
ReceiveCommand ticketBranchCmd = null;
for (TrackingRefUpdate ru : refUpdates) {
StringBuilder sb = new StringBuilder();
sb.append("updated mirror ");
Expand All @@ -161,6 +166,33 @@ public void run() {
sb.append("..");
sb.append(ru.getNewObjectId() == null ? "" : ru.getNewObjectId().abbreviate(7).name());
logger.info(sb.toString());

if (BranchTicketService.BRANCH.equals(ru.getLocalName())) {
ReceiveCommand.Type type = null;
switch (ru.getResult()) {
case NEW:
type = Type.CREATE;
break;
case FAST_FORWARD:
type = Type.UPDATE;
break;
case FORCED:
type = Type.UPDATE_NONFASTFORWARD;
break;
default:
type = null;
break;
}

if (type != null) {
ticketBranchCmd = new ReceiveCommand(ru.getOldObjectId(),
ru.getNewObjectId(), ru.getLocalName(), type);
}
}
}

if (ticketBranchCmd != null) {
repository.fireEvent(new ReceiveCommandEvent(model, ticketBranchCmd));
}
}
} catch (Exception e) {
Expand Down
15 changes: 14 additions & 1 deletion src/main/java/com/gitblit/servlet/RpcServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public class RpcServlet extends JsonServlet {

private static final long serialVersionUID = 1L;

public static final int PROTOCOL_VERSION = 6;
public static final int PROTOCOL_VERSION = 7;

private IStoredSettings settings;

Expand Down Expand Up @@ -383,6 +383,19 @@ protected void processRequest(HttpServletRequest request, HttpServletResponse re
} else {
response.sendError(notAllowedCode);
}
} else if (RpcRequest.REINDEX_TICKETS.equals(reqType)) {
if (allowManagement) {
if (StringUtils.isEmpty(objectName)) {
// reindex all tickets
gitblit.getTicketService().reindex();
} else {
// reindex tickets in a specific repository
RepositoryModel model = gitblit.getRepositoryModel(objectName);
gitblit.getTicketService().reindex(model);
}
} else {
response.sendError(notAllowedCode);
}
}

// send the result of the request
Expand Down
Loading

0 comments on commit aae5843

Please sign in to comment.