Skip to content

Commit 5f1f625

Browse files
Refactored Code and Changed logic for pr decoration (#292)
1 parent d6aa631 commit 5f1f625

File tree

6 files changed

+185
-102
lines changed

6 files changed

+185
-102
lines changed

Dockerfile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@ USER root
66

77
# Copy the entrypoint script and properties used for the action
88
COPY entrypoint.sh /app/entrypoint.sh
9+
COPY scripts /app/scripts/
910
COPY cleanup.sh /app/cleanup.sh
1011

1112
RUN chmod +x /app/entrypoint.sh \
12-
&& chmod +x /app/cleanup.sh
13+
&& chmod +x /app/cleanup.sh \
14+
&& chmod +x /app/scripts/
1315

1416

1517
HEALTHCHECK NONE

entrypoint.sh

Lines changed: 23 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -1,116 +1,38 @@
11
#!/bin/bash
22

3-
output_file=./output.log
4-
53
# ------------------------------------------------------
6-
# Multi-registry auth.json creation
4+
# Main Entrypoint Orchestrator
75
# ------------------------------------------------------
8-
# Expected env vars:
9-
# REGISTRIES="docker.io ghcr.io registry.example.com"
10-
# USERNAME_<REGISTRY> and PASSWORD_<REGISTRY>
11-
# Example: USERNAME_DOCKER_IO, PASSWORD_DOCKER_IO
12-
# USERNAME_GHCR_IO, PASSWORD_GHCR_IO
13-
14-
if [[ -n "$REGISTRIES" ]]; then
15-
echo "🔑 Creating multi-registry auth.json..."
16-
mkdir -p /github/home/.config/containers
17-
auths_entries=""
18-
19-
for reg in $REGISTRIES; do
20-
# Convert registry to env var friendly form (dots & dashes to underscores, uppercase)
21-
env_suffix=$(echo "$reg" | tr '.-' '_' | tr '[:lower:]' '[:upper:]')
22-
23-
user_var="USERNAME_${env_suffix}"
24-
pass_var="PASSWORD_${env_suffix}"
25-
26-
user="${!user_var}"
27-
pass="${!pass_var}"
28-
29-
if [[ -n "$user" && -n "$pass" ]]; then
30-
encoded=$(echo -n "${user}:${pass}" | base64 -w0)
31-
auths_entries+="\"$reg\": {\"auth\": \"$encoded\"},"
32-
echo "✅ Added credentials for $reg"
33-
else
34-
echo "⚠️ Skipping $reg — missing username/password"
35-
fi
36-
done
37-
38-
# Remove trailing comma and wrap in JSON
39-
auths_entries="${auths_entries%,}"
40-
echo "{\"auths\": {${auths_entries}}}" > /github/home/.config/containers/auth.json
41-
echo "✅ Auth.json created at /github/home/.config/containers/auth.json"
42-
else
43-
echo "⚠️ No REGISTRIES specified, skipping auth.json creation."
44-
fi
45-
46-
# Parse global params (applied to all commands)
47-
if [ -n "${GLOBAL_PARAMS}" ]; then
48-
eval "global_arr=(${GLOBAL_PARAMS})"
49-
else
50-
global_arr=()
51-
fi
52-
53-
# Parse scan-specific params
54-
if [ -n "${SCAN_PARAMS}" ]; then
55-
eval "scan_arr=(${SCAN_PARAMS})"
56-
else
57-
scan_arr=()
58-
fi
596

60-
# Parse utils-specific params
61-
if [ -n "${UTILS_PARAMS}" ]; then
62-
eval "utils_arr=(${UTILS_PARAMS})"
63-
else
64-
utils_arr=()
65-
fi
7+
# 1. Setup Global Variables
8+
export output_file=./output.log
9+
echo "Server URL: $GITHUB_SERVER_URL"
6610

67-
# Parse results-specific params
68-
if [ -n "${RESULTS_PARAMS}" ]; then
69-
eval "results_arr=(${RESULTS_PARAMS})"
11+
# 2. Determine Environment (Cloud vs On-Prem)
12+
if [ "$GITHUB_SERVER_URL" = "https://github.com" ]; then
13+
echo "Detected GitHub Cloud"
14+
export IS_CLOUD=true
7015
else
71-
results_arr=()
16+
echo "Detected GitHub Enterprise Server"
17+
export IS_CLOUD=false
7218
fi
7319

74-
# Backward compatibility: Support ADDITIONAL_PARAMS
75-
if [ -n "${ADDITIONAL_PARAMS}" ] && [ -z "${SCAN_PARAMS}" ]; then
76-
echo "⚠️ ADDITIONAL_PARAMS is deprecated. Please use SCAN_PARAMS instead."
77-
eval "scan_arr=(${ADDITIONAL_PARAMS})"
78-
fi
79-
80-
# Combine global + scan-specific params
81-
combined_scan_params=("${global_arr[@]}" "${scan_arr[@]}")
82-
20+
# A. Multi-Registry Authentication
21+
source /app/scripts/auth.sh
8322

84-
/app/bin/cx scan create --project-name "${PROJECT_NAME}" -s "${SOURCE_DIR}" --branch "${BRANCH#refs/heads/}" --scan-info-format json --agent "Github Action" "${combined_scan_params[@]}" | tee -i $output_file
85-
exitCode=${PIPESTATUS[0]}
23+
# B. Scan Execution
24+
source /app/scripts/scan.sh
8625

87-
scanId=(`grep -E '"(ID)":"((\\"|[^"])*)"' $output_file | cut -d',' -f1 | cut -d':' -f2 | tr -d '"'`)
26+
# C. PR Decoration
27+
source /app/scripts/pr_decoration.sh
8828

89-
echo "cxcli=$(cat $output_file | tr -d '\r\n')" >> $GITHUB_OUTPUT
90-
91-
if [ -n "$scanId" ] && [ -n "${PR_NUMBER}" ]; then
92-
echo "Creating PR decoration for scan ID:" $scanId
93-
# Combine global + utils-specific params
94-
combined_utils_params=("${global_arr[@]}" "${utils_arr[@]}")
95-
/app/bin/cx utils pr github --scan-id "${scanId}" --namespace "${NAMESPACE}" --repo-name "${REPO_NAME}" --pr-number "${PR_NUMBER}" --token "${GITHUB_TOKEN}" "${combined_utils_params[@]}"
96-
else
97-
echo "PR decoration not created."
98-
fi
99-
100-
101-
if [ -n "$scanId" ]; then
102-
# Combine global + results-specific params
103-
combined_results_params=("${global_arr[@]}" "${results_arr[@]}")
104-
/app/bin/cx results show --scan-id "${scanId}" --report-format markdown "${combined_results_params[@]}"
105-
cat ./cx_result.md >$GITHUB_STEP_SUMMARY
106-
rm ./cx_result.md
107-
echo "cxScanID=$scanId" >> $GITHUB_OUTPUT
108-
fi
29+
# D. Results Reporting
30+
source /app/scripts/results.sh
10931

110-
if [ $exitCode -eq 0 ]
111-
then
112-
echo "Scan completed"
32+
# 4. Final Exit Handling
33+
if [ $exitCode -eq 0 ]; then
34+
echo "Scan completed successfully."
11335
else
114-
echo "Scan failed"
115-
exit $exitCode
36+
echo "Scan failed."
37+
exit $exitCode
11638
fi

scripts/auth.sh

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#!/bin/bash
2+
3+
# ------------------------------------------------------
4+
# Logic: Handle Registry Authentication
5+
# ------------------------------------------------------
6+
7+
if [[ -n "$REGISTRIES" ]]; then
8+
echo "🔑 Creating multi-registry auth.json..."
9+
mkdir -p /github/home/.config/containers
10+
auths_entries=""
11+
12+
for reg in $REGISTRIES; do
13+
# Convert registry to env var friendly form (dots & dashes to underscores, uppercase)
14+
env_suffix=$(echo "$reg" | tr '.-' '_' | tr '[:lower:]' '[:upper:]')
15+
16+
user_var="USERNAME_${env_suffix}"
17+
pass_var="PASSWORD_${env_suffix}"
18+
19+
user="${!user_var}"
20+
pass="${!pass_var}"
21+
22+
if [[ -n "$user" && -n "$pass" ]]; then
23+
encoded=$(echo -n "${user}:${pass}" | base64 -w0)
24+
auths_entries+="\"$reg\": {\"auth\": \"$encoded\"},"
25+
echo "✅ Added credentials for $reg"
26+
else
27+
echo "⚠️ Skipping $reg — missing username/password"
28+
fi
29+
done
30+
31+
# Remove trailing comma and wrap in JSON
32+
auths_entries="${auths_entries%,}"
33+
echo "{\"auths\": {${auths_entries}}}" > /github/home/.config/containers/auth.json
34+
echo "✅ Auth.json created at /github/home/.config/containers/auth.json"
35+
else
36+
echo "⚠️ No REGISTRIES specified, skipping auth.json creation."
37+
fi

scripts/pr_decoration.sh

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#!/bin/bash
2+
3+
# ------------------------------------------------------
4+
# Logic: PR Decoration
5+
# ------------------------------------------------------
6+
7+
# Parse utils-specific params
8+
if [ -n "${UTILS_PARAMS}" ]; then
9+
eval "utils_arr=(${UTILS_PARAMS})"
10+
else
11+
utils_arr=()
12+
fi
13+
14+
combined_utils_params=("${global_arr[@]}" "${utils_arr[@]}")
15+
16+
# Detect if customer manually set code-repository-url
17+
USER_PROVIDED_CODE_REPO_URL=false
18+
for param in "${combined_utils_params[@]}"; do
19+
if [[ "$param" == "--code-repository-url" ]] || [[ "$param" == --code-repository-url=* ]]; then
20+
USER_PROVIDED_CODE_REPO_URL=true
21+
break
22+
fi
23+
done
24+
25+
if [ -n "$scanId" ] && [ -n "${PR_NUMBER}" ]; then
26+
echo "Creating PR decoration for scan ID: $scanId"
27+
28+
# Build base command
29+
base_cmd=(
30+
/app/bin/cx utils pr github
31+
--scan-id "${scanId}"
32+
--namespace "${NAMESPACE}"
33+
--repo-name "${REPO_NAME}"
34+
--pr-number "${PR_NUMBER}"
35+
--token "${GITHUB_TOKEN}"
36+
)
37+
38+
# 1. If user manually provided --code-repository-url, use it exactly as-is
39+
if [ "$USER_PROVIDED_CODE_REPO_URL" = true ]; then
40+
echo "User provided custom --code-repository-url. Using it."
41+
42+
# 2. Else if on-prem server (IS_CLOUD=false), add our default on-prem URL
43+
elif [ "$IS_CLOUD" = false ]; then
44+
echo "Detected On-Prem GitHub. Adding default code-repository-url."
45+
base_cmd+=(--code-repository-url "${GITHUB_SERVER_URL}")
46+
47+
# 3. Else Cloud,do nothing
48+
else
49+
echo "GitHub Cloud detected. No extra code-repository-url needed."
50+
fi
51+
52+
# Append ALL utils parameters (including user's custom params)
53+
base_cmd+=("${combined_utils_params[@]}")
54+
55+
# Execute
56+
"${base_cmd[@]}"
57+
58+
else
59+
echo "PR decoration not created"
60+
fi

scripts/results.sh

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#!/bin/bash
2+
3+
# ------------------------------------------------------
4+
# Logic: Generate Results/Report
5+
# ------------------------------------------------------
6+
7+
if [ -n "$scanId" ]; then
8+
# Parse results-specific params
9+
if [ -n "${RESULTS_PARAMS}" ]; then
10+
eval "results_arr=(${RESULTS_PARAMS})"
11+
else
12+
results_arr=()
13+
fi
14+
15+
# Combine global + results-specific params
16+
combined_results_params=("${global_arr[@]}" "${results_arr[@]}")
17+
18+
/app/bin/cx results show --scan-id "${scanId}" --report-format markdown "${combined_results_params[@]}"
19+
20+
cat ./cx_result.md >$GITHUB_STEP_SUMMARY
21+
rm ./cx_result.md
22+
23+
echo "cxScanID=$scanId" >> $GITHUB_OUTPUT
24+
fi

scripts/scan.sh

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#!/bin/bash
2+
3+
# ------------------------------------------------------
4+
# Logic: Parse Params and Run Scan
5+
# ------------------------------------------------------
6+
7+
# Parse global params (applied to all commands)
8+
if [ -n "${GLOBAL_PARAMS}" ]; then
9+
eval "global_arr=(${GLOBAL_PARAMS})"
10+
else
11+
global_arr=()
12+
fi
13+
14+
# Parse scan-specific params
15+
if [ -n "${SCAN_PARAMS}" ]; then
16+
eval "scan_arr=(${SCAN_PARAMS})"
17+
else
18+
scan_arr=()
19+
fi
20+
21+
# Backward compatibility: Support ADDITIONAL_PARAMS
22+
if [ -n "${ADDITIONAL_PARAMS}" ] && [ -z "${SCAN_PARAMS}" ]; then
23+
echo "⚠️ ADDITIONAL_PARAMS is deprecated. Please use SCAN_PARAMS instead."
24+
eval "scan_arr=(${ADDITIONAL_PARAMS})"
25+
fi
26+
27+
# Combine global + scan-specific params
28+
combined_scan_params=("${global_arr[@]}" "${scan_arr[@]}")
29+
30+
# Execute Scan
31+
/app/bin/cx scan create --project-name "${PROJECT_NAME}" -s "${SOURCE_DIR}" --branch "${BRANCH#refs/heads/}" --scan-info-format json --agent "Github Action" "${combined_scan_params[@]}" | tee -i "$output_file"
32+
exitCode=${PIPESTATUS[0]}
33+
34+
# Extract Scan ID
35+
scanId=(`grep -E '"(ID)":"((\\"|[^"])*)"' "$output_file" | cut -d',' -f1 | cut -d':' -f2 | tr -d '"'`)
36+
37+
# Output for GitHub Actions
38+
echo "cxcli=$(cat "$output_file" | tr -d '\r\n')" >> $GITHUB_OUTPUT

0 commit comments

Comments
 (0)