You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SummaryPage.java 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /*
  2. * Copyright 2011 gitblit.com.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.gitblit.wicket.pages;
  17. import java.awt.Color;
  18. import java.awt.Dimension;
  19. import java.text.MessageFormat;
  20. import java.text.ParseException;
  21. import java.util.ArrayList;
  22. import java.util.List;
  23. import org.apache.wicket.Component;
  24. import org.apache.wicket.PageParameters;
  25. import org.apache.wicket.markup.html.basic.Label;
  26. import org.apache.wicket.markup.html.link.BookmarkablePageLink;
  27. import org.apache.wicket.markup.html.panel.Fragment;
  28. import org.apache.wicket.markup.repeater.Item;
  29. import org.apache.wicket.markup.repeater.data.DataView;
  30. import org.apache.wicket.markup.repeater.data.ListDataProvider;
  31. import org.eclipse.jgit.lib.Repository;
  32. import org.eclipse.jgit.revwalk.RevCommit;
  33. import org.wicketstuff.googlecharts.ChartAxis;
  34. import org.wicketstuff.googlecharts.ChartAxisType;
  35. import org.wicketstuff.googlecharts.ChartProvider;
  36. import org.wicketstuff.googlecharts.ChartType;
  37. import org.wicketstuff.googlecharts.IChartData;
  38. import org.wicketstuff.googlecharts.LineStyle;
  39. import org.wicketstuff.googlecharts.MarkerType;
  40. import org.wicketstuff.googlecharts.ShapeMarker;
  41. import com.gitblit.GitBlit;
  42. import com.gitblit.Keys;
  43. import com.gitblit.models.Metric;
  44. import com.gitblit.models.PathModel;
  45. import com.gitblit.models.RepositoryModel;
  46. import com.gitblit.models.UserModel;
  47. import com.gitblit.utils.JGitUtils;
  48. import com.gitblit.utils.MarkdownUtils;
  49. import com.gitblit.utils.StringUtils;
  50. import com.gitblit.wicket.GitBlitWebSession;
  51. import com.gitblit.wicket.WicketUtils;
  52. import com.gitblit.wicket.charting.SecureChart;
  53. import com.gitblit.wicket.panels.BranchesPanel;
  54. import com.gitblit.wicket.panels.LinkPanel;
  55. import com.gitblit.wicket.panels.LogPanel;
  56. import com.gitblit.wicket.panels.RepositoryUrlPanel;
  57. import com.gitblit.wicket.panels.TagsPanel;
  58. public class SummaryPage extends RepositoryPage {
  59. public SummaryPage(PageParameters params) {
  60. super(params);
  61. int numberCommits = GitBlit.getInteger(Keys.web.summaryCommitCount, 20);
  62. if (numberCommits <= 0) {
  63. numberCommits = 20;
  64. }
  65. int numberRefs = GitBlit.getInteger(Keys.web.summaryRefsCount, 5);
  66. Repository r = getRepository();
  67. final RepositoryModel model = getRepositoryModel();
  68. UserModel user = GitBlitWebSession.get().getUser();
  69. if (user == null) {
  70. user = UserModel.ANONYMOUS;
  71. }
  72. List<Metric> metrics = null;
  73. Metric metricsTotal = null;
  74. if (!model.skipSummaryMetrics && GitBlit.getBoolean(Keys.web.generateActivityGraph, true)) {
  75. metrics = GitBlit.self().getRepositoryDefaultMetrics(model, r);
  76. metricsTotal = metrics.remove(0);
  77. }
  78. addSyndicationDiscoveryLink();
  79. // repository description
  80. add(new Label("repositoryDescription", getRepositoryModel().description));
  81. // owner links
  82. final List<String> owners = new ArrayList<String>(getRepositoryModel().owners);
  83. ListDataProvider<String> ownersDp = new ListDataProvider<String>(owners);
  84. DataView<String> ownersView = new DataView<String>("repositoryOwners", ownersDp) {
  85. private static final long serialVersionUID = 1L;
  86. int counter = 0;
  87. public void populateItem(final Item<String> item) {
  88. String ownername = item.getModelObject();
  89. UserModel ownerModel = GitBlit.self().getUserModel(ownername);
  90. if (ownerModel != null) {
  91. item.add(new LinkPanel("owner", null, ownerModel.getDisplayName(), UserPage.class,
  92. WicketUtils.newUsernameParameter(ownerModel.username)).setRenderBodyOnly(true));
  93. } else {
  94. Label owner = new Label("owner", ownername);
  95. WicketUtils.setCssStyle(owner, "text-decoration: line-through;");
  96. WicketUtils.setHtmlTooltip(owner, MessageFormat.format(getString("gb.failedToFindAccount"), ownername));
  97. item.add(owner);
  98. }
  99. counter++;
  100. item.add(new Label("comma", ",").setVisible(counter < owners.size()));
  101. item.setRenderBodyOnly(true);
  102. }
  103. };
  104. ownersView.setRenderBodyOnly(true);
  105. add(ownersView);
  106. add(WicketUtils.createTimestampLabel("repositoryLastChange",
  107. JGitUtils.getLastChange(r).when, getTimeZone(), getTimeUtils()));
  108. add(new Label("repositorySize", getRepositoryModel().size));
  109. if (metricsTotal == null) {
  110. add(new Label("branchStats", ""));
  111. } else {
  112. add(new Label("branchStats",
  113. MessageFormat.format(getString("gb.branchStats"), metricsTotal.count,
  114. metricsTotal.tag, getTimeUtils().duration(metricsTotal.duration))));
  115. }
  116. add(new BookmarkablePageLink<Void>("metrics", MetricsPage.class,
  117. WicketUtils.newRepositoryParameter(repositoryName)));
  118. add(new RepositoryUrlPanel("repositoryUrlPanel", false, user, model));
  119. add(new LogPanel("commitsPanel", repositoryName, getRepositoryModel().HEAD, r, numberCommits, 0, getRepositoryModel().showRemoteBranches));
  120. add(new TagsPanel("tagsPanel", repositoryName, r, numberRefs).hideIfEmpty());
  121. add(new BranchesPanel("branchesPanel", getRepositoryModel(), r, numberRefs, false).hideIfEmpty());
  122. if (getRepositoryModel().showReadme) {
  123. String htmlText = null;
  124. String markdownText = null;
  125. String readme = null;
  126. try {
  127. RevCommit head = JGitUtils.getCommit(r, null);
  128. List<String> markdownExtensions = GitBlit.getStrings(Keys.web.markdownExtensions);
  129. List<PathModel> paths = JGitUtils.getFilesInPath(r, null, head);
  130. for (PathModel path : paths) {
  131. if (!path.isTree()) {
  132. String name = path.name.toLowerCase();
  133. if (name.startsWith("readme")) {
  134. if (name.indexOf('.') > -1) {
  135. String ext = name.substring(name.lastIndexOf('.') + 1);
  136. if (markdownExtensions.contains(ext)) {
  137. readme = path.name;
  138. break;
  139. }
  140. }
  141. }
  142. }
  143. }
  144. if (!StringUtils.isEmpty(readme)) {
  145. String [] encodings = GitBlit.getEncodings();
  146. markdownText = JGitUtils.getStringContent(r, head.getTree(), readme, encodings);
  147. htmlText = MarkdownUtils.transformMarkdown(markdownText);
  148. }
  149. } catch (ParseException p) {
  150. markdownText = MessageFormat.format("<div class=\"alert alert-error\"><strong>{0}:</strong> {1}</div>{2}", getString("gb.error"), getString("gb.markdownFailure"), markdownText);
  151. htmlText = StringUtils.breakLinesForHtml(markdownText);
  152. }
  153. Fragment fragment = new Fragment("readme", "markdownPanel");
  154. fragment.add(new Label("readmeFile", readme));
  155. // Add the html to the page
  156. Component content = new Label("readmeContent", htmlText).setEscapeModelStrings(false);
  157. fragment.add(content.setVisible(!StringUtils.isEmpty(htmlText)));
  158. add(fragment);
  159. } else {
  160. add(new Label("readme").setVisible(false));
  161. }
  162. // Display an activity line graph
  163. insertActivityGraph(metrics);
  164. }
  165. @Override
  166. protected String getPageName() {
  167. return getString("gb.summary");
  168. }
  169. private void insertActivityGraph(List<Metric> metrics) {
  170. if ((metrics != null) && (metrics.size() > 0)
  171. && GitBlit.getBoolean(Keys.web.generateActivityGraph, true)) {
  172. IChartData data = WicketUtils.getChartData(metrics);
  173. ChartProvider provider = new ChartProvider(new Dimension(290, 100), ChartType.LINE,
  174. data);
  175. ChartAxis dateAxis = new ChartAxis(ChartAxisType.BOTTOM);
  176. dateAxis.setLabels(new String[] { metrics.get(0).name,
  177. metrics.get(metrics.size() / 2).name, metrics.get(metrics.size() - 1).name });
  178. provider.addAxis(dateAxis);
  179. ChartAxis commitAxis = new ChartAxis(ChartAxisType.LEFT);
  180. commitAxis.setLabels(new String[] { "",
  181. String.valueOf((int) WicketUtils.maxValue(metrics)) });
  182. provider.addAxis(commitAxis);
  183. provider.setLineStyles(new LineStyle[] { new LineStyle(2, 4, 0), new LineStyle(0, 4, 1) });
  184. provider.addShapeMarker(new ShapeMarker(MarkerType.CIRCLE, Color.decode("#002060"), 1, -1, 5));
  185. add(new SecureChart("commitsChart", provider));
  186. } else {
  187. add(WicketUtils.newBlankImage("commitsChart"));
  188. }
  189. }
  190. }