From 21425b10d615db48901413434a1a0ee7a11e83e2 Mon Sep 17 00:00:00 2001 From: Arlind Date: Fri, 20 Jun 2025 16:17:47 +0200 Subject: [PATCH] Update .gitea/workflows/ci.yml --- .gitea/workflows/ci.yml | 129 ++++++++++++++++++---------------------- 1 file changed, 59 insertions(+), 70 deletions(-) diff --git a/.gitea/workflows/ci.yml b/.gitea/workflows/ci.yml index cb1f7c6..c6672bf 100644 --- a/.gitea/workflows/ci.yml +++ b/.gitea/workflows/ci.yml @@ -6,16 +6,17 @@ on: workflow_dispatch: env: + README_PATH: "${{ github.workspace }}/README.md" IMAGE_NAME: osc/skins-image GITEA_API: https://${{ vars.CONTAINER_REGISTRY }}/api/v1 - README_PATH: "README.md" jobs: - update_readme: - name: Fetch Skin Data and Update README + generate_everything: + name: Full CI/CD Pipeline runs-on: ubuntu-latest container: image: ${{ vars.CONTAINER_REGISTRY }}/${{ env.IMAGE_NAME }}:latest + steps: - name: Checkout Repository uses: actions/checkout@v4 @@ -23,89 +24,67 @@ jobs: token: ${{ secrets.TOKEN }} - name: Pull latest changes - run: git pull --rebase origin main || echo "Nothing to rebase" - - - name: Generate and Update README - shell: bash run: | - set -euo pipefail + git pull --rebase origin main || echo "Nothing to rebase" - user_rows_file="/tmp/user_rows.txt" - avatar_rows_file="/tmp/avatar_rows.txt" + - name: Find Skin Repositories for all users + id: find_skins + shell: sh + run: | total_valid_entries=0 - - > "$user_rows_file" - > "$avatar_rows_file" + user_rows_file=$(mktemp) + avatar_rows_file=$(mktemp) page=1 - users_data_found=true - while "$users_data_found"; do + while :; do echo "Fetching users page $page..." >&2 - users_json=$(curl --fail --retry 3 --retry-delay 5 -sSL \ + users_json=$(curl --retry 3 --retry-delay 5 -sSL \ -H "Authorization: token ${{ secrets.TOKEN }}" \ "$GITEA_API/admin/users?limit=50&page=$page&_ts=$(date +%s)") - if [ "$(echo "$users_json" | jq 'length')" -eq 0 ]; then - users_data_found=false - break - fi + count=$(echo "$users_json" | jq 'length') + [ "$count" -eq 0 ] && break - mapfile -t user_logins < <(echo "$users_json" | jq -r '.[].login') - - for user_login in "${user_logins[@]}"; do + for i in $(seq 0 $((count - 1))); do + user_login=$(echo "$users_json" | jq -r ".[$i].login") echo "Processing user: $user_login" >&2 - repos_json=$(curl --fail --retry 3 --retry-delay 5 -sSL \ + repos_json=$(curl --retry 3 --retry-delay 5 -sSL \ -H "Authorization: token ${{ secrets.TOKEN }}" \ - "$GITEA_API/users/$user_login/repos?limit=50&page=1&_ts=$(date +%s)") + "$GITEA_API/users/$user_login/repos?_ts=$(date +%s)") - if ! echo "$repos_json" | jq -e '.[0]' >/dev/null; then - continue - fi + repos_type=$(echo "$repos_json" | jq -r 'type' 2>/dev/null || echo "not_array") + repos_count=$(echo "$repos_json" | jq 'length' 2>/dev/null || echo "0") + [ "$repos_type" != "array" ] || [ "$repos_count" -eq 0 ] && continue - mapfile -t repo_details < <(echo "$repos_json" | jq -r '.[] | .owner.login, .name, .html_url') + for j in $(seq 0 $((repos_count - 1))); do + owner=$(echo "$repos_json" | jq -r ".[$j].owner.login") + repo=$(echo "$repos_json" | jq -r ".[$j].name") + html_url=$(echo "$repos_json" | jq -r ".[$j].html_url") - for ((i=0; i<${#repo_details[@]}; i+=3)); do - owner="${repo_details[i]}" - repo="${repo_details[i+1]}" - html_url="${repo_details[i+2]}" - - echo " Checking repo: $repo" >&2 - - curl -sSL -w "%{http_code}" -o readme_content.json \ + echo " Checking repo: $repo" >&2 + http_code=$(curl --retry 3 --retry-delay 5 -s -w "%{http_code}" -o temp_readme.json \ -H "Authorization: token ${{ secrets.TOKEN }}" \ - "$GITEA_API/repos/$owner/$repo/contents/README.md?_ts=$(date +%s)" > http_code.txt + "$GITEA_API/repos/$owner/$repo/contents/README.md?_ts=$(date +%s)") - http_code=$(cat http_code.txt) - rm -f http_code.txt + [ "$http_code" != "200" ] && continue - if [ "$http_code" != "200" ]; then - echo "README not found for $owner/$repo (HTTP $http_code)" >&2 - continue - fi - - readme_content=$(jq -r '.content' readme_content.json | base64 -d || echo "") - rm -f readme_content.json - - if echo "$readme_content" | grep -qE "^---$" && \ - echo "$readme_content" | grep -qE "^gitea: none" && \ - echo "$readme_content" | grep -qE "^include_toc: true" && \ - echo "$readme_content" | grep -qE "^# Skins"; then - - osu_id=$(echo "$readme_content" | awk '/osuid:[ ]*[0-9]+/ { match($0, /[0-9]+/); print substr($0, RSTART, RLENGTH); exit }') + readme_json=$(cat temp_readme.json) + content=$(echo "$readme_json" | jq -r .content | base64 -d || echo "") + echo "$content" | grep -q "^---$" && \ + echo "$content" | grep -q "^gitea: none" && \ + echo "$content" | grep -q "^include_toc: true" && \ + echo "$content" | grep -q "^# Skins" + if [ $? -eq 0 ]; then + osu_id=$(echo "$content" | awk '/osuid:[ ]*[0-9]+/ { match($0, /[0-9]+/); print substr($0, RSTART, RLENGTH); exit }') if [ -n "$osu_id" ]; then - user_data=$(curl --fail --retry 3 --retry-delay 5 -s \ + user_data=$(curl --retry 3 --retry-delay 5 -s \ "https://osu.ppy.sh/api/get_user?k=${{ secrets.OSUAPIV1 }}&u=$osu_id&type=id&_ts=$(date +%s)") - if ! echo "$user_data" | jq -e '.[0]' >/dev/null; then - echo "No osu! data found for ID: $osu_id" >&2 - continue - fi - pp_rank=$(echo "$user_data" | jq -r '.[0].pp_rank // "9999999"') pp_country_rank=$(echo "$user_data" | jq -r '.[0].pp_country_rank // "-"') - username=$(echo "$user_data" | jq -r --arg owner "$owner" '.[0].username // $owner') + username=$(echo "$user_data" | jq -r '.[0].username // "'$owner'"') padded_rank=$(printf "%07d" "$pp_rank") printf "%s|%s%s%sProfileSkins\n" \ @@ -116,19 +95,25 @@ jobs: "$padded_rank" "$html_url" "$osu_id" "$timestamp" >> "$avatar_rows_file" total_valid_entries=$((total_valid_entries + 1)) - else - echo "osu! ID not found in README for $owner/$repo" >&2 fi - - else - echo "README for $owner/$repo does not match required format" >&2 fi done done + page=$((page + 1)) done - # Create README + echo "user_rows_file=$user_rows_file" >> "$GITHUB_OUTPUT" + echo "avatar_rows_file=$avatar_rows_file" >> "$GITHUB_OUTPUT" + echo "total_valid_entries=$total_valid_entries" >> "$GITHUB_OUTPUT" + + - name: Update README with user skins + run: | + user_rows_file="${{ steps.find_skins.outputs.user_rows_file }}" + avatar_rows_file="${{ steps.find_skins.outputs.avatar_rows_file }}" + README_PATH="README.md" # Assuming README.md is in the root of the repository + + # Use `cat >` for the initial content, no indentation inside the heredoc cat > "$README_PATH" <<-EOF # osu! Swiss Community Skin collection @@ -174,20 +159,24 @@ jobs:

EOF + rm -f "$user_rows_file" "$avatar_rows_file" + - name: Configure Git run: | git config user.email "arlind@sulej.ch" git config user.name "ci-bot" git config lfs.https://${{ vars.CONTAINER_REGISTRY }}/arlind/skins.git/info/lfs.locksverify true - - name: Commit and Push + - name: Add and Commit changes run: | git config advice.addIgnoredFile false - git add "$README_PATH" + git add README.md git commit -m "[ci skip] push back from pipeline" -q || echo "No changes to commit" + - name: Push changes and create tag + run: | if [ "${GITHUB_REF}" = "refs/heads/main" ]; then git push origin HEAD:main || echo "No changes to push" else git push origin HEAD:"${GITHUB_REF_NAME}" || echo "No changes to push" - + fi