From 1383ef3249e6ee07471b68a23367110060b97db8 Mon Sep 17 00:00:00 2001 From: Sheby <83452219+Shebyyy@users.noreply.github.com> Date: Wed, 6 Nov 2024 21:19:29 +0530 Subject: [PATCH] Added Dev Info --- .github/workflows/publish_alpha.yml | 345 +++++++++++++++++++--------- 1 file changed, 237 insertions(+), 108 deletions(-) diff --git a/.github/workflows/publish_alpha.yml b/.github/workflows/publish_alpha.yml index fb08a401..4b58bf87 100644 --- a/.github/workflows/publish_alpha.yml +++ b/.github/workflows/publish_alpha.yml @@ -21,23 +21,13 @@ jobs: runs-on: ubuntu-latest env: COMMIT_LOG: "" - + steps: - name: Checkout Repository uses: actions/checkout@v4 with: fetch-depth: 0 - - name: List Workflow Runs - id: list_runs - run: | - WORKFLOW_ID=$(gh api repos/${{ github.repository }}/actions/workflows/publish_alpha.yml -q .id) - LAST_RUN_ID=$(gh api repos/${{ github.repository }}/actions/workflows/$WORKFLOW_ID/runs \ - -q '.workflow_runs[1].id') - echo "last_run_id=$LAST_RUN_ID" >> $GITHUB_OUTPUT - env: - GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} - - name: Download last SHA artifact uses: dawidd6/action-download-artifact@v3 with: @@ -46,41 +36,47 @@ jobs: path: . continue-on-error: true - - name: Handle Missing SHA File - run: | - if [ ! -f last_sha.txt ] || [ ! -s last_sha.txt ]; then - echo "Creating initial SHA file with 10 commits ago" - git rev-list HEAD~10 -n 1 > last_sha.txt - fi - - - name: Get New Commits Since Last Successful Run - id: get_commits + - name: Get Commits Since Last Run run: | + if [ -f last_sha.txt ]; then LAST_SHA=$(cat last_sha.txt) - if ! git rev-parse --quiet --verify "$LAST_SHA^{commit}" >/dev/null; then - echo "Invalid SHA found, using last 10 commits" - LAST_SHA=$(git rev-list HEAD~10 -n 1) - fi - echo "Using SHA: $LAST_SHA" - COMMIT_COUNT=$(git rev-list --count $LAST_SHA..HEAD) - if [ "$COMMIT_COUNT" -gt 10 ]; then - echo "Too many commits, limiting to last 10" - LAST_SHA=$(git rev-list HEAD~10 -n 1) + else + LAST_SHA=$(git rev-list --max-parents=0 HEAD) fi - COMMIT_LOGS=$(git log $LAST_SHA..HEAD --pretty=format:"● %s ~%an [֍](https://github.com/${{ github.repository }}/commit/%H)") - FORMATTED_COMMIT_LOGS=$(echo "$COMMIT_LOGS" | sed -E 's/#([0-9]+)/[#\1](https:\/\/github.com\/MrBoomDeveloper\/Awery\/pull\/\1)/g') + + echo "Commits since $LAST_SHA:" + COMMIT_LOGS=$(git log "$LAST_SHA"..HEAD --pretty=format:"● %s ~%an [֍](https://github.com/MrBoomDeveloper/Awery/commit/%H)" --max-count=10) + COMMIT_LOGS=$(echo "$COMMIT_LOGS" | sed -E 's/#([0-9]+)/[#\1](https:\/\/github.com\/MrBoomDeveloper\/Awery\/pull\/\1)/g') + COMMIT_LOGS="${COMMIT_LOGS//'%'/'%25'}" + COMMIT_LOGS="${COMMIT_LOGS//$'\n'/'%0A'}" + COMMIT_LOGS="${COMMIT_LOGS//$'\r'/'%0D'}" + + echo "COMMIT_LOG=${COMMIT_LOGS}" >> $GITHUB_ENV echo "$COMMIT_LOGS" > commit_log.txt - echo "$FORMATTED_COMMIT_LOGS" > discord_commit_log.txt - echo "COMMIT_LOG<> $GITHUB_ENV - echo "$FORMATTED_COMMIT_LOGS" >> $GITHUB_ENV - echo "EOF" >> $GITHUB_ENV - - name: Set up JDK 17 + BRANCH=${{ github.ref }} + BRANCH=${BRANCH#refs/heads/} + echo "BRANCH=${BRANCH}" >> $GITHUB_ENV + continue-on-error: true + + - name: Save Current SHA for Next Run + run: echo ${{ github.sha }} > last_sha.txt + + - name: Set variables + run: | + VER=$(grep -E -o "versionName \".*\"" app/build.gradle | sed -e 's/versionName //g' | tr -d '"') + SHA=${{ github.sha }} + VERSION="$VER+${SHA:0:7}" + echo "Version $VERSION" + echo "VERSION=$VERSION" >> $GITHUB_ENV + + - name: Setup JDK 17 + if: ${{ env.SKIP_BUILD != 'true' }} uses: actions/setup-java@v4 with: - java-version: '17' - distribution: 'adopt' - cache: 'gradle' + distribution: 'temurin' + java-version: 17 + cache: gradle - name: Grant execute permission for gradlew run: chmod +x gradlew @@ -100,91 +96,224 @@ jobs: keyPassword: ${{ secrets.ANDROID_KEY_PASSWORD }} env: BUILD_TOOLS_VERSION: "34.0.0" - - - name: Prepare Release Directory - if: success() - run: | - RELEASE_DIR="release_artifacts" - mkdir -p "$RELEASE_DIR" - SOURCE_APK="app/build/outputs/apk/alpha/release/app-alpha-release-signed.apk" - TARGET_APK="$RELEASE_DIR/awery-alpha.apk" - cp "$SOURCE_APK" "$TARGET_APK" - echo "APK_PATH=$TARGET_APK" >> $GITHUB_ENV - echo "RELEASE_DIR=$RELEASE_DIR" >> $GITHUB_ENV - - - name: Upload to Artifacts - if: success() + + - name: Upload Alpha APK uses: actions/upload-artifact@v4 with: name: "Alpha APK" - path: ${{ env.RELEASE_DIR }} - - - name: Send to Telegram - if: success() - run: | - COMMIT_LOG=$(cat commit_log.txt) - if [ ${#COMMIT_LOG} -gt 1024 ]; then - COMMIT_LOG="${COMMIT_LOG:0:900}... (truncated)" - fi - TRUNCATED_COMMIT_LOG="$COMMIT_LOG" - telegram_commit_messages=$(echo "$TRUNCATED_COMMIT_LOG" | sed 's/%0A/\n/g' | sed -E 's/● (.*) ~(.*) \[֍\]\((.*)\)/● \1 ~\2 ֍<\/a>/') - telegram_commit_messages=$(echo "$telegram_commit_messages" | sed -E 's|Merge pull request #([0-9]+) from ([^ ]+)|Merge pull request #\1 from \2|g') - formatted_messages="
${telegram_commit_messages}
" - curl -v -X POST \ - "https://api.telegram.org/bot${{ secrets.TELEGRAM_BOT_TOKEN }}/sendDocument" \ - -F "chat_id=-1002206819738" \ - -F "message_thread_id=174" \ - -F "document=@$APK_PATH" \ - -F "caption=New Alpha-Build dropped 🔥 - Commits:$formatted_messages" \ - -F "parse_mode=HTML" - - - name: Send Alpha APK to Discord - if: success() + retention-days: 90 + path: "app/build/outputs/apk/alpha/release/app-alpha-release-signed.apk" + + - name: Upload APK to Discord and Telegram + shell: bash run: | - COMMIT_LOG=$(cat discord_commit_log.txt) - if [ ${#COMMIT_LOG} -gt 2000 ]; then - TRUNCATED_LOG=$(echo "$COMMIT_LOG" | head -c 1800) - LAST_COMPLETE=$(echo "$TRUNCATED_LOG" | grep -o "^●.*$" | tail -n 1) - if [ ! -z "$LAST_COMPLETE" ]; then - LAST_POS=$(echo "$TRUNCATED_LOG" | grep -b -o "^$LAST_COMPLETE" | cut -d: -f1) - COMMIT_LOG="${COMMIT_LOG:0:$LAST_POS}$LAST_COMPLETE" - else - COMMIT_LOG="${COMMIT_LOG:0:1800}... (truncated)" + fetch_user_details() { + local login=$1 + user_details=$(curl -s "https://api.github.com/users/$login") + name=$(echo "$user_details" | jq -r '.name // .login') + login=$(echo "$user_details" | jq -r '.login') + avatar_url=$(echo "$user_details" | jq -r '.avatar_url') + echo "$name|$login|$avatar_url" + } + + declare -A additional_info + additional_info["Sheby"]="\n Discord: <@612532963938271232>\n AniList: [Sheby]()" + additional_info["itsmechinmoy"]="\n Discord: <@523539866311720963>\n AniList: [itsmechinmoy]()" + additional_info["MrBoomDev"]="\n Discord: <@1034891767822176357>" + + declare -A contributor_colors + default_color="6121629" + contributor_colors["Sheby"]="#ff9b46" + contributor_colors["itsmechinmoy"]="#5d689d" + contributor_colors["MrBoomDev"]="#ff7e95" + + hex_to_decimal() { printf '%d' "0x${1#"#"}"; } + + declare -A recent_commit_counts + echo "Debug: Processing COMMIT_LOG:" + echo "$COMMIT_LOG" + while read -r count name; do + recent_commit_counts["$name"]=$count + echo "Debug: Commit count for $name: $count" + done < <(echo "$COMMIT_LOG" | sed 's/%0A/\n/g' | grep -oP '(?<=~)[^[]*' | sort | uniq -c | sort -rn) + + echo "Debug: Fetching contributors from GitHub" + contributors=$(curl -s "https://api.github.com/repos/${{ github.repository }}/contributors") + echo "Debug: Contributors response:" + echo "$contributors" + + sorted_contributors=$(for login in $(echo "$contributors" | jq -r '.[].login'); do + user_info=$(fetch_user_details "$login") + name=$(echo "$user_info" | cut -d'|' -f1) + count=${recent_commit_counts["$name"]:-0} + echo "$count|$login" + done | sort -rn | cut -d'|' -f2) + + developers="" + committers_count=0 + max_commits=0 + top_contributor="" + top_contributor_count=0 + top_contributor_avatar="" + embed_color=$default_color + + while read -r login; do + user_info=$(fetch_user_details "$login") + name=$(echo "$user_info" | cut -d'|' -f1) + login=$(echo "$user_info" | cut -d'|' -f2) + avatar_url=$(echo "$user_info" | cut -d'|' -f3) + + commit_count=${recent_commit_counts["$name"]:-0} + if [ $commit_count -gt 0 ]; then + if [ $commit_count -gt $max_commits ]; then + max_commits=$commit_count + top_contributor="$login" + top_contributor_count=1 + top_contributor_avatar="$avatar_url" + embed_color=$(hex_to_decimal "${contributor_colors[$name]:-$default_color}") + elif [ $commit_count -eq $max_commits ]; then + top_contributors+=("$login") + top_contributor_count=$((top_contributor_count + 1)) + embed_color=$default_color + fi + + branch_commit_count=$(git log --author="$login" --author="$name" --oneline | awk '!seen[$0]++' | wc -l) + + echo "Debug: recent_commit_counts contents:" + for key in "${!recent_commit_counts[@]}"; do + echo "$key: ${recent_commit_counts[$key]}" + done + + extra_info="${additional_info[$name]}" + if [ -n "$extra_info" ]; then + extra_info=$(echo "$extra_info" | sed 's/\\n/\n- /g') + fi + + developer_entry="◗ **${name}** ${extra_info} + - Github: [${login}](https://github.com/${login}) + - Commits: ${branch_commit_count}" + + if [ -n "$developers" ]; then + developers="${developers} + ${developer_entry}" + else + developers="${developer_entry}" + fi + committers_count=$((committers_count + 1)) fi + done <<< "$sorted_contributors" + + if [ $top_contributor_count -eq 1 ]; then + thumbnail_url="$top_contributor_avatar" + else + thumbnail_url="https://i.imgur.com/sP4FR70.png" + embed_color=$default_color + fi + + max_length=1000 + commit_messages=$(echo "$COMMIT_LOG" | sed 's/%0A/\n/g; s/^/\n/') + if [ ${#developers} -gt $max_length ]; then + developers="${developers:0:$max_length}... (truncated)" fi - ESCAPED_COMMIT_LOG=$(echo "$COMMIT_LOG" | sed ':a;N;$!ba;s/\n/\\n/g' | sed 's/"/\\"/g') - payload="{ - \"content\": \"<@&1208839371575529572>\", - \"embeds\": [{ - \"title\": \"New Awery Alpha Update!\", - \"description\": \"Commits:\\n${ESCAPED_COMMIT_LOG}\", - \"color\": 5814783 - }] - }" + if [ ${#commit_messages} -gt $max_length ]; then + commit_messages="${commit_messages:0:$max_length}... (truncated)" + fi + + discord_data=$(jq -nc \ + --arg field_value "$commit_messages" \ + --arg author_value "$developers" \ + --arg footer_text "Version $VERSION" \ + --arg timestamp "$(date -u +%Y-%m-%dT%H:%M:%S.000Z)" \ + --arg thumbnail_url "$thumbnail_url" \ + --argjson embed_color "$embed_color" \ + '{ + "content": "<@&1208839371575529572>", + "embeds": [ + { + "title": "New Alpha-Build dropped", + "color": $embed_color, + "fields": [ + { + "name": "Commits:", + "value": $field_value, + "inline": true + }, + { + "name": "Developers:", + "value": $author_value, + "inline": false + } + ], + "footer": { + "text": $footer_text + }, + "timestamp": $timestamp, + "thumbnail": { + "url": $thumbnail_url + } + } + ], + "attachments": [] + }') + + echo "Debug: Final Discord payload:" + echo "$discord_data" + curl -H "Content-Type: application/json" \ - -d "$payload" \ - ${{ secrets.DISCORD_WEBHOOK_URL }} - curl -H "Content-Type: multipart/form-data" \ - -F "file1=@$APK_PATH" \ - ${{ secrets.DISCORD_WEBHOOK_URL }} + -d "$discord_data" \ + ${{ secrets.DISCORD_WEBHOOK_URL }} - - name: Save Current SHA for Next Run - if: success() - run: | - echo ${{ github.sha }} > last_sha.txt - cat last_sha.txt + if [ "$SKIP_BUILD" != "true" ]; then + curl -F "payload_json=${contentbody}" \ + -F "Awery_debug=@app/build/outputs/apk/alpha/release/app-alpha-release-signed.apk" \ + ${{ secrets.DISCORD_WEBHOOK_URL }} + else + echo "Skipping APK upload to Discord due to SKIP_BUILD being set to true" + fi + + telegram_commit_messages=$(echo "$COMMIT_LOG" | sed 's/%0A/\n/g' | while read -r line; do + message=$(echo "$line" | sed -E 's/● (.*) ~(.*) \[֍\]\((.*)\)/● \1 ~\2 ֍<\/a>/') + message=$(echo "$message" | sed -E 's/\[#([0-9]+)\]\((https:\/\/github\.com\/[^)]+)\)/#\1<\/a>/g') + echo "$message" + done) + + telegram_commit_messages="
${telegram_commit_messages}
" + echo "$developers" > dev_info.txt + chmod +x workflowscripts/tel_parser.sed + ./workflowscripts/tel_parser.sed dev_info.txt >> output.txt + dev_info_tel=$(< output.txt) + + telegram_dev_info="
${dev_info_tel}
" + echo "$telegram_dev_info" + + if [ "$SKIP_BUILD" != "true" ]; then + response=$(curl -sS -f -X POST \ + "https://api.telegram.org/bot${{ secrets.TELEGRAM_BOT_TOKEN }}/sendDocument" \ + -F "chat_id=-1002206819738" \ + -F "message_thread_id=174" \ + -F "document=@app/build/outputs/apk/alpha/release/app-alpha-release-signed.apk" \ + -F "caption=New Alpha-Build dropped 🔥 + Commits: + ${telegram_commit_messages} + Dev: + ${telegram_dev_info} + version: ${VERSION}" \ + -F "parse_mode=HTML") + else + echo "Skipping because skip build set to true" + fi + + env: + COMMIT_LOG: ${{ env.COMMIT_LOG }} + VERSION: ${{ env.VERSION }} - name: Upload Current SHA as Artifact - if: success() uses: actions/upload-artifact@v4 with: name: last-sha path: last_sha.txt - retention-days: 90 - name: Upload Commit log as Artifact - if: success() uses: actions/upload-artifact@v4 with: name: commit-log