1
2
3
4
5
6
7
8
9
10 package org.jenkinsci.plugins.darcs.browsers;
11
12 import org.jenkinsci.plugins.darcs.DarcsChangeSet;
13
14 import java.io.IOException;
15 import java.net.URL;
16 import java.net.MalformedURLException;
17 import java.util.regex.Pattern;
18 import javax.servlet.ServletException;
19
20 import hudson.Extension;
21 import hudson.Util;
22 import hudson.model.Descriptor;
23 import hudson.scm.RepositoryBrowser;
24 import hudson.util.FormValidation;
25
26 import net.sf.json.JSONObject;
27
28 import org.kohsuke.stapler.DataBoundConstructor;
29 import org.kohsuke.stapler.QueryParameter;
30 import org.kohsuke.stapler.StaplerRequest;
31
32
33
34
35
36 public class DarcsWeb extends DarcsRepositoryBrowser {
37
38
39
40
41 private static final long serialVersionUID = 1L;
42 public final URL url;
43 public final String repo;
44
45 @DataBoundConstructor
46 public DarcsWeb(final URL url, final String repo) throws MalformedURLException {
47 this.url = url;
48 this.repo = repo;
49 }
50
51 protected DarcsQueryBuilder createDefaultQuery() {
52 final DarcsQueryBuilder query = new DarcsQueryBuilder(DarcsQueryBuilder.SeparatorType.SEMICOLONS);
53 return query.add("r=" + repo);
54 }
55
56 protected DarcsQueryBuilder createDefaultQuery(final String action) {
57 final DarcsQueryBuilder query = createDefaultQuery();
58 return query.add("a=" + action);
59 }
60
61
62
63
64
65
66
67
68
69
70 public URL getChangeSetLink(final DarcsChangeSet changeSet) throws IOException {
71 final DarcsQueryBuilder query = createDefaultQuery("commit");
72 query.add("h=" + changeSet.getHash());
73
74 return new URL(url + query.toString());
75 }
76
77
78
79
80
81
82
83
84
85
86
87 public URL getFileDiffLink(final DarcsChangeSet changeSet, final String file) throws IOException {
88 final DarcsQueryBuilder query = createDefaultQuery("filediff");
89 query.add("h=" + changeSet.getHash());
90 query.add("f=" + file);
91
92 return new URL(url + query.toString());
93 }
94
95
96
97
98 @Extension
99 public static class DescriptorImpl extends Descriptor<RepositoryBrowser<?>> {
100
101 private static final Pattern URI_PATTERN = Pattern.compile(".+/cgi-bin/darcsweb.cgi");
102
103 public String getDisplayName() {
104 return "Darcsweb";
105 }
106
107
108
109
110
111
112
113
114
115
116
117 public FormValidation doCheck(@QueryParameter final String value) throws IOException, ServletException {
118
119 return (new FormValidation.URLCheck() {
120
121 @Override
122 protected FormValidation check() throws IOException, ServletException {
123 final String uri = Util.fixEmpty(value);
124
125 if (null == uri) {
126 return FormValidation.ok();
127 }
128
129 if (!uri.startsWith("http://") && ! uri.startsWith("https://")) {
130 return FormValidation.errorWithMarkup("The URI should start either with <tt>http://</tt> or "
131 + "<tt>https://</tt>!");
132 }
133
134 if (!URI_PATTERN.matcher(uri).matches()) {
135 return FormValidation.errorWithMarkup("The URI should look like <tt>"
136 + "http://.../cgi-bin/darcsweb.cgi</tt>!");
137 }
138
139 try {
140 if (!findText(open(new URL(uri)), "Crece desde el pueblo el futuro / crece desde el pie")) {
141 return FormValidation.error("This is a valid URI but it doesn't look like DarcsWeb!");
142 }
143 } catch (IOException e) {
144 handleIOException(uri, e);
145 }
146
147 return FormValidation.ok();
148 }
149 }).check();
150 }
151
152 @Override
153 public DarcsWeb newInstance(StaplerRequest req, JSONObject formData) throws FormException {
154 return req.bindParameters(DarcsWeb.class, "darcsweb.darcs.");
155 }
156 }
157
158 }