Skip to content

Commit 3007212

Browse files
tmp 3
1 parent 8b95359 commit 3007212

4 files changed

Lines changed: 128 additions & 40 deletions

File tree

.github/actions/vercel/action.yml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@ name: Vercel
22
description: Generates a comment with Vercel links
33

44
inputs:
5-
project-json-location:
6-
description: Location of the project.json file
7-
required: false
8-
default: ./.vercel/project.json
5+
project-directory:
6+
description: Location of the deployed project
7+
required: true
98

109
runs:
1110
using: node24

.github/actions/vercel/dist/esm/index.mjs

Lines changed: 72 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import * as path from "path";
77
import * as events from "events";
88
import "child_process";
99
import "timers";
10+
import { resolve } from "node:path";
11+
import { readFile } from "node:fs/promises";
1012
//#region \0rolldown/runtime.js
1113
var __create = Object.create;
1214
var __defProp = Object.defineProperty;
@@ -16846,6 +16848,11 @@ var __awaiter = function(thisArg, _arguments, P, generator) {
1684616848
step((generator = generator.apply(thisArg, _arguments || [])).next());
1684716849
});
1684816850
};
16851+
function getAuthString(token, options) {
16852+
if (!token && !options.auth) throw new Error("Parameter token or opts.auth is required");
16853+
else if (token && options.auth) throw new Error("Parameters token and opts.auth may not both be specified");
16854+
return typeof options.auth === "string" ? options.auth : `token ${token}`;
16855+
}
1684916856
function getProxyAgent(destinationUrl) {
1685016857
return new import_lib.HttpClient().getAgent(destinationUrl);
1685116858
}
@@ -16862,6 +16869,16 @@ function getProxyFetch(destinationUrl) {
1686216869
function getApiBaseUrl() {
1686316870
return process.env["GITHUB_API_URL"] || "https://api.github.com";
1686416871
}
16872+
function getUserAgentWithOrchestrationId(baseUserAgent) {
16873+
var _a;
16874+
const orchId = (_a = process.env["ACTIONS_ORCHESTRATION_ID"]) === null || _a === void 0 ? void 0 : _a.trim();
16875+
if (orchId) {
16876+
const tag = `actions_orchestration_id/${orchId.replace(/[^a-z0-9_.-]/gi, "_")}`;
16877+
if (baseUserAgent === null || baseUserAgent === void 0 ? void 0 : baseUserAgent.includes(tag)) return baseUserAgent;
16878+
return `${baseUserAgent ? `${baseUserAgent} ` : ""}${tag}`;
16879+
}
16880+
return baseUserAgent;
16881+
}
1686516882
//#endregion
1686616883
//#region node_modules/universal-user-agent/index.js
1686716884
function getUserAgent() {
@@ -19292,18 +19309,67 @@ const defaults = {
1929219309
fetch: getProxyFetch(baseUrl)
1929319310
}
1929419311
};
19295-
Octokit.plugin(restEndpointMethods, paginateRest).defaults(defaults);
19312+
const GitHub = Octokit.plugin(restEndpointMethods, paginateRest).defaults(defaults);
19313+
/**
19314+
* Convience function to correctly format Octokit Options to pass into the constructor.
19315+
*
19316+
* @param token the repo PAT or GITHUB_TOKEN
19317+
* @param options other options to set
19318+
*/
19319+
function getOctokitOptions(token, options) {
19320+
const opts = Object.assign({}, options || {});
19321+
const auth = getAuthString(token, opts);
19322+
if (auth) opts.auth = auth;
19323+
const userAgent = getUserAgentWithOrchestrationId(opts.userAgent);
19324+
if (userAgent) opts.userAgent = userAgent;
19325+
return opts;
19326+
}
1929619327
//#endregion
1929719328
//#region node_modules/@actions/github/lib/github.js
1929819329
const context = new Context();
19330+
/**
19331+
* Returns a hydrated octokit ready to use for GitHub Actions
19332+
*
19333+
* @param token the repo PAT or GITHUB_TOKEN
19334+
* @param options other options to set
19335+
*/
19336+
function getOctokit(token, options, ...additionalPlugins) {
19337+
return new (GitHub.plugin(...additionalPlugins))(getOctokitOptions(token, options));
19338+
}
1929919339
//#endregion
1930019340
//#region src/index.ts
19341+
const TITLE = "### Vercel Deployments";
1930119342
(async () => {
19302-
context.payload.pull_request?.number;
19303-
const projectLoc = getInput("project-json-location");
19304-
process.env.GH_TOKEN;
19305-
console.log(projectLoc);
19306-
console.log(JSON.stringify(context, null, 4));
19343+
const projectDirectory = getInput("project-directory");
19344+
const ghToken = process.env.GH_TOKEN;
19345+
const prId = context.issue.number;
19346+
const repo = context.repo;
19347+
const [pjBuff, urlBuff] = await Promise.all([readFile(resolve(projectDirectory, ".vercel/project.json")), readFile(resolve(projectDirectory, "./url.txt"))]);
19348+
const projectName = JSON.parse(pjBuff.toString()).projectName;
19349+
const url = urlBuff.toString();
19350+
if (typeof ghToken !== "string") throw new Error("GH_TOKEN missing");
19351+
const octokit = getOctokit(ghToken);
19352+
const existingComment = (await octokit.rest.issues.listComments({
19353+
...repo,
19354+
issue_number: prId
19355+
})).data.find((comment) => comment.body?.startsWith(TITLE));
19356+
const body = existingComment?.body ?? TITLE;
19357+
const lines = body.split("\n");
19358+
const linkText = `- [${projectName}](${url})`;
19359+
const linkIndex = lines.findIndex((line) => line.startsWith(`- [${projectName}]`));
19360+
if (linkIndex >= 0) lines.push("\n", linkText);
19361+
else lines[linkIndex] = linkText;
19362+
console.log(body, lines);
19363+
if (existingComment) await octokit.rest.issues.updateComment({
19364+
...repo,
19365+
comment_id: existingComment.id,
19366+
body: lines.join("\n")
19367+
});
19368+
else await octokit.rest.issues.createComment({
19369+
...repo,
19370+
issue_number: prId,
19371+
body: lines.join("\n")
19372+
});
1930719373
})();
1930819374
//#endregion
1930919375
export {};
Lines changed: 50 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,65 @@
1-
// import { $ } from 'zx';
2-
import { getInput } from '@actions/core';
3-
import { context } from '@actions/github';
4-
// import * as asd from '@actions/cache';
1+
import * as core from '@actions/core';
2+
import * as github from '@actions/github';
3+
import { resolve } from 'node:path';
4+
import { readFile } from 'node:fs/promises';
5+
6+
const TITLE = '### Vercel Deployments';
57

68
(async () => {
7-
const prId = context.payload.pull_request?.number;
8-
const projectLoc = getInput('project-json-location');
9+
const projectDirectory = core.getInput('project-directory');
910
const ghToken = process.env.GH_TOKEN;
11+
const prId = github.context.issue.number;
12+
const repo = github.context.repo;
13+
14+
const [pjBuff, urlBuff] = await Promise.all([
15+
readFile(resolve(projectDirectory, '.vercel/project.json')),
16+
readFile(resolve(projectDirectory, './url.txt')),
17+
]);
1018

11-
console.log(projectLoc);
12-
console.log(JSON.stringify(context, null, 4));
19+
const projectName = JSON.parse(pjBuff.toString()).projectName;
20+
const url = urlBuff.toString();
1321

14-
// if (typeof ghToken !== 'string') {
15-
// throw new Error('GH_TOKEN missing');
16-
// }
22+
if (typeof ghToken !== 'string') {
23+
throw new Error('GH_TOKEN missing');
24+
}
1725

18-
// if (typeof prId !== 'number') {
19-
// }
26+
const octokit = github.getOctokit(ghToken);
2027

21-
// const octokit = github.getOctokit(ghToken);
28+
const listCommentsResponse = await octokit.rest.issues.listComments({
29+
...repo,
30+
issue_number: prId,
31+
});
2232

23-
// const comments =
33+
const existingComment = listCommentsResponse.data.find((comment) =>
34+
comment.body?.startsWith(TITLE),
35+
);
2436

25-
// const lines = body.split('\n');
37+
const body = existingComment?.body ?? TITLE;
2638

27-
// let linkIndex;
39+
const lines = body.split('\n');
2840

29-
// const linkText = `- [${project.projectName}](${url.stdout})`;
41+
const linkText = `- [${projectName}](${url})`;
42+
const linkIndex = lines.findIndex((line) => line.startsWith(`- [${projectName}]`));
3043

31-
// for (const lineIndex in lines) {
32-
// const line = lines[lineIndex];
44+
if (linkIndex >= 0) {
45+
lines.push('\n', linkText);
46+
} else {
47+
lines[linkIndex] = linkText;
48+
}
3349

34-
// if (line.startsWith(`- [${project.projectName}]`)) {
35-
// lines[lineIndex] = linkText;
36-
// linkIndex = +lineIndex;
37-
// continue;
38-
// }
39-
// }
50+
console.log(body, lines)
4051

41-
// if (typeof linkIndex !== 'number') {
42-
// lines.push('\n', linkText);
43-
// }
52+
if (existingComment) {
53+
await octokit.rest.issues.updateComment({
54+
...repo,
55+
comment_id: existingComment.id,
56+
body: lines.join('\n'),
57+
});
58+
} else {
59+
await octokit.rest.issues.createComment({
60+
...repo,
61+
issue_number: prId,
62+
body: lines.join('\n'),
63+
});
64+
}
4465
})();

.github/workflows/ci.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,4 +124,6 @@ jobs:
124124
- name: Add links
125125
uses: ./.github/actions/vercel
126126
env:
127-
GH_TOKEN: ${{ github.token }}
127+
GH_TOKEN: ${{ github.token }}
128+
with:
129+
project-directory: examples/vite

0 commit comments

Comments
 (0)