-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfossid-git.sh
178 lines (157 loc) · 3.19 KB
/
fossid-git.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#! /bin/bash
# FossID GIT Integration Script v1.0
# Copyright (c) 2020 OSBC
# jslee@osbc.co.kr
# Licensed under GPL v2+
# Release Note
# =====
# v0.1
# Initial release
# v0.2
# Structural improvement
# v0.3
# Improvement using jq
# v0.4
# Structural improvement
# v0.5
# Structural improvement
# v0.6
# create child scan if project already exists
# v0.7
# added support for git repository scanning
# v0.8
# added feature: check scan progress
url=$1
id=$2
key=$3
pname=$4
sname=$5
path=$6
git_url=$7
git_branch=$8
print_usage() {
echo "Usage: use appropriate params"
exit 2
}
function main() {
result=$(\
curl -s POST \
-d '{"group": "projects", "action": "list_projects", "data": {"username": "$id", "key": "$key"}}' \
-H "Content-Type: application/json" $url | jq '.data[] | select(.project_name == "") | .id' \
)
if [ -z "$result" ];
then
create_project
fi
if [ -z "$git_url" ];
then
create_scan
else
echo "create scan from git..."
create_scan_from_git
download_from_git
fi
run_scan
scan_finished=$(get_scan_status)
echo $scan_finished
while [ ${scan_finished} -eq 0 ]; do
echo "Scanning... $(get_scan_progress)"
scan_finished=$(get_scan_status)
sleep 2
if [[ ${scan_finished} -eq 1 ]]; then
break
fi
done
echo "Finished."
}
function create_project()
{
curl -s POST \
-H "Content-Type: application/json" \
-d "$(cat <<EOT
{
"group": "projects", "action": "create",
"data": {"username": "$id", "key": "$key", "project_code": "$pname", "project_name": "$pname"}
}
EOT
)" \
$url | jq "."
}
function create_scan()
{
curl -s POST \
-H "Content-Type: application/json" \
-d "$(cat <<EOT
{
"group": "scans", "action": "create",
"data": {"username": "$id", "key": "$key", "project_code": "$pname", "scan_code": "$sname", "scan_name": "$sname",
"target_path": "$path"}
}
EOT
)" \
$url | jq "."
}
function create_scan_from_git() {
curl -s POST \
-H "Content-Type: application/json" \
-d "$(cat <<EOT
{
"group": "scans", "action": "create",
"data": {"username": "$id", "key": "$key", "project_code": "$pname", "scan_code": "$sname", "scan_name": "$sname",
"git_repo_url": "$git_url", "git_branch": "$git_branch"}
}
EOT
)" \
$url -v | jq "."
}
function download_from_git() {
curl -X POST \
-H "Content-Type: application/json" \
-d "$(cat <<EOT
{
"group": "scans", "action": "download_content_from_git",
"data": {"username": "$id", "key": "$key", "scan_code": "$sname"}
}
EOT
)" \
$url -v | jq "."
}
function run_scan() {
curl -s POST \
-H "Content-Type: application/json" \
-d "$(cat <<EOT
{
"group": "scans", "action": "run",
"data": {"username": "$id", "key": "$key", "scan_code": "$sname"}
}
EOT
)" \
$url | jq "."
}
function get_scan_status() {
curl -s POST \
-H "Content-Type: application/json" \
-d "$(cat <<EOT
{
"group": "scans", "action": "check_status",
"data": {"username": "$id", "key": "$key", "scan_code": "$sname", "type": "SCAN"}
}
EOT
)" \
$url -s | jq '.data | .is_finished' -r
return
}
function get_scan_progress() {
curl -s POST \
-H "Content-Type: application/json" \
-d "$(cat <<EOT
{
"group": "scans", "action": "check_status",
"data": {"username": "$id", "key": "$key", "scan_code": "$sname", "type": "SCAN"}
}
EOT
)" \
$url -s | jq '.data | .percentage_done' -r
return
}
main