View Javadoc

1   /*
2    * LICENSE
3    *
4    * "THE BEER-WARE LICENSE" (Revision 42):
5    * "Sven Strittmatter" <ich@weltraumschaf.de> wrote this file.
6    * As long as you retain this notice you can do whatever you want with
7    * this stuff. If we meet some day, and you think this stuff is worth it,
8    * you can buy me a beer in return.
9    */
10  
11  package org.jenkinsci.plugins.darcs.browsers;
12  
13  import org.jenkinsci.plugins.darcs.DarcsChangeSet;
14  
15  import java.io.IOException;
16  import java.net.URL;
17  import java.net.MalformedURLException;
18  import java.util.regex.Pattern;
19  import javax.servlet.ServletException;
20  
21  import hudson.Extension;
22  import hudson.Util;
23  import hudson.model.Descriptor;
24  import hudson.scm.RepositoryBrowser;
25  import hudson.util.FormValidation;
26  
27  import net.sf.json.JSONObject;
28  
29  import org.kohsuke.stapler.DataBoundConstructor;
30  import org.kohsuke.stapler.QueryParameter;
31  import org.kohsuke.stapler.StaplerRequest;
32  
33  /**
34   *
35   * @author Sven Strittmatter <ich@weltraumschaf.de>
36   */
37  public final class Darcsden extends DarcsRepositoryBrowser {
38  
39      /**
40       * Serial version UID.
41       */
42      private static final long serialVersionUID = 1L;
43  
44      public final URL url;
45  
46      @DataBoundConstructor
47      public Darcsden(final URL url) throws MalformedURLException {
48          this.url = new URL(Util.removeTrailingSlash(url.toString()));
49      }
50  
51      public URL getChangeSetLink(final DarcsChangeSet changeSet) throws IOException {
52          final String hash = changeSet.getHash();
53          final String shortHash = hash.substring(0, hash.lastIndexOf('-'));
54          final DarcsQueryBuilder query = new DarcsQueryBuilder(DarcsQueryBuilder.SeparatorType.SLASHES);
55          query.add("patch")
56               .add(shortHash);
57  
58          return new URL(url + query.toString());
59      }
60  
61      public URL getFileDiffLink(final DarcsChangeSet changeSet, final String file) throws IOException {
62          return null;
63      }
64  
65      /**
66       * Darcsden repository browser description.
67       */
68      @Extension
69      public static class DescriptorImpl extends Descriptor<RepositoryBrowser<?>> {
70  
71          private static final Pattern URI_PATTERN = Pattern.compile("http://darcsden.com/.+");
72  
73          public String getDisplayName() {
74              return "Darcsden";
75          }
76  
77          /**
78           * Validates the URL given in the config formular.
79           *
80           * @todo implement check.
81           *
82           * @param value
83           * @return
84           * @throws IOException
85           * @throws ServletException
86           */
87          public FormValidation doCheck(@QueryParameter final String value) throws IOException, ServletException {
88  
89              return (new FormValidation.URLCheck() {
90  
91                  @Override
92                  protected FormValidation check() throws IOException, ServletException {
93                      final String uri = Util.fixEmpty(value);
94  
95                      if (null == uri) { // nothing entered yet
96                          return FormValidation.ok();
97                      }
98  
99                      if (!URI_PATTERN.matcher(uri).matches()) {
100                         return FormValidation.errorWithMarkup("The URI should look like "
101                                 + "<tt>http://darcsden.com/...</tt>!");
102                     }
103 
104                     return FormValidation.ok();
105                 }
106             }).check();
107         }
108 
109         @Override
110         public DarcsWeb newInstance(final StaplerRequest req, final JSONObject formData) throws Descriptor.FormException {
111             return req.bindParameters(DarcsWeb.class, "darcsden.darcs.");
112         }
113     }
114 
115 }