title: How to merge a successful build of a pull request using a Jenkinsfile
body: <p>TL;DR: Given the following declarative Jenkinsfile, how can I merge the pull request following a successful build? <a href="http://pastebin.com/uivA0MqF" rel="nofollow noreferrer">http://pastebin.com/uivA0MqF</a></p>
  
  <p>In the (Multibranch) Job configuration, under Branch Source > Advanced I have the following setup:</p>
  
  <p><a href="https://i.stack.imgur.com/Hdx5w.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Hdx5w.png" alt="enter image description here"></a></p>
  
  <p>And in GitHub I have a web hook that listens to <code>pull request</code> and <code>push</code> events.  </p>
  
  <p><strong>Edit:</strong> I have attempted the following, but it currently fails with a syntax error:</p>
  
  <pre><code>def gitPullRequestNumber = null
  ...
  ...
  stage ("Merge pull request") {
       steps {
          gitPullRequestNumber = sh "git ls-remote origin 'pull/*/head:develop'"
          sh "curl -X PUT -d '{\'commit_title\': \'Merge pull request\'}'  &lt;&lt;GIT REPO URL&gt;&gt;/pulls/${gitPullRequestNumber}/merge?access_token=&lt;&lt;ACCESS-TOKEN"
       }
  }
  </code></pre>
  
  <p>The error:</p>
  
  <p>org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:</p>
  
  <pre><code>WorkflowScript: 58: Expected a step @ line 58, column 13.
                 gitPullRequestNumber = sh "git ls-remote origin 'pull/*/head:develop'"
                 ^
  
  1 error
  </code></pre>
  
  <hr>
  
  <p>Background:</p>
  
  <p>We have a <code>master</code> branch and a <code>develop</code> branch. Developers will be doing their pull requests against the <code>develop</code> branch where tests will be run. If the tests pass, the pull request should be merged into the branch. Review of the code is done before a pull request is opened. When decided, the <code>develop</code> branch is merged to the <code>master</code> branch. In the future there may even be only a single branch.</p>
  
  <p>I understand that some may say that it is a good thing this doesn't happen but this is what we are currently trying to achieve.</p>
  
  <p>As mentioned, this all works fine and the job starts and uses the Jenkinsfile for checking out the repo based on direct pushes and/or pull requests, the build starts, the tests run and so on... however the missing piece is merging the pull requests back.</p>