blob: f263963fe58820d132d1c29ac1fdcf458b679221 [file] [log] [blame]
Artem Naluzhnyy6ba95782019-06-10 16:39:54 +02001#!/bin/bash
2
3# Copyright 2019 Samsung Electronics Co., Ltd.
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17set -Eeuxo pipefail
18PS4='+['$(readlink -f "$0")' ${FUNCNAME[0]%main}#$LINENO] '
19
20echo '---> maven-coverity.sh'
21
Artem Naluzhnyyec81f732019-08-01 14:38:18 +020022SUBMISSION_ATTEMPTS=5
23SUBMISSION_INITIAL_REST_INTERVAL=30 # seconds, will be doubled after each attempt
24
Artem Naluzhnyy6ba95782019-06-10 16:39:54 +020025#-----------------------------------------------------------------------------
Artem Naluzhnyy59bc0372019-07-01 10:52:48 +020026# Process parameters for JS/PHP/Ruby files analysis
27
28FS_CAPTURE_SEARCH_PARAMS=''
29if [ -n "${SEARCH_PATHS:=}" ]; then
30 for SEARCH_PATH in ${SEARCH_PATHS}; do
31 if [ -d "${SEARCH_PATH}" ]; then
32 FS_CAPTURE_SEARCH_PARAMS="${FS_CAPTURE_SEARCH_PARAMS} --fs-capture-search '${SEARCH_PATH}'"
33 else
34 echo "'${SEARCH_PATH}' from \$SEARCH_PATHS is not an existing directory." >&2
35 exit 1
36 fi
37 done
38fi
39
40for EXCLUDE_REGEX in ${SEARCH_EXCLUDE_REGEXS:=}; do
41 FS_CAPTURE_SEARCH_PARAMS="${FS_CAPTURE_SEARCH_PARAMS} --fs-capture-search-exclude-regex '${EXCLUDE_REGEX}'"
42done
43
44#-----------------------------------------------------------------------------
Artem Naluzhnyy373fc1c2019-06-11 17:14:07 +020045# Check if we are allowed to submit results to Coverity Scan service
46# and have not exceeded our upload quota limits
47# See also: https://scan.coverity.com/faq#frequency
48
49CURL_OUTPUT=$(
50 curl \
51 --verbose \
52 --silent \
53 --show-error \
54 --fail \
55 --form "project=${COVERITY_PROJECT_NAME}" \
56 --form "token=${COVERITY_TOKEN}" \
57 'https://scan.coverity.com/api/upload_permitted'
58)
59
60IS_COVERITY_UPLOAD_PERMITTED=$(
61 echo "${CURL_OUTPUT}" \
62 | jq '.upload_permitted'
63)
64if [ x"${IS_COVERITY_UPLOAD_PERMITTED}" != x'true' ]; then
65 echo "Upload quota reached. Next upload permitted at "$(echo "${CURL_OUTPUT}" | jq '.next_upload_permitted_at') >&2
66 exit 1
67fi
68
69#-----------------------------------------------------------------------------
Artem Naluzhnyy6ba95782019-06-10 16:39:54 +020070# Get Coverity Scan build tool
71
72curl \
73 --verbose \
74 --silent \
75 --show-error \
76 --fail \
77 --form "project=${COVERITY_PROJECT_NAME}" \
78 --form "token=${COVERITY_TOKEN}" \
79 --output 'coverity_tool.tgz' \
80 'https://scan.coverity.com/download/linux64'
81
Artem Naluzhnyy450f6c62019-06-11 17:19:40 +020082curl \
83 --verbose \
84 --silent \
85 --show-error \
86 --fail \
87 --form "project=${COVERITY_PROJECT_NAME}" \
88 --form "token=${COVERITY_TOKEN}" \
89 --form 'md5=1' \
90 --output 'coverity_tool.md5' \
91 'https://scan.coverity.com/download/linux64'
92
93echo -n ' coverity_tool.tgz' >> 'coverity_tool.md5'
94md5sum --check 'coverity_tool.md5'
95
Artem Naluzhnyy6ba95782019-06-10 16:39:54 +020096tar \
97 --extract \
98 --gunzip \
99 --file='coverity_tool.tgz'
100
101COVERITY_BUILD_TOOL_DIRECTORY=$(
102 head -1 <( \
103 tar \
104 --list \
105 --gunzip \
106 --file='coverity_tool.tgz'
107 )
108)
109COVERITY_BINARY_DIRECTORY="${COVERITY_BUILD_TOOL_DIRECTORY}bin"
110test -d "${COVERITY_BINARY_DIRECTORY}" \
111 || exit 1
112export PATH="${PATH}:${COVERITY_BINARY_DIRECTORY}"
113
114rm 'coverity_tool.tgz'
115
116#-----------------------------------------------------------------------------
117# Build
118
119export MAVEN_OPTS
120
Artem Naluzhnyy59bc0372019-07-01 10:52:48 +0200121eval cov-build \
Artem Naluzhnyy6ba95782019-06-10 16:39:54 +0200122 --dir 'cov-int' \
Artem Naluzhnyy59bc0372019-07-01 10:52:48 +0200123 ${FS_CAPTURE_SEARCH_PARAMS} \
Artem Naluzhnyy6ba95782019-06-10 16:39:54 +0200124 "${MVN}" clean install \
125 --errors \
126 --global-settings "${GLOBAL_SETTINGS_FILE}" \
127 --settings "${SETTINGS_FILE}" \
128 ${MAVEN_OPTIONS:=} \
129 ${MAVEN_PARAMS:=}
130
131cov-import-scm \
132 --dir 'cov-int' \
133 --scm 'git'
134
Artem Naluzhnyy097f7342019-06-27 14:14:14 +0200135cov-manage-emit \
136 --dir cov-int \
137 list \
138| grep \
139 --invert-match \
140 '^Translation unit:$' \
141| sed \
142 's!^[[:digit:]]\+ -> !!' \
143> 'coverity-scan-analysed-files.log'
144
Artem Naluzhnyy6ba95782019-06-10 16:39:54 +0200145#-----------------------------------------------------------------------------
146# Submit results to Coverity service
147
148tar \
149 --create \
150 --gzip \
151 --file='results.tgz' \
152 'cov-int'
153
Artem Naluzhnyyec81f732019-08-01 14:38:18 +0200154for (( ATTEMPT=1; ATTEMPT<=SUBMISSION_ATTEMPTS; ATTEMPT++ )); do
155 CURL_OUTPUT=$(
156 curl \
157 --verbose \
158 --silent \
159 --show-error \
160 --fail \
161 --write-out '\n%{http_code}' \
162 --form "project=${COVERITY_PROJECT_NAME}" \
163 --form "email=${COVERITY_USER_EMAIL}" \
164 --form "token=${COVERITY_TOKEN}" \
165 --form 'file=@results.tgz' \
166 --form "version=${GIT_COMMIT:0:7}" \
167 --form "description=${GIT_BRANCH}" \
168 'https://scan.coverity.com/builds'
169 )
170 HTTP_RESPONSE_CODE=$(echo -n "${CURL_OUTPUT}" | tail -1)
171 test x"${HTTP_RESPONSE_CODE}" = x"200" \
172 && break
173
174 sleep "${SUBMISSION_REST_INTERVAL:-$SUBMISSION_INITIAL_REST_INTERVAL}"
175
176 SUBMISSION_REST_INTERVAL=$(( ${SUBMISSION_REST_INTERVAL:-$SUBMISSION_INITIAL_REST_INTERVAL} * 2 ))
177done
178
179HTTP_RESPONSE=$(echo -n "${CURL_OUTPUT}" | head -n -1 | tr -d '\n')
180if [ x"${HTTP_RESPONSE}" != x"Build successfully submitted." ]; then
181 echo "Coverity Scan service responded with '${HTTP_RESPONSE}' while 'Build successfully submitted.' expected." >&2
182 exit 1
183fi
Artem Naluzhnyy6ba95782019-06-10 16:39:54 +0200184
185#-----------------------------------------------------------------------------
186
187exit 0