old_contents: import logging
  import urllib
  
  from flask import redirect, g, abort
  from flask import Blueprint
  from google.appengine.api import urlfetch
  
  from sparkprs import app
  from sparkprs.models import Issue
  
  
  jenkins = Blueprint('jenkins', __name__)
  
  
  @jenkins.route("/trigger-jenkins/<int:number>", methods=['GET', 'POST'])
  def test_pr(number):
      """
      Triggers a parametrized Jenkins build for testing Spark pull requests.
      """
      if not (g.user and g.user.has_capability("jenkins")):
          return abort(403)
      pr = Issue.get_or_create(number)
      commit = pr.pr_json["head"]["sha"]
      target_branch = pr.pr_json["base"]["ref"]
      # The parameter names here were chosen to match the ones used by Jenkins' GitHub pull request
      # builder plugin: https://wiki.jenkins-ci.org/display/JENKINS/Github+pull+request+builder+plugin
      # In the Spark repo, the https://github.com/apache/spark/blob/master/dev/run-tests-jenkins
      # script reads these variables when posting pull request feedback.
      query = {
          'token': app.config['JENKINS_PRB_TOKEN'],
          'ghprbPullId': number,
          'ghprbActualCommit': commit,
          'ghprbTargetBranch': target_branch,
          'ghprbPullTitle': pr.raw_title,
          # This matches the Jenkins plugin's logic; see
          # https://github.com/jenkinsci/ghprb-plugin/blob/master/src/main/java/org/jenkinsci/plugins/ghprb/GhprbTrigger.java#L146
          #
          # It looks like origin/pr/*/merge ref points to the last successful test merge commit that
          # GitHub generates when it checks for mergeability.  This API technically isn't documented,
          # but enough plugins seem to rely on it that it seems unlikely to change anytime soon
          # (if it does, we can always upgrade our tests to perform the merge ourselves).
          #
          # See also: https://developer.github.com/changes/2013-04-25-deprecating-merge-commit-sha/
          'sha1': ("origin/pr/%i/merge" % number) if pr.is_mergeable else commit,
      }
      trigger_url = "%sbuildWithParameters?%s" % (app.config["JENKINS_PRB_JOB_URL"],
                                                  urllib.urlencode(query))
      logging.debug("Triggering Jenkins with url %s" % trigger_url)
      response = urlfetch.fetch(trigger_url, method="POST")
      if response.status_code not in (200, 201):
          logging.error("Jenkins responded with status code %i" % response.status_code)
          return response.content
      else:
          return redirect(app.config["JENKINS_PRB_JOB_URL"])
  
new_contents: import logging
  import urllib
  
  from flask import redirect, g, abort
  from flask import Blueprint
  from google.appengine.api import urlfetch
  
  from sparkprs import app
  from sparkprs.models import Issue
  
  
  jenkins = Blueprint('jenkins', __name__)
  
  
  @jenkins.route("/trigger-jenkins/<int:number>", methods=['GET', 'POST'])
  def test_pr(number):
      """
      Triggers a parametrized Jenkins build for testing Spark pull requests.
      """
      if not (g.user and g.user.has_capability("jenkins")):
          return abort(403)
      pr = Issue.get_or_create(number)
      commit = pr.pr_json["head"]["sha"]
      target_branch = pr.pr_json["base"]["ref"]
      # The parameter names here were chosen to match the ones used by Jenkins' GitHub pull request
      # builder plugin: https://wiki.jenkins-ci.org/display/JENKINS/Github+pull+request+builder+plugin
      # In the Spark repo, the https://github.com/apache/spark/blob/master/dev/run-tests-jenkins
      # script reads these variables when posting pull request feedback.
      query = {
          'token': app.config['JENKINS_PRB_TOKEN'],
          'ghprbPullId': number,
          'ghprbActualCommit': commit,
          'ghprbTargetBranch': target_branch,
          'ghprbPullTitle': pr.raw_title.encode('utf-8'),
          # This matches the Jenkins plugin's logic; see
          # https://github.com/jenkinsci/ghprb-plugin/blob/master/src/main/java/org/jenkinsci/plugins/ghprb/GhprbTrigger.java#L146
          #
          # It looks like origin/pr/*/merge ref points to the last successful test merge commit that
          # GitHub generates when it checks for mergeability.  This API technically isn't documented,
          # but enough plugins seem to rely on it that it seems unlikely to change anytime soon
          # (if it does, we can always upgrade our tests to perform the merge ourselves).
          #
          # See also: https://developer.github.com/changes/2013-04-25-deprecating-merge-commit-sha/
          'sha1': ("origin/pr/%i/merge" % number) if pr.is_mergeable else commit,
      }
      trigger_url = "%sbuildWithParameters?%s" % (app.config["JENKINS_PRB_JOB_URL"],
                                                  urllib.urlencode(query))
      logging.debug("Triggering Jenkins with url %s" % trigger_url)
      response = urlfetch.fetch(trigger_url, method="POST")
      if response.status_code not in (200, 201):
          logging.error("Jenkins responded with status code %i" % response.status_code)
          return response.content
      else:
          return redirect(app.config["JENKINS_PRB_JOB_URL"])
  
subject: Handle UTF8 characters in PR titles when triggering Jenkins.