Search code examples
gitapisonarqubesonarscanner

SonarQube: obtaining git commit hashes via web API


If I've understood correctly, according to the web API documentation of SonarQube it should be possible to get the commit hash through api/issues/search, however, when I do that and compare that hash with a:

git log --all | grep "<hash>"

I get nothing as a response, which leads me to two questions:

  1. Have I misunderstood the (quite cryptic sometimes) web API documentation and that is a hash for something else or might I have something missconfigured and that's why I'm getting random values?

  2. Is there a way to get a correlation of the issues/measures from a project and the respective commit hashes? Using a SQL query to PostgreSQL could be an option in this case? If so, any idea about where that information would be stored?


Solution

  • So, after waiting for a while it looks like there's no answer, however, since you're already here... Let's avoid this

    So here you have piece of bash code to generate a table with the correlation between the dates from the file with your extracted data and the commits of the project.

    Probably not the best approach neither the fastest one, but it will give you what you need :)

    Save in a file.sh and use as: ./file.sh GithubUser/ProjectName YourFileWithTheExtractedData.csv

    #!/bin/bash
    
    gitproject="git@github.com:${1}.git"
    gitfolder=$(echo $gitproject | sed -E 's/(.+)\/(.+\.git$)/\2/g')
    workdir="$(pwd)"
    measuresFile=$2
    
    if [ ! -d "$workdir/$gitfolder" ];
    then
            $(git -C $workdir clone -q --bare $gitproject)
    fi
    
    echo '"sonar-timestamp","git-timestamp","commit-hash"' >> $workdir/Hashes_$2
    cat $workdir/$measuresFile | grep -Eo '"[[:digit:]]{4}-[[:digit:]]{2}-[[:digit:]]{2}T[[:digit:]]{2}:[[:digit:]]{2}:[[:digit:]]{2}\+[[:digit:]]{4}",' | sed -E 's/"|",//g' | sort -u >> $workdir/TMP_Hashes_$2
    for i in $( cat $workdir/TMP_Hashes_$2 | sort -u ); do echo $(echo $i | date '+%s' -f - ),"$i" >> $workdir/Hashes_$2 ; done
    rm -rf $workdir/TMP_Hashes_$2
    
    gHashes=( $(TZ=Europe/London git -C $workdir/$gitfolder log --all --date=format-local:%Y-%m-%dT%H:%M:%S%z --format=%ad\ %H | sort | tr '\n' ' ') )
    
    COUNTER=0
    while (("$COUNTER" < "${#gHashes[@]}"))
    do
            sust=$(date -d "${gHashes[$COUNTER]}" '+%s')
            notInc=$(cat $workdir/Hashes_$2 | grep -o "^${sust}" | wc -l )
            $(sed -i -E "s/(^${sust}),(.+)/\"\2\",\"${gHashes[($COUNTER)]}\",\"${gHashes[($COUNTER + 1)]}\"/g" $workdir/Hashes_$2)
    
            if (( "$notInc" == "0" ));
            then
                    rHashes+=( "${gHashes[($COUNTER)]}" "${gHashes[($COUNTER + 1)]}" )
            fi
    
            let COUNTER=COUNTER+2
    done
    
    $(sed -i -E "s/(^[0-9]+),(.+)/\"\2\",\"\",\"\"/g" $workdir/Hashes_$2)
    
    COUNTER=0
    while (("$COUNTER" < "${#rHashes[@]}"))
    do
            echo "\"\",\"${rHashes[($COUNTER)]}\",\"${rHashes[($COUNTER + 1)]}\"" >> $workdir/Hashes_$2
            let COUNTER=COUNTER+2
    done