|
| 1 | +name: SDK size checks |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + |
| 6 | +env: |
| 7 | + SDKS: "stream-chat-android-client stream-chat-android-offline stream-chat-android-ui-components stream-chat-android-compose" |
| 8 | + MAX_TOLERANCE: 500 |
| 9 | + FINE_TOLERANCE: 250 |
| 10 | + |
| 11 | +jobs: |
| 12 | + compare-sdk-sizes: |
| 13 | + runs-on: ubuntu-latest |
| 14 | + |
| 15 | + steps: |
| 16 | + - name: Checkout code |
| 17 | + uses: actions/checkout@v3 |
| 18 | + - uses: ./.github/actions/setup-java |
| 19 | + - uses: ./.github/actions/gradle-cache |
| 20 | + with: |
| 21 | + key-prefix: gradle-build |
| 22 | + |
| 23 | + - name: Get develop SDK sizes |
| 24 | + run: | |
| 25 | + # Reads the develop SDK sizes from JSON file |
| 26 | + # and define to a variable using a compact JSON format |
| 27 | + # so it can be exported for the next job step |
| 28 | + DEVELOP_SDK_SIZES=$(jq -c .develop stream-chat-android.json) |
| 29 | + echo "DEVELOP_SDK_SIZES=$DEVELOP_SDK_SIZES" >> $GITHUB_ENV |
| 30 | +
|
| 31 | + - name: Assemble SDKs |
| 32 | + run: | |
| 33 | + for sdk in $SDKS; do |
| 34 | + modules+=" :$sdk:assembleDebug" |
| 35 | + done |
| 36 | + ./gradlew $modules |
| 37 | +
|
| 38 | + - name: Calculate PR branch SDK sizes |
| 39 | + run: | |
| 40 | + echo '{}' > pr_sdk_sizes.json |
| 41 | +
|
| 42 | + # Calculate sizes from the .aar files and save them into a temporary JSON file |
| 43 | + # so it can be exported for the next job step |
| 44 | + for sdk in $SDKS; do |
| 45 | + size=$(du -k $sdk/build/outputs/aar/*.aar | awk '{print $1}') |
| 46 | + jq -c --arg sdk "$sdk" --arg size "$size" '. + {($sdk): ($size | tonumber)}' pr_sdk_sizes.json |
| 47 | + done |
| 48 | +
|
| 49 | + echo "PR_SDK_SIZES=$(cat pr_sdk_sizes.json)" >> $GITHUB_ENV |
| 50 | +
|
| 51 | + - name: Post comment on PR |
| 52 | + uses: actions/github-script@v6 |
| 53 | + with: |
| 54 | + script: | |
| 55 | + const maxTolerance = process.env.MAX_TOLERANCE |
| 56 | + const fineTolerance = process.env.FINE_TOLERANCE |
| 57 | + const developSdkSizes = JSON.parse(process.env.DEVELOP_SDK_SIZES); |
| 58 | + const prSdkSizes = JSON.parse(process.env.PR_SDK_SIZES); |
| 59 | + const commentHeader = '## SDK Size Comparison'; |
| 60 | +
|
| 61 | + // Prepare the comparison table |
| 62 | +
|
| 63 | + let commentBody = ` |
| 64 | + ${commentHeader} |
| 65 | +
|
| 66 | + | SDK | Before | After | Difference | Status | |
| 67 | + |-|-|-|-|-| |
| 68 | + `; |
| 69 | + |
| 70 | + Object.keys(prSdkSizes).forEach(sdk => { |
| 71 | + const developSize = developSdkSizes[sdk] || 0; |
| 72 | + const prSize = prSdkSizes[sdk]; |
| 73 | + const diff = prSize - developSize; |
| 74 | +
|
| 75 | + let status; |
| 76 | + if (diff < 0) { |
| 77 | + status = "🚀"; |
| 78 | + } else if (diff >= maxTolerance) { |
| 79 | + status = "🔴"; |
| 80 | + } else if (diff >= fineTolerance) { |
| 81 | + status = "🟡"; |
| 82 | + } else { |
| 83 | + status = "🟢"; |
| 84 | + } |
| 85 | +
|
| 86 | + commentBody += `| ${sdk} | ${developSize} KB | ${prSize} KB | ${diff} KB | ${status} |\n`; |
| 87 | + }); |
| 88 | +
|
| 89 | + // Post or update the PR comment |
| 90 | +
|
| 91 | + // Find existing comment |
| 92 | + const { data: comments } = await github.rest.issues.listComments({ |
| 93 | + owner: context.repo.owner, |
| 94 | + repo: context.repo.repo, |
| 95 | + issue_number: context.payload.pull_request.number, |
| 96 | + }); |
| 97 | +
|
| 98 | + const comment = comments.find(c => c.body.includes(commentHeader)); |
| 99 | +
|
| 100 | + if (comment) { |
| 101 | + // Update the existing comment |
| 102 | + await github.rest.issues.updateComment({ |
| 103 | + owner: context.repo.owner, |
| 104 | + repo: context.repo.repo, |
| 105 | + comment_id: comment.id, |
| 106 | + body: commentBody, |
| 107 | + }); |
| 108 | + } else { |
| 109 | + // Create a new comment |
| 110 | + await github.rest.issues.createComment({ |
| 111 | + owner: context.repo.owner, |
| 112 | + repo: context.repo.repo, |
| 113 | + issue_number: context.payload.pull_request.number, |
| 114 | + body: commentBody, |
| 115 | + }); |
| 116 | + } |
0 commit comments