View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.report.projectinfo;
20  
21  import javax.inject.Inject;
22  
23  import java.util.Arrays;
24  import java.util.HashSet;
25  import java.util.List;
26  import java.util.Locale;
27  import java.util.Set;
28  
29  import org.apache.maven.doxia.sink.Sink;
30  import org.apache.maven.model.CiManagement;
31  import org.apache.maven.model.Model;
32  import org.apache.maven.model.Notifier;
33  import org.apache.maven.plugins.annotations.Mojo;
34  import org.apache.maven.project.ProjectBuilder;
35  import org.apache.maven.reporting.MavenReportException;
36  import org.apache.maven.repository.RepositorySystem;
37  import org.codehaus.plexus.i18n.I18N;
38  
39  /**
40   * Generates the Project Continuous Integration Management report.
41   *
42   * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton </a>
43   * @since 2.0
44   */
45  @Mojo(name = "ci-management")
46  public class CiManagementReport extends AbstractProjectInfoReport {
47  
48      @Inject
49      public CiManagementReport(RepositorySystem repositorySystem, I18N i18n, ProjectBuilder projectBuilder) {
50          super(repositorySystem, i18n, projectBuilder);
51      }
52      // ----------------------------------------------------------------------
53      // Public methods
54      // ----------------------------------------------------------------------
55  
56      @Override
57      public boolean canGenerateReport() throws MavenReportException {
58          boolean result = super.canGenerateReport();
59          if (result && skipEmptyReport) {
60              CiManagement cim = getProject().getModel().getCiManagement();
61              result = cim != null;
62          }
63  
64          return result;
65      }
66  
67      @Override
68      public void executeReport(Locale locale) {
69          CiManagementRenderer r =
70                  new CiManagementRenderer(getSink(), getProject().getModel(), getI18N(locale), locale);
71  
72          r.render();
73      }
74  
75      /** {@inheritDoc} */
76      public String getOutputName() {
77          return "ci-management";
78      }
79  
80      @Override
81      protected String getI18Nsection() {
82          return "ci-management";
83      }
84  
85      // ----------------------------------------------------------------------
86      // Private
87      // ----------------------------------------------------------------------
88  
89      /**
90       * Internal renderer class
91       */
92      private static class CiManagementRenderer extends AbstractProjectInfoRenderer {
93  
94          private static final Set<String> SYSTEMS = new HashSet<>(Arrays.asList(
95                  "anthill",
96                  "bamboo",
97                  "buildforge",
98                  "continuum",
99                  "cruisecontrol",
100                 "github",
101                 "hudson",
102                 "jenkins",
103                 "luntbuild",
104                 "teamcity",
105                 "travis"));
106 
107         private Model model;
108 
109         CiManagementRenderer(Sink sink, Model model, I18N i18n, Locale locale) {
110             super(sink, i18n, locale);
111 
112             this.model = model;
113         }
114 
115         @Override
116         protected String getI18Nsection() {
117             return "ci-management";
118         }
119 
120         @Override
121         protected void renderBody() {
122             CiManagement cim = model.getCiManagement();
123             if (cim == null) {
124                 startSection(getTitle());
125 
126                 paragraph(getI18nString("nocim"));
127 
128                 endSection();
129 
130                 return;
131             }
132 
133             String system = cim.getSystem();
134             String url = cim.getUrl();
135             List<Notifier> notifiers = cim.getNotifiers();
136 
137             // Overview
138             startSection(getI18nString("overview.title"));
139 
140             sink.paragraph();
141             linkPatternedText(getIntroForCiManagementSystem(system));
142             sink.paragraph_();
143 
144             endSection();
145 
146             // Access
147             startSection(getI18nString("access"));
148 
149             if (!(url == null || url.isEmpty())) {
150                 paragraph(getI18nString("url"));
151 
152                 verbatimLink(url, url);
153             } else {
154                 paragraph(getI18nString("nourl"));
155             }
156 
157             endSection();
158 
159             // Notifiers
160             startSection(getI18nString("notifiers.title"));
161 
162             if (notifiers == null || notifiers.isEmpty()) {
163                 paragraph(getI18nString("notifiers.nolist"));
164             } else {
165                 sink.paragraph();
166                 sink.text(getI18nString("notifiers.intro"));
167                 sink.paragraph_();
168 
169                 startTable();
170 
171                 String type = getI18nString("notifiers.column.type");
172                 String address = getI18nString("notifiers.column.address");
173                 String configuration = getI18nString("notifiers.column.configuration");
174 
175                 tableHeader(new String[] {type, address, configuration});
176 
177                 for (Notifier notifier : notifiers) {
178                     tableRow(new String[] {
179                         notifier.getType(),
180                         createLinkPatternedText(notifier.getAddress(), notifier.getAddress()),
181                         propertiesToString(notifier.getConfiguration())
182                     });
183                 }
184 
185                 endTable();
186             }
187 
188             endSection();
189         }
190 
191         /**
192          * Search system description.
193          *
194          * @param system a system for description
195          * @return system description from properties
196          */
197         private String getIntroForCiManagementSystem(String system) {
198             if (system == null || system.isEmpty()) {
199                 return getI18nString("general.intro");
200             }
201 
202             String systemLowerCase = system.toLowerCase(Locale.ENGLISH);
203 
204             for (String systemName : SYSTEMS) {
205                 if (systemLowerCase.startsWith(systemName)) {
206                     return getI18nString(systemName + ".intro");
207                 }
208             }
209 
210             return getI18nString("general.intro");
211         }
212     }
213 }