Sunday, June 4, 2017

Getting started with Sonar part 3

Hello Guys,

This is the 3rd and last part of sonar series in which I will explain about how you can detect the pull request which is raise on github it is safe to merge with your main or master branch or not and it will also comment what are the reasons why is it not safe and it will comment on github so lets getting started. with it.
 so first thing first.

1. You have git repository setup
2. Standard sonar setup.

Now How we can active it.

So for testing we have a testing repository
https://github.com/navneet-rathi/sample-code-java.git

and we have a pull request raise for the repository for me the pull request raise we need to know the pull request no. which we can find out through the
   
from This I come to know that the pull request no is 2

you need to provide access the sonar so that it can write on your github. which is possible using personal access token which you will get using github you need to provide proper


I have given the below access it will give me a access code please note it down.

now its start to work.

I will use maven to check go to the project repository and fire the below command

mvn -Dsonar.analysis.mode=preview \
              -Dsonar.github.pullRequest=2 \
              -Dsonar.github.repository=navneet-rathi/sample-code-java \
              -Dsonar.github.oauth=<This token which you have previously noted down> \
              -Dsonar.host.url=http://localhost:9000 \
              -Dsonar.login=admin \
              -Dsonar.password=<your sonar password>

and thats it after this you will where it is feasible to merge the request or not if we merge what will be the consequence.

In my case its failed and the below is the reason.
  
Ohh yes guys below is the genral command 

mvn -Dsonar.analysis.mode=preview \
              -Dsonar.github.pullRequest=$PULL_REQUEST_ID \
              -Dsonar.github.repository=myOrganisation/myProject \
              -Dsonar.github.oauth=$GITHUB_ACCESS_TOKEN \
              -Dsonar.host.url=https://server/sonarqube \
              -Dsonar.login=$SONARQUBE_ACCESS_TOKEN or sonar username \
              -Dsonar.passord=$ Sonar Password
 
So thats it guys let me know if you want to know any thing more on this.

Sunday, May 14, 2017

Getting started with Basic sonar Use Part -2

Hello,

lets start with the sonar analysis we will do the analysis of sonarqube code itself.

first we will download or clone the code on local using below command.I have fork from the sonar repo so I will work on this you can do the same with sonarqube repo as well.

$ git clone https://github.com/navneet-rathi/sonarqube.git

After downloading the code from the github you will see its a maven project we have to install the maven first which is pretty straight forward.

after installing maven we need to change the directory and open then sonarqube directory by command

$ cd sonarqube

Lets start with clean and do analysis of sonarqube

$mvn clean sonar:sonar -Dsonar.host.url=http://localhost:9000 -Dsonar.login=admin -Dsonar.password=<your sonar password>

you can even use -X option with maven for debugging info.

and done then just wait and watch  and rest of work is done by maven and sonar.
you will see some thing after its start its  sonar who is doing analysis.



you can also see some thing like this if you have build breaker plugin install in your sonar.This plugin is useful when you are working with sonar and jenkins together.


Once the analysis is complete on the command line you can go to browser and login in the sonarqube and check the sonarqube code quality check for the project.


  

You can see some thing like this after the analysis is completed.So this is about the basic use of sonar with maven.In the net article i will show you how you can analysis the pull request in github using sonar if it is ok to merge or not.







Install Sonarqube on Ubuntu Part-1

Hello Guys,

Today I will show you how to install the sonarqube on ubuntu box and then will go with some configuration of sonarqube so lets start.

Lets install  JDK 8 on ubuntu

$ sudo apt-get install oracle-java8-installer

now lets install mysql on ubuntu

$ wget http://dev.mysql.com/get/mysql-apt-config_0.6.0-1_all.deb
 
$ sudo dpkg -i mysql-apt-config_0.6.0-1_all.deb

This will install the repo of mysql 5.7 on ubuntu server and now lets install mysql server sometimes unzip utility is not present on the server ubuntu server so install it exploratory. 

$ sudo apt-get update && sudo apt-get install mysql-server && sudo apt-get instll unzip zip

Create SonarQube database and user

$mysql -u root -p
 
Then create the database and a user:
 
mysql> CREATE DATABASE sonar CHARACTER SET utf8 COLLATE utf8_general_ci;
mysql> CREATE USER 'sonar' IDENTIFIED BY 'sonar';
mysql> GRANT ALL ON sonar.* TO 'sonar'@'%' IDENTIFIED BY 'sonar';
mysql> GRANT ALL ON sonar.* TO 'sonar'@'localhost' IDENTIFIED BY 'sonar';
mysql> FLUSH PRIVILEGES;
mysql> exit; 

So We have completed  prerequisite now lets move ahead with the installation of sonarqube
Download the latest source of sonarqube from https://www.sonarqube.org/
in my case its 6.3.1
$ wget  https://sonarsource.bintray.com/Distribution/sonarqube/sonarqube-6.3.1.zip
$ unzip sonarqube-6.3.1.zip
$ sudo sonarqube-6.3.1 /opt/sonar

Edit sonar.properties

Open /opt/sonar/conf/sonar.properties with your favourite text editor, and modify it.in my case its vim so

$ vim /opt/sonar/conf/sonar.properties

MySQL settings
Uncomment the user credentials and the MySQL related settings:

sonar.jdbc.username=sonar
sonar.jdbc.password=sonar

sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance

Web Server settings
The following settings allow you to run the server on page http://localhost:9000/sonar
Copy sonar.sh to etc/init.d/sonar and modify it according to your platform.

$ sudo cp /opt/sonar/bin/linux-x86-64/sonar.sh /etc/init.d/sonar
$ sudo vim /etc/init.d/sonar

Insert two new lines:

SONAR_HOME=/opt/sonar
PLATFORM=linux-x86-64

Modify the following lines:

WRAPPER_CMD="${SONAR_HOME}/bin/${PLATFORM}/wrapper"
WRAPPER_CONF="${SONAR_HOME}/conf/wrapper.conf"
...
PIDDIR="/var/run"

Register as a Linux service:

$ sudo update-rc.d -f sonar remove
$ sudo chmod 755 /etc/init.d/sonar
$ sudo update-rc.d sonar defaults



now lets rock and role as the sonar is install on your local so lets start it with

$ /etc/init.d/sonar start

open the browser and http://<ip-address of server>:9000

remaing configuration section i will show you in the next article .