Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

plugins_extensions.mkd 10KB

před 10 roky
před 10 roky
před 10 roky
před 10 roky
před 10 roky
před 10 roky
před 10 roky
před 10 roky
před 10 roky
před 10 roky
před 10 roky
před 10 roky
před 10 roky
před 10 roky
před 10 roky
před 10 roky
před 10 roky
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. ## Extension Points
  2. Gitblit offers several extension points for enhancing and customizing it's runtime behavior.
  3. Each available extension point has a sample implementation in the [gitblit-cookbook-plugin (Maven project)](https://github.com/gitblit-org/gitblit-cookbook-plugin).
  4. **NOTE:**
  5. 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:
  6. ```java
  7. import com.gitblit.extensions.GitblitPlugin;
  8. import com.gitblit.servlet.GitblitContext;
  9. import com.gitblit.manager.IRuntimeManager;
  10. import com.gitblit.manager.IUserManager;
  11. import com.gitblit.manager.IAuthenticationManager;
  12. import com.gitblit.manager.INotificationManager;
  13. import com.gitblit.manager.IRepositoryManager;
  14. import com.gitblit.manager.IProjectManager;
  15. import com.gitblit.manager.IFederationManager;
  16. import com.gitblit.manager.IPluginManager;
  17. import com.gitblit.manager.IGitblit;
  18. import ro.fortsoft.pf4j.Version;
  19. public class ExamplePlugin extends GitblitPlugin {
  20. @Override
  21. public void start() {
  22. IRuntimeManager runtime = GitblitContext.getManager(IRuntimeManager.class);
  23. IUserManager users = GitblitContext.getManager(IUserManager.class);
  24. IAuthenticationManager auth = GitblitContext.getManager(IAuthenticationManager.class);
  25. INotificationManager notifications = GitblitContext.getManager(INotificationManager.class);
  26. IRepositoryManager repos = GitblitContext.getManager(IRepositoryManager.class);
  27. IProjectManager projects = GitblitContext.getManager(IProjectManager.class);
  28. IFederationManager federation = GitblitContext.getManager(IFederationManager.class);
  29. IPluginManager plugins = GitblitContext.getManager(IPluginManager.class);
  30. IGitblit gitblit = GitblitContext.getManager(IGitblit.class);
  31. }
  32. @Override
  33. public void stop() {
  34. }
  35. @Override
  36. public void onInstall() {
  37. }
  38. @Override
  39. public void onUpgrade(Version oldVersion) {
  40. }
  41. @Override
  42. public void onUninstall() {
  43. }
  44. }
  45. /**
  46. * You can also create Webapp plugins that register pages.
  47. */
  48. public class ExampleWicketPlugin extends GitblitWicketPlugin {
  49. @Override
  50. public void start() {
  51. }
  52. @Override
  53. public void stop() {
  54. }
  55. @Override
  56. public void onInstall() {
  57. }
  58. @Override
  59. public void onUpgrade(Version oldVersion) {
  60. }
  61. @Override
  62. public void onUninstall() {
  63. }
  64. @Override
  65. protected void init(GitblitWicketApp app) {
  66. app.mount("/logo", LogoPage.class);
  67. app.mount("/hello", HelloWorldPage.class);
  68. }
  69. }
  70. ```
  71. ### SSH Dispatch Command
  72. *SINCE 1.5.0*
  73. You can provide your own custom SSH command hierarchies by subclassing the *DispatchCommand* class.
  74. ```java
  75. import ro.fortsoft.pf4j.Extension;
  76. import org.kohsuke.args4j.Option;
  77. import org.slf4j.Logger;
  78. import org.slf4j.LoggerFactory;
  79. import com.gitblit.transport.ssh.commands.CommandMetaData;
  80. import com.gitblit.transport.ssh.commands.DispatchCommand;
  81. import com.gitblit.transport.ssh.commands.UsageExample;
  82. @Extension
  83. @CommandMetaData(name = "mycommands", description = "Sample SSH dispatcher")
  84. public class MyDispatcher extends DispatchCommand {
  85. @Override
  86. protected void setup() {
  87. // commands in this dispatcher
  88. register(CommandA.class);
  89. register(CommandB.class);
  90. // nested dispatchers
  91. register(SubDispatcher1.class);
  92. register(SubDispatcher2.class);
  93. }
  94. @CommandMetaData(name = "commanda", aliases = { "ca" }, description = "description of command a")
  95. @UsageExample(syntax = "${cmd} --myflag", description = "description of commanda with --myflag")
  96. public static class CommandA extends SshCommand {
  97. protected final Logger log = LoggerFactory.getLogger(getClass());
  98. @Option(name = "--myflag", aliases = { "-m" }, usage = "enable myflag")
  99. boolean myflag;
  100. @Override
  101. public void run() throws Failure {
  102. if (myflag) {
  103. log.info("Run with --myflag");
  104. } else {
  105. log.info("Run without --myflag");
  106. }
  107. }
  108. }
  109. }
  110. ```
  111. ### Pre- and Post- Receive Hook
  112. *SINCE 1.5.0*
  113. You can provide your own custom pre and/or post receive hooks by subclassing the *ReceiveHook* class.
  114. ```java
  115. import com.gitblit.extensions.ReceiveHook;
  116. import java.util.Collection;
  117. import org.eclipse.jgit.transport.ReceiveCommand;
  118. import ro.fortsoft.pf4j.Extension;
  119. @Extension
  120. public class MyReceiveHook extends ReceiveHook {
  121. @Override
  122. public void onPreReceive(GitblitReceivePack receivePack, Collection<ReceiveCommand> commands) {
  123. }
  124. @Override
  125. public void onPostReceive(GitblitReceivePack receivePack, Collection<ReceiveCommand> commands) {
  126. }
  127. }
  128. ```
  129. ### Patchset Hook
  130. *SINCE 1.5.0*
  131. You can provide your own custom patchset hook by subclassing the *PatchsetHook* class.
  132. ```java
  133. import com.gitblit.extensions.PatchsetHook;
  134. import com.gitblit.models.TicketModel;
  135. import ro.fortsoft.pf4j.Extension;
  136. @Extension
  137. public class MyPatchsetHook extends PatchsetHook {
  138. @Override
  139. public void onNewPatchset(TicketModel ticket) {
  140. }
  141. @Override
  142. public void onUpdatePatchset(TicketModel ticket) {
  143. }
  144. @Override
  145. public void onMergePatchset(TicketModel ticket) {
  146. }
  147. }
  148. ```
  149. ### Ticket Hook
  150. *SINCE 1.5.0*
  151. You can provide your own custom ticket hook by subclassing the *TicketHook* class.
  152. ```java
  153. import com.gitblit.extensions.TicketHook;
  154. import com.gitblit.models.TicketModel;
  155. import com.gitblit.models.TicketModel.Change;
  156. import ro.fortsoft.pf4j.Extension;
  157. @Extension
  158. public class MyTicketHook extends TicketHook {
  159. @Override
  160. public void onNewTicket(TicketModel ticket) {
  161. }
  162. @Override
  163. public void onUpdateTicket(TicketModel ticket, Change change) {
  164. }
  165. }
  166. ```
  167. ### Request Filter
  168. *SINCE 1.6.0*
  169. You can provide your own custom request filter by subclassing the *HttpRequestFilter* class.
  170. ```java
  171. import com.gitblit.extensions.HttpRequestFilter;
  172. import ro.fortsoft.pf4j.Extension;
  173. @Extension
  174. public class MyRequestFilter extends HttpRequestFilter {
  175. @Override
  176. public void doFilter(ServletRequest request, ServletResponse response,
  177. FilterChain chain) throws IOException, ServletException {
  178. }
  179. }
  180. ```
  181. ### User Menu Items
  182. *SINCE 1.6.0*
  183. You can provide your own user menu items by subclassing the *UserMenuExtension* class.
  184. ```java
  185. import java.util.Arrays;
  186. import java.util.List;
  187. import ro.fortsoft.pf4j.Extension;
  188. import com.gitblit.extensions.UserMenuExtension;
  189. import com.gitblit.models.Menu.ExternalLinkMenuItem;
  190. import com.gitblit.models.Menu.MenuItem;
  191. import com.gitblit.models.UserModel;
  192. @Extension
  193. public class MyUserMenuContributor extends UserMenuExtension {
  194. @Override
  195. public List<MenuItem> getMenuItems(UserModel user) {
  196. MenuItem item = new ExternalLinkMenuItem("Github", String.format("https://github.com/%s", user.username));
  197. return Arrays.asList(item);
  198. }
  199. }
  200. ```
  201. ### Navigation Links
  202. *SINCE 1.6.0*
  203. You can provide your own top-level navigation links by subclassing the *NavLinkExtension* class.
  204. ```java
  205. import java.util.Arrays;
  206. import java.util.List;
  207. import ro.fortsoft.pf4j.Extension;
  208. import com.gitblit.extensions.NavLinkExtension;
  209. import com.gitblit.models.UserModel;
  210. @Extension
  211. public class MyNavLink extends NavLinkExtension {
  212. @Override
  213. public List<NavLink> getNavLinks(UserModel user) {
  214. NavLink link = new ExternalLinkMenuItem("Github", String.format("https://github.com/%s", user.username));
  215. return Arrays.asList(link);
  216. }
  217. }
  218. ```
  219. ### Server Lifecycle Listener
  220. *SINCE 1.6.0*
  221. You can provide a lifecycle listener to be notified when Gitblit has completely started and just before Gitblit is gracefully terminated.
  222. ```java
  223. import org.slf4j.Logger;
  224. import org.slf4j.LoggerFactory;
  225. import ro.fortsoft.pf4j.Extension;
  226. import com.gitblit.extensions.LifeCycleListener;
  227. @Extension
  228. public class MyLifeCycleListener extends LifeCycleListener {
  229. final Logger log = LoggerFactory.getLogger(getClass());
  230. @Override
  231. public void onStartup() {
  232. log.info("Gitblit is Ready!!");
  233. }
  234. @Override
  235. public void onShutdown() {
  236. log.info("Gitblit is Going Down!!");
  237. }
  238. }
  239. ```
  240. ### Repository Lifecycle Listener
  241. *SINCE 1.6.0*
  242. You can provide a lifecycle listener to be notified when Gitblit has created or deleted a repository.
  243. ```java
  244. import org.slf4j.Logger;
  245. import org.slf4j.LoggerFactory;
  246. import ro.fortsoft.pf4j.Extension;
  247. import com.gitblit.extensions.RepositoryLifeCycleListener;
  248. import com.gitblit.models.RepositoryModel;
  249. @Extension
  250. public class MyRepoLifeCycleListener extends RepositoryLifeCycleListener {
  251. final Logger log = LoggerFactory.getLogger(getClass());
  252. @Override
  253. public void onCreation(RepositoryModel repo) {
  254. log.info("Gitblit created {}", repo);
  255. }
  256. @Override
  257. public void onFork(RepositoryModel origin, RepositoryModel fork) {
  258. log.info("{} forked to {}", origin, fork);
  259. }
  260. @Override
  261. public void onRename(String oldName, RepositoryModel repo) {
  262. log.info("{} renamed to {}", oldName, repo);
  263. }
  264. @Override
  265. public void onDeletion(RepositoryModel repo) {
  266. log.info("Gitblit deleted {}", repo);
  267. }
  268. }
  269. ```
  270. ### User/Team Lifecycle Listener
  271. *SINCE 1.6.0*
  272. You can provide a lifecycle listener to be notified when Gitblit has created or deleted a user or a team.
  273. ```java
  274. import org.slf4j.Logger;
  275. import org.slf4j.LoggerFactory;
  276. import ro.fortsoft.pf4j.Extension;
  277. import com.gitblit.extensions.UserTeamLifeCycleListener;
  278. import com.gitblit.models.TeamModel;
  279. import com.gitblit.models.UserModel;
  280. @Extension
  281. public class MyUserTeamLifeCycleListener extends UserTeamLifeCycleListener {
  282. final Logger log = LoggerFactory.getLogger(getClass());
  283. @Override
  284. public void onCreation(UserModel user) {
  285. log.info("Gitblit created user {}", user);
  286. }
  287. @Override
  288. public void onDeletion(UserModel user) {
  289. log.info("Gitblit deleted user {}", user);
  290. }
  291. @Override
  292. public void onCreation(TeamModel team) {
  293. log.info("Gitblit created team {}", team);
  294. }
  295. @Override
  296. public void onDeletion(TeamModel team) {
  297. log.info("Gitblit deleted team {}", team);
  298. }
  299. }
  300. ```