@@ -24,40 +24,29 @@ associated documentation files (the "Software"), to deal in the Software without
2424package org .jenkinsci .plugins .gogs ;
2525
2626import hudson .Extension ;
27- import hudson .tasks .Builder ;
28- import hudson .model .Descriptor ;
27+ import hudson .model .Job ;
2928import hudson .model .UnprotectedRootAction ;
30-
31- import java .util .Map ;
32- import java .util .List ;
33- import java .util .LinkedList ;
34- import java .util .LinkedHashMap ;
35-
36- import java .io .IOException ;
37- import java .net .URLDecoder ;
38- import java .util .logging .Level ;
39- import java .util .logging .Logger ;
40- import java .io .UnsupportedEncodingException ;
41- import javax .inject .Inject ;
42- import javax .servlet .ServletException ;
43-
44- import net .sf .json .JSONObject ;
45-
4629import jenkins .model .Jenkins ;
47-
30+ import net . sf . json . JSONObject ;
4831import org .apache .commons .io .IOUtils ;
49- import org .kohsuke .stapler .HttpResponse ;
50- import org .kohsuke .stapler .QueryParameter ;
5132import org .kohsuke .stapler .StaplerRequest ;
5233import org .kohsuke .stapler .StaplerResponse ;
5334
35+ import java .io .IOException ;
36+ import java .io .UnsupportedEncodingException ;
37+ import java .net .URLDecoder ;
38+ import java .util .LinkedHashMap ;
39+ import java .util .Map ;
40+ import java .util .logging .Logger ;
41+
5442/**
5543 * @author Alexander Verhaar
5644 */
5745@ Extension
5846public class GogsWebHook implements UnprotectedRootAction {
5947 private final static Logger LOGGER = Logger .getLogger (GogsWebHook .class .getName ());
6048 public static final String URLNAME = "gogs-webhook" ;
49+ private static final String DEFAULT_CHARSET = "UTF-8" ;
6150 private Jenkins jenkins = Jenkins .getInstance ();
6251 private StaplerResponse resp ;
6352
@@ -79,10 +68,9 @@ public String getUrlName() {
7968 * @param req request
8069 */
8170 public void doIndex (StaplerRequest req , StaplerResponse rsp ) throws IOException {
82- String url = null ;
8371 GogsResults result = new GogsResults ();
8472 GogsPayloadProcessor payloadProcessor = new GogsPayloadProcessor ();
85- GogsProjectProperty . DescriptorImpl projectProperty = jenkins . getDescriptorByType ( GogsProjectProperty . DescriptorImpl . class );
73+
8674 this .resp = rsp ;
8775
8876 // Get X-Gogs-Event
@@ -109,41 +97,48 @@ public void doIndex(StaplerRequest req, StaplerResponse rsp) throws IOException
10997 }
11098
11199 // Get the POST stream
112- String body = IOUtils .toString (req .getInputStream ());
100+ String body = IOUtils .toString (req .getInputStream (), DEFAULT_CHARSET );
113101 if ( !body .isEmpty () && req .getRequestURI ().contains ("/" + URLNAME + "/" ) ) {
114102 String contentType = req .getContentType ();
115103 if (contentType != null && contentType .startsWith ("application/x-www-form-urlencoded" )) {
116- body = URLDecoder .decode (body );
104+ body = URLDecoder .decode (body , DEFAULT_CHARSET );
117105 }
118106 if (body .startsWith ("payload=" )) {
119107 body = body .substring (8 );
120108 }
121109
122110 JSONObject jsonObject = JSONObject .fromObject (body );
123111 String gSecret = jsonObject .getString ("secret" ); /* Secret provided by Gogs */
124- String jSecret = projectProperty .getGogsSecret (); /* Secret provided by Jenkins */
125- // JSONObject repo = jsonObject.getJSONObject("repository");
126- // if (repo!=null) {
127- // url = repo.getString("url");
128- // }
129-
130- if ( gSecret !=null && !gSecret .isEmpty () ) {
131- /* Gogs secret is set */
132- if ( jSecret !=null && !jSecret .isEmpty ()) {
133- /* Jenkins secret is set */
134- if ( !jSecret .equals (gSecret ) ) {
135- /* Gogs and Jenkins secrets differs */
136- result .setStatus (403 , "Incorrect secret" );
137- } else {
138- /* Password is set in Jenkins and Gogs, and is correct */
139- result = payloadProcessor .triggerJobs (jobName , gogsDelivery );
112+
113+ String jSecret = null ;
114+ /* secret is stored in the properties of Job */
115+ boolean foundJob = false ;
116+ for (Job job : jenkins .getAllItems (Job .class )) {
117+ foundJob = job .getName ().equals (jobName );
118+ if (foundJob ) {
119+ final GogsProjectProperty property = (GogsProjectProperty ) job
120+ .getProperty (GogsProjectProperty .class );
121+ if (property != null ) { /* only if Gogs secret is defined on the job */
122+ jSecret = property .getGogsSecret (); /* Secret provided by Jenkins */
140123 }
141- } else {
142- result . setStatus ( 403 , "Incorrect secret" ) ;
124+ /* no need to go through all other jobs */
125+ break ;
143126 }
144- } else {
145- /* No password is set in Jenkins or Gogs, run without secrets */
127+ }
128+
129+ if (!foundJob ) {
130+ String msg = String .format ("Job '%s' is not defined in Jenkins" , jobName );
131+ result .setStatus (404 , msg );
132+ LOGGER .warning (msg );
133+ } else if (isNullOrEmpty (jSecret ) && isNullOrEmpty (gSecret )) {
134+ /* No password is set in Jenkins and Gogs, run without secrets */
135+ result = payloadProcessor .triggerJobs (jobName , gogsDelivery );
136+ } else if (!isNullOrEmpty (jSecret ) && jSecret .equals (gSecret )) {
137+ /* Password is set in Jenkins and Gogs, and is correct */
146138 result = payloadProcessor .triggerJobs (jobName , gogsDelivery );
139+ } else {
140+ /* Gogs and Jenkins secrets differs */
141+ result .setStatus (403 , "Incorrect secret" );
147142 }
148143 } else {
149144 result .setStatus (404 , "No payload or URI contains invalid entries." );
@@ -155,7 +150,7 @@ public void doIndex(StaplerRequest req, StaplerResponse rsp) throws IOException
155150 /**
156151 * Exit the WebHook
157152 *
158- * @param results GogsResults
153+ * @param result GogsResults
159154 */
160155 private void exitWebHook (GogsResults result ) throws IOException {
161156 if ( result .getStatus () != 200 ) {
@@ -180,12 +175,16 @@ private static Map<String, String> splitQuery(String qs) throws UnsupportedEncod
180175 final String [] pairs = qs .split ("&" );
181176 for (String pair : pairs ) {
182177 final int idx = pair .indexOf ("=" );
183- final String key = idx > 0 ? URLDecoder .decode (pair .substring (0 , idx ), "UTF-8" ) : pair ;
184- final String value = idx > 0 && pair .length () > idx + 1 ? URLDecoder .decode (pair .substring (idx + 1 ), "UTF-8" ) : null ;
178+ final String key = idx > 0 ? URLDecoder .decode (pair .substring (0 , idx ), DEFAULT_CHARSET ) : pair ;
179+ final String value = idx > 0 && pair .length () > idx + 1 ? URLDecoder .decode (pair .substring (idx + 1 ), DEFAULT_CHARSET ) : null ;
185180 query_pairs .put (key ,value );
186181 }
187182 return query_pairs ;
188183 }
184+
185+ private boolean isNullOrEmpty (String s ) {
186+ return s == null || s .trim ().isEmpty ();
187+ }
189188}
190189
191190// vim: set ts=4 sw=4 tw=0 ft=java et :
0 commit comments