|
| 1 | +name: Android CI Comment |
| 2 | + |
| 3 | +on: [pull_request_target] |
| 4 | + |
| 5 | +permissions: |
| 6 | + issues: write |
| 7 | + |
| 8 | +jobs: |
| 9 | + comment: |
| 10 | + name: Comment on PR with APK links |
| 11 | + runs-on: ubuntu-latest |
| 12 | + steps: |
| 13 | + - name: Checkout base branch |
| 14 | + uses: actions/checkout@v3 |
| 15 | + with: |
| 16 | + ref: ${{ github.base_ref }} |
| 17 | + |
| 18 | + - name: Download Run ID Artifact |
| 19 | + uses: actions/download-artifact@v4 |
| 20 | + with: |
| 21 | + name: run-id |
| 22 | + |
| 23 | + - name: Read Run ID |
| 24 | + id: read-run-id |
| 25 | + run: echo "RUN_ID=$(cat run_id.txt)" >> $GITHUB_ENV |
| 26 | + |
| 27 | + - name: Comment on PR with APK download links |
| 28 | + env: |
| 29 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 30 | + uses: actions/github-script@v6 |
| 31 | + with: |
| 32 | + script: | |
| 33 | + try { |
| 34 | + const token = process.env.GH_TOKEN; |
| 35 | + if (!token) { |
| 36 | + throw new Error('GITHUB_TOKEN is not set.'); |
| 37 | + } |
| 38 | +
|
| 39 | + const runId = "${{ env.RUN_ID }}"; |
| 40 | + if (!runId) { |
| 41 | + throw new Error('Run ID not found.'); |
| 42 | + } |
| 43 | +
|
| 44 | + const { data: { artifacts } } = await github.rest.actions.listWorkflowRunArtifacts({ |
| 45 | + owner: context.repo.owner, |
| 46 | + repo: context.repo.repo, |
| 47 | + run_id: runId |
| 48 | + }); |
| 49 | +
|
| 50 | + if (!artifacts || artifacts.length === 0) { |
| 51 | + console.log('No artifacts found for this workflow run.'); |
| 52 | + return; |
| 53 | + } |
| 54 | +
|
| 55 | + const betaArtifact = artifacts.find(artifact => artifact.name === "betaDebugAPK"); |
| 56 | + const prodArtifact = artifacts.find(artifact => artifact.name === "prodDebugAPK"); |
| 57 | +
|
| 58 | + if (!betaArtifact || !prodArtifact) { |
| 59 | + console.log('Could not find both Beta and Prod APK artifacts.'); |
| 60 | + console.log('Available artifacts:', artifacts.map(a => a.name).join(', ')); |
| 61 | + return; |
| 62 | + } |
| 63 | +
|
| 64 | + const betaDownloadUrl = `${context.serverUrl}/${context.repo.owner}/${context.repo.repo}/suites/${runId}/artifacts/${betaArtifact.id}`; |
| 65 | + const prodDownloadUrl = `${context.serverUrl}/${context.repo.owner}/${context.repo.repo}/suites/${runId}/artifacts/${prodArtifact.id}`; |
| 66 | +
|
| 67 | + const commentBody = ` |
| 68 | + 📱 **APK for pull request is ready to see the changes** 📱 |
| 69 | + - [Download Beta APK](${betaDownloadUrl}) |
| 70 | + - [Download Prod APK](${prodDownloadUrl}) |
| 71 | + `; |
| 72 | +
|
| 73 | + await github.rest.issues.createComment({ |
| 74 | + issue_number: context.issue.number, |
| 75 | + owner: context.repo.owner, |
| 76 | + repo: context.repo.repo, |
| 77 | + body: commentBody |
| 78 | + }); |
| 79 | +
|
| 80 | + console.log('Successfully posted comment with APK download links'); |
| 81 | + } catch (error) { |
| 82 | + console.error('Error in PR comment creation:', error); |
| 83 | + core.setFailed(`Workflow failed: ${error.message}`); |
| 84 | + } |
0 commit comments