mirror of
https://github.com/LostArtefacts/TRX.git
synced 2025-04-28 12:47:58 +03:00
build: improve build link comment job
Taken from ddev/ddev.
This commit is contained in:
parent
85327e6164
commit
e5d445022d
1 changed files with 121 additions and 32 deletions
153
.github/workflows/comment_build.yml
vendored
153
.github/workflows/comment_build.yml
vendored
|
@ -3,62 +3,151 @@ on:
|
|||
workflow_run:
|
||||
workflows: ['Create a test build']
|
||||
types: [completed]
|
||||
|
||||
permissions:
|
||||
actions: write
|
||||
contents: write
|
||||
pull-requests: write
|
||||
jobs:
|
||||
pr_comment:
|
||||
if: github.event.workflow_run.event == 'pull_request' && github.event.workflow_run.conclusion == 'success'
|
||||
runs-on: ubuntu-latest
|
||||
permissions:
|
||||
pull-requests: write
|
||||
issues: write
|
||||
steps:
|
||||
- uses: actions/github-script@v6
|
||||
- uses: actions/github-script@v7
|
||||
with:
|
||||
# This snippet is public-domain, taken from
|
||||
# This snippet is public-domain, combined from
|
||||
# https://github.com/oprypin/nightly.link/blob/master/.github/workflows/pr-comment.yml
|
||||
# https://github.com/AKSW/submission.d2r2.aksw.org/blob/main/.github/workflows/pr-comment.yml
|
||||
script: |
|
||||
async function upsertComment(owner, repo, issue_number, purpose, body) {
|
||||
const {data: comments} = await github.rest.issues.listComments(
|
||||
{owner, repo, issue_number});
|
||||
// Function Definitions
|
||||
|
||||
/**
|
||||
* Fetch PR details for a given commit SHA.
|
||||
* @returns {Object} PR details containing prNumber, prRef, prRepoId.
|
||||
* @throws {Error} If no matching PR is found.
|
||||
*/
|
||||
async function fetchPRDetails() {
|
||||
const iterator = github.paginate.iterator(github.rest.pulls.list, {
|
||||
owner: context.repo.owner,
|
||||
repo: context.repo.repo,
|
||||
});
|
||||
for await (const { data } of iterator) {
|
||||
for (const pull of data) {
|
||||
if (pull.head.sha === '${{github.event.workflow_run.head_sha}}') {
|
||||
return {
|
||||
prNumber: pull.number,
|
||||
prRef: pull.head.ref,
|
||||
prRepoId: pull.head.repo.id
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
throw new Error("No matching PR found for the commit SHA");
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch all artifacts for a given workflow run.
|
||||
* @returns {Object} All artifacts data.
|
||||
* @throws {Error} If no artifacts are found.
|
||||
*/
|
||||
async function fetchAllArtifacts() {
|
||||
const artifactsResponse = await github.rest.actions.listWorkflowRunArtifacts({
|
||||
owner: context.repo.owner,
|
||||
repo: context.repo.repo,
|
||||
run_id: context.payload.workflow_run.id,
|
||||
});
|
||||
if (!(artifactsResponse.data && artifactsResponse.data.artifacts && artifactsResponse.data.artifacts.length)) {
|
||||
throw new Error("No artifacts found for the workflow run");
|
||||
}
|
||||
return artifactsResponse.data.artifacts;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create or update a comment on the PR.
|
||||
* @param {number} prNumber - The PR number.
|
||||
* @param {string} purpose - The purpose of the comment.
|
||||
* @param {string} body - The comment body.
|
||||
* @throws {Error} If the comment creation or update fails.
|
||||
*/
|
||||
async function upsertComment(prNumber, purpose, body) {
|
||||
const { data: comments } = await github.rest.issues.listComments({
|
||||
owner: context.repo.owner,
|
||||
repo: context.repo.repo,
|
||||
issue_number: prNumber,
|
||||
});
|
||||
const marker = `<!-- bot: ${purpose} -->`;
|
||||
body = marker + "\n" + body;
|
||||
|
||||
const existing = comments.filter((c) => c.body.includes(marker));
|
||||
const existing = comments.filter(c => c.body.includes(marker));
|
||||
if (existing.length > 0) {
|
||||
const last = existing[existing.length - 1];
|
||||
core.info(`Updating comment ${last.id}`);
|
||||
await github.rest.issues.updateComment({
|
||||
owner, repo,
|
||||
body,
|
||||
owner: context.repo.owner,
|
||||
repo: context.repo.repo,
|
||||
body: body,
|
||||
comment_id: last.id,
|
||||
});
|
||||
} else {
|
||||
core.info(`Creating a comment in issue / PR #${issue_number}`);
|
||||
await github.rest.issues.createComment({issue_number, body, owner, repo});
|
||||
core.info(`Creating a comment in PR #${prNumber}`);
|
||||
await github.rest.issues.createComment({
|
||||
owner: context.repo.owner,
|
||||
repo: context.repo.repo,
|
||||
body: body,
|
||||
issue_number: prNumber,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
const {owner, repo} = context.repo;
|
||||
const run_id = ${{github.event.workflow_run.id}};
|
||||
|
||||
const pull_requests = ${{ toJSON(github.event.workflow_run.pull_requests) }};
|
||||
if (!pull_requests.length) {
|
||||
return core.error("This workflow doesn't match any pull requests!");
|
||||
/**
|
||||
* Handle and log errors with detailed context and exit the process.
|
||||
* @param {Error} error - The error object.
|
||||
* @param {string} description - Description of the context where the error occurred.
|
||||
*/
|
||||
function handleError(error, description, prNumber) {
|
||||
core.error(`Failed to ${description}`);
|
||||
core.error(`Message: ${error.message}`);
|
||||
core.error(`Stack Trace: ${error.stack || 'No stack trace available'}`);
|
||||
if (prNumber) {
|
||||
core.error(`PR Number: ${prNumber}`);
|
||||
}
|
||||
core.error(`PRs: https://api.github.com/repos/${context.repo.owner}/${context.repo.repo}/pulls`);
|
||||
core.error(`SHA: ${{github.event.workflow_run.head_sha}}`);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
const artifacts = await github.paginate(
|
||||
github.rest.actions.listWorkflowRunArtifacts, {owner, repo, run_id});
|
||||
if (!artifacts.length) {
|
||||
return core.error(`No artifacts found`);
|
||||
}
|
||||
let body = `Download the built assets for this pull request:\n`;
|
||||
for (const art of artifacts) {
|
||||
body += `\n* [${art.name}.zip](https://nightly.link/${owner}/${repo}/actions/artifacts/${art.id}.zip)`;
|
||||
// Main Code Execution
|
||||
|
||||
let prNumber, prRef, prRepoId;
|
||||
|
||||
// Fetch PR details
|
||||
try {
|
||||
({ prNumber, prRef, prRepoId } = await fetchPRDetails());
|
||||
core.info(`Found PR: #${prNumber}, Ref: ${prRef}, Repo ID: ${prRepoId}`);
|
||||
} catch (error) {
|
||||
handleError(error, 'fetch PR details', undefined);
|
||||
}
|
||||
|
||||
core.info("Review thread message body:", body);
|
||||
|
||||
for (const pr of pull_requests) {
|
||||
await upsertComment(owner, repo, pr.number,
|
||||
"nightly-link", body);
|
||||
// Fetch all artifacts
|
||||
let allArtifacts;
|
||||
try {
|
||||
allArtifacts = await fetchAllArtifacts();
|
||||
core.info(`Artifacts fetched successfully`);
|
||||
} catch (error) {
|
||||
handleError(error, 'fetch artifacts', prNumber);
|
||||
}
|
||||
|
||||
// Construct the comment body
|
||||
let body = allArtifacts.reduce((acc, item) => {
|
||||
if (item.name === "assets") return acc;
|
||||
acc += `\n* [${item.name}.zip](https://nightly.link/${context.repo.owner}/${context.repo.repo}/actions/artifacts/${item.id}.zip)`;
|
||||
return acc;
|
||||
}, 'Download the built assets for this pull request:\n');
|
||||
|
||||
// Upsert the comment on the PR
|
||||
try {
|
||||
await upsertComment(prNumber, "nightly-link", body);
|
||||
core.info("Comment created/updated successfully");
|
||||
} catch (error) {
|
||||
handleError(error, 'create/update comment', prNumber);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue