|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +import { |
| 4 | + PREVIEW_COMMENT_MARKER, |
| 5 | + buildPreviewCommentBody, |
| 6 | +} from "./compute-preview-utils.mjs"; |
| 7 | + |
| 8 | +async function main() { |
| 9 | + const githubToken = getRequiredEnv("GITHUB_TOKEN"); |
| 10 | + const repository = getRequiredEnv("GITHUB_REPOSITORY"); |
| 11 | + const prNumber = getRequiredEnv("PREVIEW_PR_NUMBER"); |
| 12 | + const branchName = getRequiredEnv("PREVIEW_BRANCH_NAME"); |
| 13 | + const serviceName = getRequiredEnv("PREVIEW_SERVICE_NAME"); |
| 14 | + const serviceUrl = getRequiredEnv("PREVIEW_SERVICE_URL"); |
| 15 | + const versionUrl = process.env.PREVIEW_VERSION_URL?.trim(); |
| 16 | + const [owner, repo] = repository.split("/"); |
| 17 | + |
| 18 | + if (!owner || !repo) { |
| 19 | + throw new Error(`Invalid GITHUB_REPOSITORY value "${repository}".`); |
| 20 | + } |
| 21 | + |
| 22 | + const body = buildPreviewCommentBody({ |
| 23 | + branchName, |
| 24 | + serviceName, |
| 25 | + serviceUrl, |
| 26 | + versionUrl, |
| 27 | + }); |
| 28 | + const comments = await githubRequest({ |
| 29 | + githubToken, |
| 30 | + method: "GET", |
| 31 | + path: `/repos/${owner}/${repo}/issues/${prNumber}/comments?per_page=100`, |
| 32 | + }); |
| 33 | + const existingComment = comments.find((comment) => |
| 34 | + typeof comment.body === "string" && |
| 35 | + comment.body.includes(PREVIEW_COMMENT_MARKER), |
| 36 | + ); |
| 37 | + |
| 38 | + if (existingComment) { |
| 39 | + await githubRequest({ |
| 40 | + body: { body }, |
| 41 | + githubToken, |
| 42 | + method: "PATCH", |
| 43 | + path: `/repos/${owner}/${repo}/issues/comments/${existingComment.id}`, |
| 44 | + }); |
| 45 | + return; |
| 46 | + } |
| 47 | + |
| 48 | + await githubRequest({ |
| 49 | + body: { body }, |
| 50 | + githubToken, |
| 51 | + method: "POST", |
| 52 | + path: `/repos/${owner}/${repo}/issues/${prNumber}/comments`, |
| 53 | + }); |
| 54 | +} |
| 55 | + |
| 56 | +async function githubRequest(args) { |
| 57 | + const { body, githubToken, method, path } = args; |
| 58 | + const response = await fetch(`https://api.github.com${path}`, { |
| 59 | + body: body ? JSON.stringify(body) : undefined, |
| 60 | + headers: { |
| 61 | + Accept: "application/vnd.github+json", |
| 62 | + Authorization: `Bearer ${githubToken}`, |
| 63 | + "Content-Type": "application/json", |
| 64 | + "User-Agent": "studio-compute-preview", |
| 65 | + "X-GitHub-Api-Version": "2022-11-28", |
| 66 | + }, |
| 67 | + method, |
| 68 | + }); |
| 69 | + |
| 70 | + if (!response.ok) { |
| 71 | + throw new Error( |
| 72 | + `GitHub API request failed (${response.status} ${response.statusText}): ${await response.text()}`, |
| 73 | + ); |
| 74 | + } |
| 75 | + |
| 76 | + return method === "GET" ? await response.json() : null; |
| 77 | +} |
| 78 | + |
| 79 | +function getRequiredEnv(name) { |
| 80 | + const value = process.env[name]?.trim(); |
| 81 | + |
| 82 | + if (!value) { |
| 83 | + throw new Error(`Missing required environment variable ${name}.`); |
| 84 | + } |
| 85 | + |
| 86 | + return value; |
| 87 | +} |
| 88 | + |
| 89 | +await main(); |
0 commit comments