summaryrefslogtreecommitdiffstats
path: root/src/site/plugins_extensions.mkd
blob: 82dd30bc2d9a75c7f7426c8d5e3634c7e25625c5 (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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
## Extension Points

Gitblit offers several extension points for enhancing and customizing it's runtime behavior.

Each available extension point has a sample implementation in the [gitblit-cookbook-plugin (Maven project)](https://github.com/gitblit/gitblit-cookbook-plugin).

**NOTE:**
Gitblit does not yet offer a comprehensize dependency injection architecture.  This will be addressed in a subsequent release.  For now you may access all of Gitblit's core managers through a static singleton app context:

```java
import com.gitblit.extensions.GitblitPlugin;
import com.gitblit.servlet.GitblitContext;
import com.gitblit.manager.IRuntimeManager;
import com.gitblit.manager.IUserManager;
import com.gitblit.manager.IAuthenticationManager;
import com.gitblit.manager.INotificationManager;
import com.gitblit.manager.IRepositoryManager;
import com.gitblit.manager.IProjectManager;
import com.gitblit.manager.IFederationManager;
import com.gitblit.manager.IPluginManager;
import com.gitblit.manager.IGitblit;
import ro.fortsoft.pf4j.Version;

public class ExamplePlugin extends GitblitPlugin {

    @Override
    public void start() {
        IRuntimeManager runtime = GitblitContext.getManager(IRuntimeManager.class);
        IUserManager users = GitblitContext.getManager(IUserManager.class);
        IAuthenticationManager auth = GitblitContext.getManager(IAuthenticationManager.class);
        INotificationManager notifications = GitblitContext.getManager(INotificationManager.class);
        IRepositoryManager repos = GitblitContext.getManager(IRepositoryManager.class);
        IProjectManager projects = GitblitContext.getManager(IProjectManager.class);
        IFederationManager federation = GitblitContext.getManager(IFederationManager.class);
        IPluginManager plugins = GitblitContext.getManager(IPluginManager.class);
        IGitblit gitblit = GitblitContext.getManager(IGitblit.class);
    }

    @Override
    public void stop() {
    }

    @Override
    public void onInstall() {
    }

    @Override
    public void onUpgrade(Version oldVersion) {
    }

    @Override
    public void onUninstall() {
    }
}

/**
 * You can also create Webapp plugins that register pages.
 */
public class ExampleWicketPlugin extends GitblitWicketPlugin {
    @Override
    public void start() {
    }

    @Override
    public void stop() {
    }

    @Override
    public void onInstall() {
    }

    @Override
    public void onUpgrade(Version oldVersion) {
    }

    @Override
    public void onUninstall() {
    }

    @Override
    protected void init(GitblitWicketApp app) {
        app.mount("/logo", LogoPage.class);
        app.mount("/hello", HelloWorldPage.class);
    }
}
```

### SSH Dispatch Command

*SINCE 1.5.0*

You can provide your own custom SSH command hierarchies by subclassing the *DispatchCommand* class.

```java
import ro.fortsoft.pf4j.Extension;
import org.kohsuke.args4j.Option;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.gitblit.transport.ssh.commands.CommandMetaData;
import com.gitblit.transport.ssh.commands.DispatchCommand;
import com.gitblit.transport.ssh.commands.UsageExample;

@Extension
@CommandMetaData(name = "mycommands", description = "Sample SSH dispatcher")
public class MyDispatcher extends DispatchCommand {

    @Override
    protected void setup() {
        // commands in this dispatcher
        register(CommandA.class);
        register(CommandB.class);

        // nested dispatchers
        register(SubDispatcher1.class);
        register(SubDispatcher2.class);
    }

    @CommandMetaData(name = "commanda", aliases = { "ca" }, description = "description of command a")
    @UsageExample(syntax = "${cmd} --myflag", description = "description of commanda with --myflag")
    public static class CommandA extends SshCommand {

        protected final Logger log = LoggerFactory.getLogger(getClass());

        @Option(name = "--myflag", aliases = { "-m" }, usage = "enable myflag")
        boolean myflag;

        @Override
        public void run() throws Failure {
            if (myflag) {
                log.info("Run with --myflag");
            } else {
                log.info("Run without --myflag");
            }
        }
    }
}
```

### Pre- and Post- Receive Hook

*SINCE 1.5.0*

You can provide your own custom pre and/or post receive hooks by subclassing the *ReceiveHook* class.

```java
import com.gitblit.extensions.ReceiveHook;
import java.util.Collection;
import org.eclipse.jgit.transport.ReceiveCommand;
import ro.fortsoft.pf4j.Extension;

@Extension
public class MyReceiveHook extends ReceiveHook {

    @Override
    public void onPreReceive(GitblitReceivePack receivePack, Collection<ReceiveCommand> commands) {
    }

    @Override
    public void onPostReceive(GitblitReceivePack receivePack, Collection<ReceiveCommand> commands) {
    }
}

```

### Patchset Hook

*SINCE 1.5.0*

You can provide your own custom patchset hook by subclassing 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) {
    }
}
```

### Ticket Hook

*SINCE 1.5.0*

You can provide your own custom ticket hook by subclassing the *TicketHook* class.

```java
import com.gitblit.extensions.TicketHook;
import com.gitblit.models.TicketModel;
import com.gitblit.models.TicketModel.Change;
import ro.fortsoft.pf4j.Extension;

