Skip to content

Commit 8dd1091

Browse files
Separate Workflow for Commenting on PR with APK Links (#6199)
* Update android.yml * Create android-ci-comment.yml * Update android.yml
1 parent 44f69fc commit 8dd1091

File tree

2 files changed

+93
-60
lines changed

2 files changed

+93
-60
lines changed
+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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+
}

.github/workflows/android.yml

+9-60
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ name: Android CI
33
on: [push, pull_request, workflow_dispatch]
44

55
permissions:
6-
pull-requests: write
76
contents: read
87
actions: read
98

@@ -107,64 +106,14 @@ jobs:
107106
with:
108107
name: prodDebugAPK
109108
path: app/build/outputs/apk/prod/debug/app-*.apk
110-
111-
- name: Comment on PR with APK download links
109+
110+
- name: Store Workflow Run ID
112111
if: github.event_name == 'pull_request'
113-
uses: actions/github-script@v6
112+
run: echo "${{ github.run_id }}" > run_id.txt
113+
114+
- name: Upload Run ID as Artifact
115+
if: github.event_name == 'pull_request'
116+
uses: actions/upload-artifact@v4
114117
with:
115-
github-token: ${{secrets.GITHUB_TOKEN}}
116-
script: |
117-
try {
118-
const token = process.env.GITHUB_TOKEN;
119-
if (!token) {
120-
throw new Error('GITHUB_TOKEN is not set. Please check workflow permissions.');
121-
}
122-
123-
124-
const { data: { artifacts } } = await github.rest.actions.listWorkflowRunArtifacts({
125-
owner: context.repo.owner,
126-
repo: context.repo.repo,
127-
run_id: context.runId
128-
});
129-
130-
if (!artifacts || artifacts.length === 0) {
131-
console.log('No artifacts found for this workflow run.');
132-
return;
133-
}
134-
135-
const betaArtifact = artifacts.find(artifact => artifact.name === "betaDebugAPK");
136-
const prodArtifact = artifacts.find(artifact => artifact.name === "prodDebugAPK");
137-
138-
if (!betaArtifact || !prodArtifact) {
139-
console.log('Could not find both Beta and Prod APK artifacts.');
140-
console.log('Available artifacts:', artifacts.map(a => a.name).join(', '));
141-
return;
142-
}
143-
144-
const betaDownloadUrl = `${context.serverUrl}/${context.repo.owner}/${context.repo.repo}/suites/${context.runId}/artifacts/${betaArtifact.id}`;
145-
const prodDownloadUrl = `${context.serverUrl}/${context.repo.owner}/${context.repo.repo}/suites/${context.runId}/artifacts/${prodArtifact.id}`;
146-
147-
const commentBody = `
148-
📱 **APK for pull request is ready to see the changes** 📱
149-
- [Download Beta APK](${betaDownloadUrl})
150-
- [Download Prod APK](${prodDownloadUrl})
151-
`;
152-
153-
await github.rest.issues.createComment({
154-
issue_number: context.issue.number,
155-
owner: context.repo.owner,
156-
repo: context.repo.repo,
157-
body: commentBody
158-
});
159-
160-
console.log('Successfully posted comment with APK download links');
161-
} catch (error) {
162-
console.error('Error in PR comment creation:', error);
163-
if (error.message.includes('GITHUB_TOKEN')) {
164-
core.setFailed('Missing or invalid GITHUB_TOKEN. Please check repository secrets configuration.');
165-
} else if (error.status === 403) {
166-
core.setFailed('Permission denied. Please check workflow permissions in repository settings.');
167-
} else {
168-
core.setFailed(`Workflow failed: ${error.message}`);
169-
}
170-
}
118+
name: run-id
119+
path: run_id.txt

0 commit comments

Comments
 (0)