forked from SonarSource/sonar-loc-count
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbitbucket_org.sh
executable file
·155 lines (123 loc) · 6.92 KB
/
bitbucket_org.sh
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#!/bin/bash
#set -x
#***********************************************************************************#
# #
# @project : LoC Counting bash Scripts #
# @package : #
# @subpackage : bitbucket #
# @access : #
# @paramtype : user token,workspace ,reponame #
# @argument : #
# @description : Get Number ligne of Code in Bitbucket DevOPS #
# @usage : ./bitbucket_org.sh <user> <token> <workspace> and optional <repoName> #
# #
# #
# @author Emmanuel COLUSSI #
# @version 1.01 #
# #
#***********************************************************************************#
if [ $# -lt 3 ]; then
echo "Usage: `basename $0` <user> <token> <workspace> and optional <repoName>"
exit
fi
# Set Variables user,token,org,BaseAPI
#--------------------------------------------------------------------------------------#
user=$1
connectionToken=$2
wks=$3
BaseAPI="https://api.bitbucket.org/2.0"
BaseAPI1="bitbucket.org"
#--------------------------------------------------------------------------------------#
# If you use mac osx you have to install the gnu-sed (brew install gnu-sed) then you set in the scripts the variable SED. SED=gsed
# For linux SED=sed
SED="gsed"
i=1
LISTF=""
LIST=""
NBCLOC="cpt.txt"
cpt=0
EXCLUDE=".clocignore"
# Test if request for for 1 Repo or more Repo
if [ -z ${4} ]; then
jq_args=".values[] | \"\(.name):\(.uuid)\""
# If you have more than 100 repos Change Value of parameter page=Number_of_page
# 1 Page = 100 repos max
# Example for 150 repos :
# GetAPI="orgs/$org/repos?pagelen=100&page=2"
GetAPI="repositories/$wks?pagelen=100&page=1"
else
jq_args="\"\(.name):\(.uuid)\""
GetAPI="repositories/$wks/$4"
fi
# Get List of Repositories : get Name , ID and http_url_to_repo
curl -s -u $user:$connectionToken $BaseAPI/$GetAPI|jq -r ''"${jq_args}"''| while IFS=: read -r Name UUID;
do
echo "-----------------------------------------------------------------------------------------------------------------------"
echo -e "Repository Number :$i Name : $Name uuid : $UUID"
# Get List of Branches
# Replace space by - in Repository name for created local file
NameFile=` echo $Name|$SED s/' '/'-'/g`
# Replace space by %20 in Repository name for Repository URL
name1=` echo $Name|$SED s/' '/'%20'/g`
curl -s -u $user:$connectionToken $BaseAPI/repositories/$wks/$Name/refs/branches | jq -r '.values[].name' | while read -r BrancheName ;
do
# Get Branche Name without path reference
BrancheNameF1=` echo $BrancheName|$SED s/'refs\/heads\/'/''/g`
# Replace / by - in Branche Name for created local file
BrancheNameF=` echo $BrancheNameF1|$SED s/'\/'/'-'/g|$SED s/' '/'-'/g`
LISTF="${NameFile}_${BrancheNameF}.cloc"
echo -e "\n Branche Name : $BrancheNameF1\n"
# Create Commad Git clone
git clone https://$user:${connectionToken}@$BaseAPI1/$wks/$name1 --depth 1 --branch $BrancheNameF1 $NameFile
# Run Analyse : run cloc on the local repository
if [ -s $EXCLUDE ]; then
cloc $NameFile --force-lang-def=sonar-lang-defs.txt --report-file=${LISTF} --exclude-dir=$(tr '\n' ',' < .clocignore) --timeout 0
else
cloc $NameFile --force-lang-def=sonar-lang-defs.txt --report-file=${LISTF} --timeout 0
fi
# Delete Directory projet
/bin/rm -r $NameFile
$SED -i "1i\Report for project ${Name} / ${BrancheName}\n" $LISTF
done
# Generate reports
echo -e "\nBuilding final report for projet $NameFile : $NameFile.txt"
echo "-----------------------------------------------------------------------------------------------------------------------"
# BRTAB2 : array with branche Name , The index is number max of cloc
# NBTAB1 : array with number max of cloc by branche
([ `printf *.cloc` != '*.cloc' ] || [ -f '*.txt' ]) && for j in `ls -la *.cloc|awk '{print $9}'`; do CMD1=`cat $j |grep SUM:|awk '{print $5}'`;BRTAB2["$CMD1"]=${j%?????};NBTAB1+="$CMD1 ";cat $j >> $NameFile.txt;done && /bin/rm *.cloc || echo ""
# Find
IFS=' ' NBRCLOC=( $NBTAB1 )
IFS=$'\n' sorted=( $(sort -nr <<<"${NBRCLOC[*]}") )
INDEX01=${sorted[0]}
if [ -z "$INDEX01" ]; then
MESSAGE01="0"
MESSAGE02="No"
INDEX01=0
else
MESSAGE01="$INDEX01"
SEA="${NameFile}_"
MESSAGE02=`echo ${BRTAB2[${INDEX01}]}| $SED s/$SEA/''/g`
fi
printf "The maximum lines of code in the < %s > project is : < %' .f > for the branch : < %s >\n" "${NameFile}" "${MESSAGE01}" "${MESSAGE02}"
echo -e "\n---------------------------------------------------------------------------------------------------------------------" >> $NameFile.txt
printf "\nThe maximum lines of code in the < %s > project is : < %' .f > for the branch : < %s >\n" "${NameFile}" "${MESSAGE01}" "${MESSAGE02}" >> $NameFile.txt
echo -e "-----------------------------------------------------------------------------------------------------------------------" >> $NameFile.txt
# set Nbr Loc by Project in File cpt.txt
echo "${INDEX01}" >> $NBCLOC
LISTF=""
NBTAB1=()
BRTAB2=()
let "i=i+1"
done
# Generate Gobal report
while read line
do
cpt=$(expr $cpt + $line)
done < $NBCLOC
/bin/rm $NBCLOC
echo -e "\n-------------------------------------------------------------------------------------------"
printf "The maximum lines of code on the repository is : < %' .f > result in <Report_global.txt>\n" "${cpt}"
echo -e "\n-------------------------------------------------------------------------------------------"
echo -e "-------------------------------------------------------------------------------------------\n" > Report_global.txt
printf "The maximum lines of code on the repository is : < %' .f > result in <Report_global.txt>\n" "${cpt}" >> Report_global.txt
echo -e "---------------------------------------------------------------------------------------------" >> Report_global.txt