Skip to content

Commit 77c75f7

Browse files
committed
Merge branch 'dev'
2 parents 95fddb0 + ab27d3e commit 77c75f7

File tree

8 files changed

+158
-20
lines changed

8 files changed

+158
-20
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,3 @@ release.properties
2626
dependency-reduced-pom.xml
2727
buildNumber.properties
2828
.mvn/timing.properties
29-

README.md

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,3 @@ http(s)://<< jenkins-server >>/gogs-webhook/?job=<< jobname >>
99

1010
Example how your the webhook in Gogs should look like:
1111
![Example webhook](https://raw.githubusercontent.com/sanderv32/gogs-webhook-plugin/master/bin/gogs-webhook-screenshot.png)
12-
13-
#### TODO:
14-
- Implement Gogs secret

pom.xml

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,23 @@
66
<version>1.580</version><!-- which version of Jenkins is this plugin built against? -->
77
</parent>
88

9+
<developers>
10+
<developer>
11+
<id>sanderv32</id>
12+
<name>Alexander Verhaar</name>
13+
</developer>
14+
</developers>
15+
16+
<licenses>
17+
<license>
18+
<name>MIT</name>
19+
<url>http://www.opensource.org/licenses/mit-license.php</url>
20+
</license>
21+
</licenses>
22+
923
<groupId>org.jenkins-ci.plugins</groupId>
1024
<artifactId>gogs-webhook</artifactId>
11-
<version>0.8.13</version>
25+
<version>0.8.45</version>
1226
<packaging>hpi</packaging>
1327
<name>Jenkins Gogs plugin</name>
1428
<description>Adds Gogs integration to Jenkins</description>

src/main/java/org/jenkinsci/plugins/gogs/GogsPayloadProcessor.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,18 +49,18 @@ public GogsResults triggerJobs(String jobName, String deliveryID) {
4949

5050
SecurityContext old = Jenkins.getInstance().getACL().impersonate(ACL.SYSTEM);
5151
for (AbstractProject<?,?> project : Jenkins.getInstance().getAllItems(AbstractProject.class)) {
52-
if ( project.getName().equals(jobName)) {
52+
if ( project.getName().equals(jobName) ) {
5353

5454
Cause cause = new GogsCause(deliveryID);
5555
project.scheduleBuild(0, cause);
5656
didJob = true;
57-
result.Message = String.format("Job '%s' is executed",jobName);
57+
result.setMessage(String.format("Job '%s' is executed",jobName));
5858
}
5959
}
6060
if (!didJob) {
61-
result.Status = 404;
62-
result.Message = String.format("Job '%s' is not defined in Jenkins",jobName);
63-
LOGGER.warning(result.Message);
61+
String msg = String.format("Job '%s' is not defined in Jenkins",jobName);
62+
result.setStatus(404, msg);
63+
LOGGER.warning(msg);
6464
}
6565
SecurityContextHolder.setContext(old);
6666

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
3+
The MIT License (MIT)
4+
Copyright (c) 2016 Alexander Verhaar
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
7+
associated documentation files (the "Software"), to deal in the Software without restriction, including
8+
without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to
10+
the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all copies or substantial
13+
portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
16+
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
17+
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
19+
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
20+
OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21+
22+
*/
23+
24+
package org.jenkinsci.plugins.gogs;
25+
26+
import org.jenkinsci.plugins.gogs.GogsWebHook;
27+
import hudson.Extension;
28+
import hudson.model.Job;
29+
import hudson.model.JobProperty;
30+
import hudson.model.JobPropertyDescriptor;
31+
32+
import net.sf.json.JSONObject;
33+
34+
import org.kohsuke.stapler.StaplerRequest;
35+
import org.kohsuke.stapler.DataBoundConstructor;
36+
37+
import java.util.logging.Logger;
38+
39+
public class GogsProjectProperty extends JobProperty<Job<?, ?>> {
40+
private final String gogsSecret;
41+
42+
@DataBoundConstructor
43+
public GogsProjectProperty(String gogsSecret) {
44+
this.gogsSecret = gogsSecret;
45+
}
46+
47+
public String getGogsSecret() {
48+
return this.gogsSecret;
49+
}
50+
51+
private static final Logger LOGGER = Logger.getLogger(GogsWebHook.class.getName());
52+
53+
@Extension
54+
public static final class DescriptorImpl extends JobPropertyDescriptor {
55+
public static final String GOGS_PROJECT_BLOCK_NAME = "gogsProject";
56+
private String gogsSecret;
57+
58+
public String getGogsSecret() {
59+
return gogsSecret;
60+
}
61+
62+
public JobProperty<?> newInstance(StaplerRequest req, JSONObject formData) throws FormException {
63+
GogsProjectProperty tpp = req.bindJSON(
64+
GogsProjectProperty.class,
65+
formData.getJSONObject(GOGS_PROJECT_BLOCK_NAME)
66+
);
67+
if ( tpp != null ) {
68+
LOGGER.info(formData.toString());
69+
LOGGER.info(tpp.gogsSecret);
70+
71+
gogsSecret = tpp.gogsSecret;
72+
}
73+
return tpp;
74+
}
75+
76+
@Override
77+
public String getDisplayName() {
78+
return "Gogs Secret";
79+
}
80+
}
81+
}

src/main/java/org/jenkinsci/plugins/gogs/GogsResults.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,28 @@ associated documentation files (the "Software"), to deal in the Software without
2424
package org.jenkinsci.plugins.gogs;
2525

2626
public class GogsResults {
27-
int Status;
28-
String Message;
27+
private int Status;
28+
private String Message;
2929

3030
public GogsResults() {
3131
this.Status = 200;
3232
this.Message = "OK";
3333
}
3434

35+
public void setMessage(String msg) {
36+
this.Message = msg;
37+
}
38+
3539
public void setStatus(int status, String msg) {
3640
this.Status = status;
3741
this.Message = msg;
3842
}
43+
44+
public int getStatus() {
45+
return this.Status;
46+
}
47+
48+
public String getMessage() {
49+
return this.Message;
50+
}
3951
}

src/main/java/org/jenkinsci/plugins/gogs/GogsWebHook.java

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ associated documentation files (the "Software"), to deal in the Software without
2424
package org.jenkinsci.plugins.gogs;
2525

2626
import hudson.Extension;
27+
import hudson.tasks.Builder;
28+
import hudson.model.Descriptor;
2729
import hudson.model.UnprotectedRootAction;
2830

2931
import java.util.Map;
@@ -41,6 +43,8 @@ associated documentation files (the "Software"), to deal in the Software without
4143

4244
import net.sf.json.JSONObject;
4345

46+
import jenkins.model.Jenkins;
47+
4448
import org.apache.commons.io.IOUtils;
4549
import org.kohsuke.stapler.HttpResponse;
4650
import org.kohsuke.stapler.QueryParameter;
@@ -54,6 +58,7 @@ associated documentation files (the "Software"), to deal in the Software without
5458
public class GogsWebHook implements UnprotectedRootAction {
5559
private final static Logger LOGGER = Logger.getLogger(GogsWebHook.class.getName());
5660
public static final String URLNAME = "gogs-webhook";
61+
private Jenkins jenkins = Jenkins.getInstance();
5762
private StaplerResponse resp;
5863

5964
public String getDisplayName() {
@@ -77,6 +82,7 @@ public String getUrlName() {
7782
public void doIndex(StaplerRequest req, StaplerResponse rsp) throws IOException {
7883
GogsResults result = new GogsResults();
7984
GogsPayloadProcessor payloadProcessor = new GogsPayloadProcessor();
85+
GogsProjectProperty.DescriptorImpl projectProperty = jenkins.getDescriptorByType(GogsProjectProperty.DescriptorImpl.class);
8086
this.resp = rsp;
8187

8288
// Get X-Gogs-Event
@@ -88,7 +94,7 @@ public void doIndex(StaplerRequest req, StaplerResponse rsp) throws IOException
8894

8995
// Get X-Gogs-Delivery header with deliveryID
9096
String gogsDelivery = req.getHeader("X-Gogs-Delivery");
91-
if (gogsDelivery.isEmpty()) {
97+
if ( gogsDelivery==null && gogsDelivery.isEmpty() ) {
9298
gogsDelivery = "Triggered by Jenkins-Gogs-Plugin. Delivery ID unknown.";
9399
} else {
94100
gogsDelivery = "Gogs-ID: " + gogsDelivery;
@@ -97,7 +103,7 @@ public void doIndex(StaplerRequest req, StaplerResponse rsp) throws IOException
97103
// Get querystring from the URI
98104
Map querystring = splitQuery(req.getQueryString());
99105
String jobName = querystring.get("job").toString();
100-
if ( jobName!=null && jobName.isEmpty()) {
106+
if ( jobName!=null && jobName.isEmpty() ) {
101107
result.setStatus(404, "Parameter 'job' is missing or no value assigned.");
102108
exitWebHook(result);
103109
}
@@ -114,9 +120,28 @@ public void doIndex(StaplerRequest req, StaplerResponse rsp) throws IOException
114120
}
115121

116122
JSONObject jsonObject = JSONObject.fromObject(body);
123+
String gSecret = jsonObject.getString("secret"); /* Secret provided by Gogs */
124+
String jSecret = projectProperty.getGogsSecret(); /* Secret provided by Jenkins */
117125
String url = jsonObject.getJSONObject("repository").getString("url");
118126

119-
result = payloadProcessor.triggerJobs(jobName, gogsDelivery);
127+
if ( gSecret!=null && !gSecret.isEmpty() ) {
128+
/* Gogs secret is set */
129+
if ( jSecret!=null && !jSecret.isEmpty()) {
130+
/* Jenkins secret is set */
131+
if ( !jSecret.equals(gSecret) ) {
132+
/* Gogs and Jenkins secrets differs */
133+
result.setStatus(403, "Incorrect secret");
134+
} else {
135+
/* Password is set in Jenkins and Gogs, and is correct */
136+
result = payloadProcessor.triggerJobs(jobName, gogsDelivery);
137+
}
138+
} else {
139+
result.setStatus(403, "Incorrect secret");
140+
}
141+
} else {
142+
/* No password is set in Jenkins or Gogs, run without secrets */
143+
result = payloadProcessor.triggerJobs(jobName, gogsDelivery);
144+
}
120145
} else {
121146
result.setStatus(404, "No payload or URI contains invalid entries.");
122147
}
@@ -130,13 +155,13 @@ public void doIndex(StaplerRequest req, StaplerResponse rsp) throws IOException
130155
* @param results GogsResults
131156
*/
132157
private void exitWebHook(GogsResults result) throws IOException {
133-
if ( result.Status != 200 ) {
134-
LOGGER.warning(result.Message);
158+
if ( result.getStatus() != 200 ) {
159+
LOGGER.warning(result.getMessage());
135160
}
136161
JSONObject json = new JSONObject();
137-
json.put("result", result.Status==200 ? "OK" : "ERROR");
138-
json.put("message", result.Message);
139-
resp.setStatus(result.Status);
162+
json.put("result", result.getStatus()==200 ? "OK" : "ERROR");
163+
json.put("message", result.getMessage());
164+
resp.setStatus(result.getStatus());
140165
resp.addHeader("Content-Type","application/json");
141166
resp.getWriter().print(json.toString());
142167
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?jelly escape-by-default='true'?>
2+
<j:jelly xmlns:j="jelly:core" xmlns:f="/lib/form">
3+
<f:block>
4+
<f:optionalBlock name="${descriptor.GOGS_PROJECT_BLOCK_NAME}" title="Use Gogs secret" checked="${instance.gogsSecret != null}">
5+
<f:entry title="${%Secret}">
6+
<f:password field="gogsSecret" />
7+
</f:entry>
8+
</f:optionalBlock>
9+
</f:block>
10+
</j:jelly>

0 commit comments

Comments
 (0)