forked from SonarSource/sonar-loc-count
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathazure_devops_services.sh
executable file
·171 lines (128 loc) · 7.16 KB
/
azure_devops_services.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
156
157
158
159
160
161
162
163
164
#!/bin/bash
# set -x
#**************************************************************************#
# #
# @project : LoC Counting bash Scripts #
# @package : #
# @subpackage : azure #
# @access : #
# @paramtype : connectionToken,organization, project #
# @argument : #
# @description : Get Number ligne of Code in Azure DevOPS #
# @usage : ./azuredevops.sh <token> <organization> and optional <Project> #
# #
# #
# @author Emmanuel COLUSSI #
# @version 1.02 #
# #
#**************************************************************************#
if [ $# -lt 2 ]; then
echo "Usage: `basename $0` <token> <organization> optional <projects>"
exit
fi
# Set Variables token,org,BaseAPI
#--------------------------------------------------------------------------------------#
connectionToken=$1
org=$2
BaseAPI="https://dev.azure.com"
#--------------------------------------------------------------------------------------#
# 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="sed"
i=1
LISTF=""
LIST=""
NBCLOC="cpt.txt"
cpt=0
EXCLUDE=".clocignore"
# Test if request for for 1 Project or more Project
if [ -z ${3} ]; then
jq_args=".value[] | \"\(.name):\(.id)\""
GetAPI="$org/_apis/projects?api-version=7.0"
else
jq_args="\"\(.name):\(.id)\""
GetAPI="$org/_apis/projects/${3}?api-version=7.0"
fi
# Parse Project
curl -s -u :$connectionToken $BaseAPI/$GetAPI|jq -r ''"${jq_args}"''| while IFS=: read -r Name ID;
do
echo "--------------------------------------------------------------------------------------------"
echo -e "Project Number :$i Name : $Name id : $ID"
# Get List of Project
# Replace space by - in Project name for created local file
NameFile=` echo $Name|$SED s/' '/'-'/g`
# Replace space by %20 in Project name for Project URL
name1=` echo $Name|$SED s/' '/'%20'/g`
# Parse Project : Get Repositories
if [ -n ${3} ]; then jq_args=".value[] | \"\(.name):\(.id)\""
fi
curl -s -u :$connectionToken "$BaseAPI/$org/$name1/_apis/git/repositories?api-version=7.0" | jq -r ''"${jq_args}"'' | while IFS=: read -r RepoName RepoID;
do
# Replace / by - in Repo Name for created local file
RepoNameF=` echo $RepoName|$SED s/'\/'/'-'/g|$SED s/' '/'-'/g`
echo "--------------------------------------------------------------------------------------------"
echo -e " Repository Name : $RepoName id : $RepoID"
# Get List of Branches
curl -s -u :$connectionToken "$BaseAPI/$org/$Name/_apis/git/repositories/$RepoID/refs?filter=heads/&api-version=7.0" | jq -r '.value[].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://[email protected]/$org/$name1/_git/$RepoName --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 --ignore-case-ext --report-file=${LISTF} --exclude-dir=$(tr '\n' ',' < .clocignore) --timeout 0
else
cloc $NameFile --force-lang-def=sonar-lang-defs.txt --ignore-case-ext --report-file=${LISTF} --timeout 0
fi
# Delete Directory projet
/bin/rm -r $NameFile
$SED -i "1i\Report for project ${Name} / ${BrancheName}\n" $LISTF
done
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 Organization 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 Organization is : < %' .f > result in <Report_global.txt>\n" "${cpt}" >> Report_global.txt
echo -e "---------------------------------------------------------------------------------------------" >> Report_global.txt