@@ -102,6 +102,7 @@ private void setGogsSignature(String gogsSignature) {
102102 * @param rsp response
103103 * @throws IOException problem while parsing
104104 */
105+ @ SuppressWarnings ("WeakerAccess" )
105106 public void doIndex (StaplerRequest req , StaplerResponse rsp ) throws IOException {
106107 GogsPayloadProcessor payloadProcessor = new GogsPayloadProcessor ();
107108 GogsCause gogsCause = new GogsCause ();
@@ -121,7 +122,8 @@ public void doIndex(StaplerRequest req, StaplerResponse rsp) throws IOException
121122 if (!body .isEmpty () && req .getRequestURI ().contains ("/" + URLNAME + "/" )) {
122123 JSONObject jsonObject = JSONObject .fromObject (body );
123124 JSONObject commits = (JSONObject ) jsonObject .getJSONArray ("commits" ).get (0 );
124- String message = (String ) commits .get ("message" );
125+ String ref = jsonObject .getString ("ref" );
126+ String message = commits .getString ("message" );
125127
126128 if (message .startsWith ("[IGNORE]" )) {
127129 // Ignore commits starting with message "[IGNORE]"
@@ -140,22 +142,23 @@ public void doIndex(StaplerRequest req, StaplerResponse rsp) throws IOException
140142
141143 AtomicReference <String > jSecret = new AtomicReference <>(null );
142144 AtomicBoolean foundJob = new AtomicBoolean (false );
145+ AtomicBoolean isRefMatched = new AtomicBoolean (true );
143146 gogsCause .setGogsPayloadData (jsonObject .toString ());
144147 gogsCause .setDeliveryID (getGogsDelivery ());
145148 payloadProcessor .setCause (gogsCause );
146149
147- try (ACLContext ctx = ACL .as (ACL .SYSTEM )) {
150+ try (ACLContext ignored = ACL .as (ACL .SYSTEM )) {
148151 StringJoiner stringJoiner = new StringJoiner ("%2F" );
149- Pattern .compile ("/" ).splitAsStream ((String ) jsonObject .get ("ref" )).skip (2 )
150- .forEach (stringJoiner ::add );
151- String ref = stringJoiner .toString ();
152+ Pattern .compile ("/" ).splitAsStream (jsonObject .getString ("ref" )).skip (2 ).forEach (stringJoiner ::add );
153+ String ref_strj = stringJoiner .toString ();
152154
153155 /* secret is stored in the properties of Job */
154- Stream .of (jobName , jobName + "/" + ref ).map (j -> GogsUtils .find (j , Job .class )).filter (Objects ::nonNull ).forEach (job -> {
156+ Stream .of (jobName , jobName + "/" + ref_strj ).map (j -> GogsUtils .find (j , Job .class )).filter (Objects ::nonNull ).forEach (job -> {
155157 foundJob .set (true );
156158 final GogsProjectProperty property = (GogsProjectProperty ) job .getProperty (GogsProjectProperty .class );
157159 if (property != null ) { /* only if Gogs secret is defined on the job */
158160 jSecret .set (property .getGogsSecret ()); /* Secret provided by Jenkins */
161+ isRefMatched .set (property .filterBranch (ref ));
159162 }
160163 });
161164 }
@@ -178,6 +181,10 @@ public void doIndex(StaplerRequest req, StaplerResponse rsp) throws IOException
178181 String msg = String .format ("Job '%s' is not defined in Jenkins" , jobName );
179182 result .setStatus (404 , msg );
180183 LOGGER .warning (msg );
184+ } else if (!isRefMatched .get ()) {
185+ String msg = String .format ("received ref ('%s') is not matched with branch filter in job '%s'" , ref , jobName );
186+ result .setStatus (200 , msg );
187+ LOGGER .info (msg );
181188 } else if (isNullOrEmpty (jSecret .get ()) && isNullOrEmpty (gSecret )) {
182189 /* No password is set in Jenkins and Gogs, run without secrets */
183190 result = payloadProcessor .triggerJobs (jobName );
0 commit comments