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

NullPointerException when manually triggering build

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Resolved (View Workflow)
    • Priority: Major
    • Resolution: Fixed
    • Component/s: gerrit-trigger-plugin
    • Labels:
      None
    • Environment:
      Jenkins 1.417 running on Ubuntu 10.04.2 LTS 64-bit. Using Java HotSpot(TM) 64-Bit Server VM 1.6.0_24
    • Similar Issues:

      Description

      A build manually triggered with the gerrit-trigger plugin failed with the following output:

      Manually triggered by user anonymous for Gerrit: http://localhost:8010/1
      Checkout:workspace / /var/lib/jenkins/jobs/Playground-gerrit/workspace - hudson.remoting.LocalChannel@bd5d765
      Using strategy: Gerrit Trigger
      Checkout:workspace / /var/lib/jenkins/jobs/Playground-gerrit/workspace - hudson.remoting.LocalChannel@bd5d765
      Fetching changes from the remote Git repository
      Fetching upstream changes from ssh://xxxx@ubisrv1:29418/playground.git
      Commencing build of Revision 636920c6a48c59f5cdf1dee1d3346ecc1dde4821 (null)
      Checking out Revision 636920c6a48c59f5cdf1dee1d3346ecc1dde4821 (null)
      FATAL: null
      java.lang.NullPointerException
      at com.sonyericsson.hudson.plugins.gerrit.trigger.hudsontrigger.GerritTriggerBuildChooser.getFirstParent(GerritTriggerBuildChooser.java:108)
      at com.sonyericsson.hudson.plugins.gerrit.trigger.hudsontrigger.GerritTriggerBuildChooser.prevBuildForChangelog(GerritTriggerBuildChooser.java:79)
      at hudson.plugins.git.GitSCM.computeChangeLog(GitSCM.java:1210)
      at hudson.plugins.git.GitSCM.access$700(GitSCM.java:57)
      at hudson.plugins.git.GitSCM$4.invoke(GitSCM.java:1175)
      at hudson.plugins.git.GitSCM$4.invoke(GitSCM.java:1120)
      at hudson.FilePath.act(FilePath.java:758)
      at hudson.FilePath.act(FilePath.java:740)
      at hudson.plugins.git.GitSCM.checkout(GitSCM.java:1120)
      at hudson.model.AbstractProject.checkout(AbstractProject.java:1182)
      at hudson.model.AbstractBuild$AbstractRunner.checkout(AbstractBuild.java:537)
      at hudson.model.AbstractBuild$AbstractRunner.run(AbstractBuild.java:425)
      at hudson.model.Run.run(Run.java:1376)
      at hudson.model.FreeStyleBuild.run(FreeStyleBuild.java:46)
      at hudson.model.ResourceController.execute(ResourceController.java:88)
      at hudson.model.Executor.run(Executor.java:146)

        Attachments

          Activity

          Hide
          flatzoli Flat Zoli added a comment -

          The problem occurs when the commit to build is the first in the repository (it does not have a parent commit).

          After manually verifying and submitting the review, the next commit has been built successfully.

          Show
          flatzoli Flat Zoli added a comment - The problem occurs when the commit to build is the first in the repository (it does not have a parent commit). After manually verifying and submitting the review, the next commit has been built successfully.
          Hide
          tlee Tony Lee added a comment -

          Because getFirstParent() returns null if the base is an initial commit. The method runs "git log -1 --pretty=format:%P revName". The resultant string will be an empty string for an initial commit.
          I'd like to suggest to use current commit as parent commit. So if it's an empty string, run "git log -1 --format=%H".

          Show
          tlee Tony Lee added a comment - Because getFirstParent() returns null if the base is an initial commit. The method runs "git log -1 --pretty=format:%P revName". The resultant string will be an empty string for an initial commit. I'd like to suggest to use current commit as parent commit. So if it's an empty string, run "git log -1 --format=%H".
          Hide
          tlee Tony Lee added a comment -

          For what's worth, I'm sharing what I did to workaround this issue. I ended up doing 2 things that seems to work around the issues completely (doing #1 only should be enough though):

          1. I modified com.sonyericsson.hudson.plugins.gerrit.trigger.hudsontrigger.GerritTriggerBuildChooser, please help verifying if what I did make sense. The logic is to skip building a parent revision if there is no parent.

          [code snippet]
          74 public Build prevBuildForChangelog(String singleBranch, BuildData data, IGitAPI git) {
          75 ObjectId sha1 = git.revParse("FETCH_HEAD");
          76
          77 // Now we cheat and add the parent as the last build on the branch, so we can
          78 // get the changelog working properly-ish.
          79 ObjectId parentSha1 = getFirstParent(ObjectId.toString(sha1), git);
          80 // Returns null right away if there is revision found.

          81 // Initial commit doesn't have parent revision, i.e. parent's SHA1.
          82 if (parentSha1 == null)

          { 83 return null; 84 }

          80 Revision parentRev = new Revision(parentSha1);
          81 parentRev.getBranches().add(new Branch(singleBranch, parentSha1));
          82
          83 int prevBuildNum = 0;
          84 Result r = null;
          85
          86 Build lastBuild = data.getLastBuildOfBranch(singleBranch);
          87 if (lastBuild != null)

          { 88 prevBuildNum = lastBuild.getBuildNumber(); 89 r = lastBuild.getBuildResult(); 90 }

          91
          92 Build newLastBuild = new Build(parentRev, prevBuildNum, r);
          93
          94 return newLastBuild;
          95 }
          96
          97 //CS IGNORE RedundantThrows FOR NEXT 20 LINES. REASON: Informative, and could happen.
          103 /**
          104 * Gets the top parent of the given revision.
          105 *
          106 * @param revName Revision
          107 * @param git GitAPI object
          108 * @return object id for parent
          109 * @throws GitException In case of error in git call
          110 */
          111 private ObjectId getFirstParent(String revName, IGitAPI git) throws GitException {
          112 String result = ((GitAPI)git).launchCommand("log", "1", "-pretty=format:%P", revName);

          113 // Returns null right away if no parent commit is found, i.e. root commit.
          114 if (result.length() == 0)

          { 115 return null; 116 }

          117

          118 String parents = firstLine(result).trim();
          119 String firstParent = parents.split(" ")[0];
          120 return ObjectId.fromString(firstParent);
          121 }
          122

          2. I also install the Post Build Task plugin and write a postbuild.sh, to catch build failure caused by no parent revision. The script verifies the Gerrit change set.

          [code snippet]
          #!/bin/bash

          echo "###"
          echo "# Forces verification on an initial commit."
          echo "# Runs only when "ERROR: Couldn't find any revision to build.*" regex pattern is found.
          echo "###"

          root_commit_verification

          function root_commit_verification() {
          git fetch ssh://gerrit.corpzlab.com:29418/${GERRIT_PROJECT} ${GERRIT_REFSPEC}
          git checkout FETCH_HEAD

          PARENT_SHA1=$(git log -1 --pretty=format:%P)

          if [ -z ${PARENT_SHA1} ]; then
          echo "Force verification for root commit."
          ssh -p 29418 zjenkins@gerrit.corpzlab.com gerrit approve --verified=+1 --project=${GERRIT_PROJECT} $(git log --pretty=format:%H)
          fi
          }

          Show
          tlee Tony Lee added a comment - For what's worth, I'm sharing what I did to workaround this issue. I ended up doing 2 things that seems to work around the issues completely (doing #1 only should be enough though): 1. I modified com.sonyericsson.hudson.plugins.gerrit.trigger.hudsontrigger.GerritTriggerBuildChooser, please help verifying if what I did make sense. The logic is to skip building a parent revision if there is no parent. [code snippet] 74 public Build prevBuildForChangelog(String singleBranch, BuildData data, IGitAPI git) { 75 ObjectId sha1 = git.revParse("FETCH_HEAD"); 76 77 // Now we cheat and add the parent as the last build on the branch, so we can 78 // get the changelog working properly-ish. 79 ObjectId parentSha1 = getFirstParent(ObjectId.toString(sha1), git); 80 // Returns null right away if there is revision found. 81 // Initial commit doesn't have parent revision, i.e. parent's SHA1. 82 if (parentSha1 == null) { 83 return null; 84 } 80 Revision parentRev = new Revision(parentSha1); 81 parentRev.getBranches().add(new Branch(singleBranch, parentSha1)); 82 83 int prevBuildNum = 0; 84 Result r = null; 85 86 Build lastBuild = data.getLastBuildOfBranch(singleBranch); 87 if (lastBuild != null) { 88 prevBuildNum = lastBuild.getBuildNumber(); 89 r = lastBuild.getBuildResult(); 90 } 91 92 Build newLastBuild = new Build(parentRev, prevBuildNum, r); 93 94 return newLastBuild; 95 } 96 97 //CS IGNORE RedundantThrows FOR NEXT 20 LINES. REASON: Informative, and could happen. 103 /** 104 * Gets the top parent of the given revision. 105 * 106 * @param revName Revision 107 * @param git GitAPI object 108 * @return object id for parent 109 * @throws GitException In case of error in git call 110 */ 111 private ObjectId getFirstParent(String revName, IGitAPI git) throws GitException { 112 String result = ((GitAPI)git).launchCommand("log", " 1", " -pretty=format:%P", revName); 113 // Returns null right away if no parent commit is found, i.e. root commit. 114 if (result.length() == 0) { 115 return null; 116 } 117 118 String parents = firstLine(result).trim(); 119 String firstParent = parents.split(" ") [0] ; 120 return ObjectId.fromString(firstParent); 121 } 122 2. I also install the Post Build Task plugin and write a postbuild.sh, to catch build failure caused by no parent revision. The script verifies the Gerrit change set. [code snippet] #!/bin/bash echo "###" echo "# Forces verification on an initial commit." echo "# Runs only when "ERROR: Couldn't find any revision to build.*" regex pattern is found. echo "###" root_commit_verification function root_commit_verification() { git fetch ssh://gerrit.corpzlab.com:29418/${GERRIT_PROJECT} ${GERRIT_REFSPEC} git checkout FETCH_HEAD PARENT_SHA1=$(git log -1 --pretty=format:%P) if [ -z ${PARENT_SHA1} ]; then echo "Force verification for root commit." ssh -p 29418 zjenkins@gerrit.corpzlab.com gerrit approve --verified=+1 --project=${GERRIT_PROJECT} $(git log --pretty=format:%H) fi }
          Hide
          scm_issue_link SCM/JIRA link daemon added a comment -

          Code changed in jenkins
          User: David Pursehouse
          Path:
          gerrithudsontrigger/src/main/java/com/sonyericsson/hudson/plugins/gerrit/trigger/hudsontrigger/GerritTriggerBuildChooser.java
          http://jenkins-ci.org/commit/gerrit-trigger-plugin/ba602dfede0cd21e58d77a328e0c0b747fbf9dbc
          Log:
          JENKINS-10055: NPE when commit to build is first commit in repository

          When the commit to build is the first commit in the repository, it
          does not have a parent commit. In this case use the revision of
          the commit itself as parent.

          Change-Id: I167c02f10e2b657433da63323e04cc7f77733d65

          Show
          scm_issue_link SCM/JIRA link daemon added a comment - Code changed in jenkins User: David Pursehouse Path: gerrithudsontrigger/src/main/java/com/sonyericsson/hudson/plugins/gerrit/trigger/hudsontrigger/GerritTriggerBuildChooser.java http://jenkins-ci.org/commit/gerrit-trigger-plugin/ba602dfede0cd21e58d77a328e0c0b747fbf9dbc Log: JENKINS-10055 : NPE when commit to build is first commit in repository When the commit to build is the first commit in the repository, it does not have a parent commit. In this case use the revision of the commit itself as parent. Change-Id: I167c02f10e2b657433da63323e04cc7f77733d65
          Hide
          dpursehouse David Pursehouse added a comment -

          Fixed by the commit mentioned in the previous comment.

          Show
          dpursehouse David Pursehouse added a comment - Fixed by the commit mentioned in the previous comment.
          Hide
          rsandell rsandell added a comment -

          Released in 2.6.0

          Show
          rsandell rsandell added a comment - Released in 2.6.0

            People

            • Assignee:
              rsandell rsandell
              Reporter:
              flatzoli Flat Zoli
            • Votes:
              2 Vote for this issue
              Watchers:
              4 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: