Skip to content

Commit 95038d4

Browse files
tmp 3
1 parent 8b95359 commit 95038d4

4 files changed

Lines changed: 132 additions & 51 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: 69 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,64 @@ 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
1930119341
(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));
19342+
const projectDirectory = getInput("project-directory");
19343+
const ghToken = process.env.GH_TOKEN;
19344+
const prId = context.issue.number;
19345+
const repo = context.repo;
19346+
const [pjBuff, urlBuff] = await Promise.all([readFile(resolve(projectDirectory, ".vercel/project.json")), readFile(resolve(projectDirectory, "./url.txt"))]);
19347+
const projectName = JSON.parse(pjBuff.toString()).projectName;
19348+
const url = urlBuff.toString();
19349+
if (typeof ghToken !== "string") throw new Error("GH_TOKEN missing");
19350+
const octokit = getOctokit(ghToken);
19351+
const existingComment = (await octokit.rest.issues.listComments({
19352+
...repo,
19353+
issue_number: prId
19354+
})).data.find((comment) => comment.body?.startsWith("### Vercel Deployments"));
19355+
const lines = (existingComment?.body ?? "### Vercel Deployments").split("\n");
19356+
const linkText = `- [${projectName}](${url})`;
19357+
const linkIndex = lines.findIndex((line) => line.startsWith(`- [${projectName}]`));
19358+
if (typeof linkIndex !== "number") lines.push("\n", linkText);
19359+
else lines[linkIndex] = linkText;
19360+
if (existingComment) await octokit.rest.issues.updateComment({
19361+
...repo,
19362+
comment_id: existingComment.id,
19363+
body: lines.join("\n")
19364+
});
19365+
else await octokit.rest.issues.createComment({
19366+
...repo,
19367+
issue_number: prId,
19368+
body: lines.join("\n")
19369+
});
1930719370
})();
1930819371
//#endregion
1930919372
export {};
Lines changed: 57 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,61 @@
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';
55

66
(async () => {
7-
const prId = context.payload.pull_request?.number;
8-
const projectLoc = getInput('project-json-location');
7+
const projectDirectory = core.getInput('project-directory');
98
const ghToken = process.env.GH_TOKEN;
10-
11-
console.log(projectLoc);
12-
console.log(JSON.stringify(context, null, 4));
13-
14-
// if (typeof ghToken !== 'string') {
15-
// throw new Error('GH_TOKEN missing');
16-
// }
17-
18-
// if (typeof prId !== 'number') {
19-
// }
20-
21-
// const octokit = github.getOctokit(ghToken);
22-
23-
// const comments =
24-
25-
// const lines = body.split('\n');
26-
27-
// let linkIndex;
28-
29-
// const linkText = `- [${project.projectName}](${url.stdout})`;
30-
31-
// for (const lineIndex in lines) {
32-
// const line = lines[lineIndex];
33-
34-
// if (line.startsWith(`- [${project.projectName}]`)) {
35-
// lines[lineIndex] = linkText;
36-
// linkIndex = +lineIndex;
37-
// continue;
38-
// }
39-
// }
40-
41-
// if (typeof linkIndex !== 'number') {
42-
// lines.push('\n', linkText);
43-
// }
9+
const prId = github.context.issue.number;
10+
const repo = github.context.repo;
11+
12+
const [pjBuff, urlBuff] = await Promise.all([
13+
readFile(resolve(projectDirectory, '.vercel/project.json')),
14+
readFile(resolve(projectDirectory, './url.txt')),
15+
]);
16+
17+
const projectName = JSON.parse(pjBuff.toString()).projectName;
18+
const url = urlBuff.toString();
19+
20+
if (typeof ghToken !== 'string') {
21+
throw new Error('GH_TOKEN missing');
22+
}
23+
24+
const octokit = github.getOctokit(ghToken);
25+
26+
const listCommentsResponse = await octokit.rest.issues.listComments({
27+
...repo,
28+
issue_number: prId,
29+
});
30+
31+
const existingComment = listCommentsResponse.data.find((comment) =>
32+
comment.body?.startsWith('### Vercel Deployments'),
33+
);
34+
35+
const body = existingComment?.body ?? '### Vercel Deployments';
36+
37+
const lines = body.split('\n');
38+
39+
const linkText = `- [${projectName}](${url})`;
40+
const linkIndex = lines.findIndex((line) => line.startsWith(`- [${projectName}]`));
41+
42+
if (typeof linkIndex !== 'number') {
43+
lines.push('\n', linkText);
44+
} else {
45+
lines[linkIndex] = linkText;
46+
}
47+
48+
if (existingComment) {
49+
await octokit.rest.issues.updateComment({
50+
...repo,
51+
comment_id: existingComment.id,
52+
body: lines.join('\n'),
53+
});
54+
} else {
55+
await octokit.rest.issues.createComment({
56+
...repo,
57+
issue_number: prId,
58+
body: lines.join('\n'),
59+
});
60+
}
4461
})();

.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)