Software Development

Migrate sonar data from old to new sonar version

At my current project we were setting up a new buildserver. One of the things we had to do was move an old sonar implementation running on top of H2 to a new version running on Oracle. The problem was that just migrating the data is nearly impossible to do when you start with H2, and when the versions were so far apart as in our case. During some research we stumbled upon the following migration Gist page showing a possible migration path: https://gist.github.com/aslakknutsen/2422117

What the example script on that page does, is that it retieves all the tags from a Git repository and just starts running maven and the sonar plugin to refill the new sonar. With a couple of additions and changes we managed to get this running for our scenario and were able to refill our new sonar with historical data:

# Based on: https://gist.github.com/aslakknutsen/2422117
GIT_REPO=$1
START_TAG=$2
 
MVN_COMMAND="mvn clean install -fn"
SONAR_COMMAND="mvn org.codehaus.sonar:sonar-maven3-plugin:3.7:sonar"
 
if [ -z "$GIT_REPO" ]; then
    echo "Missing program argument: repository"
    echo "Usage: ./sonar_history.sh git_repository_path [start-tag]"
    exit
fi
 
pushd $GIT_REPO
 
# set these for partial imports
done=0
skip=0
toProcess=100
 
for tag in `git for-each-ref --format="%(taggerdate): %(refname)" --sort=taggerdate | grep -iv ^: | cut -d " " -f 7 | cut -c 11- | grep -i <release-prefix>`
do
    echo $tag
    if [ $((done - skip)) -eq "$toProcess" ]; then
      break
    fi
     
    if [[ -n "$START_TAG" && "$START_TAG" > "$tag" ]] ; then
        echo "Skipping $tag (start tag $START_TAG)"
        continue
    fi
 
    if [ "$done" -ge "$skip" ]; then
      TAG_DATE=`git show $tag --date=iso | grep Date: -m1 | cut -d' ' -f 4`
      echo "Checking out source from $TAG_DATE tagged as $tag"
 
      # we force a complete cleanup each time
      git fetch --all
      git reset --hard origin/master
      git checkout -f $tag
      git clean -df
 
      SONAR_PROJECT_COMMAND="$SONAR_COMMAND -Dsonar.projectDate=$TAG_DATE -Dsonar.jdbc.url=jdbc:oracle:thin:@<host>:1521:DBELBLD -Dsonar.jdbc.username=<username> -Dsonar.jdbc.password=<password>"
 
      echo "Executing Maven: $MVN_COMMAND"
      $MVN_COMMAND
 
      # not all files have the correct version set, which messes up the push to sonar
      # replace the version with the release property for correct processing
      sed -i -- "s/<version>00_00_00-SNAPSHOT<\/version>/<version>$tag<\/version>/g" pom.xml
 
      echo "Executing Sonar: $SONAR_PROJECT_COMMAND"
      $SONAR_PROJECT_COMMAND
    fi
 
    done=$((done+1))
done
popd

The above code was run against with SonarQube 4.5.2, but should also work with SonarQube 5.x.

Reference: Migrate sonar data from old to new sonar version from our JCG partner Jos Dirksen at the Smart Java blog.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button