@Extension
public class MyTicketHook extends TicketHook {

    @Override
    public void onNewTicket(TicketModel ticket) {
    }

    @Override
    public void onUpdateTicket(TicketModel ticket, Change change) {
    }
}
```

### Request Filter

*SINCE 1.6.0*

You can provide your own custom request filter by subclassing the *HttpRequestFilter* class.

```java
import com.gitblit.extensions.HttpRequestFilter;
import ro.fortsoft.pf4j.Extension;

@Extension
public class MyRequestFilter extends HttpRequestFilter {

    @Override
    public void doFilter(ServletRequest request, ServletResponse response,
            FilterChain chain) throws IOException, ServletException {
    }
}
```

### User Menu Items

*SINCE 1.6.0*

You can provide your own user menu items by subclassing the *UserMenuExtension* class.

```java
import java.util.Arrays;
import java.util.List;
import ro.fortsoft.pf4j.Extension;
import com.gitblit.extensions.UserMenuExtension;
import com.gitblit.models.Menu.ExternalLinkMenuItem;
import com.gitblit.models.Menu.MenuItem;
import com.gitblit.models.UserModel;

@Extension
public class MyUserMenuContributor extends UserMenuExtension {

    @Override
    public List<MenuItem> getMenuItems(UserModel user) {
        MenuItem item = new ExternalLinkMenuItem("Github", String.format("https://github.com/%s", user.username));
        return Arrays.asList(item);
    }
}
```

### Navigation Links

*SINCE 1.6.0*

You can provide your own top-level navigation links by subclassing the *NavLinkExtension* class.

```java
import java.util.Arrays;
import java.util.List;
import ro.fortsoft.pf4j.Extension;
import com.gitblit.extensions.NavLinkExtension;
import com.gitblit.models.UserModel;

@Extension
public class MyNavLink extends NavLinkExtension {

    @Override
    public List<NavLink> getNavLinks(UserModel user) {
        NavLink link = new ExternalLinkMenuItem("Github", String.format("https://github.com/%s", user.username));
        return Arrays.asList(link);
    }
}
```

### Server Lifecycle Listener

*SINCE 1.6.0*

You can provide a lifecycle listener to be notified when Gitblit has completely started and just before Gitblit is gracefully terminated.

```java
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ro.fortsoft.pf4j.Extension;
import com.gitblit.extensions.LifeCycleListener;

@Extension
public class MyLifeCycleListener extends LifeCycleListener {

    final Logger log = LoggerFactory.getLogger(getClass());
    
    @Override
    public void onStartup() {
    	log.info("Gitblit is Ready!!");
    }

    @Override
    public void onShutdown() {
    	log.info("Gitblit is Going Down!!");
    }
}
```

### Repository Lifecycle Listener

*SINCE 1.6.0*

You can provide a lifecycle listener to be notified when Gitblit has created or deleted a repository.

```java
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ro.fortsoft.pf4j.Extension;
import com.gitblit.extensions.RepositoryLifeCycleListener;
import com.gitblit.models.RepositoryModel;

@Extension
public class MyRepoLifeCycleListener extends RepositoryLifeCycleListener {

    final Logger log = LoggerFactory.getLogger(getClass());
    
    @Override
    public void onCreation(RepositoryModel repo) {
    	log.info("Gitblit created {}", repo);
    }

    @Override
    public void onFork(RepositoryModel origin, RepositoryModel fork) {
    	log.info("{} forked to {}", origin, fork);
    }

    @Override
    public void onRename(String oldName, RepositoryModel repo) {
    	log.info("{} renamed to {}", oldName, repo);
    }

    @Override
    public void onDeletion(RepositoryModel repo) {
    	log.info("Gitblit deleted {}", repo);
    }
}
```

### User/Team Lifecycle Listener

*SINCE 1.6.0*

You can provide a lifecycle listener to be notified when Gitblit has created or deleted a user or a team.

```java
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ro.fortsoft.pf4j.Extension;
import com.gitblit.extensions.UserTeamLifeCycleListener;
import com.gitblit.models.TeamModel;
import com.gitblit.models.UserModel;

@Extension
public class MyUserTeamLifeCycleListener extends UserTeamLifeCycleListener {

    final Logger log = LoggerFactory.getLogger(getClass());
    
    @Override
    public void onCreation(UserModel user) {
    	log.info("Gitblit created user {}", user);
    }

    @Override
    public void onDeletion(UserModel user) {
    	log.info("Gitblit deleted user {}", user);
    }

    @Override
    public void onCreation(TeamModel team) {
    	log.info("Gitblit created team {}", team);
    }

    @Override
    public void onDeletion(TeamModel team) {
    	log.info("Gitblit deleted team {}", team);
    }
}
```