Uploaded image for project: 'Jenkins'
  1. Jenkins
  2. JENKINS-71777

Bitbucket Cloud webhook trigger does not trigger build

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Not A Defect
    • Icon: Major Major
    • bitbucket-plugin
    • None

      Hi there,

      I have a problem with the Bitbucket webhooks and Bitbucket Cloud.

      Bitbucket Cloud has the following webhook configured: https://JENKINSURL/bitbucket-hook/ (trigger set to Pull Request -> Merged).

      My Jenkins Pipeline has "Build when a change is pushed to BitBucket" activated and the Override Repository URL is set to "https://bitbucket.org/PROJECT/jenkins".

      When merging a pull request I can see in Bitbucket Cloud that the webhook request was send:

      Request

      Headers

      X-Event-Key pullrequest:fulfilled
      X-Hook-UUID SOME_ID
      X-B3-SpanId SOME_ID
      X-Event-Time Tue, 08 Aug 2023 12:04:36 GMT
      User-Agent Bitbucket-Webhooks/2.0
      X-B3-Sampled 1
      X-B3-TraceId SOME_ID
      X-Attempt-Number 1
      X-Request-UUID SOME_ID
      Content-Type application/json

      Body

      ""

      {
        "repository": {
          "type": "repository",
          "full_name": "PROJECT/jenkins",
          "links": {
            "self":

      {         "href": "https://api.bitbucket.org/2.0/repositories/PROJECT/jenkins"       }

      ,
            "html":

      {         "href": "https://bitbucket.org/PROJECT/jenkins"       }

      ,
            "avatar":

      {         "href": "https://bytebucket.org/ravatar/SOMEID"       }

          },
          "name": "jenkins",
          "scm": "git",

      [...]

      ""

      I can see with the Jenkins Log-Recorder, that the request arrives at Jenkins. (therefore no network issue with Bitbucket Cloud).

      But, for some reason the Jenkins Job is not triggered automatically. I took a deeper look into the sourcecode of the plugin, in particular BitbucketJobProbe.java, and tried the logic with the Jenkins script console:

      import com.cloudbees.jenkins.plugins.BitBucketTrigger;
      import jenkins.triggers.SCMTriggerItem;
      import hudson.scm.SCM;
      import hudson.plugins.git.GitStatus;
      import org.eclipse.jgit.transport.RemoteConfig;
      import hudson.plugins.git.GitSCM;
      import org.eclipse.jgit.transport.URIish;
      import org.apache.commons.lang.StringUtils;
      
      // the overrideUrl in the jenkins job is exactly the same as the url in the request payload. (see String url = repo.getJSONObject("links").getJSONObject("html").getString("href"); in BitbucketPayloadProcessor.java)
      
      String overrideUrl = "https://bitbucket.org/PROJECT/jenkins";
      URIish url = new URIish(overrideUrl);
      
      for (Job<?, ?> job : Jenkins.getInstance().getAllItems(Job.class)) {
        BitBucketTrigger bTrigger = null;                   
        if (job instanceof ParameterizedJobMixIn.ParameterizedJob) {
          ParameterizedJobMixIn.ParameterizedJob pJob = (ParameterizedJobMixIn.ParameterizedJob) job;
          for (Object trigger : pJob.getTriggers().values()) {
            if (trigger instanceof BitBucketTrigger) {
              bTrigger = (BitBucketTrigger) trigger;
              break;
            }
          }
        }
        if(bTrigger != null) {
          println "possible match: " + job.getFullDisplayName();
          
          SCMTriggerItem item = SCMTriggerItem.SCMTriggerItems.asSCMTriggerItem(job);
          
          if(item == null) {
            println "item is null";
          } else {
            println "override url is: " + bTrigger.getOverrideUrl();
            for (SCM scmTrigger : item.getSCMs()) {
              for (RemoteConfig remoteConfig : ((GitSCM) scmTrigger).getRepositories()) {
                for (URIish urIish : remoteConfig.getURIs()) {
                  if (urIish.getPath().startsWith("/scm")) {
                    urIish = urIish.setPath(urIish.getPath().substring(4));
                  }                    
                  if (StringUtils.isEmpty(url.getHost())) {
                    urIish = urIish.setHost(url.getHost());
                  }
                  
                  if (GitStatus.looselyMatches(urIish, url)) {
                    println "git status loosely matches";
                  } else if (overrideUrl != null && !overrideUrl.isEmpty()) {     
                    if(overrideUrl.contentEquals(url.toString())) {
                      println "overrideUrl matches"
                    }
                  }
                }
              }
            }
          }
        }
      } 

      I can see, that in theory the job is matched with the payload and the job should be triggered - but it isn't.

       

      Is this a bug in jenkins or/and in the plugin? From my point of view the configuration should be correct (and worked before with Bitbucket Server).

      Kind regards
      Dennis

            Unassigned Unassigned
            dennis_otto Dennis Otto
            Votes:
            1 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved: