@polka-codes/github 0.7.9

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,2 @@
1
+ export { fetchIssue_alias_1 as fetchIssue } from './_tsup-dts-rollup.js';
2
+ export { fetchPR_alias_1 as fetchPR } from './_tsup-dts-rollup.js';
package/dist/index.js ADDED
@@ -0,0 +1,167 @@
1
+ // src/processBody.ts
2
+ import remarkGfm from "remark-gfm";
3
+ import remarkParse from "remark-parse";
4
+ import remarkStringify from "remark-stringify";
5
+ import { unified } from "unified";
6
+ import { SKIP, visit } from "unist-util-visit";
7
+ var polkaCodesRegex = /<!-- polka-codes-start -->[\s\S]*?<!-- polka-codes-end -->/g;
8
+ var htmlCommentRegex = /<!--[\s\S]*?-->/g;
9
+ var transformer = (tree) => {
10
+ visit(tree, "html", (node, index, parent) => {
11
+ const updated = node.value.replace(htmlCommentRegex, "");
12
+ if (updated.length === 0 && parent && index !== void 0) {
13
+ parent.children.splice(index, 1);
14
+ return [SKIP, index];
15
+ }
16
+ node.value = updated;
17
+ });
18
+ };
19
+ var plugin = () => {
20
+ return transformer;
21
+ };
22
+ var processBody = async (body) => {
23
+ const processedBody = body.replace(polkaCodesRegex, "");
24
+ const output = await unified().use(remarkParse).use(remarkStringify).use(remarkGfm).use(plugin).process(processedBody);
25
+ return output.toString();
26
+ };
27
+
28
+ // src/queries/fetchIssue.gql
29
+ var fetchIssue_default = "query fetchIssue($owner: String!, $repo: String!, $issueNumber: Int!) {\n repository(owner: $owner, name: $repo) {\n issue(number: $issueNumber) {\n number\n title\n body\n author {\n login\n }\n createdAt\n comments(last: 100) {\n totalCount\n nodes {\n author {\n login\n }\n body\n createdAt\n }\n }\n }\n }\n}\n";
30
+
31
+ // src/queries/fetchPR.gql
32
+ var fetchPR_default = "query fetchPR($owner: String!, $repo: String!, $prNumber: Int!) {\n repository(owner: $owner, name: $repo) {\n pullRequest(number: $prNumber) {\n number\n title\n body\n author {\n login\n }\n createdAt\n comments(last: 100) {\n totalCount\n nodes {\n author {\n login\n }\n body\n createdAt\n minimizedReason\n }\n }\n reviews(last: 100) {\n totalCount\n nodes {\n author {\n login\n }\n body\n createdAt\n comments(last: 100) {\n totalCount\n nodes {\n ...ReviewCommentFields\n }\n }\n }\n }\n reviewThreads(last: 100) {\n totalCount\n nodes {\n isResolved\n comments(last: 100) {\n totalCount\n nodes {\n ...ReviewCommentFields\n }\n }\n }\n }\n }\n }\n}\n\nfragment ReviewCommentFields on PullRequestReviewComment {\n author {\n login\n }\n body\n createdAt\n diffHunk\n minimizedReason\n outdated\n}\n";
33
+
34
+ // src/github.ts
35
+ var query = (octokit) => octokit.graphql;
36
+ async function fetchIssue({ octokit, owner, repo, issueNumber }) {
37
+ const resp = await query(octokit)(fetchIssue_default, { owner, repo, issueNumber });
38
+ const issue = resp.repository?.issue;
39
+ if (!issue) {
40
+ throw new Error("Issue not found");
41
+ }
42
+ const issueBody = await processBody(issue.body);
43
+ let text = `#${issue.number}: ${issue.title}
44
+ ${issueBody}
45
+ `;
46
+ const comments = issue.comments?.nodes;
47
+ if (comments) {
48
+ const totalCount = issue.comments?.totalCount ?? 0;
49
+ const skipped = totalCount - (comments?.length ?? 0);
50
+ text += "\n============ Comments ============\n";
51
+ if (skipped > 0) {
52
+ text += `${skipped} comment${skipped > 1 ? "s" : ""} skipped
53
+ `;
54
+ }
55
+ for (const comment of comments) {
56
+ if (!comment) {
57
+ continue;
58
+ }
59
+ const commentBody = await processBody(comment.body);
60
+ const author = comment.author?.login ?? "unknown";
61
+ text += `${comment.createdAt} @${author}:
62
+ ${commentBody}
63
+ ========================
64
+ `;
65
+ }
66
+ }
67
+ return text;
68
+ }
69
+ async function fetchPR({ octokit, owner, repo, prNumber }) {
70
+ const resp = await query(octokit)(fetchPR_default, { owner, repo, prNumber });
71
+ const pr = resp.repository?.pullRequest;
72
+ if (!pr) {
73
+ throw new Error("PR not found");
74
+ }
75
+ const diff = await octokit.request({
76
+ method: "GET",
77
+ url: `https://api.github.com/repos/${owner}/${repo}/pulls/${prNumber}`,
78
+ headers: {
79
+ accept: "application/vnd.github.v3.diff"
80
+ }
81
+ });
82
+ const prBody = await processBody(pr.body);
83
+ let text = `#${pr.number}: ${pr.title}
84
+ ${prBody}
85
+ ============ Diff ============
86
+ ${diff.data}
87
+ `;
88
+ const comments = pr.comments?.nodes;
89
+ if (comments && comments.length > 0) {
90
+ const totalCount = pr.comments?.totalCount ?? 0;
91
+ const skipped = totalCount - (comments?.length ?? 0);
92
+ text += "\n============ Comments ============\n";
93
+ if (skipped > 0) {
94
+ text += `${skipped} comment${skipped > 1 ? "s" : ""} skipped
95
+ `;
96
+ }
97
+ for (const comment of comments) {
98
+ if (!comment) {
99
+ continue;
100
+ }
101
+ if (comment.minimizedReason) {
102
+ continue;
103
+ }
104
+ const commentBody = await processBody(comment.body);
105
+ const author = comment.author?.login ?? "unknown";
106
+ text += `${comment.createdAt} @${author}:
107
+ ${commentBody}
108
+ ========================
109
+ `;
110
+ }
111
+ }
112
+ const reviews = pr.reviews?.nodes;
113
+ if (reviews && reviews.length > 0) {
114
+ const totalCount = pr.reviews?.totalCount ?? 0;
115
+ const skipped = totalCount - (reviews?.length ?? 0);
116
+ if (skipped > 0) {
117
+ text += `${skipped} review${skipped > 1 ? "s" : ""} skipped
118
+ `;
119
+ }
120
+ text += "\n============ Reviews ============\n";
121
+ for (const review of reviews) {
122
+ if (!review) {
123
+ continue;
124
+ }
125
+ const author = review.author?.login ?? "unknown";
126
+ const reviewBody = await processBody(review.body);
127
+ text += `${review.createdAt} @${author}:
128
+ ${reviewBody}
129
+ `;
130
+ const reviewComments = review.comments?.nodes;
131
+ if (reviewComments && reviewComments.length > 0) {
132
+ text += "\n------------ Review Comments ------------\n";
133
+ let lastDiff;
134
+ for (const comment of reviewComments) {
135
+ if (!comment) {
136
+ continue;
137
+ }
138
+ if (comment.minimizedReason || comment.outdated) {
139
+ continue;
140
+ }
141
+ const commentBody = await processBody(comment.body);
142
+ const author2 = comment.author?.login ?? "unknown";
143
+ if (comment.diffHunk === lastDiff) {
144
+ text += `${comment.createdAt} @${author2}:
145
+ ${commentBody}
146
+ -----------------------
147
+ `;
148
+ } else {
149
+ text += `${comment.createdAt} @${author2}:
150
+ Diff:
151
+ ${comment.diffHunk}
152
+ Comment:
153
+ ${commentBody}
154
+ -----------------------
155
+ `;
156
+ }
157
+ }
158
+ }
159
+ }
160
+ text += "========================\n";
161
+ }
162
+ return text;
163
+ }
164
+ export {
165
+ fetchIssue,
166
+ fetchPR
167
+ };
package/package.json ADDED
@@ -0,0 +1,25 @@
1
+ {
2
+ "name": "@polka-codes/github",
3
+ "version": "0.7.9",
4
+ "license": "AGPL-3.0",
5
+ "author": "github@polka.codes",
6
+ "type": "module",
7
+ "exports": {
8
+ ".": {
9
+ "import": "./dist/index.js",
10
+ "types": "./dist/index.d.ts"
11
+ }
12
+ },
13
+ "files": ["dist"],
14
+ "scripts": {
15
+ "build": "tsup src/index.ts --experimental-dts --format esm --clean --loader .gql=text"
16
+ },
17
+ "dependencies": {
18
+ "@octokit/core": "^6.1.4",
19
+ "remark-gfm": "^4.0.1",
20
+ "remark-parse": "^11.0.0",
21
+ "remark-stringify": "^11.0.0",
22
+ "unified": "^11.0.5",
23
+ "unist-util-visit": "^5.0.0"
24
+ }
25
+ }