You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

generate_batch_reports.sh 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #!/usr/bin/env bash
  2. ##############################
  3. #
  4. # Generates batch reports dumps of a specific project into a given dump repository
  5. # from the Git history of that project.
  6. #
  7. # This script runs the analysis of the project in the current directory of the N
  8. # last commits of the current project. N can be configured.
  9. #
  10. # Batch report dumps can be processed with the replay_batch.sh script
  11. #
  12. #
  13. ##############################
  14. set -euo pipefail
  15. clean() {
  16. rm -Rf $BUILD_DIR
  17. }
  18. createPrivateClone() {
  19. BRANCH=$(git symbolic-ref -q HEAD)
  20. BRANCH=${BRANCH##refs/heads/}
  21. BRANCH=${BRANCH:-HEAD}
  22. LOCATION=$(pwd)
  23. REPO_DIR=$(git rev-parse --show-toplevel)
  24. REPO_NAME=${REPO_DIR##*/}
  25. RELATIVE_PATH=${LOCATION#$REPO_DIR}
  26. # create temp directory and register trap to clean it on exit
  27. BUILD_DIR=$(mktemp -d -t ${REPO_NAME}.XXXXXXXX 2>/dev/null)
  28. trap "clean" INT TERM EXIT
  29. # Private build
  30. cd $REPO_DIR
  31. git clone --single-branch -slb "${BRANCH}" . ${BUILD_DIR}
  32. cd ${BUILD_DIR}${RELATIVE_PATH}
  33. }
  34. showHelp() {
  35. echo "Usage: $0 -d DIR_PATH [ -l integer ] [ -h URL ] [ -u string ] [ -p string ]
  36. -d : path to directory where batch report bumpds will be created
  37. -l : number of commit in the past (optional: default is $HISTORY_LENGTH)
  38. -h : URL of the SQ instance (optional: default is $SONAR_HOST)
  39. -u : username to authentication on the SQ instance (optional: default is $SONAR_USER)
  40. -p : password to authentication on the SQ instance (optional: default is $SONAR_USER)"
  41. }
  42. checkOptions() {
  43. if [[ -z "$DUMP_DIR" ]]; then
  44. >&2 echo "-d option is mandatory"
  45. showHelp
  46. exit 1
  47. fi
  48. }
  49. DUMP_DIR=""
  50. HISTORY_LENGTH=30
  51. SONAR_HOST="http://localhost:9000"
  52. SONAR_USER="admin"
  53. SONAR_PASSWORD="admin"
  54. SONAR_JDBC_URL="jdbc:postgresql://localhost:5432/sonar"
  55. SONAR_JDBC_USERNAME="sonar"
  56. SONAR_JDBC_PASSWORD="sonar"
  57. while getopts ":d:l:h:u:p:" opt; do
  58. case "$opt" in
  59. d) DUMP_DIR=$OPTARG
  60. ;;
  61. l) HISTORY_LENGTH=$OPTARG
  62. ;;
  63. h) SONAR_HOST=$OPTARG
  64. ;;
  65. u) SONAR_USER=$OPTARG
  66. ;;
  67. p) SONAR_PASSWORD=$OPTARG
  68. ;;
  69. :)
  70. >&2 echo "option $OPTARG requires an argument"
  71. showHelp
  72. exit 1
  73. ;;
  74. \?)
  75. >&2 echo "Unsupported option $OPTARG"
  76. showHelp
  77. exit 1
  78. ;;
  79. esac
  80. done
  81. checkOptions
  82. createPrivateClone
  83. # retrieve ${HISTORY_LENGTH} commits for the current directory
  84. git log -${HISTORY_LENGTH} --pretty=%h -- . | tac > /tmp/sha1s.txt
  85. git co -b working_branch
  86. while read sha1; do
  87. echo $sha1
  88. git reset --hard $sha1
  89. date=`git show -s --format=%ci | sed 's/ /T/' | sed 's/ //'`
  90. echo ""
  91. echo "======================== analyzing at $date ($sha1) ======================================="
  92. $M2_HOME/bin/mvn sonar:sonar -Dsonar.host.url=$SONAR_HOST -Dsonar.login=$SONAR_USER -Dsonar.password=$SONAR_PASSWORD -Dsonar.analysis.mode=analysis -Dsonar.issuesReport.html.enable= -Dsonar.issuesReport.console.enable= -Dsonar.jdbc.url=$SONAR_JDBC_URL -Dsonar.jdbc.username=$SONAR_JDBC_USERNAME -Dsonar.jdbc.password=$SONAR_JDBC_PASSWORD -Dsonar.batch.dumpReportDir=$DUMP_DIR -Dsonar.projectDate=$date
  93. done < /tmp/sha1s.txt