mirror of
https://github.com/commons-app/apps-android-commons.git
synced 2025-10-26 04:13:53 +01:00
Comment in PR with generated builds. (#6226)
* revert prior attempt Signed-off-by: parneet-guraya <gurayaparneet@gmail.com> * trigger workflows_run event after uploading artifacts Signed-off-by: parneet-guraya <gurayaparneet@gmail.com> --------- Signed-off-by: parneet-guraya <gurayaparneet@gmail.com>
This commit is contained in:
parent
6b56075df8
commit
139a296bd3
3 changed files with 104 additions and 109 deletions
96
.github/workflows/android-ci-comment.yml
vendored
96
.github/workflows/android-ci-comment.yml
vendored
|
|
@ -1,96 +0,0 @@
|
|||
name: Android CI Comment
|
||||
|
||||
on:
|
||||
workflow_run:
|
||||
workflows: ["Android CI"]
|
||||
types: [completed]
|
||||
branches: [main]
|
||||
|
||||
permissions:
|
||||
issues: write
|
||||
|
||||
jobs:
|
||||
comment:
|
||||
name: Comment on PR with APK links
|
||||
runs-on: ubuntu-latest
|
||||
if: github.event.workflow_run.conclusion == 'success'
|
||||
steps:
|
||||
- name: Checkout base branch
|
||||
uses: actions/checkout@v4
|
||||
with:
|
||||
ref: ${{ github.event.workflow_run.head_branch }}
|
||||
- name: Download Run ID Artifact
|
||||
uses: actions/download-artifact@v4
|
||||
with:
|
||||
name: run-id
|
||||
run-id: ${{ github.event.workflow_run.id }}
|
||||
|
||||
- name: Read Run ID
|
||||
id: read-run-id
|
||||
run: echo "RUN_ID=$(cat run_id.txt)" >> $GITHUB_ENV
|
||||
|
||||
- name: Comment on PR with APK download links
|
||||
env:
|
||||
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
uses: actions/github-script@v6
|
||||
with:
|
||||
script: |
|
||||
try {
|
||||
const token = process.env.GH_TOKEN;
|
||||
if (!token) {
|
||||
throw new Error('GITHUB_TOKEN is not set.');
|
||||
}
|
||||
|
||||
const runId = "${{ env.RUN_ID }}";
|
||||
if (!runId) {
|
||||
throw new Error('Run ID not found.');
|
||||
}
|
||||
|
||||
// Get the PR number from the workflow_run event
|
||||
const prNumber = ${{ github.event.workflow_run.pull_requests[0].number }};
|
||||
if (!prNumber) {
|
||||
console.log('No PR number found in workflow_run event.');
|
||||
return;
|
||||
}
|
||||
|
||||
const { data: { artifacts } } = await github.rest.actions.listWorkflowRunArtifacts({
|
||||
owner: context.repo.owner,
|
||||
repo: context.repo.repo,
|
||||
run_id: runId
|
||||
});
|
||||
|
||||
if (!artifacts || artifacts.length === 0) {
|
||||
console.log('No artifacts found for this workflow run.');
|
||||
return;
|
||||
}
|
||||
|
||||
const betaArtifact = artifacts.find(artifact => artifact.name === "betaDebugAPK");
|
||||
const prodArtifact = artifacts.find(artifact => artifact.name === "prodDebugAPK");
|
||||
|
||||
if (!betaArtifact || !prodArtifact) {
|
||||
console.log('Could not find both Beta and Prod APK artifacts.');
|
||||
console.log('Available artifacts:', artifacts.map(a => a.name).join(', '));
|
||||
return;
|
||||
}
|
||||
|
||||
const betaDownloadUrl = `${context.serverUrl}/${context.repo.owner}/${context.repo.repo}/suites/${runId}/artifacts/${betaArtifact.id}`;
|
||||
const prodDownloadUrl = `${context.serverUrl}/${context.repo.owner}/${context.repo.repo}/suites/${runId}/artifacts/${prodArtifact.id}`;
|
||||
|
||||
const commentBody = `
|
||||
📱 **APK for pull request is ready to see the changes** 📱
|
||||
- [Download Beta APK](${betaDownloadUrl})
|
||||
- [Download Prod APK](${prodDownloadUrl})
|
||||
`;
|
||||
|
||||
await github.rest.issues.createComment({
|
||||
issue_number: prNumber,
|
||||
owner: context.repo.owner,
|
||||
repo: context.repo.repo,
|
||||
body: commentBody
|
||||
});
|
||||
|
||||
console.log('Successfully posted comment with APK download links');
|
||||
} catch (error) {
|
||||
console.error('Error in PR comment creation:', error);
|
||||
core.setFailed(`Workflow failed: ${error.message}`);
|
||||
}
|
||||
17
.github/workflows/android.yml
vendored
17
.github/workflows/android.yml
vendored
|
|
@ -2,10 +2,6 @@ name: Android CI
|
|||
|
||||
on: [push, pull_request, workflow_dispatch]
|
||||
|
||||
permissions:
|
||||
contents: read
|
||||
actions: read
|
||||
|
||||
concurrency:
|
||||
group: build-${{ github.event.pull_request.number || github.ref }}
|
||||
cancel-in-progress: true
|
||||
|
|
@ -107,13 +103,12 @@ jobs:
|
|||
name: prodDebugAPK
|
||||
path: app/build/outputs/apk/prod/debug/app-*.apk
|
||||
|
||||
- name: Store Workflow Run ID
|
||||
if: github.event_name == 'pull_request'
|
||||
run: echo "${{ github.run_id }}" > run_id.txt
|
||||
- name: Create and PR number artifact
|
||||
run: |
|
||||
echo "{\"pr_number\": ${{ github.event.pull_request.number || 'null' }}}" > pr_number.json
|
||||
|
||||
- name: Upload Run ID as Artifact
|
||||
if: github.event_name == 'pull_request'
|
||||
- name: Upload PR number artifact
|
||||
uses: actions/upload-artifact@v4
|
||||
with:
|
||||
name: run-id
|
||||
path: run_id.txt
|
||||
name: pr_number
|
||||
path: ./pr_number.json
|
||||
|
|
|
|||
96
.github/workflows/comment_artifacts_on_PR.yml
vendored
Normal file
96
.github/workflows/comment_artifacts_on_PR.yml
vendored
Normal file
|
|
@ -0,0 +1,96 @@
|
|||
name: Comment Artifacts on PR
|
||||
|
||||
on:
|
||||
workflow_run:
|
||||
workflows: [ "Android CI" ]
|
||||
types: [ completed ]
|
||||
|
||||
permissions:
|
||||
pull-requests: write
|
||||
contents: read
|
||||
|
||||
concurrency:
|
||||
group: comment-${{ github.event.workflow_run.id }}
|
||||
cancel-in-progress: true
|
||||
|
||||
jobs:
|
||||
comment:
|
||||
runs-on: ubuntu-latest
|
||||
if: ${{ github.event.workflow_run.conclusion == 'success' && github.event.workflow_run.event == 'pull_request' }}
|
||||
steps:
|
||||
- name: Download and process artifacts
|
||||
uses: actions/github-script@v7
|
||||
with:
|
||||
github-token: ${{ secrets.GITHUB_TOKEN }}
|
||||
script: |
|
||||
const fs = require('fs');
|
||||
const runId = context.payload.workflow_run.id;
|
||||
|
||||
const allArtifacts = await github.rest.actions.listWorkflowRunArtifacts({
|
||||
owner: context.repo.owner,
|
||||
repo: context.repo.repo,
|
||||
run_id: runId,
|
||||
});
|
||||
|
||||
const prNumberArtifact = allArtifacts.data.artifacts.find(artifact => artifact.name === "pr_number");
|
||||
if (!prNumberArtifact) {
|
||||
console.log("pr_number artifact not found.");
|
||||
return;
|
||||
}
|
||||
|
||||
const download = await github.rest.actions.downloadArtifact({
|
||||
owner: context.repo.owner,
|
||||
repo: context.repo.repo,
|
||||
artifact_id: prNumberArtifact.id,
|
||||
archive_format: 'zip',
|
||||
});
|
||||
|
||||
fs.writeFileSync(`${process.env.GITHUB_WORKSPACE}/pr_number.zip`, Buffer.from(download.data));
|
||||
const { execSync } = require('child_process');
|
||||
execSync('unzip -q pr_number.zip -d ./pr_number/');
|
||||
fs.unlinkSync('pr_number.zip');
|
||||
|
||||
const prData = JSON.parse(fs.readFileSync('./pr_number/pr_number.json', 'utf8'));
|
||||
const prNumber = prData.pr_number;
|
||||
|
||||
if (!prNumber || prNumber === 'null') {
|
||||
console.log("No valid PR number found in pr_number.json. Skipping.");
|
||||
return;
|
||||
}
|
||||
|
||||
const artifactsToLink = allArtifacts.data.artifacts.filter(artifact => artifact.name !== "pr_number");
|
||||
if (artifactsToLink.length === 0) {
|
||||
console.log("No artifacts to link found.");
|
||||
return;
|
||||
}
|
||||
|
||||
const comments = await github.rest.issues.listComments({
|
||||
owner: context.repo.owner,
|
||||
repo: context.repo.repo,
|
||||
issue_number: Number(prNumber),
|
||||
});
|
||||
|
||||
const oldComments = comments.data.filter(comment =>
|
||||
comment.body.startsWith("✅ Generated APK variants!")
|
||||
);
|
||||
for (const comment of oldComments) {
|
||||
await github.rest.issues.deleteComment({
|
||||
owner: context.repo.owner,
|
||||
repo: context.repo.repo,
|
||||
comment_id: comment.id,
|
||||
});
|
||||
console.log(`Deleted old comment ID: ${comment.id}`);
|
||||
};
|
||||
|
||||
const commentBody = `✅ Generated APK variants!\n` +
|
||||
artifactsToLink.map(artifact => {
|
||||
const artifactUrl = `https://github.com/${context.repo.owner}/${context.repo.repo}/actions/runs/${runId}/artifacts/${artifact.id}`;
|
||||
return `- 🤖 [Download ${artifact.name}](${artifactUrl})`;
|
||||
}).join('\n');
|
||||
|
||||
await github.rest.issues.createComment({
|
||||
owner: context.repo.owner,
|
||||
repo: context.repo.repo,
|
||||
issue_number: Number(prNumber),
|
||||
body: commentBody
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue