Skip to content

Commit

Permalink
Updated ECLAIR integration scripts (#60)
Browse files Browse the repository at this point in the history
* Improved ECL configurations.

* Added manual workflow ECLAIR_final.

* Updated action.helpers.

* Updated action.helpers.

* Updated action.helpers.

* Fixed image link.

* Updated scripts.

---------

Co-authored-by: Simone Ballarin <[email protected]>
  • Loading branch information
sballari and Simone Ballarin authored Jul 20, 2023
1 parent 2b16a07 commit f762fdc
Show file tree
Hide file tree
Showing 6 changed files with 285 additions and 100 deletions.
86 changes: 72 additions & 14 deletions ECLAIR/action.helpers
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
if [ -n "${GITLAB_CI+x}" ]; then
if [ -n "${GITLAB_CI:-}" ]; then
ci=gitlab
elif [ -n "${GITHUB_ACTION+x}" ]; then
elif [ -n "${GITHUB_ACTION:-}" ]; then
ci=github
elif [ -n "${JENKINS_HOME:-}" ]; then
ci=jenkins
else
echo "Unexpected CI/CD context" >&2
exit 1
Expand Down Expand Up @@ -32,18 +34,55 @@ summary() {
eval "${var}=${val}"
done <"${updateLog}"

case "${ci}" in
github)
nl="\\"
;;
gitlab)
nl=""
;;
jenkins)
nl="<br/>"
;;
*)
nl=""
;;
esac

if [ -z "${newReports}" ]; then
countsMsg="Unfixed reports: ${unfixedReports}"
fixedMsg=""
unfixedMsg="Unfixed reports: ${unfixedReports}"
countsMsg="${unfixedMsg}"
else
countsMsg="Fixed reports: ${fixedReports}
Unfixed reports: ${unfixedReports} [new: ${newReports}]"
fixedMsg="Fixed reports: ${fixedReports}"
unfixedMsg="Unfixed reports: ${unfixedReports} [new: ${newReports}]"
countsMsg="${fixedMsg}${nl}
${unfixedMsg}"
fi
cat <<EOF >"${summaryTxt}"
# [![ECLAIR](${eclairReportUrlPrefix}/rsrc/eclair.png)](https://www.bugseng.com/eclair) Analysis summary
${jobHeadline}
${countsMsg}
case "${ci}" in
jenkins)
cat <<EOF >"${summaryTxt}"
${countsMsg} ${nl}
<a href="https://www.bugseng.com/eclair">
<img src="${eclairReportUrlPrefix}/rsrc/eclair.svg" width="100" />
</a>
<h3>${jobHeadline}</h3>
<a href="${indexHtmlUrl}">Browse analysis results</a>
EOF
;;
*)
cat <<EOF >"${summaryTxt}"
<a href="https://www.bugseng.com/eclair">
<img src="${eclairReportUrlPrefix}/rsrc/eclair.svg" width="100" />
</a>
Analysis Summary

${jobHeadline}${nl}
${countsMsg}${nl}
[Browse analysis](${indexHtmlUrl})
EOF
;;
esac

case ${ci} in
github)
Expand All @@ -55,10 +94,18 @@ EOF
cat <<EOF
${jobHeadline}
${countsMsg}
Browse analysys: ${esc}[33m${indexHtmlUrl}${esc}[m
Browse analysis: ${esc}[33m${indexHtmlUrl}${esc}[m
EOF
close_section ECLAIR_summary
;;
jenkins)
cat <<EOF
${jobHeadline}
${fixedMsg}
${unfixedMsg}
Browse analysis: ${indexHtmlUrl}
EOF
;;
*)
echo "Unexpected CI/CD context" >&2
exit 1
Expand All @@ -78,7 +125,7 @@ log_file() {
fi

case ${ci} in
github)
github | jenkins)
echo "${section_name}"
;;
gitlab)
Expand All @@ -93,7 +140,7 @@ log_file() {
cat "${file}"

case ${ci} in
github) ;;
github | jenkins) ;;
gitlab)
close_section "${section_id}"
;;
Expand All @@ -108,7 +155,7 @@ maybe_log_file_exit() {
exit_code=$4

case ${ci} in
github)
github | jenkins)
echo "${section_name}"
;;
gitlab)
Expand All @@ -125,11 +172,22 @@ maybe_log_file_exit() {
fi

case ${ci} in
github) ;;
github | jenkins) ;;
gitlab)
close_section "${section_id}"
;;
*) ;;
esac
return "${exit_code}"
}

is_enabled() {
case "$1" in
true | TRUE | y | Y | yes | YES | 1)
return 0
;;
*)
return 1
;;
esac
}
113 changes: 82 additions & 31 deletions ECLAIR/action.settings
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,65 @@

variantSubDir=
variantHeadline=
if [ -n "${VARIANT+x}" ]; then
if [ -n "${VARIANT:-}" ]; then
variantSubDir="/${VARIANT}"
variantHeadline=" [${VARIANT}]"
fi

# AUTO PR Feature
# If the following variables are defined, then all pipelines
# of other branches will be considered pull-requests to
# autoPRBranch.
# To be customized (keep it empty to disable the AUTO PR feature)
autoPRRepository=
# To be customized (keep it empty to disable the AUTO PR feature)
autoPRBranch=

# To be customized
keepOldAnalyses=0

# To be customized
artifactsRoot=/var/local/eclair/github

case "${ci}" in
github)
autoPRRemoteUrl="${GITHUB_SERVER_URL}/${autoPRRepository:-}"
# To be customized
repository=${GITHUB_REPOSITORY}
jobId=${GITHUB_RUN_NUMBER}

case "${GITHUB_EVENT_NAME}" in
pull_request*)
event="pull_request"
pullRequestId=${GITHUB_EVENT_PULL_REQUEST_NUMBER}
pullRequestHeadRepo=${PR_HEAD_REPO}
pullRequestHeadRef=${PR_HEAD_REF}
pullRequestBaseRef=${PR_BASE_REF}
pullRequestUser=${PR_USER}
# baseCommitId and headCommitId are the most recent merge points without conflicts
git fetch -q --deepen=2
baseCommitId=$(git show -s --pretty=%H HEAD^1)
headCommitId=$(git show -s --pretty=%H HEAD^2)
;;
push | workflow_dispatch)
event="push"
# Extract the branch name from "refs/heads/<branch>"
branch=${GITHUB_REF#refs/heads/}
headCommitId=${GITHUB_SHA}
pushUser=${GITHUB_ACTOR}
;;
*)
echo "Unexpected GITHUB_REF ${GITHUB_REF}" >&2
exit 1
;;
esac
;;
gitlab)
gitlabApiUrl="${CI_SERVER_PROTOCOL}://${CI_SERVER_HOST}:${CI_SERVER_PORT}/api/v4"
autoPRRemoteUrl="${CI_SERVER_PROTOCOL}://${CI_SERVER_HOST}:${CI_SERVER_PORT}/${autoPRRepository:-}"
# To be customized
gitlabBotToken="${ECLAIRIT_TOKEN}"
gitlabBotToken="${ECLAIR_BOT_TOKEN:-}"
# To be customized
artifactsRoot=/home/eclair-gitlab/gitlab
repository=${CI_PROJECT_PATH}
jobId=${CI_JOB_ID}

Expand All @@ -32,49 +79,45 @@ gitlab)
event="push"
branch=${CI_COMMIT_BRANCH}
headCommitId=${CI_COMMIT_SHA}
pushUser=${GITLAB_USER_NAME}
;;
*)
echo "Unexpected event ${CI_PIPELINE_SOURCE}" >&2
exit 1
;;
esac
;;
github)
jenkins)
jenkinsApiUrl="${JENKINS_URL}"
autoPRRemoteUrl="${JENKINS_URL}/${autoPRRepository:-}"
# To be customized
artifactsRoot=/home/eclair-github/public
repository=${GITHUB_REPOSITORY}
jobId=${GITHUB_RUN_NUMBER}
jenkinsBotUsername="${ECLAIR_BOT_USERNAME:-}"
jenkinsBotToken="${ECLAIR_BOT_TOKEN:-}"

case "${GITHUB_EVENT_NAME}" in
pull_request*)
event="pull_request"
pullRequestId=${GITHUB_EVENT_PULL_REQUEST_NUMBER}
pullRequestHeadRepo=${PR_HEAD_REPO}
pullRequestHeadRef=${PR_HEAD_REF}
pullRequestBaseRef=${PR_BASE_REF}
pullRequestUser=${PR_USER}
# baseCommitId and headCommitId are the most recent merge points without conflicts
git fetch -q --deepen=2
baseCommitId=$(git show -s --pretty=%H HEAD^1)
headCommitId=$(git show -s --pretty=%H HEAD^2)
;;
push | workflow_dispatch)
event="push"
# Extract the branch name from "refs/heads/<branch>"
branch=${GITHUB_REF#refs/heads/}
headCommitId=${GITHUB_SHA}
;;
*)
echo "Unexpected GITHUB_REF ${GITHUB_REF}" >&2
exit 1
;;
esac
# To be customized
repository="${JOB_BASE_NAME}"
project="${JOB_NAME}"
jobId=${BUILD_NUMBER}

