Files
rinoa-docker/.gitea/workflows/branch-sonarscan-pr-merge.yml
T

174 lines
6.8 KiB
YAML

name: Gitea Branch PR, SonarQube Analyze, and Merge Workflow
on:
push:
branches-ignore:
- main
jobs:
check-and-create-pr:
name: Check and Create PR
runs-on: ubuntu-latest
outputs:
pr_number: ${{ steps.cc-pr.outputs.pr_index }}
steps:
- name: Checkout Code
uses: actions/checkout@v4
- name: PR Check/Create
id: cc-pr
run: |
echo "Checking for existing PR..."
pr_check=$(curl ${{ vars.RINOA_GITEA_URL }}/api/v1/repos/${{ github.repository }}/pulls/main/${{ github.ref_name }} \
-X 'GET' \
-H 'Accept: application/json' \
-H 'Authorization: token ${{ secrets.BOT_GITEA_TOKEN }}' \
-s | jq '{index: .number, state: .state}')
pr_status=$(echo ${pr_check} | jq -r '.state')
if [ "${pr_status}" == "open" ]; then
echo "PR already exists. PR number: $(echo ${pr_check} | jq -r '.index')"
echo "pr_created=false" >> "$GITHUB_OUTPUT"
echo "pr_index=$(echo ${pr_check} | jq -r '.index')" >> "$GITHUB_OUTPUT"
elif [ "${pr_status}" == "closed" ]; then
echo "PR does not exist. Creating PR..."
pr_response=$(curl ${{ vars.RINOA_GITEA_URL }}/api/v1/repos/${{ github.repository }}/pulls -s \
-X 'POST' \
-H 'Accept: application/json' \
-H 'Authorization: token ${{ secrets.BOT_GITEA_TOKEN }}' \
-H 'Content-Type: application/json' \
-d '{
"base": "main",
"head": "'"${{ github.ref_name }}"'",
"title": "Automated PR for branch '"${{ github.ref_name }}"'",
"body": "This is an automated PR created for branch '"${{ github.ref_name }}"'."
}')
pr_index=$(echo ${pr_response} | jq -r '.number')
echo "PR created. PR number: ${pr_index}"
echo "pr_created=true" >> "$GITHUB_OUTPUT"
echo "pr_index=${pr_index}" >> "$GITHUB_OUTPUT"
else
echo "Error checking for existing PR. Exiting..."
exit 1
fi
sonarqube-analysis-dry-run:
name: SonarQube Analysis & Compose Dry-Run
runs-on: ubuntu-latest
needs: check-and-create-pr
outputs:
qg_status: ${{ steps.quality-gate.outputs.quality-gate-status }}
qg_results: ${{ steps.quality-gate-check.outputs.quality-gate-result }}
steps:
- name: Checkout Code
uses: actions/checkout@v4
- name: SonarQube Scan
uses: sonarsource/sonarqube-scan-action@v4.1.0
env:
SONAR_HOST_URL: ${{ secrets.SONARQUBE_HOST }}
SONAR_TOKEN: ${{ secrets.SONARQUBE_TOKEN }}
- name: SonarQube Quality Gate
id: quality-gate
uses: sonarsource/sonarqube-quality-gate-action@v1.1.0
env:
SONAR_HOST_URL: ${{ secrets.SONARQUBE_HOST }}
SONAR_TOKEN: ${{ secrets.SONARQUBE_TOKEN }}
- name: Custom Quality Gate Check
uses: DesarrolloORT/sonarqube-quality-gate-action@v1.0.1
id: quality-gate-check
with:
sonar-project-key: rinoa-docker
sonar-host-url: ${{ secrets.SONARQUBE_HOST }}
sonar-token: ${{ secrets.SONARQUBE_TOKEN }}
- name: Generate Ephemeral .env for Dry Run
run: |
echo "${{ secrets.RINOA_ENV }}" > .env
- name: Docker Compose Lint
id: docker-compose-lint
uses: sjafferali/docker-compose-lint-action@v0.1.2
with:
compose-file: './docker-compose.yml'
env:
DOCKER_HOST: tcp://dockerproxy:2375
scan-json-merge-pr:
name: Merge PR if checks pass
runs-on: ubuntu-latest
needs: sonarqube-analysis-dry-run
steps:
- name: Checkout Code
uses: actions/checkout@v4
- name: JSON clean-up for Custom Quality Gate Check...
id: json-cleanup
run: |
echo "Cleaning up quality gate response..."
echo '${QG_RESULTS}' > qg_input.txt
sed -E 's/([a-zA-Z0-9_]+):/\\"\1\\":/g; s/:([^",{}\[\]]+)/:"\1"/g' qg_input.txt > qg_raw.json
jq -c '.' qg_raw.json > qg_fixed_json.json
echo "qgfixedjson=$(cat qg_fixed_json.json)" >> $GITHUB_OUTPUT
echo "JSON cleanup complete."
projstatus=$(jq -r '.projectStatus.status' qg_fixed_json.json)
caycStatus=$(jq -r '.projectStatus.caycStatus' qg_fixed_json.json)
conditions=$(jq -c '.projectStatus.conditions' qg_fixed_json.json)
echo "projstatus=${projstatus}" >> $GITHUB_OUTPUT
echo "caycStatus=${caycStatus}" >> $GITHUB_OUTPUT
echo "conditions=${conditions}" >> $GITHUB_OUTPUT
env:
QG_RESULTS: ${{ needs.sonarqube-analysis-dry-run.outputs.qg_results }}
- name: Convert JSON to Markdown Table
id: convert-json-to-md
uses: buildingcash/json-to-markdown-table-action@v1.1.0
with:
json: "${{ steps.json-cleanup.outputs.conditions }}"
- name: Post SonarQube Results as Comment
env:
PR_NUMBER: ${{ needs.check-and-create-pr.outputs.pr_number }}
SQ_RESULTS: ${{ steps.convert-json-to-md.outputs.table }}
QG_STATUS: ${{ needs.quality-gate.outputs.qg_status }}
QG_RESULTS: ${{ needs.sonarqube-analysis-dry-run.outputs.qg_results }}
RINOA_GITEA_URL: ${{ vars.RINOA_GITEA_URL }}
GITHUB_REPOSITORY: ${{ github.repository }}
BOT_GITEA_TOKEN: ${{ secrets.BOT_GITEA_TOKEN }}
run: |
formatted_results=$(echo "${SQ_RESULTS}" | sed 's/\\n/\
/g')
payload=$(jq -n \
--arg body "SonarQube analysis results:
<br>
${{ env.SQ_RESULTS }}" \
'{ body: $body }')
response=$(curl -s -o response.json -w "%{http_code}" \
-X POST \
-H "Accept: application/json" \
-H "Authorization: token ${BOT_GITEA_TOKEN}" \
-H "Content-Type: application/json" \
-d "$payload" \
"${RINOA_GITEA_URL}/api/v1/repos/${GITHUB_REPOSITORY}/pulls/${PR_NUMBER}/reviews")
# - name: Merge PR in Gitea
# uses: prasiman/gocurl@v1
# with:
# url: "${{ secrets.GITEA_INSTANCE_URL }}/repos/${{ github.repository_owner }}/${{ github.event.repository.name }}/pulls/${{ needs.check-and-create-pr.outputs.pr_index }}"
# method: "POST"
# headers: '{ "Authorization": "token ${{ secrets.GITEA_API_TOKEN }}", "Content-Type": "application/json" }'
# params: >-
# {
# "Do": "merge",
# "delete_branch_after_merge": true,
# "force_merge": true,
# "merge_when_checks_succeed": true
# }
# - name: Confirm Merge
# run: echo "PR has been successfully merged into main."