James Moger
2014-04-11 aa89bef3a9e06404afb268426cbab1ef20c3857c
Add PatchsetHook extensions
1 files added
2 files modified
128 ■■■■ changed files
src/main/java/com/gitblit/extensions/PatchsetHook.java 53 ●●●●● patch | view | raw | blame | history
src/main/java/com/gitblit/git/PatchsetReceivePack.java 33 ●●●●● patch | view | raw | blame | history
src/site/setup_plugins.mkd 42 ●●●●● patch | view | raw | blame | history
src/main/java/com/gitblit/extensions/PatchsetHook.java
New file
@@ -0,0 +1,53 @@
/*
 * 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.extensions;
import ro.fortsoft.pf4j.ExtensionPoint;
import com.gitblit.models.TicketModel;
/**
 * Extension point for plugins to respond to Ticket patchset changes.
 *
 * @author James Moger
 *
 */
public abstract class PatchsetHook implements ExtensionPoint {
    /**
     * Called after a new patchset has been created.  This patchset
     * may not be the first patchset of the ticket.  The ticket may be a new
     * proposal or it may be a existing ticket that now has a new patchset.
     *
     * @param ticket
     */
    public abstract void onNewPatchset(TicketModel ticket);
    /**
     * Called after a patchset has been FAST-FORWARD updated.
     *
     * @param ticket
     */
    public abstract void onUpdatePatchset(TicketModel ticket);
    /**
     * Called after a patchset has been merged to the integration branch specified
     * in the ticket.
     *
     * @param ticket
     */
    public abstract void onMergePatchset(TicketModel ticket);
}
src/main/java/com/gitblit/git/PatchsetReceivePack.java
@@ -51,6 +51,7 @@
import com.gitblit.Constants;
import com.gitblit.Keys;
import com.gitblit.extensions.PatchsetHook;
import com.gitblit.manager.IGitblit;
import com.gitblit.models.RepositoryModel;
import com.gitblit.models.TicketModel;
@@ -716,6 +717,15 @@
                RefLogUtils.updateRefLog(user, getRepository(),
                        Arrays.asList(new ReceiveCommand(cmd.getOldId(), cmd.getNewId(), cmd.getRefName())));
                // call any patchset hooks
                for (PatchsetHook hook : gitblit.getExtensions(PatchsetHook.class)) {
                    try {
                        hook.onNewPatchset(ticket);
                    } catch (Exception e) {
                        LOGGER.error("Failed to execute extension", e);
                    }
                }
                return ticket;
            } else {
                sendError("FAILED to create ticket");
@@ -742,6 +752,20 @@
                // log the new patchset ref
                RefLogUtils.updateRefLog(user, getRepository(),
                    Arrays.asList(new ReceiveCommand(cmd.getOldId(), cmd.getNewId(), cmd.getRefName())));
                // call any patchset hooks
                final boolean isNewPatchset = change.patchset.rev == 1;
                for (PatchsetHook hook : gitblit.getExtensions(PatchsetHook.class)) {
                    try {
                        if (isNewPatchset) {
                            hook.onNewPatchset(ticket);
                        } else {
                            hook.onUpdatePatchset(ticket);
                        }
                    } catch (Exception e) {
                        LOGGER.error("Failed to execute extension", e);
                    }
                }
                // return the updated ticket
                return ticket;
@@ -1167,6 +1191,15 @@
                        ObjectId.fromString(mergeResult.sha), oldRef.getName());
                RefLogUtils.updateRefLog(user, getRepository(), Arrays.asList(cmd));
            }
            // call patchset hooks
            for (PatchsetHook hook : gitblit.getExtensions(PatchsetHook.class)) {
                try {
                    hook.onMergePatchset(ticket);
                } catch (Exception e) {
                    LOGGER.error("Failed to execute extension", e);
                }
            }
            return mergeResult.status;
        } else {
            LOGGER.error("FAILED to resolve ticket {} by merge from web ui", ticketId);
src/site/setup_plugins.mkd
@@ -67,34 +67,50 @@
```java
import com.gitblit.extensions.ReceiveHook;
import java.util.Collection;
import org.eclipse.jgit.transport.ReceiveCommand;
import ro.fortsoft.pf4j.Extension;
@Extension
public class MyHook extends ReceiveHook {
public class MyReceiveHook extends ReceiveHook {
    @Override
    public void onPreReceive(GitblitReceivePack receivePack, Collection<ReceiveCommand> commands) {
        RepositoryModel repository = receivePack.getRepositoryModel();
        UserModel user = receivePack.getUserModel();
        receivePack.sendInfo("Hi {0}, I see {1} commands for {2} onPreReceive",
                        user.getDisplayName(),
                        commands.size(),
                        repository.name);
    }
    @Override
    public void onPostReceive(GitblitReceivePack receivePack, Collection<ReceiveCommand> commands) {
        RepositoryModel repository = receivePack.getRepositoryModel();
        UserModel user = receivePack.getUserModel();
        receivePack.sendInfo("Hi {0}, I see {1} commands for {2} onPostReceive",
                        user.getDisplayName(),
                        commands.size(),
                        repository.name);
    }
}
```
### Extension Point: Patchset Hook
You can provide your own custom patchset hook by extending the *PatchsetHook* class.
```java
import com.gitblit.extensions.PatchsetHook;
import com.gitblit.models.TicketModel;
import ro.fortsoft.pf4j.Extension;
@Extension
public class MyPatchsetHook extends PatchsetHook {
    @Override
    public void onNewPatchset(TicketModel ticket) {
    }
    @Override
    public void onUpdatePatchset(TicketModel ticket) {
    }
    @Override
    public void onMergePatchset(TicketModel ticket) {
    }
}
```
### Mac OSX Fonts
Gitblit's core SSH commands and those in the *powertools* plugin rely on use of ANSI border characters to provide a pretty presentation of data.  Unfortunately, the fonts provided by Apple - while very nice - don't work well with ANSI border characters.  The following public domain fixed-width, fixed-point, bitmapped fonts work very nicely.  I find the 6x12 font with a line spacing of ~0.8 to be quite acceptable.