Files
Draupnir/.github/scripts/pr-ghcr-image-comment.js
T
Catalan Lover 5b3da3ee07
Docker Hub - Develop / docker-latest (push) Has been skipped
GHCR - Development Branches / ghcr-publish (push) Failing after 38s
Tests / Build & Lint (push) Failing after 2m30s
Tests / Unit tests (push) Successful in 3m11s
Tests / Integration tests (push) Failing after 13s
Tests / Application Service Integration tests (push) Failing after 17s
Add Quick Access to images comment to PRs (#1133)
* Add Quick Access to images comment to PRs

* Refactor Image comment workflow to use shared script pattern.

Also refactors away dead crud.

* Refine comment output and normalise names to prevent bugs.

* Fix PR comment writing not working.

* Please the linter

* Remove Sha Tag due to it going stale if any branch movement happens.
2026-05-19 15:35:54 +02:00

87 lines
2.9 KiB
JavaScript

// SPDX-FileCopyrightText: 2026 Catalan Lover <catalanlover@protonmail.com>
//
// SPDX-License-Identifier: Apache-2.0
/**
* @param {Object} args
* @param {import('@actions/github').getOctokit} args.github
* @param {import('@actions/github').context} args.context
* @param {import('@actions/core')} args.core
*/
module.exports = async ({ github, context, core }) => {
const pullRequest = context.payload.pull_request;
if (!pullRequest) {
core.notice("No pull_request payload found; skipping.");
return;
}
const marker = "<!-- ghcr-pr-image-link -->";
const repo = context.repo.repo;
const ownerContext = context.repo.owner;
const prNumber = pullRequest.number;
// prOwner is a bit confusingly named it could be argued. Its Head Branch Owner effectively.
// It is used to determine the GHCR namespace for the PR build.
// In most cases this will be the same as the repo owner,
// but in fork scenarios it will be different and we want to ensure the GHCR namespace is correct,
// to keep this workflow functional for its intended purpose of providing image refs for PRs independently of origin.
const prOwner = pullRequest.head?.repo?.owner?.login || ownerContext;
const branch = pullRequest.head?.ref || "";
const sha = pullRequest.head?.sha || context.sha;
const shortSha = sha.substring(0, 7);
const normalizedBranch = branch.replace(/\//g, "-");
const lowerOwner = prOwner.toLowerCase();
const lowerRepo = repo.toLowerCase();
const imageNamespace = `ghcr.io/${lowerOwner}/${lowerRepo}`;
// The root package URL on GitHub. We can't link to the exact version ID without querying the GH API,
// so we link to the package landing page where the user can see the recent tags.
const packageUrl = `https://github.com/${prOwner}/${repo}/pkgs/container/${lowerRepo}`;
const branchTag = normalizedBranch || shortSha;
const body = [
`GHCR image refs for this PR:`,
"",
`- Namespace: \`${imageNamespace}\``,
`- Branch tag: \`${branchTag}\``,
`- Package Page: [GHCR Link](${packageUrl})`,
`- Pull: \`docker pull ${imageNamespace}:${branchTag}\``,
"",
`This comment is generated from PR metadata only. It does not checkout or run PR code and is generated in a static manner`,
marker,
].join("\n");
// Update or create comments
const comments = await github.rest.issues.listComments({
owner: ownerContext,
repo,
issue_number: prNumber,
per_page: 100,
});
const existingComment = comments.data.find(
(comment) =>
comment.user?.type === "Bot" &&
typeof comment.body === "string" &&
comment.body.includes(marker)
);
if (existingComment) {
await github.rest.issues.updateComment({
owner: ownerContext,
repo,
comment_id: existingComment.id,
body,
});
} else {
await github.rest.issues.createComment({
owner: ownerContext,
repo,
issue_number: prNumber,
body,
});
}
};