-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub-comment.ts
More file actions
149 lines (118 loc) · 4.05 KB
/
github-comment.ts
File metadata and controls
149 lines (118 loc) · 4.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import { Octokit } from '@octokit/rest';
import type { ChangeAnalysis } from '../analyzer/change-summarizer.js';
export interface CommentOptions {
owner: string;
repo: string;
pullNumber: number;
analysis: ChangeAnalysis;
recordingUrl?: string;
screenshots?: string[];
script: string;
rerunUrl?: string;
}
const COMMENT_MARKER = '<!-- git-glimpse-demo -->';
export interface SkipCommentOptions {
owner: string;
repo: string;
pullNumber: number;
reason: string;
}
export async function postSkipComment(
token: string,
options: SkipCommentOptions
): Promise<{ commentId: number; url: string }> {
const octokit = new Octokit({ auth: token });
const body = `${COMMENT_MARKER}
## GitGlimpse — Skipped
${options.reason}
---
*Generated by [git-glimpse](https://github.com/git-glimpse/git-glimpse)*`;
const existing = await findExistingComment(octokit, options);
if (existing) {
const response = await octokit.rest.issues.updateComment({
owner: options.owner,
repo: options.repo,
comment_id: existing.id,
body,
});
return { commentId: response.data.id, url: response.data.html_url };
}
const response = await octokit.rest.issues.createComment({
owner: options.owner,
repo: options.repo,
issue_number: options.pullNumber,
body,
});
return { commentId: response.data.id, url: response.data.html_url };
}
export async function postPRComment(
token: string,
options: CommentOptions
): Promise<{ commentId: number; url: string }> {
const octokit = new Octokit({ auth: token });
const body = buildCommentBody(options);
// Check for an existing git-glimpse comment to update instead of creating a new one
const existing = await findExistingComment(octokit, options);
if (existing) {
const response = await octokit.rest.issues.updateComment({
owner: options.owner,
repo: options.repo,
comment_id: existing.id,
body,
});
return { commentId: response.data.id, url: response.data.html_url };
}
const response = await octokit.rest.issues.createComment({
owner: options.owner,
repo: options.repo,
issue_number: options.pullNumber,
body,
});
return { commentId: response.data.id, url: response.data.html_url };
}
async function findExistingComment(
octokit: Octokit,
options: Pick<CommentOptions, 'owner' | 'repo' | 'pullNumber'>
): Promise<{ id: number } | null> {
const comments = await octokit.rest.issues.listComments({
owner: options.owner,
repo: options.repo,
issue_number: options.pullNumber,
per_page: 100,
});
const existing = comments.data.find((c) => c.body?.includes(COMMENT_MARKER));
return existing ? { id: existing.id } : null;
}
function buildCommentBody(options: CommentOptions): string {
const { analysis, recordingUrl, screenshots, script, rerunUrl } = options;
const changedFilesList = analysis.changedFiles
.slice(0, 5)
.map((f) => `\`${f}\``)
.join(', ');
const moreFiles = analysis.changedFiles.length > 5
? ` (+${analysis.changedFiles.length - 5} more)`
: '';
const mediaSection = recordingUrl
? `\n\n[📱 Can't see the preview? Open it directly](${recordingUrl})`
: screenshots && screenshots.length > 0
? screenshots
.map((s, i) => `\n\n[📱 Can't see screenshot ${i + 1}? Open it directly](${s})`)
.join('\n\n')
: '_No recording available._';
const rerunSection = rerunUrl ? `\n\n[↺ Re-run demo](${rerunUrl})` : '';
const logoUrl =
'https://raw.githubusercontent.com/DeDuckProject/git-glimpse/main/assets/logo_square_small.png';
return `${COMMENT_MARKER}
## <img src="${logoUrl}" width="40" height="40" align="absmiddle" alt="git-glimpse logo" /> UI Demo Preview
**Changes detected in**: ${changedFilesList}${moreFiles}
**What changed**: ${analysis.changeDescription}
${mediaSection}
<details>
<summary>Demo script (auto-generated)</summary>
\`\`\`typescript
${script}
\`\`\`
</details>
---
*Generated by [git-glimpse](https://github.com/git-glimpse/git-glimpse)${rerunSection}*`;
}