@verisource/cli 0.0.0-stage → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,24 @@
1
+ export declare const VERISOURCE_PR_COMMENT_MARKER = "<!-- verisource-pr-gate -->";
2
+ export interface GithubPrContext {
3
+ token: string;
4
+ repository: string;
5
+ prNumber: number;
6
+ apiBaseUrl: string;
7
+ }
8
+ export interface GithubPrCommentResult {
9
+ posted: boolean;
10
+ action: 'created' | 'updated' | 'skipped';
11
+ commentId: number | null;
12
+ htmlUrl: string | null;
13
+ reason?: string;
14
+ }
15
+ export declare function resolveGithubPrContext(overrides?: {
16
+ token?: string;
17
+ repository?: string;
18
+ prNumber?: number;
19
+ apiBaseUrl?: string;
20
+ }): GithubPrContext | null;
21
+ export declare function upsertGithubPrComment(params: {
22
+ markdownBody: string;
23
+ context?: GithubPrContext | null;
24
+ }): Promise<GithubPrCommentResult>;
@@ -0,0 +1,129 @@
1
+ import fs from 'node:fs';
2
+ export const VERISOURCE_PR_COMMENT_MARKER = '<!-- verisource-pr-gate -->';
3
+ export function resolveGithubPrContext(overrides) {
4
+ const token = (overrides?.token ??
5
+ process.env.GITHUB_TOKEN ??
6
+ process.env.GH_TOKEN ??
7
+ '').trim();
8
+ const repository = (overrides?.repository ??
9
+ process.env.GITHUB_REPOSITORY ??
10
+ '').trim();
11
+ if (!token ||
12
+ !/^[A-Za-z0-9_.-]+\/[A-Za-z0-9_.-]+$/.test(repository)) {
13
+ return null;
14
+ }
15
+ let prNumber = overrides?.prNumber ?? Number(process.env.GITHUB_PR_NUMBER || 0);
16
+ if ((!Number.isInteger(prNumber) || prNumber <= 0) && process.env.GITHUB_EVENT_PATH) {
17
+ try {
18
+ const raw = fs.readFileSync(process.env.GITHUB_EVENT_PATH, 'utf8');
19
+ const parsed = JSON.parse(raw);
20
+ const candidate = parsed.pull_request?.number ??
21
+ (parsed.issue?.pull_request ? parsed.issue.number : undefined) ??
22
+ parsed.number ??
23
+ 0;
24
+ if (Number.isInteger(candidate) && candidate > 0) {
25
+ prNumber = candidate;
26
+ }
27
+ }
28
+ catch {
29
+ // Ignore unreadable event payload
30
+ }
31
+ }
32
+ if (!Number.isInteger(prNumber) || prNumber <= 0) {
33
+ return null;
34
+ }
35
+ const apiBaseUrl = (overrides?.apiBaseUrl ??
36
+ process.env.GITHUB_API_URL ??
37
+ 'https://api.github.com').replace(/\/+$/, '');
38
+ return {
39
+ token,
40
+ repository,
41
+ prNumber,
42
+ apiBaseUrl,
43
+ };
44
+ }
45
+ export async function upsertGithubPrComment(params) {
46
+ const ctx = params.context ?? resolveGithubPrContext();
47
+ if (!ctx) {
48
+ return {
49
+ posted: false,
50
+ action: 'skipped',
51
+ commentId: null,
52
+ htmlUrl: null,
53
+ reason: 'NO_PR_CONTEXT',
54
+ };
55
+ }
56
+ const bodyWithMarker = params.markdownBody.includes(VERISOURCE_PR_COMMENT_MARKER)
57
+ ? params.markdownBody
58
+ : `${VERISOURCE_PR_COMMENT_MARKER}\n${params.markdownBody}`;
59
+ const commonHeaders = {
60
+ Accept: 'application/vnd.github+json',
61
+ Authorization: `Bearer ${ctx.token}`,
62
+ 'X-GitHub-Api-Version': '2022-11-28',
63
+ 'User-Agent': 'VerisourceCLI/1.0',
64
+ };
65
+ try {
66
+ const listRes = await fetch(`${ctx.apiBaseUrl}/repos/${ctx.repository}/issues/${ctx.prNumber}/comments?per_page=100`, {
67
+ method: 'GET',
68
+ headers: commonHeaders,
69
+ signal: AbortSignal.timeout(8000),
70
+ });
71
+ if (listRes.status === 401 || listRes.status === 403 || listRes.status === 404) {
72
+ return {
73
+ posted: false,
74
+ action: 'skipped',
75
+ commentId: null,
76
+ htmlUrl: null,
77
+ reason: `GITHUB_API_${listRes.status}`,
78
+ };
79
+ }
80
+ let existingCommentId = null;
81
+ if (listRes.ok) {
82
+ const comments = (await listRes.json());
83
+ const match = comments.find((c) => typeof c.id === 'number' &&
84
+ typeof c.body === 'string' &&
85
+ c.body.includes(VERISOURCE_PR_COMMENT_MARKER));
86
+ if (match?.id) {
87
+ existingCommentId = match.id;
88
+ }
89
+ }
90
+ const writeUrl = existingCommentId
91
+ ? `${ctx.apiBaseUrl}/repos/${ctx.repository}/issues/comments/${existingCommentId}`
92
+ : `${ctx.apiBaseUrl}/repos/${ctx.repository}/issues/${ctx.prNumber}/comments`;
93
+ const writeMethod = existingCommentId ? 'PATCH' : 'POST';
94
+ const writeRes = await fetch(writeUrl, {
95
+ method: writeMethod,
96
+ headers: {
97
+ ...commonHeaders,
98
+ 'Content-Type': 'application/json',
99
+ },
100
+ body: JSON.stringify({ body: bodyWithMarker }),
101
+ signal: AbortSignal.timeout(8000),
102
+ });
103
+ if (!writeRes.ok) {
104
+ return {
105
+ posted: false,
106
+ action: 'skipped',
107
+ commentId: null,
108
+ htmlUrl: null,
109
+ reason: `GITHUB_WRITE_${writeRes.status}`,
110
+ };
111
+ }
112
+ const saved = (await writeRes.json());
113
+ return {
114
+ posted: true,
115
+ action: existingCommentId ? 'updated' : 'created',
116
+ commentId: saved.id ?? existingCommentId,
117
+ htmlUrl: saved.html_url ?? null,
118
+ };
119
+ }
120
+ catch (err) {
121
+ return {
122
+ posted: false,
123
+ action: 'skipped',
124
+ commentId: null,
125
+ htmlUrl: null,
126
+ reason: err instanceof Error ? err.message : 'NETWORK_ERROR',
127
+ };
128
+ }
129
+ }
@@ -0,0 +1,7 @@
1
+ export * from './store.js';
2
+ export * from './mapper.js';
3
+ export * from './skills.js';
4
+ export * from './logs.js';
5
+ export * from './browser.js';
6
+ export * from './cloud.js';
7
+ export * from './github-pr.js';