aboutsummaryrefslogtreecommitdiffstats
path: root/scripts/generate_batch_reports.sh
blob: 4bdd00fd97f52a7c3c44b8bb3e60874d9f364f82 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#!/bin/bash

##############################
#
# Generates batch reports dumps of a specific project into a given dump repository
# from the Git history of that project.
#
# This script runs the analysis of the project in the current directory of the N
# last commits of the current project. N can be configured.
#
# Batch report dumps can be processed with the replay_batch.sh script
#
#
##############################


set -euo pipefail

function clean() {
  rm -Rf $BUILD_DIR
}

function createPrivateClone() {
  BRANCH=$(git symbolic-ref -q HEAD)
  BRANCH=${BRANCH##refs/heads/}
  BRANCH=${BRANCH:-HEAD}
  LOCATION=$(pwd)
  REPO_DIR=$(git rev-parse --show-toplevel)
  REPO_NAME=${REPO_DIR##*/}
  RELATIVE_PATH=${LOCATION#$REPO_DIR}

  # create temp directory and register trap to clean it on exit
  BUILD_DIR=$(mktemp -d -t ${REPO_NAME}.XXXXXXXX 2>/dev/null)
  trap "clean" INT TERM EXIT

  # Private build
  cd $REPO_DIR
  git clone --single-branch -slb "${BRANCH}" . ${BUILD_DIR}
  cd ${BUILD_DIR}${RELATIVE_PATH}
}

function showHelp() {
  echo "Usage: $0 -d DIR_PATH [ -l integer ] [ -h URL ] [ -u string ] [ -p string ]"
  echo " -d : path to directory where batch report bumpds will be created"
  echo " -l : number of commit in the past (optional: default is $HISTORY_LENGTH)"
  echo " -h : URL of the SQ instance (optional: default is $SONAR_HOST)"
  echo " -u : username to authentication on the SQ instance (optional: default is $SONAR_USER)"
  echo " -p : password to authentication on the SQ instance (optional: default is $SONAR_USER)"
}

function checkOptions() {
  if [ "$DUMP_DIR" = "" ]; then
    >&2 echo "-d option is mandatory"
    showHelp
    exit 1
  fi
}

DUMP_DIR=""
HISTORY_LENGTH=30
SONAR_HOST="http://localhost:9000"
SONAR_USER="admin"
SONAR_PASSWORD="admin"
SONAR_JDBC_URL="jdbc:postgresql://localhost:5432/sonar"
SONAR_JDBC_USERNAME="sonar"
SONAR_JDBC_PASSWORD="sonar"

while getopts ":d:l:h:u:p:" opt; do
  case "$opt" in
    d) DUMP_DIR=$OPTARG
      ;;
    l) HISTORY_LENGTH=$OPTARG
      ;;
    h) SONAR_HOST=$OPTARG
      ;;
    u) SONAR_USER=$OPTARG
      ;;
    p) SONAR_PASSWORD=$OPTARG
      ;;
    :)
      >&2 echo "option $OPTARG requires an argument"
      showHelp
      exit 1
      ;;
    \?)
      >&2 echo "Unsupported option $OPTARG"
      showHelp
      exit 1 
      ;;
  esac
done
checkOptions

createPrivateClone

# retrieve ${HISTORY_LENGTH} commits for the current directory
git log -${HISTORY_LENGTH} --pretty=%h -- . | tac > /tmp/sha1s.txt

git co -b working_branch 
while read sha1; do
  echo $sha1
  git reset --hard $sha1
  date=`git show -s --format=%ci | sed 's/ /T/' | sed 's/ //'`

  echo ""
  echo "======================== analyzing at $date ($sha1) ======================================="
  $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
  
done < /tmp/sha1s.txt