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 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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.text.MessageFormat;
  18. import java.text.ParseException;
  19. import java.text.SimpleDateFormat;
  20. import java.util.ArrayList;
  21. import java.util.Date;
  22. import java.util.List;
  23. import org.apache.wicket.Component;
  24. import org.apache.wicket.PageParameters;
  25. import org.apache.wicket.behavior.HeaderContributor;
  26. import org.apache.wicket.markup.html.basic.Label;
  27. import org.apache.wicket.markup.html.link.BookmarkablePageLink;
  28. import org.apache.wicket.markup.html.panel.Fragment;
  29. import org.apache.wicket.markup.repeater.Item;
  30. import org.apache.wicket.markup.repeater.data.DataView;
  31. import org.apache.wicket.markup.repeater.data.ListDataProvider;
  32. import org.eclipse.jgit.lib.Repository;
  33. import org.eclipse.jgit.revwalk.RevCommit;
  34. import com.gitblit.Keys;
  35. import com.gitblit.models.Metric;
  36. import com.gitblit.models.RepositoryModel;
  37. import com.gitblit.models.UserModel;
  38. import com.gitblit.utils.JGitUtils;
  39. import com.gitblit.utils.StringUtils;
  40. import com.gitblit.wicket.CacheControl;
  41. import com.gitblit.wicket.CacheControl.LastModified;
  42. import com.gitblit.wicket.GitBlitWebSession;
  43. import com.gitblit.wicket.MarkupProcessor;
  44. import com.gitblit.wicket.MarkupProcessor.MarkupDocument;
  45. import com.gitblit.wicket.MarkupProcessor.MarkupSyntax;
  46. import com.gitblit.wicket.WicketUtils;
  47. import com.gitblit.wicket.charting.Chart;
  48. import com.gitblit.wicket.charting.Charts;
  49. import com.gitblit.wicket.charting.Flotr2Charts;
  50. import com.gitblit.wicket.panels.BranchesPanel;
  51. import com.gitblit.wicket.panels.LinkPanel;
  52. import com.gitblit.wicket.panels.LogPanel;
  53. import com.gitblit.wicket.panels.RepositoryUrlPanel;
  54. import com.gitblit.wicket.panels.TagsPanel;
  55. @CacheControl(LastModified.REPOSITORY)
  56. public class SummaryPage extends RepositoryPage {
  57. public SummaryPage(PageParameters params) {
  58. super(params);
  59. int numberCommits = app().settings().getInteger(Keys.web.summaryCommitCount, 20);
  60. if (numberCommits <= 0) {
  61. numberCommits = 20;
  62. }
  63. int numberRefs = app().settings().getInteger(Keys.web.summaryRefsCount, 5);
  64. Repository r = getRepository();
  65. final RepositoryModel model = getRepositoryModel();
  66. UserModel user = GitBlitWebSession.get().getUser();
  67. if (user == null) {
  68. user = UserModel.ANONYMOUS;
  69. }
  70. List<Metric> metrics = null;
  71. Metric metricsTotal = null;
  72. if (!model.skipSummaryMetrics && app().settings().getBoolean(Keys.web.generateActivityGraph, true)) {
  73. metrics = app().repositories().getRepositoryDefaultMetrics(model, r);
  74. metricsTotal = metrics.remove(0);
  75. }
  76. addSyndicationDiscoveryLink();
  77. // repository description
  78. add(new Label("repositoryDescription", getRepositoryModel().description));
  79. // owner links
  80. final List<String> owners = new ArrayList<String>(getRepositoryModel().owners);
  81. ListDataProvider<String> ownersDp = new ListDataProvider<String>(owners);
  82. DataView<String> ownersView = new DataView<String>("repositoryOwners", ownersDp) {
  83. private static final long serialVersionUID = 1L;
  84. int counter = 0;
  85. @Override
  86. public void populateItem(final Item<String> item) {
  87. String ownername = item.getModelObject();
  88. UserModel ownerModel = app().users().getUserModel(ownername);
  89. if (ownerModel != null) {
  90. item.add(new LinkPanel("owner", null, ownerModel.getDisplayName(), UserPage.class,
  91. WicketUtils.newUsernameParameter(ownerModel.username)).setRenderBodyOnly(true));
  92. } else {
  93. Label owner = new Label("owner", ownername);
  94. WicketUtils.setCssStyle(owner, "text-decoration: line-through;");
  95. WicketUtils.setHtmlTooltip(owner, MessageFormat.format(getString("gb.failedToFindAccount"), ownername));
  96. item.add(owner);
  97. }
  98. counter++;
  99. item.add(new Label("comma", ",").setVisible(counter < owners.size()));
  100. item.setRenderBodyOnly(true);
  101. }
  102. };
  103. ownersView.setRenderBodyOnly(true);
  104. add(ownersView);
  105. add(WicketUtils.createTimestampLabel("repositoryLastChange",
  106. JGitUtils.getLastChange(r).when, getTimeZone(), getTimeUtils()));
  107. add(new Label("repositorySize", getRepositoryModel().size));
  108. if (metricsTotal == null) {
  109. add(new Label("branchStats", ""));
  110. } else {
  111. add(new Label("branchStats",
  112. MessageFormat.format(getString("gb.branchStats"), metricsTotal.count,
  113. metricsTotal.tag, getTimeUtils().duration(metricsTotal.duration))));
  114. }
  115. add(new BookmarkablePageLink<Void>("metrics", MetricsPage.class,
  116. WicketUtils.newRepositoryParameter(repositoryName)));
  117. add(new RepositoryUrlPanel("repositoryUrlPanel", false, user, model));
  118. add(new LogPanel("commitsPanel", repositoryName, getRepositoryModel().HEAD, r, numberCommits, 0, getRepositoryModel().showRemoteBranches));
  119. add(new TagsPanel("tagsPanel", repositoryName, r, numberRefs).hideIfEmpty());
  120. add(new BranchesPanel("branchesPanel", getRepositoryModel(), r, numberRefs, false).hideIfEmpty());
  121. if (app().settings().getBoolean(Keys.web.summaryShowReadme, false)) {
  122. // show a readme on the summary page
  123. MarkupDocument markupDoc = null;
  124. RevCommit head = JGitUtils.getCommit(r, null);
  125. if (head != null) {
  126. MarkupProcessor processor = new MarkupProcessor(app().settings(), app().xssFilter());
  127. markupDoc = processor.getReadme(r, repositoryName, getBestCommitId(head));
  128. }
  129. if (markupDoc == null || markupDoc.markup == null) {
  130. add(new Label("readme").setVisible(false));
  131. } else {
  132. Fragment fragment = new Fragment("readme", MarkupSyntax.PLAIN.equals(markupDoc.syntax) ? "plaintextPanel" : "markdownPanel", this);
  133. fragment.add(new Label("readmeFile", markupDoc.documentPath));
  134. // Add the html to the page
  135. Component content = new Label("readmeContent", markupDoc.html).setEscapeModelStrings(false);
  136. fragment.add(content.setVisible(!StringUtils.isEmpty(markupDoc.html)));
  137. add(fragment);
  138. }
  139. } else {
  140. // global, no readme on summary page
  141. add(new Label("readme").setVisible(false));
  142. }
  143. if (metrics == null || metrics.isEmpty()) {
  144. add(new Label("commitsChart").setVisible(false));
  145. } else {
  146. Charts charts = createCharts(metrics);
  147. add(new HeaderContributor(charts));
  148. }
  149. }
  150. @Override
  151. protected String getPageName() {
  152. return getString("gb.summary");
  153. }
  154. private Charts createCharts(List<Metric> metrics) {
  155. Charts charts = new Flotr2Charts();
  156. SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
  157. String displayFormat = "MMM dd";
  158. if(metrics.size() > 0 && metrics.get(0).name.length() == 7){
  159. df = new SimpleDateFormat("yyyy-MM");
  160. displayFormat = "yyyy MMM";
  161. }
  162. df.setTimeZone(getTimeZone());
  163. // build google charts
  164. Chart chart = charts.createLineChart("commitsChart", getString("gb.activity"), "day", getString("gb.commits"));
  165. chart.setDateFormat(displayFormat);
  166. for (Metric metric : metrics) {
  167. Date date;
  168. try {
  169. date = df.parse(metric.name);
  170. } catch (ParseException e) {
  171. logger().error("Unable to parse date: " + metric.name);
  172. return charts;
  173. }
  174. chart.addValue(date, (int)metric.count);
  175. if(metric.tag > 0 ){
  176. chart.addHighlight(date, (int)metric.count);
  177. }
  178. }
  179. charts.addChart(chart);
  180. return charts;
  181. }
  182. }