event="push"
branch=${GIT_BRANCH}
headCommitId=${GIT_COMMIT}
pushUser=$(git show --pretty='format:%aN' -s)
;;
*)
echo "Unexpected CI/CD context" >&2
exit 1
;;
esac

if [ "${event}" = "push" ] && [ -n "${autoPRBranch:-}" ]; then
# AUTO PR Feature enabled
if ! [ "${branch}" = "${autoPRBranch}" ] ||
! [ "${repository}" = "${autoPRRepository}" ]; then
event=auto_pull_request
fi
fi

case "${event}" in
pull_request)
subDir=${pullRequestHeadRepo}.ecdf/${pullRequestBaseRef}
Expand All @@ -85,6 +128,14 @@ push)
jobHeadline="ECLAIR ${ANALYSIS_KIND} on repository ${repository}: branch ${branch} (${headCommitId})"
badgeLabel="ECLAIR ${ANALYSIS_KIND} ${branch}${variantHeadline} #${jobId}"
;;
auto_pull_request)
git remote remove autoPRRemote || true
git remote add autoPRRemote "${autoPRRemoteUrl}"
git fetch autoPRRemote
subDir=${branch}
baseCommitId=$(git merge-base "autoPRRemote/${autoPRBranch}" HEAD)
jobHeadline="ECLAIR ${ANALYSIS_KIND} on repository ${repository}: ${pushUser} wants to merge ${repository}:${branch} (${headCommitId}) into ${autoPRRepository}/${autoPRBranch} (${baseCommitId})"
;;
*)
echo "Unexpected event ${event}" >&2
exit 1
Expand Down
45 changes: 24 additions & 21 deletions ECLAIR/action_pull_request.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ curl -sS "${eclairReportUrlPrefix}/ext/update_pull_request" \
-F "jobId=${jobId}" \
-F "jobHeadline=${jobHeadline}" \
-F "baseCommitId=${baseCommitId}" \
-F "keepOldAnalyses=${keepOldAnalyses}" \
-F "db=@${analysisOutputDir}/PROJECT.ecd" \
>"${updateLog}"
ex=0
Expand All @@ -31,24 +32,26 @@ maybe_log_file_exit PUBLISH_RESULT "Publishing results" "${updateLog}" "${ex}"

summary

case ${ci} in
github)
ex=0
gh api \
--method POST \
"/repos/${repository}/issues/${pullRequestId}/comments" \
-F "body=@${summaryTxt}" \
--silent >"${commentLog}" 2>&1 || ex=$?
maybe_log_file_exit ADD_COMMENT "Adding comment" "${commentLog}" "${ex}"
;;
gitlab)
curl -sS --request POST \
"${gitlabApiUrl}/projects/${CI_PROJECT_ID}/merge_requests/${pullRequestId}/notes" \
-H "PRIVATE-TOKEN: ${gitlabBotToken}" \
-F "body=<${summaryTxt}" >"${commentLog}"
ex=0
grep -Fq "Unfixed reports: " "${commentLog}" || ex=$?
maybe_log_file_exit ADD_COMMENT "Adding comment" "${commentLog}" "${ex}"
;;
*) ;;
esac
if is_enabled "${ENABLE_ECLAIR_BOT:-}"; then
case ${ci} in
github)
ex=0
gh api \
--method POST \
"/repos/${repository}/issues/${pullRequestId}/comments" \
-F "body=@${summaryTxt}" \
--silent >"${commentLog}" 2>&1 || ex=$?
maybe_log_file_exit ADD_COMMENT "Adding comment" "${commentLog}" "${ex}"
;;
gitlab)
curl -sS --request POST \
"${gitlabApiUrl}/projects/${CI_PROJECT_ID}/merge_requests/${pullRequestId}/notes" \
-H "PRIVATE-TOKEN: ${gitlabBotToken}" \
-F "body=<${summaryTxt}" >"${commentLog}"
ex=0
grep -Fq "Unfixed reports: " "${commentLog}" || ex=$?
maybe_log_file_exit ADD_COMMENT "Adding comment" "${commentLog}" "${ex}"
;;
*) ;;
esac
fi
Loading

0 comments on commit f762fdc

Please sign in to comment.