@@ -33,6 +33,7 @@ associated documentation files (the "Software"), to deal in the Software without
3333import org .acegisecurity .context .SecurityContextHolder ;
3434import org .apache .commons .codec .binary .Hex ;
3535import org .apache .commons .io .IOUtils ;
36+ import org .apache .commons .lang .StringUtils ;
3637import org .kohsuke .stapler .StaplerRequest ;
3738import org .kohsuke .stapler .StaplerResponse ;
3839
@@ -146,6 +147,8 @@ public void doIndex(StaplerRequest req, StaplerResponse rsp) throws IOException
146147 jobName = jobObject .toString ();
147148 }
148149
150+ final Object branchName = queryStringMap .get ("branch" );
151+
149152 // Get the POST stream
150153 String body = IOUtils .toString (req .getInputStream (), DEFAULT_CHARSET );
151154 if (!body .isEmpty () && req .getRequestURI ().contains ("/" + URLNAME + "/" )) {
@@ -160,6 +163,17 @@ public void doIndex(StaplerRequest req, StaplerResponse rsp) throws IOException
160163 return ;
161164 }
162165
166+ String ref = jsonObject .getString ("ref" );
167+ LOGGER .fine ("found ref " + ref );
168+ LOGGER .fine ("found branch " + branchName );
169+ if (null != branchName && !StringUtils .containsIgnoreCase (ref , (String ) branchName )) {
170+ // ignore all commit if they is not in context
171+ LOGGER .fine ("build was rejected" );
172+ result .setStatus (200 , String .format ("Commit is not relevant. Relevant context is %s" , branchName ));
173+ exitWebHook (result , rsp );
174+ return ;
175+ }
176+
163177 String contentType = req .getContentType ();
164178 if (contentType != null && contentType .startsWith ("application/x-www-form-urlencoded" )) {
165179 body = URLDecoder .decode (body , DEFAULT_CHARSET );
@@ -174,7 +188,7 @@ public void doIndex(StaplerRequest req, StaplerResponse rsp) throws IOException
174188 // filter branch, if ref not match branch filter, skip trigger job.
175189 boolean isRefMatched = true ;
176190
177- String ref = ( String ) jsonObject . getString ( "ref" );
191+
178192 payloadProcessor .setPayload ("ref" , ref );
179193 payloadProcessor .setPayload ("before" , jsonObject .getString ("before" ));
180194
0 commit comments